code clean
[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 ((currentIndex < 0 || currentIndex >= count) && (event.key == Qt.Key_Up || event.key == Qt.Key_Down)){
64                 currentIndex = 0
65             }
66
67             if ((event.key == Qt.Key_Return || event.key == Qt.Key_Enter) && currentIndex >= 0){
68                 itemActivated(currentIndex)
69             }
70             if ((event.key == Qt.Key_Delete) && currentIndex >= 0){
71                 removeButtonClicked()
72             }
73             if (event.key == Qt.Key_S && event.modifiers == Qt.ControlModifier){
74                 saveButtonClicked()
75             }
76             if (event.key == Qt.Key_T && event.modifiers == Qt.ControlModifier){
77                 addButtonClicked()
78             }
79             if ((event.key == Qt.Key_Space) && currentIndex >= 0){
80                 dictModel.setModelProperty(dictList.currentIndex, "isSelected")
81             }
82         }
83
84         onCurrentIndexChanged: {
85             dictModel.itemSelected(dictList.currentIndex)
86         }
87
88         delegate: Component{
89             id: dictListDelegate
90             Item {
91                 width: rectangle1.width
92                 height: {
93                     if (nameText.height + 4 > logo.height)
94                             return nameText.height + 4;
95                     else
96                             return logo.height;
97                 }
98                 MouseArea{
99                     anchors.fill: parent
100                     onClicked: {
101                         dictList.currentIndex = number
102                         rectangle1.setEnableRemove(true)
103                         rectangle1.setEnableSettings(true)
104                     }
105                     onDoubleClicked: {
106                         rectangle1.itemActivated(dictList.currentIndex)
107                     }
108                 }
109                 Row {
110                     anchors.fill: parent
111                     Checkbox{
112                         id: check
113                         width: nameText.height
114                         selected: isSelected
115                         onChanged: rectangle1.changeDictionaryState(number, selected)
116                     }
117
118
119                     Image {
120                         id: logo
121                         source: iconPath
122                         height: {
123                             var aspectRatio = sourceSize.height / sourceSize.width
124                             return logo.width * aspectRatio
125                         }
126                         anchors.leftMargin: 5
127                         anchors.verticalCenter: parent.verticalCenter
128                         width: nameText.height + 4
129                         smooth: true
130                     }
131                     Text {
132                         id: nameText
133                         text: name
134                         anchors.leftMargin: 5
135                         anchors.verticalCenter: parent.verticalCenter
136                     }
137                 }
138             }
139
140         }
141         model: dictModel
142     }
143
144
145     Item {
146         id: buttonsBox
147         width: parent.width
148         height: 30
149         anchors.bottom: parent.bottom
150         anchors.top: dictList.bottom
151         anchors.topMargin: 8
152
153         Button {
154             id: addButton
155             width: (parent.width - 4) / 4
156             height: buttonsBox.height
157             anchors.left: buttonsBox.left
158             anchors.leftMargin: 4
159             anchors.verticalCenter: parent.verticalCenter
160             textInButton: qsTr("Add")
161             onClicked: addButtonClicked();
162         }
163
164         Button {
165             id: removeButton
166             width: (parent.width - 4) / 4
167             height: buttonsBox.height
168             anchors.left: addButton.right
169             anchors.leftMargin: 4
170             anchors.verticalCenter: parent.verticalCenter
171             textInButton: qsTr("Remove")
172             enabled: false
173             onClicked: removeButtonClicked();
174         }
175
176         Button {
177             id: settingsButton
178             width: (parent.width - 4) / 4
179             height: buttonsBox.height
180             anchors.left: removeButton.right
181             anchors.leftMargin: 4
182             anchors.verticalCenter: parent.verticalCenter
183             textInButton: qsTr("Settings")
184             enabled: false
185             onClicked: settingsButtonClicked();
186         }
187
188         Button {
189             id: saveButton
190             width: (parent.width - 4) / 4
191             height: buttonsBox.height
192             anchors.left: settingsButton.right
193             anchors.leftMargin: 4
194             anchors.right: buttonsBox.right
195             anchors.rightMargin: 4
196             anchors.verticalCenter: parent.verticalCenter
197             textInButton: qsTr("Save")
198             onClicked: saveButtonClicked()
199         }
200
201     }
202
203 }