Add connections between DictManagerWidget qml, model and c++ code
authorMarcin Kaźmierczak <marcin@marcin-desktop.(none)>
Tue, 28 Dec 2010 15:48:28 +0000 (16:48 +0100)
committerMarcin Kaźmierczak <marcin@marcin-desktop.(none)>
Tue, 28 Dec 2010 15:48:28 +0000 (16:48 +0100)
src/mdictionary/gui/DictManagerModel.cpp
src/mdictionary/gui/DictManagerModel.h
src/mdictionary/gui/DictManagerWidget.cpp
src/mdictionary/qml/Button.qml
src/mdictionary/qml/DictManagerWidget.qml

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;
+}
index c722b2e..6c599f1 100644 (file)
@@ -35,13 +35,22 @@ public:
 
     Qt::ItemFlags flags(const QModelIndex &index) const;
     void setDictionaries(QHash<CommonDictInterface*, bool> dictionaries);
+    QHash<CommonDictInterface*, bool> dictionaries();
+    QList<CommonDictInterface*> dictList();
 
     void clear();
 
+public Q_SLOTS:
+    void setModelProperty(int index, const QVariant value, QString role);
+
+Q_SIGNALS:
+    void itemChanged();
+
 private:
     QHash<CommonDictInterface*, bool> _dictionaries;
     QList<CommonDictInterface*> _dictList;
 
+    int setDataPriv(int index, const QVariant &value, int role);
     void addDictionary(CommonDictInterface* dictionary, bool isActive);
 };
 
index 692ea26..62d971c 100644 (file)
@@ -112,7 +112,33 @@ void DictManagerWidget::initalizeUI() {
     #endif
 
     #ifndef Q_WS_MAEMO_5
-    //pozmieniać connecty
+    connect(rootObject, SIGNAL(addButtonClicked()),
+            this, SLOT(saveChanges()));
+    connect(rootObject, SIGNAL(addButtonClicked()),
+            this, SLOT(addNewDictButtonClicked()));
+
+    connect(rootObject, SIGNAL(removeButtonClicked()),
+            this, SLOT(saveChanges()));
+    connect(rootObject, SIGNAL(removeButtonClicked()),
+            this, SLOT(removeButtonClicked()));
+
+    connect(rootObject, SIGNAL(settingsButtonClicked()),
+            this, SLOT(saveChanges()));
+    connect(rootObject, SIGNAL(settingsButtonClicked()),
+            this, SLOT(settingsButtonClicked()));
+
+    //z modelu sygnał, oraz z okienka zmian ustawień w pluginie, gdy są zmiany
+//    oryginalnie:
+//    connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)),
+//            this, SLOT(changed()));
+    connect(model, SIGNAL(itemChanged()),
+                this, SLOT(changed()));
+    //pozmieniać connecty, to jest na dwuklik mysza na liście, sprawdzić, zrobic alternatywne sloty
+            connect(rootObject, SIGNAL(itemActivated(int)),
+                    this, SLOT(saveChanges()));
+            connect(rootObject, SIGNAL(itemActivated(int)),
+                    settingsButton, SIGNAL(clicked()));
+
 //        connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
 //                this, SLOT(saveChanges()));
 //        connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
@@ -135,6 +161,7 @@ void DictManagerWidget::initalizeUI() {
 //        setMinimumHeight(sizeHint().height());
 //        setMaximumHeight(sizeHint().height()*2);
         //connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
+        connect(rootObject, SIGNAL(saveButtonClicked()), this, SLOT(save()));
     #endif
 }
 
@@ -145,10 +172,8 @@ void DictManagerWidget::refreshDictsList() {
     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
 
     if (model == 0){
-        qDebug("ok");
         model = new DictManagerModel(dicts, this);
     } else {
-        qDebug("lol");
         model->clear();
         model->setDictionaries(dicts);
     }
index 224a548..fd49c8e 100644 (file)
@@ -27,16 +27,6 @@ BorderImage {
         style: Text.Sunken; color: "white"; styleColor: "black"; smooth: true
     }
 
-//    Rectangle {
-//        id: shade
-//        anchors.centerIn: parent;
-//        radius: parent.height*.4;
-//        color: "black";
-//        opacity: 0
-//        width:  parent.width;
-//        height: parent.height;
-//    }
-
     Image {
         id: image1
         width: (35*parent.height)/107 +1
index 83cce28..db75cde 100644 (file)
@@ -26,7 +26,15 @@ import Qt 4.7
 
 Rectangle {
     SystemPalette { id: myPalette; colorGroup: SystemPalette.Active }
-//    signal selectedRow(int nr)
+
+    function setEnableRemove(Boolean) { removeButton.enabled = Boolean }
+    function setEnableSettings(Boolean) { settingsButton.enabled = Boolean }
+
+    signal addButtonClicked;
+    signal removeButtonClicked;
+    signal settingsButtonClicked;
+    signal saveButtonClicked;
+    signal itemActivated(int nr);
 
     id: rectangle1
     color: myPalette.base
@@ -37,14 +45,16 @@ Rectangle {
         width: rectangle1.width
 //        height: rectangle1.height
         anchors.top: parent.top
+        anchors.bottom: buttonsBox.top
+        anchors.bottomMargin: buttonsBox.height + buttonsBox.anchors.topMargin
         highlightResizeSpeed: 1000
         delegate: Component{
             id: dictListDelegate
             Item {
                 width: rectangle1.width
                 height: {
-                    if (nameText.height + 10 > logo.height)
-                            return nameText.height + 10;
+                    if (nameText.height + 4 > logo.height)
+                            return nameText.height + 4;
                     else
                             return logo.height;
                 }
@@ -52,23 +62,24 @@ Rectangle {
                     anchors.fill: parent
                     onClicked: {
                         dictList.currentIndex = number
+                        rectangle1.setEnableRemove(true)
+                        rectangle1.setEnableSettings(true)
                     }
                     onDoubleClicked: {
-                        selectedRow(number)
+                        rectangle1.itemActivated(dictList.currentIndex)
                     }
                 }
                 Row {
-                    //image zaznacz/odznacz
                     anchors.fill: parent
                     Image {
                         id: checkbox
-//                        source: "qrc:/button/checkbox.png"
                         height: {
                             var aspectRatio = sourceSize.height / sourceSize.width
-                            return logo.width * aspectRatio
+                            return checkbox.width * aspectRatio
                         }
                         anchors.verticalCenter: parent.verticalCenter
-                        width: nameText.height + 10
+                        width: nameText.height
+                        smooth: true
                         states: [
                             State {
                                 name: "checked";
@@ -83,6 +94,15 @@ Rectangle {
                                 PropertyChanges { target: checkbox; source: "qrc:/button/checkbox.png" }
                             }
                         ]
+                        MouseArea{
+                            anchors.fill: parent
+                            onClicked: {
+                                dictList.currentIndex = number
+                                dictModel.setModelProperty(dictList.currentIndex, !isSelected, "isSelected")
+                                rectangle1.setEnableRemove(true)
+                                rectangle1.setEnableSettings(true)
+                            }
+                        }
                     }
 
                     Image {
@@ -92,8 +112,11 @@ Rectangle {
                             var aspectRatio = sourceSize.height / sourceSize.width
                             return logo.width * aspectRatio
                         }
+                        anchors.left: checkbox.right
+                        anchors.leftMargin: 5
                         anchors.verticalCenter: parent.verticalCenter
-                        width: nameText.height + 10
+                        width: nameText.height + 4
+                        smooth: true
                     }
                     Text {
                         id: nameText
@@ -109,6 +132,64 @@ Rectangle {
         model: dictModel
     }
 
-    //buttonki
+    //buttons
+
+    Item {
+        id: buttonsBox
+        width: parent.width
+        height: 30
+        anchors.bottom: parent.bottom
+        anchors.top: dictList.bottom
+        anchors.topMargin: 8
+
+        Button {
+            id: addButton
+            width: (parent.width - 4) / 4
+            height: buttonsBox.height
+            anchors.left: buttonsBox.left
+            anchors.leftMargin: 4
+            anchors.verticalCenter: parent.verticalCenter
+            textInButton: qsTr("Add")
+            onClicked: rectangle1.addButtonClicked
+        }
+
+        Button {
+            id: removeButton
+            width: (parent.width - 4) / 4
+            height: buttonsBox.height
+            anchors.left: addButton.right
+            anchors.leftMargin: 4
+            anchors.verticalCenter: parent.verticalCenter
+            textInButton: qsTr("Remove")
+            enabled: false
+            onClicked: rectangle1.removeButtonClicked
+        }
+
+        Button {
+            id: settingsButton
+            width: (parent.width - 4) / 4
+            height: buttonsBox.height
+            anchors.left: removeButton.right
+            anchors.leftMargin: 4
+            anchors.verticalCenter: parent.verticalCenter
+            textInButton: qsTr("Settings")
+            enabled: false
+            onClicked: rectangle1.settingsButtonClicked
+        }
+
+        Button {
+            id: saveButton
+            width: (parent.width - 4) / 4
+            height: buttonsBox.height
+            anchors.left: settingsButton.right
+            anchors.leftMargin: 4
+            anchors.right: buttonsBox.right
+            anchors.rightMargin: 4
+            anchors.verticalCenter: parent.verticalCenter
+            textInButton: qsTr("Save")
+            onClicked: rectangle1.saveButtonClicked
+        }
+
+    }
 
 }