Clean and order documentation in source files. Source ready to beta 2 release
[mdictionary] / src / mdictionary / gui / WordListWidget.cpp
index 8085a05..d568fb7 100644 (file)
 
 *******************************************************************************/
 
-//! \file WordListWidget.cpp
-//! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
+/*! \file WordListwidget.cpp
+    \brief Displays list of words found in dictionaries
+
+    \author Mateusz Półrola <mateusz.polrola@comarch.pl>
+*/
 
 #include "WordListWidget.h"
 #include "WordListProxyStyle.h"
-#include "../../common/translation.h"
+#include "../../include/translation.h"
+#include <QKeyEvent>
 
 
 WordListWidget::WordListWidget(QWidget *parent):
@@ -39,7 +43,8 @@ WordListWidget::WordListWidget(QWidget *parent):
     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
     //set our custom style to draw checkboxes as stars
-    setStyle(new WordListProxyStyle);
+    proxyStyle = new WordListProxyStyle();
+    setStyle(proxyStyle);
 
     //setting size of star in pixels, on maemo checboxes are much bigger
     #ifdef Q_WS_MAEMO_5
@@ -49,6 +54,12 @@ WordListWidget::WordListWidget(QWidget *parent):
     #endif
 }
 
+
+WordListWidget::~WordListWidget() {
+    if(proxyStyle)
+        delete proxyStyle;
+}
+
 void WordListWidget::addWord(QString word, int row) {
     QStandardItem* item = new QStandardItem(word);
 
@@ -89,17 +100,30 @@ void WordListWidget::showSearchResults(
     clear();
     searchResult = result;
 
-    model->setColumnCount(2);
-    model->setRowCount(result.count());
+    if(searchResult.count()>0) {
+        setEnabled(true);
+        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++);
+        int row=0;
+        QHash<QString, QList<Translation*> >::iterator i;
+        for(i = searchResult.begin(); i != searchResult.end(); i++) {
+               addWord(i.key(), row++);
+        }
+
+        model->sort(0);
+        resizeColumns();
+    }
+    else {
+        QStandardItem* item = new QStandardItem(tr("Can't find any matching words"));
+        item->setFlags(item->flags() ^ Qt::ItemIsEditable);
+        item->setTextAlignment(Qt::AlignCenter);
+        setEnabled(false);
+
+        model->setItem(0,item);
     }
 
-    model->sort(0);
-    resizeColumns();    
+    setFocus();
 }
 
 void WordListWidget::wordClicked(QModelIndex index) {
@@ -120,7 +144,7 @@ void WordListWidget::wordChecked(QModelIndex index) {
     QModelIndex item = selectedIndexes().at(0);
     if(!item.isValid()) return;
 
-    //to shorten lag between clicking on star and its change
+    //to shorten lag between clicking on a star and its change
     repaint();
 
     //depending on new state emit suitable signal
@@ -129,6 +153,24 @@ void WordListWidget::wordChecked(QModelIndex index) {
     }
     else {
         Q_EMIT removeBookmark(searchResult[item.data().toString()]);
+
+        Translation* t;
+        bool onlyBookmarks = true;
+        foreach(t, searchResult[item.data().toString()]) {
+            if(t->isBookmark() == 1 || t->isBookmark()==0) {
+
+                onlyBookmarks = false;
+                t->setBookmark(0);
+            }
+            else {
+                searchResult[item.data().toString()].removeAt(searchResult[item.data().toString()].indexOf(t));
+            }
+        }
+
+        if(onlyBookmarks) {
+            searchResult.remove(item.data().toString());
+            model->removeRow(item.row());
+        }
     }
 }
 
@@ -147,16 +189,16 @@ void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
     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 user doesn't click either on a word or on a 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
+        //if column is 0 user clicked on a word
         wordClicked(index);
     else
-        //else user clicked star
+        //else user clicked on a star
         wordChecked(index);
 }
 
@@ -170,6 +212,16 @@ void WordListWidget::resizeColumns() {
     setColumnWidth(1, checkBoxWidth);
 }
 
+void WordListWidget::keyPressEvent(QKeyEvent *event) {
+    QTreeView::keyPressEvent(event);
+
+    if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
+        if(selectedIndexes().count() == 0) return;
+
+        wordClicked(selectedIndexes().at(0));
+    }
+}
+
 void WordListWidget::lockList() {
     setEnabled(false);
 }