Fix bug with app crash on exit. Add keyboard support in wordList, DictTypeSelectDialo...
[mdictionary] / src / mdictionary / gui / DictManagerModel.cpp
index 4714730..7b8807c 100644 (file)
@@ -1,8 +1,36 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+/*! \file DictManagerModel.cpp
+    \brief Contains dictionaries data for qml UI
+
+    \author Marcin Kaźmierczak <marcin.kazmierczak@comarch.pl>
+*/
+
 #include "DictManagerModel.h"
 
 DictManagerModel::DictManagerModel(QHash<CommonDictInterface *, bool> dictionaries, QObject *parent) :
     QAbstractListModel(parent)
 {
+    _currentIndex = 0;
     QHash<int, QByteArray> roles;
     roles[NameRole] = "name";
     roles[IconPathRole] = "iconPath";
@@ -31,11 +59,18 @@ void DictManagerModel::setDictionaries(QHash<CommonDictInterface *, bool> dictio
 
 void DictManagerModel::clear()
 {
-    beginRemoveRows(QModelIndex(), 0, rowCount());
+    bool empty = true;
+    if (!_dictionaries.isEmpty()){
+//        beginRemoveRows(QModelIndex(), 0, rowCount());
+        beginResetModel();
+        empty = false;
+    }
     _dictionaries.clear();
     _dictList.clear();
-    endRemoveRows();
-    emit dataChanged(QModelIndex(), QModelIndex());
+
+    if (!empty){
+        endResetModel();
+    }
 }
 
 QVariant DictManagerModel::data(const QModelIndex & index, int role) const
@@ -81,7 +116,7 @@ bool DictManagerModel::setData(const QModelIndex &index, const QVariant &value,
 
 int DictManagerModel::setDataPriv(int index, const QVariant &value, int role)
 {
-    if (index < 0 || index > _dictList.count())
+    if (index < 0 || index > _dictList.count() - 1)
         return 0;
 
     CommonDictInterface* dictionary = _dictList[index];
@@ -96,9 +131,7 @@ int DictManagerModel::setDataPriv(int index, const QVariant &value, int role)
         if (value.type() == QVariant::Bool)
         {
             _dictionaries[dictionary] = value.toBool();
-//            if (index == _dictList.count())
-//                emit dataChanged(this->index(index-1), this->index(index));
-            emit dataChanged(this->index(0), this->index(_dictList.count() - 1));
+            Q_EMIT dataChanged(this->index(0), this->index(_dictList.count() - 1));
             return 2;
         }
         else
@@ -118,10 +151,36 @@ void DictManagerModel::setModelProperty(int index, const QVariant value, QString
 
 }
 
+void DictManagerModel::setModelProperty(int index, QString role)
+{
+    if (role.contains("isSelected"))
+    {
+        if (index < 0 || index > _dictionaries.count() - 1)
+            return;
+
+        setDataPriv(index, !_dictionaries[_dictList[index]], IsSelectedRole);
+    }
+
+}
+
+void DictManagerModel::itemSelected(int index)
+{
+    _currentIndex = index;
+}
+
+CommonDictInterface* DictManagerModel::currentDict()
+{
+    return _dictList[_currentIndex];
+}
+
+bool DictManagerModel::isCurrentDictSelected()
+{
+    return _dictionaries[_dictList[_currentIndex]];
+}
+
 Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const
 {
     Qt::ItemFlags fl = QAbstractItemModel::flags(index);
-    qDebug("lol1");
     return (fl | Qt::ItemIsEditable);
 }