Fix random error when remove word from bookmark. Fix show all bookmark feature.
[mdictionary] / src / mdictionary / gui / WordListModel.cpp
index 2999778..6a167a8 100644 (file)
@@ -8,7 +8,7 @@ WordListModel::WordListModel(QObject *parent) :
     roles[IsBookmarkedRole] = "isBookmarked";
     roles[NumberRole] = "number";
     setRoleNames(roles);
-
+    isBookmarkModeActive = false;
 
 }
 
@@ -39,6 +39,7 @@ void WordListModel::clear()
     if (!empty){
         endResetModel();
     }
+    isBookmarkModeActive = false;
 }
 
 QVariant WordListModel::data(const QModelIndex &index, int role) const
@@ -83,8 +84,8 @@ void WordListModel::setTranslations(QHash<QString, QList<Translation *> > transl
         i.next();
         addWord(i.key(), i.value(), wordsInBookmarks[i.key()]);
     }
-    //todo: repair sorting
-//    sort(0);
+
+    sort(0);
 
 }
 
@@ -94,41 +95,15 @@ void WordListModel::sort(int column, Qt::SortOrder order)
         return;
 
     int left = 0;
-    int right = _wordList.count();
+    int right = _wordList.count() - 1;
 
     if (left < right){
         if (order == Qt::AscendingOrder){
-            ascendingQuickSort(left, right);
+            qSort(_wordList.begin(), _wordList.end());
         } else if (order == Qt::DescendingOrder) {
-            descendingQuickSort(left, right);
-        }
-    }
-}
-
-void WordListModel::ascendingQuickSort(int left, int right){
-    int m = left;
-    for(int i = left+1; i < right; i++){
-        if(_wordList[i] < _wordList[left]){
-            _wordList.swap(++m, i);
+            qSort(_wordList.begin(), _wordList.end(), qGreater<QString>());
         }
     }
-
-    _wordList.swap(left, m);
-    ascendingQuickSort(left, m - 1);
-    ascendingQuickSort(m + 1, right);
-}
-
-void WordListModel::descendingQuickSort(int left, int right){
-    int m = left;
-    for(int i = left+1; i < right; i++){
-        if(_wordList[i] > _wordList[left]){
-            _wordList.swap(++m, i);
-        }
-    }
-
-    _wordList.swap(left, m);
-    ascendingQuickSort(left, m - 1);
-    ascendingQuickSort(m + 1, right);
 }
 
 void WordListModel::setModelProperty(int index, const QVariant value, QString role)
@@ -141,12 +116,7 @@ void WordListModel::setModelProperty(int index, const QVariant value, QString ro
 
 int WordListModel::setDataPriv(int index, const QVariant &value, int role)
 {
-    bool bookmarksOnly = false;
-    if (_wordInBookmarks.values().count(false) == 0){
-        bookmarksOnly = true;
-    }
-
-    if (index < 0 || index > _translations.count())
+    if (index < 0 || index > _translations.count() - 1)
         return 0;
 
     QString word = _wordList[index];
@@ -159,12 +129,11 @@ 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 {                
                 Q_EMIT removeFromBookmarks(word);
-                if (bookmarksOnly == true){
+                if (isBookmarkModeActive == true){
                     beginRemoveRows(QModelIndex(), index, index + 1);
                     this->_translations.remove(_wordList[index]);
                     this->_wordInBookmarks.remove(_wordList[index]);
@@ -172,6 +141,7 @@ int WordListModel::setDataPriv(int index, const QVariant &value, int role)
                     endRemoveRows();
                 }
             }
+            Q_EMIT dataChanged(this->index(0), this->index(_translations.count() - 1));
             return 2;
         }
         else
@@ -181,3 +151,8 @@ int WordListModel::setDataPriv(int index, const QVariant &value, int role)
     }
     return 0;
 }
+
+void WordListModel::setBookmarkModeActive(bool mode)
+{
+    isBookmarkModeActive = mode;
+}