+
/*******************************************************************************
This file is part of mDictionary.
#include <QDebug>
#include "../../includes/translation.h"
#include <QMultiHash>
+#include "WordListProxyStyle.h"
+
#ifdef Q_WS_MAEMO_5
#include <QMaemo5InformationBox>
#endif
WordListWidget::WordListWidget(QWidget *parent):
- QListView(parent) {
-
- wordListModel = new QStringListModel();
+ QTreeView(parent) {
+
+ //creating new model to store words and stars
+ model = new QStandardItemModel(this);
+ setModel(model);
+ setHeaderHidden(true);
+ setRootIsDecorated(false);
+ setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+
+ //set our custom style to draw checkboxes as stars
+ setStyle(new WordListProxyStyle);
+
+ //setting size of star in pixels, on maemo checboxes are much bigger
+ #ifdef Q_WS_MAEMO_5
+ checkBoxWidth = 70;
+ #else
+ checkBoxWidth = 25;
+ #endif
+}
- connect(this, SIGNAL(clicked(QModelIndex)),
- this, SLOT(itemClicked(QModelIndex)));
+void WordListWidget::addWord(QString word, int row) {
+ QStandardItem* item = new QStandardItem(word);
+
+ //we don't want to allow user to edit word
+ item->setFlags(item->flags() ^ Qt::ItemIsEditable);
+
+ QStandardItem* itemCheckBox = new QStandardItem();
+ //creating checkbox item
+ itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
+ Qt::ItemIsUserCheckable);
+
+ /*checking if word is already in bookmarks, information about that is
+ stored in its translation object (not all translations have to be in
+ bookmarks)*/
+ bool bookmark = false;
+ Translation* t;
+ foreach(t, searchResult[word]) {
+ if(t->isBookmark()) {
+ bookmark = true;
+ break;
+ }
+ }
+ if(bookmark)
+ itemCheckBox->setCheckState(Qt::Checked);
+ else
+ itemCheckBox->setCheckState(Qt::Unchecked);
- setModel(wordListModel);
+ //add item to model
+ model->setItem(row,0, item);
+ model->setItem(row,1, itemCheckBox);
}
-void WordListWidget::addWord(QString word) {
- int wordsCount = wordListModel->rowCount();
- wordListModel->insertRow(wordsCount);
+void WordListWidget::showSearchResults(
+ QHash<QString, QList<Translation *> > result) {
- QModelIndex newWordIndex = wordListModel->index(wordsCount);
+ clear();
+ qDebug()<<searchResult.count();
+ searchResult = result;
- wordListModel->setData(newWordIndex, word);
+ model->setColumnCount(2);
+ model->setRowCount(result.count());
+
+ int row=0;
+ QHash<QString, QList<Translation*> >::iterator i;
+ for(i = searchResult.begin(); i != searchResult.end(); i++) {
+ addWord(i.key(), row++);
+ }
+
+ resizeColumns();
+ model->sort(0);
}
-void WordListWidget::clear() {
- int wordsCount = wordListModel->rowCount();
+void WordListWidget::wordClicked(QModelIndex index) {
+ //we're getting translation based on data in index
+ //qDebug()<<searchResult[index.data().toString()].at(0)->dictionaryInfo();
+ Q_EMIT showTranslation(
+ searchResult[index.data().toString()]);
+}
+
+void WordListWidget::wordChecked(QModelIndex index) {
+
+ //save new item state
+ Qt::CheckState state =
+ Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
+
+
+
+ //getting index of item which contains word which should be added/removed
+ //from bookmarks
+ QModelIndex item = selectedIndexes().at(0);
+ if(!item.isValid()) return;
- for(int i = 0; i < wordsCount; i++) {
- wordListModel->removeRow(0);
+ //to shorten lag between clicking on star and its change
+ repaint();
+
+ //depending on new state emit suitable signal
+ if(state == Qt::Checked) {
+ Q_EMIT addBookmark(searchResult[item.data().toString()]);
+ }
+ else {
+ Q_EMIT removeBookmark(searchResult[item.data().toString()]);
}
}
-void WordListWidget::showSearchResults(
- QHash<QString, QList<Translation *> > result) {
- clear();
- searchResult.clear();
- searchResult = result;
- QHash<QString, QList<Translation*> >::iterator i;
- for(i = result.begin(); i != result.end(); i++) {
- addWord(i.key());
- }
+void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
+
+ //firstly we normally handle this event
+ QTreeView::mouseReleaseEvent(event);
+
+ //then we check at which item user clicked
+ QModelIndex index = indexAt(event->pos());
+ if(!index.isValid()) return;
- wordListModel->sort(0, Qt::AscendingOrder);
+ /*if there are no selected items we return, that occurs sometimes
+ on maemo, when user is scrolling list and clicks to stop the scroll,
+ system doesn't select item but emits mouseReleaseEvent*/
+ if(selectedIndexes().count() == 0) return;
- scrollTo(model()->index(0,0));
+ //if user doesn't click either on word or on star, return
+ if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
+ return;
+ int c = index.column();
+ if(c==0)
+ //if column is 0 user clicked word
+ wordClicked(index);
+ else
+ //else user clicked star
+ wordChecked(index);
}
-void WordListWidget::itemClicked(QModelIndex index) {
- emit showTranslation(searchResult[index.model()->data(index).toString()]);
+void WordListWidget::resizeEvent(QResizeEvent *event) {
+ resizeColumns();
+ QTreeView::resizeEvent(event);
+}
+
+void WordListWidget::resizeColumns() {
+ setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
+ setColumnWidth(1, checkBoxWidth);
}
void WordListWidget::lockList() {
void WordListWidget::unlockList() {
setEnabled(true);
}
+
+void WordListWidget::clear() {
+ model->clear();
+
+ QHash<QString, QList<Translation*> >::iterator i;
+ for(i = searchResult.begin(); i != searchResult.end(); i++) {
+ Translation*t;
+ foreach(t, i.value()) {
+ delete t;
+ }
+ }
+ searchResult.clear();
+}