From 6b24587a573ff5cd106215244859f7efa1aab5ae Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marcin=20Ka=C5=BAmierczak?= Date: Tue, 1 Feb 2011 14:51:43 +0100 Subject: [PATCH] qml ComboBox component and GoogleDialog complete --- src/include/ComboBoxModel.cpp | 92 ++++++++++++++++++++ src/include/ComboBoxModel.h | 75 ++++++++++++++++ src/mdictionary/mdictionary.pro | 6 +- src/mdictionary/qml/ComboBox.qml | 105 ++++++++++------------ src/mdictionary/qml/GoogleDialog.qml | 159 ++++++++++++++++++++++++++++++++++ src/plugins/google/GoogleDialog.cpp | 100 ++++++++++++++++++++- src/plugins/google/GoogleDialog.h | 33 ++++++- src/plugins/google/google.pro | 7 +- 8 files changed, 508 insertions(+), 69 deletions(-) create mode 100644 src/include/ComboBoxModel.cpp create mode 100644 src/include/ComboBoxModel.h create mode 100644 src/mdictionary/qml/GoogleDialog.qml diff --git a/src/include/ComboBoxModel.cpp b/src/include/ComboBoxModel.cpp new file mode 100644 index 0000000..56e5dfe --- /dev/null +++ b/src/include/ComboBoxModel.cpp @@ -0,0 +1,92 @@ +/******************************************************************************* + + 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 . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ + +/*! \file ComboBoxModel.cpp + \brief Contains data for ComboBox QML component + + \author Marcin Kaźmierczak +*/ + +#include "ComboBoxModel.h" + +ComboBoxModel::ComboBoxModel(QList contents, QObject *parent) : + QAbstractListModel(parent) +{ + QHash roles; + roles[ContentRole] = "content"; + roles[NumberRole] = "number"; + setRoleNames(roles); + + setContents(contents); +} + +int ComboBoxModel::rowCount(const QModelIndex &parent) const +{ + return _contents.count(); +} + +QVariant ComboBoxModel::data(const QModelIndex &index, int role) const +{ + if (index.row() < 0 || index.row() > _contents.count()) + return QVariant(); + + QString item = _contents[index.row()]; + if (role == ContentRole) + return item; + if (role == NumberRole) + return index.row(); + return QVariant(); +} + +QString ComboBoxModel::selectedItem() +{ + return _selectedItem; +} + +int ComboBoxModel::selectedIndex() +{ + return _selectedIndex; +} + +void ComboBoxModel::setSelectedItem(QString item) +{ + _selectedItem = item; +} + +void ComboBoxModel::setSelectedIndex(int index) +{ + _selectedIndex = index; +} + +void ComboBoxModel::setContents(QList contents) +{ + foreach (QString item, contents) + { + addItem(item); + } +} + +void ComboBoxModel::addItem(QString item) +{ + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + _contents << item; + endInsertRows(); +} diff --git a/src/include/ComboBoxModel.h b/src/include/ComboBoxModel.h new file mode 100644 index 0000000..ad568fb --- /dev/null +++ b/src/include/ComboBoxModel.h @@ -0,0 +1,75 @@ +/******************************************************************************* + + 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 . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ + +/*! \file ComboBoxModel.h + \brief Contains data for ComboBox QML component + + \author Marcin Kaźmierczak +*/ + + +#ifndef COMBOBOXMODEL_H +#define COMBOBOXMODEL_H + +#include +#include +#include + +/*! + Contains a list of string values. + Data source for qml ComboBox +*/ +class ComboBoxModel : public QAbstractListModel +{ + Q_OBJECT +public: + enum ComboBoxRoles + { + ContentRole = Qt::UserRole + 1, + NumberRole + }; + + //! Constructor + /*! + \param contents list of elements for ComboBox + \param parent parent of this class. + */ + explicit ComboBoxModel(QList contents, QObject *parent = 0); + + int rowCount(const QModelIndex & parent = QModelIndex()) const; + + QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; + + QString selectedItem(); + int selectedIndex(); + void setSelectedItem(QString item); + void setSelectedIndex(int index); + +private: + void setContents(QList contents); + void addItem(QString item); + QList _contents; + QString _selectedItem; + int _selectedIndex; + +}; + +#endif // COMBOBOXMODEL_H diff --git a/src/mdictionary/mdictionary.pro b/src/mdictionary/mdictionary.pro index bf2966b..74cf8a8 100644 --- a/src/mdictionary/mdictionary.pro +++ b/src/mdictionary/mdictionary.pro @@ -42,7 +42,8 @@ SOURCES += gui/main.cpp \ gui/DictTypeModel.cpp \ gui/DictManagerModel.cpp \ gui/HistoryListModel.cpp \ - gui/WordListModel.cpp + gui/WordListModel.cpp \ + ../include/ComboBoxModel.cpp HEADERS += gui/MainWindow.h \ backbone/ConfigGenerator.h \ @@ -77,7 +78,8 @@ HEADERS += gui/MainWindow.h \ gui/DictManagerModel.h \ gui/DictManagerWidget.h \ gui/HistoryListModel.h \ - gui/WordListModel.h + gui/WordListModel.h \ + ../include/ComboBoxModel.h RESOURCES += ../../data/gui.qrc diff --git a/src/mdictionary/qml/ComboBox.qml b/src/mdictionary/qml/ComboBox.qml index 34112a7..cb9e6d0 100644 --- a/src/mdictionary/qml/ComboBox.qml +++ b/src/mdictionary/qml/ComboBox.qml @@ -4,62 +4,37 @@ Rectangle { id: rectangle1 radius: 10 border.color: "#000666"; -// property int maxValue:500; -// property int minValue:0; property alias value:text1.text + property alias index: list1.currentIndex + property alias model: list1.model property bool expanded: false + property bool disabled: false + property int expandedHeight + property int basicHeight + property string startValue + height: basicHeight function show(Boolean){ - //mozna jeszcze to w tle ukrywać expanded = Boolean } - signal valueSelected(string selected); //? - -// function stringToInt(string){ -// var value=0; -// var pow10=1; -// for (var i=string.length-1;i>=0;i--){ -// value+=(string.charCodeAt(i)-48)*pow10; -// pow10= pow10*10; -// } -// if(value>maxValue) -// return maxValue; -// if(value toLabel.width){ + return fromLabel.width + 10 + } + else { + return toLabel.width + 10 + } + } + + anchors.top: infoLabel.bottom + anchors.right: revertButton.left + anchors.rightMargin: 10 + expanded: false + basicHeight: fromLabel.height + expandedHeight: parent.height - comboFrom.x - saveButton.height -20 + } + + ComboBox{ + id: comboTo + model: comboBoxModel + anchors.left: parent.left + anchors.leftMargin: { + if (fromLabel.width < 30 && toLabel.width < 30){ + return 30 + } + else if (fromLabel.width > toLabel.width){ + return fromLabel.width + 10 + } + else { + return toLabel.width + 10 + } + } + + anchors.right: revertButton.left + anchors.rightMargin: 10 + anchors.top: comboFrom.bottom + expanded: false + basicHeight: fromLabel.height + expandedHeight: parent.height - comboTo.x - saveButton.height - 20 - fromLabel.height + } + + IconButton{ + id: revertButton + width: height + height: fromLabel.height + anchors.top: fromLabel.top + anchors.topMargin: fromLabel.height /2 + anchors.right: parent.right +// pathToIcon: //gimp again, ech + pathToIcon: "qrc:/button/up_enable.png"; //temp + onClicked: { rectangle1.revertLang() } + } + + Button { + id: saveButton + height: 30 + z: 1 + anchors.bottom: parent.bottom + anchors.right: parent.right + anchors.left: parent.left + onClicked: { + rectangle1.saveButtonClicked(comboFrom.value, comboTo.value); + } + } + + states: [ + State { + name: "new" + when: newPlugin==true + PropertyChanges { target: saveButton; textInButton: qsTr("Add") } + }, + State { + name: "edit" + when: newPlugin==false + PropertyChanges { target: saveButton; textInButton: qsTr("Save settings") } + } + ] +} + diff --git a/src/plugins/google/GoogleDialog.cpp b/src/plugins/google/GoogleDialog.cpp index 657640a..4ca07eb 100644 --- a/src/plugins/google/GoogleDialog.cpp +++ b/src/plugins/google/GoogleDialog.cpp @@ -34,6 +34,8 @@ GoogleDialog::GoogleDialog(GooglePlugin *plugin, this->plugin = plugin; this->type = type; _settings = 0; + _actualLangFrom = 0; + _actualLangTo = 0; if(plugin) { _langTo=GooglePlugin::languages.key( @@ -47,8 +49,7 @@ GoogleDialog::GoogleDialog(GooglePlugin *plugin, _langFrom=GooglePlugin::languages.key("en"); } - initializeUI(); - +#ifdef Q_WS_MAEMO_5 connect(confirmButton, SIGNAL(clicked()), this, SLOT(accept())); @@ -60,10 +61,81 @@ GoogleDialog::GoogleDialog(GooglePlugin *plugin, connect(changeLangButton, SIGNAL(clicked()), this, SLOT(changeLangButtonClicked())); +#else + + int i=0,j=0; + int actualLangTo=0; + int actualLangFrom=0; + + QList langList; + foreach(QString langs, GooglePlugin::languages.keys()){ + if(langs==_langTo) + actualLangTo=j; + if(langs==_langFrom) + actualLangFrom=i; + if(langs!="Detect langlage"){ + langList.append(langs); + j++; + } + i++; + } + + _actualLangFrom = actualLangFrom; + _actualLangTo = actualLangTo; + model = new ComboBoxModel(langList); + + view= new QDeclarativeView(); + ctxt = view->rootContext(); + ctxt->setContextProperty("comboBoxModel", &(*model)); + view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/GoogleDialog.qml")); + view->setResizeMode(QDeclarativeView::SizeRootObjectToView); + //view->setAlignment(Qt::AlignCenter); + view->show(); + + mainLayout = new QVBoxLayout; + mainLayout->addWidget(view); + setLayout(mainLayout); + view->setWindowTitle(tr("Google Settings")); + + QGraphicsObject *rootObject = view->rootObject(); + + connect(rootObject, SIGNAL(saveButtonClicked(QString, QString)), + this, SLOT(saveButtonClicked(QString,QString))); + + connect(this, SIGNAL(setInfo(QVariant)), + rootObject, SLOT(setInfo(QVariant))); + connect(this,SIGNAL(setNew(QVariant)), + rootObject, SLOT(setNew(QVariant))); + connect(this,SIGNAL(setStartValues(QVariant,QVariant,QVariant,QVariant)), + rootObject, SLOT(setStartValues(QVariant, QVariant, QVariant, QVariant))); + +#endif + + initializeUI(); } void GoogleDialog::initializeUI() { +#ifndef Q_WS_MAEMO_5 + + setWindowTitle(tr("Google Plugin Settings")); + if (type != New){ + emit setNew(false); + QString info=tr("Plugin: ") + plugin->type().toUpper() +"\n" + + tr("From: ") + _langFrom + "\n" + + tr("To: ") + _langTo; + emit setInfo(info); + } + else{ + emit setNew(true); + } + emit setStartValues(_langFrom, _langTo, _actualLangFrom, _actualLangTo); + +// setMinimumSize(sizeHint()); + + +#else + int i=0,j=0; int actualLangTo=0; int actualLangFrom=0; @@ -144,6 +216,7 @@ void GoogleDialog::initializeUI() { setModal(true); setMinimumSize(sizeHint()); setMaximumSize(sizeHint()); +#endif } @@ -167,7 +240,7 @@ void GoogleDialog::changeLangButtonClicked() { } } - +#ifdef Q_WS_MAEMO_5 void GoogleDialog::accept() { saveSettings(); QDialog::accept(); @@ -187,6 +260,27 @@ void GoogleDialog::saveSettings() { GooglePlugin::languages.value(_langFrom)); } +#else +void GoogleDialog::saveButtonClicked(QString langFrom, QString langTo){ + saveSettings(langFrom, langTo); + QDialog::accept(); +} + +void GoogleDialog::saveSettings(QString langFrom, QString langTo){ + _settings = new Settings; + _langFrom = langFrom; + _langTo = langTo; + if(plugin) { + foreach(QString key, plugin->settings()->keys()) + _settings->setValue(key, plugin->settings()->value(key)); + } + + _settings->setValue("lang_to", + GooglePlugin::languages.value(langTo)); + _settings->setValue("lang_from", + GooglePlugin::languages.value(langFrom)); +} +#endif Settings* GoogleDialog::getSettings() { return _settings; diff --git a/src/plugins/google/GoogleDialog.h b/src/plugins/google/GoogleDialog.h index 52464b1..6ca7c7b 100644 --- a/src/plugins/google/GoogleDialog.h +++ b/src/plugins/google/GoogleDialog.h @@ -31,8 +31,13 @@ #include #include "../../include/settings.h" +#include "../../include/ComboBoxModel.h" #include + + #include +#include +#include #include "GooglePlugin.h" @@ -75,9 +80,17 @@ Q_SIGNALS: //! requests to show notification void notify(Notify::NotifyType, QString); +#ifndef Q_WS_MAEMO_5 + void setInfo(QVariant info); + void setNew(QVariant text); + void setStartValues(QVariant from, QVariant to, QVariant fromIndex, QVariant toIndex); +#endif + public Q_SLOTS: +#ifdef Q_Ws_MAEMO_5 //! reimplemented accept method, to save new settings void accept(); +#endif private Q_SLOTS: //! assigns the language chosen from a list(langFromComboBox) to _langFrom @@ -89,14 +102,28 @@ private Q_SLOTS: //! handles the "swap languages" button void changeLangButtonClicked(); + +#ifndef Q_Ws_MAEMO_5 + void saveButtonClicked(QString langFrom, QString langTo); +#endif + private: - QVBoxLayout* mainLayout; - QDeclarativeView *view; void initializeUI(); //! saves new settings after acceptance of dialog +#ifdef Q_Ws_MAEMO_5 void saveSettings(); +#else + void saveSettings(QString langFrom, QString langTo); + + ComboBoxModel* model; + QVBoxLayout* mainLayout; + QDeclarativeView *view; + QDeclarativeContext* ctxt; +#endif + + int lastHeight; QLabel* infoLabel; QLabel* langFromLabel; @@ -112,6 +139,8 @@ private: QHBoxLayout* changeLangLayout; QString _langFrom; QString _langTo; + int _actualLangTo; + int _actualLangFrom; Settings* _settings; GooglePlugin* plugin; diff --git a/src/plugins/google/google.pro b/src/plugins/google/google.pro index 54b09cd..c9b354c 100644 --- a/src/plugins/google/google.pro +++ b/src/plugins/google/google.pro @@ -13,7 +13,8 @@ SOURCES += \ GooglePlugin.cpp \ TranslationGoogle.cpp \ GoogleDictDialog.cpp \ - GoogleDialog.cpp + GoogleDialog.cpp \ + ../../include/ComboBoxModel.cpp HEADERS += \ GooglePlugin.h \ @@ -21,6 +22,7 @@ HEADERS += \ ../../include/translation.h \ ../../include/settings.h \ ../../include/CommonDictInterface.h \ + ../../include/ComboBoxModel.h \ TranslationGoogle.h \ GoogleDictDialog.h \ GoogleDialog.h @@ -53,3 +55,6 @@ unix { } check.commands = echo 'No check here' QMAKE_EXTRA_TARGETS += check + +OTHER_FILES += \ + ../../mdictionary/qml/GoogleDialog.qml -- 1.7.9.5