Add keyboard support to comboBox, HistoryList. Fix some issues with keyboard support.
[mdictionary] / src / mdictionary / qml / WordListWidget.qml
index 750f856..e4168f9 100644 (file)
@@ -1,36 +1,79 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+/*!
+    author: Marcin Kaźmierczak <marcin.kazmierczak@comarch.pl>
+*/
+
 import Qt 4.7
 
 Rectangle {
+    id: rectangle1
+    color: myPalette.base
+    anchors.fill: parent
 
     function changeWordState(nr, state) {
         wordList.currentIndex = nr
         wordModel.setModelProperty(wordList.currentIndex, state, "isBookmarked")
+    }
 
+    function changeWordStateByIndex() {
+        wordModel.setModelPropertyByIndex(wordList.currentIndex, "isBookmarked")
     }
 
-//    function setEnabled(Boolean) { wordList.enabled = Boolean; println(wordList.enabled) }  // slot
+    function setEnabled(Boolean) { wordList.enabled = Boolean }
+    function setWordListEmpty(Boolean) { wordList.empty = Boolean }
+    function setFocus() {
+        wordList.setFocus()
+    }
 
     signal wordSelected(string word);
-    //?
-//    signal addToBookmarks(int nr);
-//    signal removeFromBookmarks(int nr);
+    signal wordSelectedByIndex(int nr);
+    signal checkFocus();
 
     SystemPalette { id: myPalette; colorGroup: SystemPalette.Active }
 
-    id: rectangle1
-    color: myPalette.base
-    anchors.fill: parent
-
     ElementsListView{
         id: wordList
         width: rectangle1.width
-//        height: rectangle1.height
+
         anchors.fill: parent
         highlightResizeSpeed: 1000
+        property bool empty: false
+
+        Keys.onPressed: {
+            if ((currentIndex < 0 || currentIndex >= count) && (event.key == Qt.Key_Up || event.key == Qt.Key_Down)){
+                currentIndex = 0
+            }
+            if (event.key == Qt.Key_Space && currentIndex >= 0){
+                rectangle1.changeWordStateByIndex();
+            } else if ((event.key == Qt.Key_Return || event.key == Qt.Key_Enter) && currentIndex >= 0){
+                rectangle1.wordSelectedByIndex(currentIndex)
+            }
+        }
 
         delegate: Component{
             id: wordListDelegate
             Item {
+                onActiveFocusChanged: rectangle1.checkFocus();
+
                 width: rectangle1.width
                 height: {
                     if (wordText.height + 4 > check.height)
@@ -38,53 +81,85 @@ Rectangle {
                     else
                             return check.height;
                 }
-                Row {
+                MouseArea{
                     anchors.fill: parent
-
-                    Text {
-                        id: wordText
-                        text:
-                        {
-                            if (word == "!@#$%"){
-                                qsTr("Can't find any matching words")
-                            } else {
-                                word
-                            }
-                        }
-
-                        MouseArea{
-                            anchors.fill: parent
-                            onClicked: {
-                                wordList.currentIndex = number
-                                console.log("lolol")
-                                rectangle1.wordSelected(word)
-                            }
-                        }
+                    onClicked: wordList.currentIndex = index 
+                    onDoubleClicked: {
+                        wordList.currentIndex = number
+                        rectangle1.wordSelected(word)
                     }
+                }
 
-                    Checkbox{
-                        id: check
-                        width: wordText.height
-                        selected: isBookmarked
-                        pathToCheckedImage: CheckedPath
-                        pathToUncheckedImage: UncheckedPath
-                        anchors.leftMargin: 5
-                        anchors.verticalCenter: parent.verticalCenter
-                        onChanged: rectangle1.changeWordState(number, selected)
-                        visible: {
-                            if (word == "!@#$%"){
-                                false
-                            } else {
-                                true
-                            }
-                        }
-                    }
 
+                Text {
+                    id: wordText
+                    anchors.verticalCenter: parent.verticalCenter
+                    text: word
+                }
+
+                Checkbox{
+                    id: check
+                    width: wordText.height
+                    selected: isBookmarked
+                    pathToCheckedImage: CheckedPath
+                    pathToUncheckedImage: UncheckedPath
+                    anchors.right: parent.right
+                    anchors.rightMargin: 5
+                    pathToCheckedDicImage: CheckedPath
+                    pathToUncheckedDicImage: UncheckedPath
+                    anchors.verticalCenter: parent.verticalCenter
+                    onChanged: rectangle1.changeWordState(number, selected)
                 }
             }
+        }
+
+        Text {
+            id: emptyText
+            anchors.top: parent.top
+            anchors.left: parent.left
+            text: qsTr("Can't find any matching words")
+        }
 
+        Rectangle {
+            id: shadeDisable
+            anchors.centerIn: parent;
+            color: "grey";
+            opacity: 0
+            width:  parent.width;
+            height: parent.height;
         }
 
         model: wordModel
+
+        states: [
+            State {
+                name: "empty"
+                when: (wordList.empty == true);
+                PropertyChanges { target: emptyText; visible: true}
+            },
+            State {
+                name: "non-empty"
+                when: (wordList.empty == false);
+                PropertyChanges { target: emptyText; visible: false}
+            },
+            State {
+                name: "enabled"
+                when: (wordList.enabled == true);
+                PropertyChanges { target: shadeDisable; z: 30; opacity: 0.5 }
+            },
+            State {
+                name: "disabled"
+                when: (wordList.enabled == false);
+                PropertyChanges { target: shadeDisable; z: 30; opacity: 0.0 }
+            }
+        ]
     }
+
+    states: [
+            State {
+                name: "noFocus";
+                when: ((!wordList.focus) && (!rectangle1.focus))
+                PropertyChanges { target: wordList.highlightItem; opacity:0}
+           }
+    ]
 }