From: Marcin Kaźmierczak Date: Tue, 21 Dec 2010 15:20:15 +0000 (+0100) Subject: Add model class (DictManagerModel) to manage loaded dictionaries data and settings... X-Git-Url: https://vcs.maemo.org/git/?p=mdictionary;a=commitdiff_plain;h=6245abe2e56ba506f526ef5e318d1f0fcb1571ce Add model class (DictManagerModel) to manage loaded dictionaries data and settings from qml widget, and provide data to qml list. Add info about icon's path to CommonDictInterface and all plugins. --- diff --git a/src/include/CommonDictInterface.h b/src/include/CommonDictInterface.h index 0f0c415..73415b1 100644 --- a/src/include/CommonDictInterface.h +++ b/src/include/CommonDictInterface.h @@ -91,6 +91,9 @@ class CommonDictInterface : public QObject, public AccentsNormalizer { //! \returns plugin icon virtual QIcon* icon() = 0; + //! \returns plugin icon's path + virtual QString iconPath() = 0; + //! \returns empty translation object (to be fetched later) for a given key virtual Translation* getTranslationFor(QString ) {return 0;} diff --git a/src/mdictionary/gui/DictManagerModel.cpp b/src/mdictionary/gui/DictManagerModel.cpp new file mode 100644 index 0000000..c698ca7 --- /dev/null +++ b/src/mdictionary/gui/DictManagerModel.cpp @@ -0,0 +1,107 @@ +#include "DictManagerModel.h" + +DictManagerModel::DictManagerModel(QHash dictionaries, QObject *parent) : + QAbstractListModel(parent) +{ + QHash roles; + roles[NameRole] = "name"; + roles[IconPathRole] = "iconPath"; + roles[IsSelectedRole] = "isSelected"; + roles[NumberRole] = "number"; + setRoleNames(roles); + + setDictionaries(dictionaries); +} + +int DictManagerModel::rowCount(const QModelIndex &parent) const +{ + return _dictList.count(); +} + +void DictManagerModel::setDictionaries(QHash dictionaries) +{ + QHashIterator i(dictionaries); + while (i.hasNext()) { + i.next(); + addDictionary(i.key(), i.value()); + } +} + +void DictManagerModel::clear() +{ + beginRemoveRows(QModelIndex(), 0, rowCount()); + _dictionaries.clear(); + _dictList.clear(); + endRemoveRows(); + emit dataChanged(QModelIndex(), QModelIndex()); +} + +QVariant DictManagerModel::data(const QModelIndex & index, int role) const +{ + if (index.row() < 0 || index.row() > _dictList.count()) + return QVariant(); + + CommonDictInterface* dictionary = _dictList[index.row()]; + if (role == NameRole) + { + QString name; + if (dictionary->type() == "stardict") { + name = dictionary->name() + " (" + dictionary->type() + ")"; + } + else { + name = dictionary->langFrom() + " - " + dictionary->langTo() + + " (" + dictionary->type() + " " + + dictionary->name() + ")"; + } + return name; + } + if (role == NumberRole) + return index.row(); + if (role == IconPathRole) + return dictionary->iconPath(); + 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()) + return false; + + CommonDictInterface* dictionary = _dictList[index.row()]; + if (role == NameRole) + return true; + if (role == NumberRole) + return true; + if (role == IconPathRole) + return true; + if (role == IsSelectedRole) + { + if (value.type() == QVariant::Bool) + { + _dictionaries[dictionary] = value.toBool(); + emit dataChanged(index, index); + return true; + } + else + { + return false; + } + } + return false; +} + +Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags fl = QAbstractItemModel::flags(index); + return (fl | Qt::ItemIsEditable); +} + +void DictManagerModel::addDictionary(CommonDictInterface *dictionary, bool isActive) +{ + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + _dictionaries.insert(dictionary, isActive); + _dictList << dictionary; + endInsertRows(); +} diff --git a/src/mdictionary/gui/DictManagerModel.h b/src/mdictionary/gui/DictManagerModel.h new file mode 100644 index 0000000..c722b2e --- /dev/null +++ b/src/mdictionary/gui/DictManagerModel.h @@ -0,0 +1,48 @@ +#ifndef DICTMANAGERMODEL_H +#define DICTMANAGERMODEL_H + +#include +#include +#include "../../include/GUIInterface.h" + +/*! + Contains a list of installed dictionaries. + Data source for qml list view. +*/ +class DictManagerModel : public QAbstractListModel +{ + Q_OBJECT +public: + enum DictTypeRoles + { + NameRole = Qt::UserRole + 1, + IconPathRole, + IsSelectedRole, + NumberRole + }; + + //! Constructor + /*! + \param dictionaries list of dictionaries + \param parent parent of this class. + */ + explicit DictManagerModel(QHash dictionaries, QObject *parent = 0); + + int rowCount(const QModelIndex & parent = QModelIndex()) const; + + QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; + bool setData(const QModelIndex &index, const QVariant &value, int role); + + Qt::ItemFlags flags(const QModelIndex &index) const; + void setDictionaries(QHash dictionaries); + + void clear(); + +private: + QHash _dictionaries; + QList _dictList; + + void addDictionary(CommonDictInterface* dictionary, bool isActive); +}; + +#endif // DICTMANAGERMODEL_H diff --git a/src/mdictionary/gui/DictManagerWidget.cpp b/src/mdictionary/gui/DictManagerWidget.cpp index 8519c93..5084125 100644 --- a/src/mdictionary/gui/DictManagerWidget.cpp +++ b/src/mdictionary/gui/DictManagerWidget.cpp @@ -36,6 +36,9 @@ DictManagerWidget::DictManagerWidget(GUIInterface *parent) : setWindowTitle(tr("Dictionaries")); this->guiInterface = parent; + #ifndef Q_WS_MAEMO_5 + model = 0; + #endif initalizeUI(); setModal(true); @@ -45,6 +48,26 @@ void DictManagerWidget::initalizeUI() { verticalLayout = new QVBoxLayout; setLayout(verticalLayout); + #ifndef Q_WS_MAEMO_5 + qmlView = new QDeclarativeView(this); + qmlView->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/DictManagerWidget.qml")); + ctxt = qmlView->rootContext(); + + refreshDictsList(); + //model = new DictManagerModel(, this); + ctxt->setContextProperty("dictModel", &(*model)); + + QGraphicsObject *rootObject = qmlView->rootObject(); + //connect(rootObject, SIGNAL(selectedRow(int)), + // this, SLOT(pluginSelected(int))); + + qmlView->setResizeMode(QDeclarativeView::SizeRootObjectToView); + verticalLayout->addWidget(qmlView); + + //connecty zwrotne + #endif + + #ifdef Q_WS_MAEMO_5 dictList = new QListWidget; verticalLayout->addWidget(dictList); @@ -87,34 +110,51 @@ void DictManagerWidget::initalizeUI() { connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(changed())); + #endif #ifndef Q_WS_MAEMO_5 - connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)), - this, SLOT(saveChanges())); - connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)), - this, SLOT(itemSelected(QListWidgetItem*))); - connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)), - settingsButton, SIGNAL(clicked())); + //pozmieniać connecty +// connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)), +// this, SLOT(saveChanges())); +// connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)), +// this, SLOT(itemSelected(QListWidgetItem*))); +// connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)), +// settingsButton, SIGNAL(clicked())); #endif + #ifdef Q_WS_MAEMO_5 refreshDictsList(); + #endif #ifndef Q_WS_MAEMO_5 setMinimumSize(500,300); - closeButton = new QPushButton(tr("Save")); - buttonGroup->addWidget(closeButton); + //closeButton = new QPushButton(tr("Save")); + //buttonGroup->addWidget(closeButton); setMinimumWidth(sizeHint().width()*1.2); setMaximumWidth(sizeHint().width()*2); setMinimumHeight(sizeHint().height()); setMaximumHeight(sizeHint().height()*2); - connect(closeButton, SIGNAL(clicked()), this, SLOT(save())); + //connect(closeButton, SIGNAL(clicked()), this, SLOT(save())); #endif } void DictManagerWidget::refreshDictsList() { + #ifndef Q_WS_MAEMO_5 + QHash dicts = guiInterface->getDictionaries(); + + if (model == 0){ + model = new DictManagerModel(dicts, this); + } else { + model->clear(); + model->setDictionaries(dicts); + } + + #endif + + #ifdef Q_WS_MAEMO_5 dictList->clear(); dictsHash.clear(); removeDictButton->setEnabled(false); @@ -126,6 +166,7 @@ void DictManagerWidget::refreshDictsList() { while(i.hasNext()) { i.next(); + QListWidgetItem* item = new QListWidgetItem(); QString name; if (i.key()->type() == "stardict") { @@ -151,6 +192,7 @@ void DictManagerWidget::refreshDictsList() { } dictList->sortItems(); + #endif } void DictManagerWidget::showEvent(QShowEvent *e) { diff --git a/src/mdictionary/gui/DictManagerWidget.h b/src/mdictionary/gui/DictManagerWidget.h index 3e7e4d7..15befcb 100644 --- a/src/mdictionary/gui/DictManagerWidget.h +++ b/src/mdictionary/gui/DictManagerWidget.h @@ -32,6 +32,9 @@ #include #include "../../include/GUIInterface.h" #include "MenuWidget.h" +#include "DictManagerModel.h" +#include +#include /*! @@ -119,6 +122,12 @@ private: QHash dictsHash; GUIInterface* guiInterface; + #ifndef Q_WS_MAEMO_5 + QDeclarativeView* qmlView; + QDeclarativeContext* ctxt; + DictManagerModel* model; + #endif + bool _changed; void refreshDictsList(); diff --git a/src/mdictionary/mdictionary.pro b/src/mdictionary/mdictionary.pro index 8701e4e..16df3d5 100644 --- a/src/mdictionary/mdictionary.pro +++ b/src/mdictionary/mdictionary.pro @@ -39,7 +39,8 @@ SOURCES += gui/main.cpp \ gui/DBusAdapter.cpp \ gui/NotifyManager.cpp \ gui/SpinBox.cpp \ - gui/DictTypeModel.cpp + gui/DictTypeModel.cpp \ + gui/DictManagerModel.cpp HEADERS += gui/MainWindow.h \ backbone/ConfigGenerator.h \ @@ -71,7 +72,8 @@ HEADERS += gui/MainWindow.h \ gui/DBusAdapter.h \ gui/NotifyManager.h \ gui/SpinBox.h \ - gui/DictTypeModel.h + gui/DictTypeModel.h \ + gui/DictManagerModel.h RESOURCES += ../../data/gui.qrc diff --git a/src/plugins/google/GooglePlugin.cpp b/src/plugins/google/GooglePlugin.cpp index 0374115..733a51f 100644 --- a/src/plugins/google/GooglePlugin.cpp +++ b/src/plugins/google/GooglePlugin.cpp @@ -41,7 +41,8 @@ GooglePlugin::GooglePlugin(QObject *parent): CommonDictInterface(parent), _settings->setValue("type","google"); _settings->setValue("connection_accepted","true"); _dictDialog = new GoogleDictDialog(this,this); - _icon = QIcon("/usr/share/mdictionary/google.png"); + _iconPath = "/usr/share/mdictionary/google.png"; + _icon = QIcon(_iconPath); stopped = false; initLanguages(); @@ -150,6 +151,10 @@ QIcon* GooglePlugin::icon() { return &_icon; } +QString GooglePlugin::iconPath(){ + return _iconPath; +} + CommonDictInterface* GooglePlugin::getNew(const Settings* settings) const { GooglePlugin *plugin = new GooglePlugin(); diff --git a/src/plugins/google/GooglePlugin.h b/src/plugins/google/GooglePlugin.h index 2808195..f62603c 100644 --- a/src/plugins/google/GooglePlugin.h +++ b/src/plugins/google/GooglePlugin.h @@ -102,6 +102,9 @@ public: //! \returns plugin icon QIcon* icon(); + //! \returns plugin icon's resource path + QString iconPath(); + //! \returns empty translation object (to be fetched later) for a given key Translation* getTranslationFor(QString key); @@ -147,6 +150,8 @@ private: //! icon displayed during translations and when a dictionary is chosen QIcon _icon; + //! Path to icon + QString _iconPath; Settings *_settings; //! indicates if search is stopped bool stopped; diff --git a/src/plugins/stardict/StarDictPlugin.cpp b/src/plugins/stardict/StarDictPlugin.cpp index 67e2db3..229cff4 100644 --- a/src/plugins/stardict/StarDictPlugin.cpp +++ b/src/plugins/stardict/StarDictPlugin.cpp @@ -42,7 +42,8 @@ StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent), this, SIGNAL(notify(Notify::NotifyType,QString))); _settings->setValue("type","stardict"); - _icon = QIcon("/usr/share/mdictionary/stardict.png"); + _iconPath = "/usr/share/mdictionary/stardict.png"; + _icon = QIcon(_iconPath); stopped = false; _settings->setValue("strip_accents","true"); @@ -343,4 +344,8 @@ QIcon* StarDictPlugin::icon() { return &_icon; } +QString StarDictPlugin::iconPath(){ + return _iconPath; +} + Q_EXPORT_PLUGIN2(stardict, StarDictPlugin) diff --git a/src/plugins/stardict/StarDictPlugin.h b/src/plugins/stardict/StarDictPlugin.h index 5937b3a..94aa674 100644 --- a/src/plugins/stardict/StarDictPlugin.h +++ b/src/plugins/stardict/StarDictPlugin.h @@ -103,6 +103,9 @@ public: //! \returns plugin icon QIcon* icon(); + //! \returns plugin icon's resource path + QString iconPath(); + /*! plugin should delete any files (eg. cache) that have been created and are ready to be deleted @@ -194,6 +197,8 @@ private: QString _name; QString _infoNote; QIcon _icon; + //! Path to icon + QString _iconPath; volatile bool stopped; Settings *_settings; StarDictDialog* _dictDialog; diff --git a/src/plugins/xdxf/xdxfplugin.cpp b/src/plugins/xdxf/xdxfplugin.cpp index 17e8eca..044bcca 100644 --- a/src/plugins/xdxf/xdxfplugin.cpp +++ b/src/plugins/xdxf/xdxfplugin.cpp @@ -52,7 +52,8 @@ XdxfPlugin::XdxfPlugin(QObject *parent) : CommonDictInterface(parent), } _settings->setValue("type","xdxf"); - _icon = QIcon("/usr/share/mdictionary/xdxf.png"); + _iconPath = "/usr/share/mdictionary/xdxf.png"; + _icon = QIcon(_iconPath); _wordsCount = -1; stopped = false; @@ -452,6 +453,10 @@ QIcon* XdxfPlugin::icon() { return &_icon; } +QString XdxfPlugin::iconPath(){ + return _iconPath; +} + int XdxfPlugin::countWords() { if(_wordsCount>0) diff --git a/src/plugins/xdxf/xdxfplugin.h b/src/plugins/xdxf/xdxfplugin.h index 858fd49..6f32451 100644 --- a/src/plugins/xdxf/xdxfplugin.h +++ b/src/plugins/xdxf/xdxfplugin.h @@ -99,6 +99,9 @@ public: //! \returns plugin icon QIcon* icon(); + //! \returns plugin icon's resource path + QString iconPath(); + /*! plugin should delete any files (eg. cache) that have been created and are ready to be deleted @@ -185,6 +188,8 @@ private: QString _infoNote; QString _dictionaryInfo; QIcon _icon; + //! Path to icon + QString _iconPath; QSqlDatabase db; QString db_name; long _wordsCount;