From 81211b66843c093170d0ea7b00aae3e3ecb3db9c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marcin=20Ka=C5=BAmierczak?= Date: Tue, 18 Jan 2011 16:11:35 +0100 Subject: [PATCH] complete WordListWidget with model. --- src/mdictionary/gui/WordListModel.cpp | 64 ++++++++++++++++++-- src/mdictionary/gui/WordListModel.h | 13 ++--- src/mdictionary/gui/WordListWidget.cpp | 4 +- src/mdictionary/qml/WordListWidget.qml | 100 +++++++++++++++++++------------- 4 files changed, 128 insertions(+), 53 deletions(-) diff --git a/src/mdictionary/gui/WordListModel.cpp b/src/mdictionary/gui/WordListModel.cpp index 95610ed..2999778 100644 --- a/src/mdictionary/gui/WordListModel.cpp +++ b/src/mdictionary/gui/WordListModel.cpp @@ -1,6 +1,6 @@ #include "WordListModel.h" -WordListModel::WordListModel(/*QHash > translations, QHash wordsInBookmarks, */QObject *parent) : +WordListModel::WordListModel(QObject *parent) : QAbstractListModel(parent) { QHash roles; @@ -9,9 +9,7 @@ WordListModel::WordListModel(/*QHash > translation roles[NumberRole] = "number"; setRoleNames(roles); - //setTranslations(translations, wordsInBookmarks); -// connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged())); } int WordListModel::rowCount(const QModelIndex &parent) const @@ -85,6 +83,52 @@ void WordListModel::setTranslations(QHash > transl i.next(); addWord(i.key(), i.value(), wordsInBookmarks[i.key()]); } + //todo: repair sorting +// sort(0); + +} + +void WordListModel::sort(int column, Qt::SortOrder order) +{ + if (column != 0) + return; + + int left = 0; + int right = _wordList.count(); + + if (left < right){ + if (order == Qt::AscendingOrder){ + ascendingQuickSort(left, right); + } else if (order == Qt::DescendingOrder) { + descendingQuickSort(left, right); + } + } +} + +void WordListModel::ascendingQuickSort(int left, int right){ + int m = left; + for(int i = left+1; i < right; i++){ + if(_wordList[i] < _wordList[left]){ + _wordList.swap(++m, i); + } + } + + _wordList.swap(left, m); + ascendingQuickSort(left, m - 1); + ascendingQuickSort(m + 1, right); +} + +void WordListModel::descendingQuickSort(int left, int right){ + int m = left; + for(int i = left+1; i < right; i++){ + if(_wordList[i] > _wordList[left]){ + _wordList.swap(++m, i); + } + } + + _wordList.swap(left, m); + ascendingQuickSort(left, m - 1); + ascendingQuickSort(m + 1, right); } void WordListModel::setModelProperty(int index, const QVariant value, QString role) @@ -97,6 +141,11 @@ void WordListModel::setModelProperty(int index, const QVariant value, QString ro int WordListModel::setDataPriv(int index, const QVariant &value, int role) { + bool bookmarksOnly = false; + if (_wordInBookmarks.values().count(false) == 0){ + bookmarksOnly = true; + } + if (index < 0 || index > _translations.count()) return 0; @@ -113,8 +162,15 @@ int WordListModel::setDataPriv(int index, const QVariant &value, int role) Q_EMIT dataChanged(this->index(0), this->index(_translations.count() - 1)); if (_wordInBookmarks[word] == true){ Q_EMIT addToBookmarks(word); - } else { + } else { Q_EMIT removeFromBookmarks(word); + if (bookmarksOnly == true){ + beginRemoveRows(QModelIndex(), index, index + 1); + this->_translations.remove(_wordList[index]); + this->_wordInBookmarks.remove(_wordList[index]); + this->_wordList.removeAt(index); + endRemoveRows(); + } } return 2; } diff --git a/src/mdictionary/gui/WordListModel.h b/src/mdictionary/gui/WordListModel.h index a01d9ee..2db6e3b 100644 --- a/src/mdictionary/gui/WordListModel.h +++ b/src/mdictionary/gui/WordListModel.h @@ -17,7 +17,7 @@ public: NumberRole }; - explicit WordListModel(/*QHash > translations, QHash wordsInBookmarks, */QObject *parent = 0); + explicit WordListModel(QObject *parent = 0); int rowCount(const QModelIndex & parent = QModelIndex()) const; @@ -34,13 +34,9 @@ public: //! Clear model data and refresh UI void clear(); -signals: + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); -// //! Set index of current selected word -// /*! -// \param index word position in data list -// */ -// void itemSelected(int index); +signals: void addToBookmarks(QString word); @@ -64,6 +60,9 @@ private: QHash _wordInBookmarks; QList _wordList; + void ascendingQuickSort(int left, int right); + void descendingQuickSort(int left, int right); + }; #endif // WORDLISTMODEL_H diff --git a/src/mdictionary/gui/WordListWidget.cpp b/src/mdictionary/gui/WordListWidget.cpp index b52f55f..135e4ae 100644 --- a/src/mdictionary/gui/WordListWidget.cpp +++ b/src/mdictionary/gui/WordListWidget.cpp @@ -60,7 +60,6 @@ WordListWidget::WordListWidget(QWidget *parent): ctxt = qmlView->rootContext(); -// refreshDictsList(); ctxt->setContextProperty("wordModel", &(*listModel)); ctxt->setContextProperty("CheckedPath", "qrc:/icons/16x16/staron.png"); ctxt->setContextProperty("UncheckedPath", "qrc:/icons/16x16/staroff.png"); @@ -189,7 +188,6 @@ void WordListWidget::showSearchResults( listModel = new WordListModel(this); } listModel->setTranslations(result, wordsInBookmarks); - //todo: sort words #endif @@ -321,7 +319,7 @@ void WordListWidget::unlockList() { #ifdef Q_WS_MAEMO_5 setEnabled(true); #else - Q_EMIT setWordListState(false); + Q_EMIT setWordListState(true); #endif } diff --git a/src/mdictionary/qml/WordListWidget.qml b/src/mdictionary/qml/WordListWidget.qml index 750f856..07cf206 100644 --- a/src/mdictionary/qml/WordListWidget.qml +++ b/src/mdictionary/qml/WordListWidget.qml @@ -1,3 +1,27 @@ +/******************************************************************************* + + 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 . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ +/*! + author: Marcin Kaźmierczak +*/ + import Qt 4.7 Rectangle { @@ -8,12 +32,10 @@ Rectangle { } -// function setEnabled(Boolean) { wordList.enabled = Boolean; println(wordList.enabled) } // slot + function setEnabled(Boolean) { wordList.enabled = Boolean } // slot signal wordSelected(string word); - //? -// signal addToBookmarks(int nr); -// signal removeFromBookmarks(int nr); + SystemPalette { id: myPalette; colorGroup: SystemPalette.Active } @@ -24,7 +46,7 @@ Rectangle { ElementsListView{ id: wordList width: rectangle1.width -// height: rectangle1.height + anchors.fill: parent highlightResizeSpeed: 1000 @@ -38,49 +60,49 @@ Rectangle { else return check.height; } - Row { + + MouseArea{ anchors.fill: parent + onClicked: { + wordList.currentIndex = number + rectangle1.wordSelected(word) + } + } - 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) - } + Text { + id: wordText + anchors.verticalCenter: parent.verticalCenter + text: + { + if (word == "!@#$%"){ + qsTr("Can't find any matching words") + } else { + word } } + } + + Checkbox{ + id: check + width: wordText.height + selected: isBookmarked + pathToCheckedImage: CheckedPath + pathToUncheckedImage: UncheckedPath + anchors.right: parent.right + anchors.rightMargin: 5 - 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 - } + anchors.verticalCenter: parent.verticalCenter + onChanged: rectangle1.changeWordState(number, selected) + visible: { + if (word == "!@#$%"){ + false + } else { + true } } - } + } } -- 1.7.9.5