Fixed welcome screen layout
[mdictionary] / trunk / src / base / gui / WordListWidget.cpp
index bd3cf02..60cf791 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(QWidget *parent):
-    QListView(parent) {
+    QTreeView(parent) {
 
-    wordListModel = new QStringListModel();
+    model = new QStandardItemModel(this);
+    setModel(model);
+    setHeaderHidden(true);
+    setRootIsDecorated(false);
+    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
-    connect(this, SIGNAL(clicked(QModelIndex)),
-            this, SLOT(itemClicked(QModelIndex)));
+    setStyle(new WordListProxyStyle);
 
-
-    setModel(wordListModel);
+    #ifdef Q_WS_MAEMO_5
+        checkBoxWidth = 70;
+    #else
+        checkBoxWidth = 25;
+    #endif
 }
 
-void WordListWidget::addWord(QString word) {
-    int wordsCount = wordListModel->rowCount();
-
-    wordListModel->insertRow(wordsCount);
-
-    QModelIndex newWordIndex = wordListModel->index(wordsCount);
+void WordListWidget::addWord(QString word, int row) {
+    QStandardItem* item = new QStandardItem(word);
+    item->setFlags(item->flags() ^ Qt::ItemIsEditable);
+    QStandardItem* itemCheckBox = new QStandardItem();
+    itemCheckBox->setFlags(itemCheckBox->flags() ^ Qt::ItemIsEditable |
+                           Qt::ItemIsUserCheckable);
+
+    bool bookmark = false;
+    Translation* t;
+    foreach(t, searchResult[word]) {
+        if(t->isBookmark()) {
+            bookmark = true;
+            break;
+        }
+    }
 
-    wordListModel->setData(newWordIndex, word);
+    if(bookmark)
+        itemCheckBox->setCheckState(Qt::Checked);
+    else
+        itemCheckBox->setCheckState(Qt::Unchecked);
 
+    model->setItem(row,0, item);
+    model->setItem(row,1, itemCheckBox);
 }
 
-void WordListWidget::clear() {
-    int wordsCount = wordListModel->rowCount();
-
-    for(int i = 0; i < wordsCount; i++) {
-        wordListModel->removeRow(0);
-    }
-}
 
 void WordListWidget::showSearchResults(
-       QHash<QString, QList<Translation *> > result) {
-    clear();
+        QHash<QString, QList<Translation *> > result) {
+    model->clear();
     searchResult.clear();
 
+    model->setColumnCount(2);
+    model->setRowCount(result.count());
+
     searchResult = result;
+    int row=0;
     QHash<QString, QList<Translation*> >::iterator i;
     for(i = result.begin(); i != result.end(); i++) {
-           addWord(i.key());
+           addWord(i.key(), row++);
+    }
+
+    resizeColumns();
+    model->sort(0);
+}
+
+void WordListWidget::wordClicked(QModelIndex index) {
+    emit showTranslation(
+            searchResult[index.data().toString()]);
+}
+
+void WordListWidget::wordChecked(QModelIndex index) {
+    Qt::CheckState state =
+            Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
+
+    if(selectedIndexes().count()==0) return;
+    QModelIndex item = selectedIndexes().at(0);
+    if(!item.isValid()) return;
+
+    repaint();
+
+    if(state == Qt::Checked) {
+        emit addBookmark(searchResult[item.data().toString()]);
+    }
+    else {
+        emit removeBookmark(searchResult[item.data().toString()]);
     }
+}
+
+
+void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
+    QTreeView::mouseReleaseEvent(event);
 
-    wordListModel->sort(0, Qt::AscendingOrder);
 
-    scrollTo(model()->index(0,0));
+    QModelIndex index = indexAt(event->pos());
+    if(!index.isValid()) return;
+    if(selectedIndexes().count() == 0) return;
+
+    if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
+        return;
+
+    int c = index.column();
+    if(c==0)
+        wordClicked(index);
+    else
+        wordChecked(index);
+}
 
+void WordListWidget::resizeEvent(QResizeEvent *event) {
+    resizeColumns();
+    QTreeView::resizeEvent(event);
 }
 
-void WordListWidget::itemClicked(QModelIndex index) {
-    emit showTranslation(searchResult[index.model()->data(index).toString()]);
+void WordListWidget::resizeColumns() {
+    setColumnWidth(0, viewport()->width() -checkBoxWidth - 20);
+    setColumnWidth(1, checkBoxWidth);
 }
 
 void WordListWidget::lockList() {