Fix bug with app crash on exit. Add keyboard support in wordList, DictTypeSelectDialo...
[mdictionary] / src / mdictionary / qml / DictManagerWidget.qml
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21 /*!
22     author: Marcin Kaźmierczak <marcin.kazmierczak@comarch.pl>
23 */
24
25 import Qt 4.7
26
27 Rectangle {
28     SystemPalette { id: myPalette; colorGroup: SystemPalette.Active }
29
30     function setEnableRemove(Boolean) { removeButton.enabled = Boolean }
31     function setEnableSettings(Boolean) { settingsButton.enabled = Boolean }
32     function changeDictionaryState(nr, state) {
33         dictList.currentIndex = nr
34         dictModel.setModelProperty(dictList.currentIndex, state, "isSelected")
35         rectangle1.setEnableRemove(true)
36         rectangle1.setEnableSettings(true)
37     }
38
39     function setFocus() {
40         dictList.setFocus()
41     }
42
43     signal addButtonClicked;
44     signal removeButtonClicked;
45     signal settingsButtonClicked;
46     signal saveButtonClicked;
47     signal itemActivated(int nr);
48
49     id: rectangle1
50     color: myPalette.base
51     anchors.fill: parent
52
53     ElementsListView{
54         id: dictList
55         width: rectangle1.width
56 //        height: rectangle1.height
57         anchors.top: parent.top
58         anchors.bottom: buttonsBox.top
59         anchors.bottomMargin: buttonsBox.height + buttonsBox.anchors.topMargin
60         highlightResizeSpeed: 1000
61
62         Keys.onPressed: {
63             if ((event.key == Qt.Key_Return || event.key == Qt.Key_Enter) && currentIndex >= 0){
64                 itemActivated(currentIndex)
65             }
66             if ((event.key == Qt.Key_Delete) && currentIndex >= 0){
67                 removeButtonClicked()
68             }
69             if (event.key == Qt.Key_S && event.modifiers == Qt.ControlModifier){
70                 saveButtonClicked()
71             }
72             if (event.key == Qt.Key_T && event.modifiers == Qt.ControlModifier){
73                 addButtonClicked()
74             }
75             if ((event.key == Qt.Key_Space) && currentIndex >= 0){
76                 dictModel.setModelProperty(dictList.currentIndex, "isSelected")
77             }
78         }
79
80         onCurrentIndexChanged: {
81             dictModel.itemSelected(dictList.currentIndex)
82         }
83
84         delegate: Component{
85             id: dictListDelegate
86             Item {
87                 width: rectangle1.width
88                 height: {
89                     if (nameText.height + 4 > logo.height)
90                             return nameText.height + 4;
91                     else
92                             return logo.height;
93                 }
94                 MouseArea{
95                     anchors.fill: parent
96                     onClicked: {
97                         dictList.currentIndex = number
98                         rectangle1.setEnableRemove(true)
99                         rectangle1.setEnableSettings(true)
100                     }
101                     onDoubleClicked: {
102                         rectangle1.itemActivated(dictList.currentIndex)
103                     }
104                 }
105                 Row {
106                     anchors.fill: parent
107                     Checkbox{
108                         id: check
109                         width: nameText.height
110                         selected: isSelected
111                         onChanged: rectangle1.changeDictionaryState(number, selected)
112                     }
113
114
115                     Image {
116                         id: logo
117                         source: iconPath
118                         height: {
119                             var aspectRatio = sourceSize.height / sourceSize.width
120                             return logo.width * aspectRatio
121                         }
122                         anchors.leftMargin: 5
123                         anchors.verticalCenter: parent.verticalCenter
124                         width: nameText.height + 4
125                         smooth: true
126                     }
127                     Text {
128                         id: nameText
129                         text: name
130                         anchors.leftMargin: 5
131                         anchors.verticalCenter: parent.verticalCenter
132                     }
133                 }
134             }
135
136         }
137         model: dictModel
138     }
139
140     //buttons
141
142     Item {
143         id: buttonsBox
144         width: parent.width
145         height: 30
146         anchors.bottom: parent.bottom
147         anchors.top: dictList.bottom
148         anchors.topMargin: 8
149
150         Button {
151             id: addButton
152             width: (parent.width - 4) / 4
153             height: buttonsBox.height
154             anchors.left: buttonsBox.left
155             anchors.leftMargin: 4
156             anchors.verticalCenter: parent.verticalCenter
157             textInButton: qsTr("Add")
158             onClicked: addButtonClicked();
159         }
160
161         Button {
162             id: removeButton
163             width: (parent.width - 4) / 4
164             height: buttonsBox.height
165             anchors.left: addButton.right
166             anchors.leftMargin: 4
167             anchors.verticalCenter: parent.verticalCenter
168             textInButton: qsTr("Remove")
169             enabled: false
170             onClicked: removeButtonClicked();
171         }
172
173         Button {
174             id: settingsButton
175             width: (parent.width - 4) / 4
176             height: buttonsBox.height
177             anchors.left: removeButton.right
178             anchors.leftMargin: 4
179             anchors.verticalCenter: parent.verticalCenter
180             textInButton: qsTr("Settings")
181             enabled: false
182             onClicked: settingsButtonClicked();
183         }
184
185         Button {
186             id: saveButton
187             width: (parent.width - 4) / 4
188             height: buttonsBox.height
189             anchors.left: settingsButton.right
190             anchors.leftMargin: 4
191             anchors.right: buttonsBox.right
192             anchors.rightMargin: 4
193             anchors.verticalCenter: parent.verticalCenter
194             textInButton: qsTr("Save")
195             onClicked: saveButtonClicked()
196         }
197
198     }
199
200 }