add focus in qml
authorjakub <jakub.jaszczynski@comarch.com>
Fri, 25 Feb 2011 08:33:31 +0000 (09:33 +0100)
committerjakub <jakub.jaszczynski@comarch.com>
Fri, 25 Feb 2011 08:33:31 +0000 (09:33 +0100)
14 files changed:
data/gui.qrc
src/mdictionary/gui/SearchBarWidget.cpp
src/mdictionary/gui/SearchBarWidget.h
src/mdictionary/gui/SettingsWidget.cpp
src/mdictionary/gui/TranslationView.cpp
src/mdictionary/qml/Button.qml
src/mdictionary/qml/Checkbox.qml
src/mdictionary/qml/FlickableWebView.qml
src/mdictionary/qml/IconButton.qml
src/mdictionary/qml/MySpinBox.qml
src/mdictionary/qml/MyTextLineEdit.qml
src/mdictionary/qml/SearchBarWidget.qml
src/mdictionary/qml/SettingsWidget.qml
src/mdictionary/qml/TranslationView.qml

index 2890e96..1f1cdb4 100644 (file)
@@ -32,5 +32,8 @@
         <file>button/revert.png</file>
         <file>button/checkboxCheckedDis.png</file>
         <file>button/checkboxDis.png</file>
+        <file>button/buttonCenterFocus.png</file>
+        <file>button/buttonLeftFocus.png</file>
+        <file>button/buttonRFocus.png</file>
     </qresource>
 </RCC>
index 338b87a..da482d0 100644 (file)
@@ -52,6 +52,7 @@ SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
 
     view= new QDeclarativeView();
     ctxt = view->rootContext();
+    ctxt->setContextProperty("focusss",true);
 
     view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/SearchBarWidget.qml"));
     view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
@@ -80,6 +81,10 @@ SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
             this, SLOT(nextCompleter()));
     connect(rootObject, SIGNAL(prevCompleter()),
             this, SLOT(prevCompleter()));
+    connect(rootObject, SIGNAL(checkFocus()),
+            this, SLOT(checkFocus()));
+    connect(rootObject, SIGNAL(nextFocus()),
+            this, SLOT(nextFocus()));
 
     connect(this, SIGNAL(progresSetMax(QVariant)),
             rootObject2, SLOT(setMax(QVariant)));
@@ -105,6 +110,8 @@ SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
 
     connect(this, SIGNAL(setCompleterText(QVariant)),
             rootObject, SLOT(setCompleterText(QVariant)));
+    connect(this, SIGNAL(focusOff()),
+            rootObject, SLOT(focusOff()));
 
     connect(busyTimer, SIGNAL(timeout()),
             this, SLOT(updateBusyTimer()));
@@ -116,8 +123,6 @@ SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
     connect(&delayTimer, SIGNAL(timeout()),
             this, SLOT(delaySearchTimeout()));
 
-    view->setFocus();
-
 #else
     initializeUI();
     connect(searchPushButton, SIGNAL(clicked()),
@@ -137,7 +142,7 @@ SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
 
     searchWordLineEdit->setFocus();
 #endif
-
+    setFocus();
     busy = false;
     setEnabled(true);
     updateHistoryButtons(false,false,false);
@@ -147,6 +152,16 @@ SearchBarWidget::~SearchBarWidget() {
 
 }
 
+void SearchBarWidget::checkFocus(){
+    qDebug()<<"focus:"<<QApplication::focusWidget();
+    if(focusWidget()!=QApplication::focusWidget())
+        emit focusOff();
+}
+
+void SearchBarWidget::nextFocus(){
+    parentWidget()->nextInFocusChain()->setFocus();
+}
+
 void SearchBarWidget::textChange(QString text){
     QLineEdit line;
     QString toSend="";
@@ -250,7 +265,6 @@ QIcon SearchBarWidget::generateIcon(QIcon original, qreal rotation) {
 }
 
 void SearchBarWidget::setFocus() {
-    qDebug()<<"test3";
 #ifndef Q_WS_MAEMO_5
         view->setFocus();
 #else
index 0e84bee..d19ce21 100644 (file)
@@ -61,6 +61,7 @@ Q_SIGNALS:
     void progresSetValue(QVariant);
     void progresSetValue2(QVariant);
     void setCompleterText(QVariant);
+    void focusOff();
 
     //! Requests to search for a list of words matching a word passed as
     //! a parameter
@@ -135,6 +136,8 @@ public Q_SLOTS:
     void updateHistoryButtons(bool prev, bool next, bool list);
 
     void setFocus();
+    void nextFocus();
+    void checkFocus();
 
 private Q_SLOTS:
     //! Clears search word line edit
@@ -155,6 +158,7 @@ private Q_SLOTS:
 
     void prevCompleter();
 
+
 private:
 
     QVBoxLayout* mainLayout;
index 4c37017..bb070f2 100644 (file)
@@ -321,6 +321,7 @@ void SettingsWidget::changed() {
 #ifndef Q_WS_MAEMO_5
     void SettingsWidget::save() {
         _save = true;
+        qDebug()<<"tuuu";
         hide();
     }
 #endif
index d60a2f0..e54498a 100644 (file)
@@ -49,6 +49,8 @@ TranslationView::TranslationView(QWidget *parent) : QWebView(parent) {
 
     connect(this, SIGNAL(setUrl(QVariant)),
            rootObject, SLOT(setUrl(QVariant)));
+    emit setUrl(QDir::homePath() + "/.mdictionary/" + "html.html");
+
 #else
 
     realParent = qobject_cast<TranslationWidget*>(parent);
@@ -66,7 +68,8 @@ TranslationView::TranslationView(QWidget *parent) : QWebView(parent) {
 
     connect(searchSelectedAction, SIGNAL(triggered()),
             this, SIGNAL(search()));
-    connect(page(), SIGNAL(selectionChanged()), this, SLOT(selection()));
+    connect(page(), SIGNAL(selectionChanged()),
+            this, SLOT(selection()));
 
 #endif
 }
index dd94a40..953fdbc 100644 (file)
@@ -1,6 +1,6 @@
 import Qt 4.7
 
-BorderImage {
+Rectangle {
     id: button
     property alias textInButton: buttonText.text
     property bool checkable: false
@@ -14,7 +14,14 @@ BorderImage {
 
     function setText(string) { textInButton = qsTr(string); }
     function press() { mouseArea.pressed }
-    border { left: 10; top: 10; right: 10; bottom: 10 }
+
+    Keys.onReturnPressed: {
+        button.clicked();
+        if(checkable){
+            button.checked=!button.checked;
+            button.changeCheck();
+        }
+    }
 
     MouseArea {
         id: mouseArea
@@ -43,7 +50,7 @@ BorderImage {
 
     Image {
         id: image1
-        width:  (image1.sourceSize.width*parent.height)/image1.sourceSize.height +1 //(35*parent.height)/107 +1
+        width:  (image1.sourceSize.width*parent.height)/image1.sourceSize.height +1
         height: parent.height
         anchors.left: parent.left
         source: "qrc:/button/buttonLeft.png"
@@ -60,13 +67,15 @@ BorderImage {
         anchors.left: image1.right
         anchors.leftMargin: -1
         source: "qrc:/button/buttonCenter.png"
-        fillMode:Image.Stretch
+        //fillMode:Image.Stretch
+        fillMode: Image.TileHorizontally
+
     }
 
     Image {
         id: image2
         smooth: true
-        width: (image1.sourceSize.width*parent.height)/image1.sourceSize.height +1//(35*parent.height)/107 +1
+        width: (image1.sourceSize.width*parent.height)/image1.sourceSize.height +1
         height: parent.height
         anchors.right: parent.right
         source: "qrc:/button/buttonR.png"
@@ -89,6 +98,15 @@ BorderImage {
             PropertyChanges { target: image1; source: "qrc:/button/buttonLeftDisabled.png" }
             PropertyChanges { target: image3; source: "qrc:/button/buttonCenterDisabled.png" }
             PropertyChanges { target: image2; source: "qrc:/button/buttonRightDisabled.png" }
+        },
+        State {
+            name: "focused";
+            when: (button.enabled && !mouseArea.pressed && button.focus)
+
+            PropertyChanges { target: image1; source: "qrc:/button/buttonLeftFocus.png" }
+            PropertyChanges { target: image3; source: "qrc:/button/buttonCenterFocus.png" }
+            PropertyChanges { target: image2; source: "qrc:/button/buttonRFocus.png" }
         }
+
     ]
 }
index 7e96512..75046e3 100644 (file)
@@ -32,6 +32,12 @@ Image {
     property string pathToCheckedDicImage: "qrc:/button/checkboxCheckedDis.png"
     property string pathToUncheckedDicImage: "qrc:/button/checkboxDis.png"
     signal changed
+
+    Keys.onPressed: {
+        if (event.key == Qt.Key_Space)
+            selected=!selected
+    }
+
     height: {
         var aspectRatio = sourceSize.height / sourceSize.width
         return checkbox.width * aspectRatio
@@ -74,4 +80,21 @@ Image {
             }
         }
     }
+
+    Rectangle{
+        id: focusRectangle
+        color: "#000000"
+        border.color: "#000000"
+        opacity: 0;
+        radius: 1
+        anchors.centerIn: parent
+        z:1;
+    }
+
+    onFocusChanged: {
+        if(focus)
+            focusRectangle.opacity=0.5;
+        else
+            focusRectangle.opacity=0;
+    }
 }
index edd448d..42818ef 100644 (file)
@@ -72,10 +72,6 @@ Flickable {
         contentsScale: 1
         pressGrabTime: 0
 
-        Keys.onRightPressed: {
-            console.log("test");
-        }
-
         function doZoom(zoom,centerX,centerY)
         {
             var tempX = flickable.width/2 - parent.x*zoom;
index 9be44f1..37825a7 100644 (file)
@@ -33,17 +33,16 @@ Rectangle {
 
     Image {
         id: image1
-        z: 1
         smooth: true
-        width: (35*parent.height)/107 +1
+        width:  (image1.sourceSize.width*parent.height)/image1.sourceSize.height +1 //(35*parent.height)/107 +1
         height: parent.height
         anchors.left: parent.left
         source: "qrc:/button/buttonLeft.png"
+        fillMode:Image.Stretch
     }
 
     Image {
         id: image3
-        z: 1
         smooth: true
         height: parent.height
         anchors.right: image2.left
@@ -51,18 +50,20 @@ Rectangle {
         anchors.left: image1.right
         anchors.leftMargin: -1
         source: "qrc:/button/buttonCenter.png"
+        fillMode:Image.Stretch
+        //fillMode: Image.TileHorizontally
 
     }
 
 
     Image {
         id: image2
-        z: 1
         smooth: true
-        width: (35*parent.height)/107 +1
+        width: (image1.sourceSize.width*parent.height)/image1.sourceSize.height +1//(35*parent.height)/107 +1
         height: parent.height
         anchors.right: parent.right
         source: "qrc:/button/buttonR.png"
+        fillMode:Image.Stretch
     }
 
 
@@ -80,6 +81,14 @@ Rectangle {
             PropertyChanges { target: image1; source: "qrc:/button/buttonLeftPushed.png" }
             PropertyChanges { target: image3; source: "qrc:/button/buttonCenterPushed.png" }
             PropertyChanges { target: image2; source: "qrc:/button/buttonRightPushed.png" }
+        },
+        State {
+            name: "focused";
+            when: (rectangle.enabled && !mouseArea.pressed && rectangle.focus)
+
+            PropertyChanges { target: image1; source: "qrc:/button/buttonLeftFocus.png" }
+            PropertyChanges { target: image3; source: "qrc:/button/buttonCenterFocus.png" }
+            PropertyChanges { target: image2; source: "qrc:/button/buttonRFocus.png" }
         }
     ]
 }
index 54fe47d..a30ecba 100644 (file)
@@ -30,6 +30,10 @@ Rectangle {
         return value;
     }
 
+    function setFocus(){
+        text_input1.focus=true;
+    }
+
     TextInput {
         id: text_input1
         x: 1
@@ -52,6 +56,15 @@ Rectangle {
             if(focus==false)
                text=stringToInt(text);
         }
+        Keys.onPressed: {
+            if (event.key == Qt.Key_Up)
+                text_input1.text=((stringToInt(text_input1.text)+singleStep)>maxValue)?(maxValue):(stringToInt(text_input1.text)+singleStep);
+            else if (event.key == Qt.Key_Down){
+                text_input1.text=((stringToInt(text_input1.text)-singleStep)<minValue)?(minValue):(stringToInt(text_input1.text)-singleStep);
+                if(isTextInMinValue && stringToInt(text_input1.text)==minValue)
+                    text_input1.text=textInMinValue
+            }
+        }
     }
 
     Timer {
@@ -148,5 +161,11 @@ Rectangle {
             timerDown.running=true;
         }
     }
+    states: [
+        State {
+            name: "focusState"; when: text_input1.focus && rectangle1.enabled;
+            PropertyChanges { target: rectangle1; border.width: 2 }
+        }
+    ]
 }
 
index 28f697c..004e198 100644 (file)
@@ -13,6 +13,9 @@ Rectangle {
     signal nextCompleter();
     signal prevCompleter();
 
+    signal isFocused();
+    signal checkFocus();
+
     function setText(string) { textInLineEdit = string; }
     function setCompleter(string) { completerItemText.text=string; }
     function hideCompleter() { completerItem.visible=false; }
@@ -27,6 +30,13 @@ Rectangle {
         completerItemText.text="";
     }
 
+    function setFocus(){
+        if(rectangle1.enabled){
+            text_input1.focus=true;
+            isFocused();
+        }
+    }
+
     TextInput {
         id: text_input1
         width: rectangle1.width-20
@@ -43,7 +53,6 @@ Rectangle {
             else
                completerItem.visible=false;
         }
-        focus: rectangle1.focus;
         Keys.priority : Keys.AfterItem
         Keys.onPressed: {
             if ((event.key == Qt.Key_Enter) || (event.key == Qt.Key_Return)){
@@ -70,6 +79,8 @@ Rectangle {
             }
         }
         onTextChanged: rectangle1.textChange(text);
+        onFocusChanged: if(focus) isFocused();
+        onActiveFocusChanged: rectangle1.checkFocus();
     }
 
 
@@ -102,14 +113,14 @@ Rectangle {
 
     states: [
         State {
-            name: "FokusState"; when: text_input1.focus==true && rectangle1.enabled==true;
+            name: "FokusState"; when: text_input1.focus && rectangle1.enabled;
             PropertyChanges {
                 target: rectangle1
                 border.width: 3
             }
         },
         State {
-            name: "DisableState"; when: rectangle1.enabled==false;
+            name: "DisableState"; when: !rectangle1.enabled;
             PropertyChanges { target: shadeDisable; z: 3; opacity: 0.5 }
         }
     ]
index 2210de8..6590e45 100644 (file)
@@ -6,6 +6,8 @@ Rectangle {
     color : myPalette.window;
 
     property alias enableLineEdit: inputSearchText.enabled;
+    property int focusIndex:-1;
+
 
     function setButtonText(string) { searchButton.setText(string) }
     function setEnableHistoryPrev(Boolean) { historyPrevToolButton.enabled = Boolean }
@@ -17,15 +19,63 @@ Rectangle {
     function setLineEditText(string) { inputSearchText.setText(string) }
     function setCompleterText(string) { inputSearchText.setCompleter(string) }
 
+    function setFocus(){
+        if(focusIndex==0){
+            inputSearchText.setFocus();
+            if(!inputSearchText.enabled)
+                focusIndex++;
+        }
+        if(focusIndex==1)
+            clearButton.focus=true;
+
+        if(focusIndex==2){
+            searchButton.focus = true
+            if(!searchButton.enabled)
+                focusIndex++;
+        }
+
+        if(focusIndex==3){
+            historyPrevToolButton.focus = true
+            if(!historyPrevToolButton.enabled)
+                focusIndex++;
+        }
+
+        if(focusIndex==4){
+            historyShowToolButton.focus = true
+            if(!historyShowToolButton.enabled)
+                focusIndex++;
+        }
+
+        if(focusIndex==5){
+            historyNextToolButton.focus = true
+            if(!historyNextToolButton.enabled)
+                focusIndex++;
+        }
+
+        if (focusIndex>5){
+            nextFocus();
+            focusIndex=-1;
+            focus=true;
+        }
+    }
+
+    function focusOff(){ focus=true; focusIndex=-1;}
+
+    signal nextFocus();
+    signal checkFocus();
     signal searchButtonClicked(string text);
     signal historyNextToolButtonClicked;
     signal historyShowToolButtonClicked;
     signal historyPrevToolButtonClicked;
     signal textChange(string text);
-
     signal nextCompleter();
     signal prevCompleter();
 
+    Keys.onTabPressed: {
+        focusIndex++;
+        setFocus();
+    }
+
     MyTextLineEdit{
         id: inputSearchText
         width: (searchBarWidget.width - (searchBarWidget.height*3.5 + searchButton.width + 9));
@@ -33,7 +83,6 @@ Rectangle {
         anchors.left: parent.left
         anchors.verticalCenter: parent.verticalCenter
         onEnterPressed: searchBarWidget.searchButtonClicked(text);
-        focus: searchBarWidget.focus
         useCompleter: true;
         IconButton {
             id: clearButton;
@@ -45,10 +94,13 @@ Rectangle {
             enabled: true;
             pathToIcon: "qrc:/button/go-clear.png";
             onClicked: clear();
+            onActiveFocusChanged:searchBarWidget.checkFocus();
         }
         onTextChange: searchBarWidget.textChange(text)
         onNextCompleter: searchBarWidget.nextCompleter();
         onPrevCompleter: searchBarWidget.prevCompleter();
+        onIsFocused: searchBarWidget.focusIndex=0;
+        onCheckFocus: searchBarWidget.checkFocus();
     }
 
     Item {
@@ -70,6 +122,7 @@ Rectangle {
                 searchBarWidget.searchButtonClicked(inputSearchText.textInLineEdit);
                 inputSearchText.hideCompleter();
             }
+            onActiveFocusChanged:searchBarWidget.checkFocus();
         }
 
         IconButton {
@@ -81,6 +134,7 @@ Rectangle {
             enabled: true;
             pathToIcon: "qrc:/button/go-next.png";
             onClicked: searchBarWidget.historyNextToolButtonClicked();
+            onActiveFocusChanged:searchBarWidget.checkFocus();
         }
 
         IconButton {
@@ -93,6 +147,7 @@ Rectangle {
             enabled: true;
             pathToIcon: "qrc:/button/go-show.png";
             onClicked: searchBarWidget.historyShowToolButtonClicked();
+            onActiveFocusChanged:searchBarWidget.checkFocus();
         }
 
         IconButton {
@@ -105,6 +160,7 @@ Rectangle {
             enabled: true;
             pathToIcon: "qrc:/button/go-previous.png";
             onClicked: searchBarWidget.historyPrevToolButtonClicked();
+            onActiveFocusChanged:searchBarWidget.checkFocus();
         }
     }
 }
index b51f788..5f32049 100644 (file)
@@ -7,6 +7,9 @@ Rectangle {
 
     SystemPalette { id: myPalette; colorGroup: SystemPalette.Active }
     color : myPalette.window;
+    focus: true;
+    property int focusIndex:-1;
+
 
     signal historySizeValueChanged(int intiger);
     signal searchResulValueChanged(int intiger);
@@ -40,6 +43,42 @@ Rectangle {
     function setCheckedDictionaries(bool){
         dictionariesCheckBox.selected=bool;
     }
+    function setFocus(){
+        if(focusIndex==0){
+            searchSpinbox.setFocus();
+            if(!searchSpinbox.enabled)
+                focusIndex++;
+        }
+        if(focusIndex==1){
+            historySpinbox.setFocus();
+            if(!historySpinbox.enabled)
+                focusIndex++;
+        }
+        if(focusIndex==2){
+            bookmarkCheckBox.focus = true
+            if(!bookmarkCheckBox.enabled)
+                focusIndex++;
+        }
+        if(focusIndex==3){
+            dictionariesCheckBox.focus = true
+            if(!dictionariesCheckBox.enabled)
+                focusIndex++;
+        }
+        if(focusIndex==4){
+            saveButton.focus = true
+            if(!saveButton.enabled)
+                focusIndex++;
+        }
+        if(focusIndex>4){
+            focusIndex=-1;
+            focus=true;
+        }
+    }
+
+    Keys.onTabPressed: {
+        focusIndex++;
+        setFocus();
+    }
 
     Text {
         id: textSearch
@@ -107,6 +146,7 @@ Rectangle {
         anchors.left: textSearch.right
         anchors.leftMargin: 20
         onValueChange: rectangle1.searchResulValueChanged(intiger);
+        onFocusChanged: if(focus) focusIndex=0;
     }
 
     MySpinBox {
@@ -117,6 +157,7 @@ Rectangle {
         anchors.top: searchSpinbox.bottom
         anchors.topMargin: 10
         onValueChange: rectangle1.historySizeValueChanged(intiger);
+        onFocusChanged: if(focus) focusIndex=1;
     }
 
     Checkbox {
@@ -128,6 +169,7 @@ Rectangle {
         anchors.top: textSearchIn.bottom
         anchors.topMargin: 0
         onChanged: rectangle1.bookmarksCheckBoxChanged(selected);
+        onFocusChanged: if(focus) focusIndex=2;
     }
 
     Checkbox {
@@ -138,6 +180,7 @@ Rectangle {
         anchors.top: bookmarkCheckBox.bottom
         anchors.topMargin: 3
         onChanged: rectangle1.dictionariesCheckBoxChanged(selected);
+        onFocusChanged: if(focus) focusIndex=3;
     }
 
     Button {
@@ -149,6 +192,7 @@ Rectangle {
         anchors.right: parent.right
         anchors.rightMargin: 5
         textInButton: qsTr("Save")
-        //onClicked: rectangle1.saveButtonClicked();
+        onClicked: rectangle1.saveButtonClicked();
+        onFocusChanged: if(focus) focusIndex=4;
     }
 }
index c0a546a..79cd850 100644 (file)
@@ -18,6 +18,7 @@ Rectangle {
         id: webView
         width:parent.width
         height:parent.height
+        url:"file:///home/jakub/test.html";
         anchors { top: headerSpace.bottom; left: parent.left; right: parent.right; bottom: parent.bottom }
     }