From 9a5bedfd71b6c1a09fe77aea909d3d78a771a6e8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marcin=20Ka=C5=BAmierczak?= Date: Mon, 28 Feb 2011 17:02:22 +0100 Subject: [PATCH] Add keyboard support to comboBox, HistoryList. Fix some issues with keyboard support. --- src/include/ComboBoxModel.cpp | 5 +++++ src/include/ComboBoxModel.h | 3 +++ src/mdictionary/gui/HistoryListDialog.cpp | 2 ++ src/mdictionary/gui/MainWindow.cpp | 4 +++- src/mdictionary/qml/ComboBox.qml | 23 +++++++++++++---------- src/mdictionary/qml/DictManagerWidget.qml | 4 ++++ src/mdictionary/qml/HistoryListDialog.qml | 14 ++++++++++++++ src/mdictionary/qml/WordListWidget.qml | 3 +++ 8 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/include/ComboBoxModel.cpp b/src/include/ComboBoxModel.cpp index 56963e1..35b1f87 100644 --- a/src/include/ComboBoxModel.cpp +++ b/src/include/ComboBoxModel.cpp @@ -89,3 +89,8 @@ void ComboBoxModel::addItem(QString item) _contents << item; endInsertRows(); } + +QString ComboBoxModel::valueOnPosition(int index) +{ + return _contents[index]; +} diff --git a/src/include/ComboBoxModel.h b/src/include/ComboBoxModel.h index ad568fb..8f91c37 100644 --- a/src/include/ComboBoxModel.h +++ b/src/include/ComboBoxModel.h @@ -63,6 +63,9 @@ public: void setSelectedItem(QString item); void setSelectedIndex(int index); +public Q_SLOTS: + QString valueOnPosition(int index); + private: void setContents(QList contents); void addItem(QString item); diff --git a/src/mdictionary/gui/HistoryListDialog.cpp b/src/mdictionary/gui/HistoryListDialog.cpp index 804f907..e5bb27c 100644 --- a/src/mdictionary/gui/HistoryListDialog.cpp +++ b/src/mdictionary/gui/HistoryListDialog.cpp @@ -55,6 +55,8 @@ HistoryListDialog::HistoryListDialog(QStringList words, QWidget *parent): QGraphicsObject *rootObject = view->rootObject(); + view->setFocus(); + connect(rootObject,SIGNAL(selectedRow(int)), this,SLOT(itemClicked(int))); diff --git a/src/mdictionary/gui/MainWindow.cpp b/src/mdictionary/gui/MainWindow.cpp index a4942aa..491b12b 100644 --- a/src/mdictionary/gui/MainWindow.cpp +++ b/src/mdictionary/gui/MainWindow.cpp @@ -235,7 +235,9 @@ void MainWindow::wordListReady() { setExactSearch(false); } } - wordListWidget->setFocus(); + WordListWidget* myWidget = qobject_cast(wordListWidget); + myWidget->setFocusOnElement(); +// wordListWidget->setFocus(); } bool MainWindow::checkExactSearch( diff --git a/src/mdictionary/qml/ComboBox.qml b/src/mdictionary/qml/ComboBox.qml index 5ceb5b5..56a0a40 100644 --- a/src/mdictionary/qml/ComboBox.qml +++ b/src/mdictionary/qml/ComboBox.qml @@ -27,18 +27,10 @@ Rectangle { signal valueSelected(string selected); - /* Keys.onPressed: { - console.log("tu ----"); - if (event.key == Qt.Key_Escape) - rectangle1.expanded=false; - else if (event.key == Qt.Key_Space) + Keys.onPressed: { + if (event.key == Qt.Key_Space) rectangle1.expanded=true; - else if (event.key == Qt.Key_Return){ - rectangle1.expanded=false; - console.log("tu enter "+list1.content); - } } - */ Text { id: text1 @@ -96,6 +88,17 @@ Rectangle { visible: false property string selected: rectangle1.startValue + Keys.onPressed: { + + if ((event.key == Qt.Key_Return || event.key == Qt.Key_Enter) && currentIndex >= 0){ + selectedValue(currentIndex, model.valueOnPosition(currentIndex)) + } + if (event.key == Qt.Key_Escape){ + rectangle1.show(!rectangle1.expanded) + event.accepted = true + } + } + function selectedValue(nr, value) { currentIndex = nr selected = value diff --git a/src/mdictionary/qml/DictManagerWidget.qml b/src/mdictionary/qml/DictManagerWidget.qml index a512f2a..d15be32 100644 --- a/src/mdictionary/qml/DictManagerWidget.qml +++ b/src/mdictionary/qml/DictManagerWidget.qml @@ -60,6 +60,10 @@ Rectangle { highlightResizeSpeed: 1000 Keys.onPressed: { + if ((currentIndex < 0 || currentIndex >= count) && (event.key == Qt.Key_Up || event.key == Qt.Key_Down)){ + currentIndex = 0 + } + if ((event.key == Qt.Key_Return || event.key == Qt.Key_Enter) && currentIndex >= 0){ itemActivated(currentIndex) } diff --git a/src/mdictionary/qml/HistoryListDialog.qml b/src/mdictionary/qml/HistoryListDialog.qml index feb7ad9..7b9ddc0 100644 --- a/src/mdictionary/qml/HistoryListDialog.qml +++ b/src/mdictionary/qml/HistoryListDialog.qml @@ -3,6 +3,9 @@ import Qt 4.7 Rectangle { SystemPalette { id: myPalette; colorGroup: SystemPalette.Active } color : myPalette.window; + function setFocus() { + historyList.setFocus() + } signal selectedRow(int nr) @@ -26,6 +29,17 @@ Rectangle { anchors.bottom: parent.bottom anchors.top: parent.top highlightResizeSpeed: 1000 + + Keys.onPressed: { + if ((currentIndex < 0 || currentIndex >= count) && (event.key == Qt.Key_Up || event.key == Qt.Key_Down)){ + currentIndex = 0 + } + + if ((event.key == Qt.Key_Return || event.key == Qt.Key_Enter) && currentIndex >= 0){ + selectedRow(currentIndex) + } + } + delegate: Component{ id: historyListDelegate Item { diff --git a/src/mdictionary/qml/WordListWidget.qml b/src/mdictionary/qml/WordListWidget.qml index 4cd5f97..e4168f9 100644 --- a/src/mdictionary/qml/WordListWidget.qml +++ b/src/mdictionary/qml/WordListWidget.qml @@ -59,6 +59,9 @@ Rectangle { 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){ -- 1.7.9.5