Fix random error when remove word from bookmark. Fix show all bookmark feature.
[mdictionary] / src / mdictionary / gui / WordListModel.cpp
index 95610ed..6a167a8 100644 (file)
@@ -1,6 +1,6 @@
 #include "WordListModel.h"
 
-WordListModel::WordListModel(/*QHash<QString, QList<Translation *> > translations, QHash<QString, bool> wordsInBookmarks, */QObject *parent) :
+WordListModel::WordListModel(QObject *parent) :
     QAbstractListModel(parent)
 {
     QHash<int, QByteArray> roles;
@@ -8,10 +8,8 @@ WordListModel::WordListModel(/*QHash<QString, QList<Translation *> > translation
     roles[IsBookmarkedRole] = "isBookmarked";
     roles[NumberRole] = "number";
     setRoleNames(roles);
+    isBookmarkModeActive = false;
 
-    //setTranslations(translations, wordsInBookmarks);
-
-//    connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged()));
 }
 
 int WordListModel::rowCount(const QModelIndex &parent) const
@@ -41,6 +39,7 @@ void WordListModel::clear()
     if (!empty){
         endResetModel();
     }
+    isBookmarkModeActive = false;
 }
 
 QVariant WordListModel::data(const QModelIndex &index, int role) const
@@ -85,6 +84,26 @@ void WordListModel::setTranslations(QHash<QString, QList<Translation *> > transl
         i.next();
         addWord(i.key(), i.value(), wordsInBookmarks[i.key()]);
     }
+
+    sort(0);
+
+}
+
+void WordListModel::sort(int column, Qt::SortOrder order)
+{
+    if (column != 0)
+        return;
+
+    int left = 0;
+    int right = _wordList.count() - 1;
+
+    if (left < right){
+        if (order == Qt::AscendingOrder){
+            qSort(_wordList.begin(), _wordList.end());
+        } else if (order == Qt::DescendingOrder) {
+            qSort(_wordList.begin(), _wordList.end(), qGreater<QString>());
+        }
+    }
 }
 
 void WordListModel::setModelProperty(int index, const QVariant value, QString role)
@@ -97,7 +116,7 @@ void WordListModel::setModelProperty(int index, const QVariant value, QString ro
 
 int WordListModel::setDataPriv(int index, const QVariant &value, int role)
 {
-    if (index < 0 || index > _translations.count())
+    if (index < 0 || index > _translations.count() - 1)
         return 0;
 
     QString word = _wordList[index];
@@ -110,12 +129,19 @@ int WordListModel::setDataPriv(int index, const QVariant &value, int role)
         if (value.type() == QVariant::Bool)
         {
             _wordInBookmarks[word] = value.toBool();
-            Q_EMIT dataChanged(this->index(0), this->index(_translations.count() - 1));
             if (_wordInBookmarks[word] == true){
                 Q_EMIT addToBookmarks(word);
-            } else {
+            } else {                
                 Q_EMIT removeFromBookmarks(word);
+                if (isBookmarkModeActive == true){
+                    beginRemoveRows(QModelIndex(), index, index + 1);
+                    this->_translations.remove(_wordList[index]);
+                    this->_wordInBookmarks.remove(_wordList[index]);
+                    this->_wordList.removeAt(index);
+                    endRemoveRows();
+                }
             }
+            Q_EMIT dataChanged(this->index(0), this->index(_translations.count() - 1));
             return 2;
         }
         else
@@ -125,3 +151,8 @@ int WordListModel::setDataPriv(int index, const QVariant &value, int role)
     }
     return 0;
 }
+
+void WordListModel::setBookmarkModeActive(bool mode)
+{
+    isBookmarkModeActive = mode;
+}