From: Marcin Kaźmierczak Date: Thu, 13 Jan 2011 14:49:07 +0000 (+0100) Subject: fix bug with empty dictionary list. Add first version of WordListView (qml) X-Git-Url: http://vcs.maemo.org/git/?p=mdictionary;a=commitdiff_plain;h=346a68ad33b6a6946fbee3993cfc7b3cb9e1690d fix bug with empty dictionary list. Add first version of WordListView (qml) --- diff --git a/src/mdictionary/gui/DictManagerModel.cpp b/src/mdictionary/gui/DictManagerModel.cpp index ecb93bd..f2aee34 100644 --- a/src/mdictionary/gui/DictManagerModel.cpp +++ b/src/mdictionary/gui/DictManagerModel.cpp @@ -59,11 +59,18 @@ void DictManagerModel::setDictionaries(QHash dictio void DictManagerModel::clear() { - beginRemoveRows(QModelIndex(), 0, rowCount()); + bool empty = true; + if (!_dictionaries.isEmpty()){ +// beginRemoveRows(QModelIndex(), 0, rowCount()); + beginResetModel(); + empty = false; + } _dictionaries.clear(); _dictList.clear(); - endRemoveRows(); - Q_EMIT dataChanged(QModelIndex(), QModelIndex()); + + if (!empty){ + endResetModel(); + } } QVariant DictManagerModel::data(const QModelIndex & index, int role) const @@ -124,8 +131,6 @@ int DictManagerModel::setDataPriv(int index, const QVariant &value, int role) if (value.type() == QVariant::Bool) { _dictionaries[dictionary] = value.toBool(); -// if (index == _dictList.count()) -// emit dataChanged(this->index(index-1), this->index(index)); Q_EMIT dataChanged(this->index(0), this->index(_dictList.count() - 1)); return 2; } @@ -164,7 +169,6 @@ bool DictManagerModel::isCurrentDictSelected() Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const { Qt::ItemFlags fl = QAbstractItemModel::flags(index); - qDebug("lol1"); return (fl | Qt::ItemIsEditable); } diff --git a/src/mdictionary/gui/DictManagerWidget.h b/src/mdictionary/gui/DictManagerWidget.h index f988659..15befcb 100644 --- a/src/mdictionary/gui/DictManagerWidget.h +++ b/src/mdictionary/gui/DictManagerWidget.h @@ -35,7 +35,6 @@ #include "DictManagerModel.h" #include #include -#include /*! diff --git a/src/mdictionary/gui/WordListModel.cpp b/src/mdictionary/gui/WordListModel.cpp new file mode 100644 index 0000000..95610ed --- /dev/null +++ b/src/mdictionary/gui/WordListModel.cpp @@ -0,0 +1,127 @@ +#include "WordListModel.h" + +WordListModel::WordListModel(/*QHash > translations, QHash wordsInBookmarks, */QObject *parent) : + QAbstractListModel(parent) +{ + QHash roles; + roles[WordRole] = "word"; + roles[IsBookmarkedRole] = "isBookmarked"; + roles[NumberRole] = "number"; + setRoleNames(roles); + + //setTranslations(translations, wordsInBookmarks); + +// connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged())); +} + +int WordListModel::rowCount(const QModelIndex &parent) const +{ + return _wordList.count(); +} + +void WordListModel::addWord(QString word, QList translations, bool isBookmarked) +{ + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + _translations.insert(word, translations); + _wordList << word; + _wordInBookmarks.insert(word, isBookmarked); + endInsertRows(); +} + +void WordListModel::clear() +{ + bool empty = true; + if (!_wordList.isEmpty()){ + beginResetModel(); + empty = false; + } + _translations.clear(); + _wordInBookmarks.clear(); + _wordList.clear(); + if (!empty){ + endResetModel(); + } +} + +QVariant WordListModel::data(const QModelIndex &index, int role) const +{ + if (index.row() < 0 || index.row() > _translations.count()){ + return QVariant(); + } + + if (role == WordRole) + { + return _wordList[index.row()]; + } + if (role == NumberRole){ + return index.row(); + } + if (role == IsBookmarkedRole){ + return _wordInBookmarks[_wordList[index.row()]]; + } + return QVariant(); +} + +bool WordListModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + int res = setDataPriv(index.row(), value, role); + if (res == 0) + return false; + if (res > 0) + return true; + return true; +} + +Qt::ItemFlags WordListModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags fl = QAbstractItemModel::flags(index); + return (fl | Qt::ItemIsEditable); +} + +void WordListModel::setTranslations(QHash > translations, QHash wordsInBookmarks) +{ + QHashIterator > i(translations); + while (i.hasNext()) { + i.next(); + addWord(i.key(), i.value(), wordsInBookmarks[i.key()]); + } +} + +void WordListModel::setModelProperty(int index, const QVariant value, QString role) +{ + if (role.contains("isBookmarked")) + { + setDataPriv(index, value, IsBookmarkedRole); + } +} + +int WordListModel::setDataPriv(int index, const QVariant &value, int role) +{ + if (index < 0 || index > _translations.count()) + return 0; + + QString word = _wordList[index]; + if (role == WordRole) + return 1; + if (role == NumberRole) + return 1; + if (role == IsBookmarkedRole) + { + if (value.type() == QVariant::Bool) + { + _wordInBookmarks[word] = value.toBool(); + Q_EMIT dataChanged(this->index(0), this->index(_translations.count() - 1)); + if (_wordInBookmarks[word] == true){ + Q_EMIT addToBookmarks(word); + } else { + Q_EMIT removeFromBookmarks(word); + } + return 2; + } + else + { + return 0; + } + } + return 0; +} diff --git a/src/mdictionary/gui/WordListModel.h b/src/mdictionary/gui/WordListModel.h new file mode 100644 index 0000000..a01d9ee --- /dev/null +++ b/src/mdictionary/gui/WordListModel.h @@ -0,0 +1,69 @@ +#ifndef WORDLISTMODEL_H +#define WORDLISTMODEL_H + +#include +//#include "../../include/GUIInterface.h" +#include "../../include/translation.h" + +class WordListModel : public QAbstractListModel +{ + Q_OBJECT +public: + + enum DictTypeRoles + { + WordRole = Qt::UserRole + 1, + IsBookmarkedRole, + NumberRole + }; + + explicit WordListModel(/*QHash > translations, QHash wordsInBookmarks, */QObject *parent = 0); + + int rowCount(const QModelIndex & parent = QModelIndex()) const; + + QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; + bool setData(const QModelIndex &index, const QVariant &value, int role); + + Qt::ItemFlags flags(const QModelIndex &index) const; + + //! Replace model data and refresh ui. + /*! + \param dictionaries hash set (dictionary, is active) with dictionaries + */ + void setTranslations(QHash > translations, QHash wordsInBookmarks); + //! Clear model data and refresh UI + void clear(); + +signals: + +// //! Set index of current selected word +// /*! +// \param index word position in data list +// */ +// void itemSelected(int index); + + void addToBookmarks(QString word); + + void removeFromBookmarks(QString word); + +public slots: + + //! Set value at role in index row of data. + /*! + \param index word position in data list + \param value new value for role + \param role role name + */ + void setModelProperty(int index, const QVariant value, QString role); + +private: + int setDataPriv(int index, const QVariant &value, int role); + void addWord(QString word, QList translations, bool isBookmarked); + + QHash > _translations; + QHash _wordInBookmarks; + QList _wordList; + +}; + +#endif // WORDLISTMODEL_H diff --git a/src/mdictionary/gui/WordListWidget.cpp b/src/mdictionary/gui/WordListWidget.cpp index d568fb7..b52f55f 100644 --- a/src/mdictionary/gui/WordListWidget.cpp +++ b/src/mdictionary/gui/WordListWidget.cpp @@ -36,16 +36,54 @@ WordListWidget::WordListWidget(QWidget *parent): QTreeView(parent) { //creating new model to store words and stars +#ifdef Q_WS_MAEMO_5 model = new QStandardItemModel(this); setModel(model); - setHeaderHidden(true); - setRootIsDecorated(false); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //set our custom style to draw checkboxes as stars proxyStyle = new WordListProxyStyle(); setStyle(proxyStyle); + //for future use, set checkbox icons for maemo +// ctxt->setContextProperty("CheckedPath", "qrc:/icons/96x96/staron.png"); +// ctxt->setContextProperty("UncheckedPath", "qrc:/icons/96x96/staroff.png"); +#else + + listModel = new WordListModel(this); + + verticalLayout = new QVBoxLayout; + setLayout(verticalLayout); + + qmlView = new QDeclarativeView(this); + + ctxt = qmlView->rootContext(); + +// refreshDictsList(); + ctxt->setContextProperty("wordModel", &(*listModel)); + ctxt->setContextProperty("CheckedPath", "qrc:/icons/16x16/staron.png"); + ctxt->setContextProperty("UncheckedPath", "qrc:/icons/16x16/staroff.png"); + + qmlView->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/WordListWidget.qml")); + + QGraphicsObject *rootObject = qmlView->rootObject(); + + qmlView->setResizeMode(QDeclarativeView::SizeRootObjectToView); + + verticalLayout->addWidget(qmlView); + + connect(rootObject, SIGNAL(wordSelected(QString)), this, SLOT(wordClicked(QString))); + connect(listModel, SIGNAL(addToBookmarks(QString)), this, SLOT(addToBookmarks(QString))); + connect(listModel, SIGNAL(removeFromBookmarks(QString)), this, SLOT(removeFromBookmarks(QString))); + + connect(this, SIGNAL(setWordListState(QVariant)), rootObject, SLOT(setEnabled(QVariant))); + +#endif + + setHeaderHidden(true); + setRootIsDecorated(false); + //setting size of star in pixels, on maemo checboxes are much bigger #ifdef Q_WS_MAEMO_5 checkBoxWidth = 70; @@ -100,6 +138,7 @@ void WordListWidget::showSearchResults( clear(); searchResult = result; +#ifdef Q_WS_MAEMO if(searchResult.count()>0) { setEnabled(true); model->setColumnCount(2); @@ -122,10 +161,42 @@ void WordListWidget::showSearchResults( model->setItem(0,item); } +#else + + QHash wordsInBookmarks; + QHashIterator > i(result); + while (i.hasNext()){ + i.next(); + + bool bookmark = false; + Translation* t; + foreach(t, searchResult[i.key()]) { + if(t->isBookmark()) { + bookmark = true; + break; + } + } + wordsInBookmarks.insert(i.key(), bookmark); + } + + if (result.count() == 0){ + result.insert("!@#$%", QList()); + wordsInBookmarks.insert("!@#$%", false); + Q_EMIT setWordListState(false); + } + + if (listModel == 0){ + listModel = new WordListModel(this); + } + listModel->setTranslations(result, wordsInBookmarks); + //todo: sort words + +#endif setFocus(); } +#ifdef Q_WS_MAEMO void WordListWidget::wordClicked(QModelIndex index) { //we're getting translation based on data in index Q_EMIT showTranslation( @@ -173,8 +244,23 @@ void WordListWidget::wordChecked(QModelIndex index) { } } } +#else + void WordListWidget::wordClicked(QString word){ + emit showTranslation(searchResult[word]); + } + + void WordListWidget::addToBookmarks(QString word){ + emit addBookmark(searchResult[word]); + } + + void WordListWidget::removeFromBookmarks(QString word){ + emit removeBookmark(searchResult[word]); + } + +#endif +#ifdef Q_WS_MAEMO_5 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) { //firstly we normally handle this event @@ -207,11 +293,6 @@ void WordListWidget::resizeEvent(QResizeEvent *event) { QTreeView::resizeEvent(event); } -void WordListWidget::resizeColumns() { - setColumnWidth(0, viewport()->width() -checkBoxWidth - 5); - setColumnWidth(1, checkBoxWidth); -} - void WordListWidget::keyPressEvent(QKeyEvent *event) { QTreeView::keyPressEvent(event); @@ -221,17 +302,37 @@ void WordListWidget::keyPressEvent(QKeyEvent *event) { wordClicked(selectedIndexes().at(0)); } } +#endif + +void WordListWidget::resizeColumns() { + setColumnWidth(0, viewport()->width() -checkBoxWidth - 5); + setColumnWidth(1, checkBoxWidth); +} void WordListWidget::lockList() { +#ifdef Q_WS_MAEMO_5 setEnabled(false); +#else + Q_EMIT setWordListState(false); +#endif } void WordListWidget::unlockList() { +#ifdef Q_WS_MAEMO_5 setEnabled(true); +#else + Q_EMIT setWordListState(false); +#endif } void WordListWidget::clear() { +#ifdef Q_WS_MAEMO_5 model->clear(); +#else + if (listModel != 0){ + listModel->clear(); + } +#endif QHash >::iterator i; for(i = searchResult.begin(); i != searchResult.end(); i++) { diff --git a/src/mdictionary/gui/WordListWidget.h b/src/mdictionary/gui/WordListWidget.h index 9f7c3a2..2a95c37 100644 --- a/src/mdictionary/gui/WordListWidget.h +++ b/src/mdictionary/gui/WordListWidget.h @@ -29,8 +29,12 @@ #include #include +#include #include "../backbone/backbone.h" #include "WordListProxyStyle.h" +#include "WordListModel.h" +#include +#include /*! It allows user to select word to see its translation or to mark or unmark @@ -58,6 +62,8 @@ Q_SIGNALS: //! Requests to remove selected word from bookmarks void removeBookmark(QList); + void setWordListState(QVariant state); + public Q_SLOTS: //! Shows search results @@ -76,6 +82,7 @@ public Q_SLOTS: void clear(); protected: +#ifdef Q_WS_MAEMO_5 //! Reimplemented standard mouseReleaseEvent to check if user clicked on //! a word or on its star to emit suitable signal void mouseReleaseEvent(QMouseEvent *event); @@ -86,8 +93,9 @@ protected: //! Checks if user press return and if so displays translation of selected word void keyPressEvent( QKeyEvent * event); - +#endif private Q_SLOTS: +#ifdef Q_WS_MAEMO_5 //! Emits signal to show translation of clicked item. Signal is emitted //! only when a word was clicked. void wordClicked(QModelIndex index); @@ -95,7 +103,11 @@ private Q_SLOTS: //! Emits signal to show add or remove word from bookmarks. //! Signal is emitted only when a star was clicked. void wordChecked(QModelIndex index); - +#else + void wordClicked(QString word); + void addToBookmarks(QString word); + void removeFromBookmarks(QString word); +#endif private: @@ -113,6 +125,13 @@ private: //! Association between words and their translations QHash > searchResult; WordListProxyStyle* proxyStyle; + +#ifndef Q_WS_MAEMO_5 + QVBoxLayout* verticalLayout; + QDeclarativeView* qmlView; + QDeclarativeContext* ctxt; + WordListModel* listModel; +#endif }; #endif // WORDLISTWIDGET_H diff --git a/src/mdictionary/mdictionary.pro b/src/mdictionary/mdictionary.pro index a055f31..cf327d7 100644 --- a/src/mdictionary/mdictionary.pro +++ b/src/mdictionary/mdictionary.pro @@ -40,7 +40,8 @@ SOURCES += gui/main.cpp \ gui/NotifyManager.cpp \ gui/SpinBox.cpp \ gui/DictTypeModel.cpp \ - gui/DictManagerModel.cpp + gui/DictManagerModel.cpp \ + gui/WordListModel.cpp HEADERS += gui/MainWindow.h \ backbone/ConfigGenerator.h \ @@ -73,7 +74,8 @@ HEADERS += gui/MainWindow.h \ gui/NotifyManager.h \ gui/SpinBox.h \ gui/DictTypeModel.h \ - gui/DictManagerModel.h + gui/DictManagerModel.h \ + gui/WordListModel.h RESOURCES += ../../data/gui.qrc diff --git a/src/mdictionary/qml/Checkbox.qml b/src/mdictionary/qml/Checkbox.qml index c1a0ea7..3ef0fc3 100644 --- a/src/mdictionary/qml/Checkbox.qml +++ b/src/mdictionary/qml/Checkbox.qml @@ -27,6 +27,8 @@ import Qt 4.7 Image { id: checkbox property bool selected + property string pathToCheckedImage: "qrc:/button/checkboxChecked.png" + property string pathToUncheckedImage: "qrc:/button/checkbox.png" signal changed height: { var aspectRatio = sourceSize.height / sourceSize.width @@ -39,13 +41,13 @@ Image { name: "checked"; when: (checkbox.selected == true); - PropertyChanges { target: checkbox; source: "qrc:/button/checkboxChecked.png" } + PropertyChanges { target: checkbox; source: pathToCheckedImage } }, State { name: "unchecked"; when: (checkbox.selected == false); - PropertyChanges { target: checkbox; source: "qrc:/button/checkbox.png" } + PropertyChanges { target: checkbox; source: pathToUncheckedImage } } ] MouseArea{ diff --git a/src/mdictionary/qml/ProgressBar.qml b/src/mdictionary/qml/ProgressBar.qml index 2e102c3..b273a3d 100644 --- a/src/mdictionary/qml/ProgressBar.qml +++ b/src/mdictionary/qml/ProgressBar.qml @@ -68,6 +68,6 @@ Rectangle { anchors.verticalCenter: parent.verticalCenter color: "white" font.bold: true - text: (value>-1) ? (Math.floor((value - minimum) / (maximum - minimum) * 100) + '%') : ("???"); + text: (value>-1) ? (Math.floor((value - minimum) / (maximum - minimum) * 100) + '%') : (""); } } diff --git a/src/mdictionary/qml/WordListWidget.qml b/src/mdictionary/qml/WordListWidget.qml index 15b01c6..750f856 100644 --- a/src/mdictionary/qml/WordListWidget.qml +++ b/src/mdictionary/qml/WordListWidget.qml @@ -1,6 +1,90 @@ import Qt 4.7 Rectangle { - width: 100 - height: 62 + + function changeWordState(nr, state) { + wordList.currentIndex = nr + wordModel.setModelProperty(wordList.currentIndex, state, "isBookmarked") + + } + +// function setEnabled(Boolean) { wordList.enabled = Boolean; println(wordList.enabled) } // slot + + signal wordSelected(string word); + //? +// signal addToBookmarks(int nr); +// signal removeFromBookmarks(int nr); + + 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 + + delegate: Component{ + id: wordListDelegate + Item { + width: rectangle1.width + height: { + if (wordText.height + 4 > check.height) + return wordText.height + 4; + else + return check.height; + } + Row { + 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) + } + } + } + + 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 + } + } + } + + } + } + + } + + model: wordModel + } }