Add connections between DictManagerWidget qml, model and c++ code
[mdictionary] / src / mdictionary / gui / DictManagerModel.cpp
index c698ca7..4714730 100644 (file)
@@ -11,6 +11,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
@@ -55,46 +57,71 @@ 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));
+            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);
+    }
+
 }
 
 Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const
 {
     Qt::ItemFlags fl = QAbstractItemModel::flags(index);
+    qDebug("lol1");
     return (fl | Qt::ItemIsEditable);
 }
 
@@ -105,3 +132,13 @@ void DictManagerModel::addDictionary(CommonDictInterface *dictionary, bool isAct
     _dictList << dictionary;
     endInsertRows();
 }
+
+QHash<CommonDictInterface*, bool> DictManagerModel::dictionaries()
+{
+    return _dictionaries;
+}
+
+QList<CommonDictInterface*> DictManagerModel::dictList()
+{
+    return _dictList;
+}