detect a disconect error in GooglePlugin
[mdictionary] / trunk / src / base / gui / WordListWidget.cpp
index 94fcde3..59f4724 100644 (file)
@@ -1,3 +1,4 @@
+
 /*******************************************************************************
 
     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(Backbone *backbone, QWidget *parent):
-    QListView(parent) {
+WordListWidget::WordListWidget(QWidget *parent):
+    QTreeView(parent) {
 
-    this->backbone = backbone;
+    //creating new model to store words and stars
+    model = new QStandardItemModel(this);
+    setModel(model);
+    setHeaderHidden(true);
+    setRootIsDecorated(false);
+    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
-    wordListModel = new QStringListModel();
+    //set our custom style to draw checkboxes as stars
+    setStyle(new WordListProxyStyle);
 
-    connect(backbone, SIGNAL(ready()),
-            this, SLOT(showSearchResults()));
+    //setting size of star in pixels, on maemo checboxes are much bigger
+    #ifdef Q_WS_MAEMO_5
+        checkBoxWidth = 70;
+    #else
+        checkBoxWidth = 25;
+    #endif
+}
+
+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;
+        }
+    }
 
-    connect(this, SIGNAL(clicked(QModelIndex)),
-            this, SLOT(itemClicked(QModelIndex)));
+    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);
-}
 
-void WordListWidget::clear() {
-    int wordsCount = wordListModel->rowCount();
+    model->setColumnCount(2);
+    model->setRowCount(result.count());
 
-    for(int i = 0; i < wordsCount; i++) {
-        wordListModel->removeRow(0);
+    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::showSearchResults() {
-    clear();
-    searchResult.clear();
-    QMultiHash<QString, Translation*> result = backbone->result();
+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()]);
+}
 
-    QMultiHash<QString, Translation*>::iterator i;
-    for(i = result.begin(); i != result.end(); i++) {
-        if(!searchResult.contains(i.key())) {
-           addWord(i.key());
-        }
-        searchResult[i.key()].push_back(i.value());
+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;
+
+    //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::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;
+
+    /*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;
+
+    //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);
+}
 
-    wordListModel->sort(0, Qt::AscendingOrder);
+void WordListWidget::resizeEvent(QResizeEvent *event) {
+    resizeColumns();
+    QTreeView::resizeEvent(event);
 }
 
-void WordListWidget::itemClicked(QModelIndex index) {
-    backbone->searchHtml(searchResult[index.model()->data(index).toString()]);
+void WordListWidget::resizeColumns() {
+    setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
+    setColumnWidth(1, checkBoxWidth);
+}
+
+void WordListWidget::lockList() {
+    setEnabled(false);
+}
+
+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();
 }