fixed wordListWidget acting when word list is empty
[mdictionary] / src / mdictionary / gui / WordListWidget.cpp
index c9acee2..af05ae0 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"
 WordListWidget::WordListWidget(QWidget *parent):
     QTreeView(parent) {
 
+    _isBookmarkModeActive = false;
+
     //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();
+
+    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)));
+    connect(this, SIGNAL(setWordListEmpty(QVariant)), rootObject, SLOT(setWordListEmpty(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;
@@ -97,21 +140,70 @@ void WordListWidget::showSearchResults(
     clear();
     searchResult = result;
 
-    model->setColumnCount(2);
-    model->setRowCount(result.count());
+#ifdef Q_WS_MAEMO
+    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->sort(0);
-    resizeColumns();    
+        model->setItem(0,item);
+    }
+#else
+
+    QHash<QString, bool> wordsInBookmarks;
+    QHashIterator<QString, QList<Translation *> > 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<Translation*>());
+//        wordsInBookmarks.insert("!@#$%", false);
+        Q_EMIT setWordListState(false);
+        Q_EMIT setWordListEmpty(true);
+    } else {
+        Q_EMIT setWordListEmpty(false);
+    }
+
+    if (listModel == 0){
+        listModel = new WordListModel(this);
+    }
+
+    listModel->setBookmarkModeActive(_isBookmarkModeActive);
+    _isBookmarkModeActive = false;
+    listModel->setTranslations(result, wordsInBookmarks);
+
+#endif
 
     setFocus();
 }
 
+#ifdef Q_WS_MAEMO
 void WordListWidget::wordClicked(QModelIndex index) {
     //we're getting translation based on data in index
     Q_EMIT showTranslation(
@@ -143,12 +235,14 @@ void WordListWidget::wordChecked(QModelIndex index) {
         Translation* t;
         bool onlyBookmarks = true;
         foreach(t, searchResult[item.data().toString()]) {
-            qDebug()<<t->isBookmark();
             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) {
@@ -157,8 +251,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
@@ -191,11 +300,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);
 
@@ -205,17 +309,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(true);
+#endif
 }
 
 void WordListWidget::clear() {
+#ifdef Q_WS_MAEMO_5
     model->clear();
+#else
+    if (listModel != 0){
+        listModel->clear();
+    }
+#endif
 
     QHash<QString, QList<Translation*> >::iterator i;
     for(i = searchResult.begin(); i != searchResult.end(); i++) {
@@ -226,3 +350,7 @@ void WordListWidget::clear() {
     }
     searchResult.clear();
 }
+
+void WordListWidget::bookmarkModeActive(){
+    _isBookmarkModeActive = true;
+}