From f2fd884da9b79619fc72fb2a691dd94ecbdcfbcd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marcin=20Ka=C5=BAmierczak?= Date: Tue, 28 Dec 2010 16:48:28 +0100 Subject: [PATCH] Add connections between DictManagerWidget qml, model and c++ code --- src/mdictionary/gui/DictManagerModel.cpp | 61 +++++++++++++---- src/mdictionary/gui/DictManagerModel.h | 9 +++ src/mdictionary/gui/DictManagerWidget.cpp | 31 ++++++++- src/mdictionary/qml/Button.qml | 10 --- src/mdictionary/qml/DictManagerWidget.qml | 101 ++++++++++++++++++++++++++--- 5 files changed, 177 insertions(+), 35 deletions(-) diff --git a/src/mdictionary/gui/DictManagerModel.cpp b/src/mdictionary/gui/DictManagerModel.cpp index c698ca7..4714730 100644 --- a/src/mdictionary/gui/DictManagerModel.cpp +++ b/src/mdictionary/gui/DictManagerModel.cpp @@ -11,6 +11,8 @@ DictManagerModel::DictManagerModel(QHash 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 DictManagerModel::dictionaries() +{ + return _dictionaries; +} + +QList DictManagerModel::dictList() +{ + return _dictList; +} diff --git a/src/mdictionary/gui/DictManagerModel.h b/src/mdictionary/gui/DictManagerModel.h index c722b2e..6c599f1 100644 --- a/src/mdictionary/gui/DictManagerModel.h +++ b/src/mdictionary/gui/DictManagerModel.h @@ -35,13 +35,22 @@ public: Qt::ItemFlags flags(const QModelIndex &index) const; void setDictionaries(QHash dictionaries); + QHash dictionaries(); + QList dictList(); void clear(); +public Q_SLOTS: + void setModelProperty(int index, const QVariant value, QString role); + +Q_SIGNALS: + void itemChanged(); + private: QHash _dictionaries; QList _dictList; + int setDataPriv(int index, const QVariant &value, int role); void addDictionary(CommonDictInterface* dictionary, bool isActive); }; diff --git a/src/mdictionary/gui/DictManagerWidget.cpp b/src/mdictionary/gui/DictManagerWidget.cpp index 692ea26..62d971c 100644 --- a/src/mdictionary/gui/DictManagerWidget.cpp +++ b/src/mdictionary/gui/DictManagerWidget.cpp @@ -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 dicts = guiInterface->getDictionaries(); if (model == 0){ - qDebug("ok"); model = new DictManagerModel(dicts, this); } else { - qDebug("lol"); model->clear(); model->setDictionaries(dicts); } diff --git a/src/mdictionary/qml/Button.qml b/src/mdictionary/qml/Button.qml index 224a548..fd49c8e 100644 --- a/src/mdictionary/qml/Button.qml +++ b/src/mdictionary/qml/Button.qml @@ -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 diff --git a/src/mdictionary/qml/DictManagerWidget.qml b/src/mdictionary/qml/DictManagerWidget.qml index 83cce28..db75cde 100644 --- a/src/mdictionary/qml/DictManagerWidget.qml +++ b/src/mdictionary/qml/DictManagerWidget.qml @@ -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 + } + + } } -- 1.7.9.5