#include "WordListModel.h"
-WordListModel::WordListModel(/*QHash<QString, QList<Translation *> > translations, QHash<QString, bool> wordsInBookmarks, */QObject *parent) :
+WordListModel::WordListModel(QObject *parent) :
QAbstractListModel(parent)
{
QHash<int, QByteArray> roles;
roles[NumberRole] = "number";
setRoleNames(roles);
- //setTranslations(translations, wordsInBookmarks);
-// connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged()));
}
int WordListModel::rowCount(const QModelIndex &parent) const
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)
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;
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;
}
NumberRole
};
- explicit WordListModel(/*QHash<QString, QList<Translation*> > translations, QHash<QString, bool> wordsInBookmarks, */QObject *parent = 0);
+ explicit WordListModel(QObject *parent = 0);
int rowCount(const QModelIndex & parent = QModelIndex()) const;
//! 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);
QHash<QString, bool > _wordInBookmarks;
QList<QString> _wordList;
+ void ascendingQuickSort(int left, int right);
+ void descendingQuickSort(int left, int right);
+
};
#endif // WORDLISTMODEL_H
ctxt = qmlView->rootContext();
-// refreshDictsList();
ctxt->setContextProperty("wordModel", &(*listModel));
ctxt->setContextProperty("CheckedPath", "qrc:/icons/16x16/staron.png");
ctxt->setContextProperty("UncheckedPath", "qrc:/icons/16x16/staroff.png");
listModel = new WordListModel(this);
}
listModel->setTranslations(result, wordsInBookmarks);
- //todo: sort words
#endif
#ifdef Q_WS_MAEMO_5
setEnabled(true);
#else
- Q_EMIT setWordListState(false);
+ Q_EMIT setWordListState(true);
#endif
}
+/*******************************************************************************
+
+ 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 {
}
-// 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 }
ElementsListView{
id: wordList
width: rectangle1.width
-// height: rectangle1.height
+
anchors.fill: parent
highlightResizeSpeed: 1000
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
}
}
-
}
+
}
}