features of DictManagerWidget restored in qml version.
[mdictionary] / src / mdictionary / gui / DictManagerModel.cpp
index c698ca7..ecb93bd 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";
@@ -11,6 +39,8 @@ DictManagerModel::DictManagerModel(QHash<CommonDictInterface *, bool> dictionari
     setRoleNames(roles);
 
     setDictionaries(dictionaries);
+
+    connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged()));
 }
 
 int DictManagerModel::rowCount(const QModelIndex &parent) const
@@ -33,7 +63,7 @@ void DictManagerModel::clear()
     _dictionaries.clear();
     _dictList.clear();
     endRemoveRows();
-    emit dataChanged(QModelIndex(), QModelIndex());
+    Q_EMIT dataChanged(QModelIndex(), QModelIndex());
 }
 
 QVariant DictManagerModel::data(const QModelIndex & index, int role) const
@@ -55,46 +85,86 @@ QVariant DictManagerModel::data(const QModelIndex & index, int role) const
         }
         return name;
     }
-    if (role == NumberRole)
+    if (role == NumberRole){
         return index.row();
-    if (role == IconPathRole)
+    }
+    if (role == IconPathRole){
         return dictionary->iconPath();
-    if (role == IsSelectedRole)
+    }
+    if (role == IsSelectedRole){
         return _dictionaries[dictionary];
+    }
     return QVariant();
 }
 
 bool DictManagerModel::setData(const QModelIndex &index, const QVariant &value, int role)
 {
-    if (index.row() < 0 || index.row() > _dictList.count())
+    int res = setDataPriv(index.row(), value, role);
+    if (res == 0)
         return false;
+    if (res > 0)
+        return true;
+    return true;
+}
 
-    CommonDictInterface* dictionary = _dictList[index.row()];
+int DictManagerModel::setDataPriv(int index, const QVariant &value, int role)
+{
+    if (index < 0 || index > _dictList.count())
+        return 0;
+
+    CommonDictInterface* dictionary = _dictList[index];
     if (role == NameRole)
-        return true;
+        return 1;
     if (role == NumberRole)
-        return true;
+        return 1;
     if (role == IconPathRole)
-        return true;
+        return 1;
     if (role == IsSelectedRole)
     {
         if (value.type() == QVariant::Bool)
         {
             _dictionaries[dictionary] = value.toBool();
-            emit dataChanged(index, index);
-            return true;
+//            if (index == _dictList.count())
+//                emit dataChanged(this->index(index-1), this->index(index));
+            Q_EMIT dataChanged(this->index(0), this->index(_dictList.count() - 1));
+            return 2;
         }
         else
         {
-            return false;
+            return 0;
         }
     }
-    return false;
+    return 0;
+}
+
+void DictManagerModel::setModelProperty(int index, const QVariant value, QString role)
+{
+    if (role.contains("isSelected"))
+    {
+        setDataPriv(index, value, 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);
 }
 
@@ -105,3 +175,13 @@ void DictManagerModel::addDictionary(CommonDictInterface *dictionary, bool isAct
     _dictList << dictionary;
     endInsertRows();
 }
+
+QHash<CommonDictInterface*, bool> DictManagerModel::dictionaries()
+{
+    return _dictionaries;
+}
+
+QList<CommonDictInterface*> DictManagerModel::dictList()
+{
+    return _dictList;
+}