diff --git a/sources/awesome-widget/plugin/awcustomkeysconfig.cpp b/sources/awesome-widget/plugin/awcustomkeysconfig.cpp new file mode 100644 index 0000000..5788697 --- /dev/null +++ b/sources/awesome-widget/plugin/awcustomkeysconfig.cpp @@ -0,0 +1,173 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets 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. * + * * + * awesome-widgets 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 awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + +#include "awcustomkeysconfig.h" +#include "ui_awcustomkeysconfig.h" + +#include + +#include + +#include "awabstractselector.h" +#include "awdebug.h" +#include "awcustomkeyshelper.h" + + +AWCustomKeysConfig::AWCustomKeysConfig(QWidget *_parent, const QStringList &_keys) + : QDialog(_parent) + , ui(new Ui::AWCustomKeysConfig) + , m_keys(_keys) +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; + + ui->setupUi(this); + init(); + + connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept())); + connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject())); +} + + +AWCustomKeysConfig::~AWCustomKeysConfig() +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; + + clearSelectors(); + + delete m_helper; + delete ui; +} + + +void AWCustomKeysConfig::showDialog() +{ + // update dialog + updateDialog(); + // exec dialog + return execDialog(); +} + + +void AWCustomKeysConfig::updateUi() +{ + QPair current + = static_cast(sender())->current(); + int index + = m_selectors.indexOf(static_cast(sender())); + + if ((current.first.isEmpty()) && (current.second.isEmpty())) { + // remove current selector if it is empty and does not last + if (sender() == m_selectors.last()) + return; + AWAbstractSelector *selector = m_selectors.takeAt(index); + ui->verticalLayout->removeWidget(selector); + selector->deleteLater(); + } else { + // add new selector if something changed + if (sender() != m_selectors.last()) + return; + auto keys = initKeys(); + addSelector(keys.first, keys.second, QPair()); + } +} + + +void AWCustomKeysConfig::addSelector(const QStringList &_keys, + const QStringList &_values, + const QPair &_current) +{ + qCDebug(LOG_AW) << "Add selector with keys" << _keys << "values" << _values + << "and current ones" << _current; + + AWAbstractSelector *selector + = new AWAbstractSelector(ui->scrollAreaWidgetContents, {true, false}); + selector->init(_keys, _values, _current); + ui->verticalLayout->insertWidget(ui->verticalLayout->count() - 1, selector); + connect(selector, SIGNAL(selectionChanged()), this, SLOT(updateUi())); + m_selectors.append(selector); +} + + +void AWCustomKeysConfig::clearSelectors() +{ + for (auto &selector : m_selectors) { + disconnect(selector, SIGNAL(selectionChanged()), this, + SLOT(updateUi())); + ui->verticalLayout->removeWidget(selector); + selector->deleteLater(); + } + m_selectors.clear(); +} + + +void AWCustomKeysConfig::execDialog() +{ + int ret = exec(); + QHash data; + for (auto &selector : m_selectors) { + QPair select = selector->current(); + if (select.first.isEmpty()) + continue; + data[select.first] = select.second; + } + + // save configuration if required + switch (ret) { + case 0: + break; + case 1: + default: + m_helper->writeItems(data); + m_helper->removeUnusedKeys(data.keys()); + break; + } +} + + +void AWCustomKeysConfig::init() +{ + delete m_helper; + m_helper = new AWCustomKeysHelper(this); +} + + +QPair AWCustomKeysConfig::initKeys() const +{ + // we are adding empty string at the start + QStringList keys = QStringList() << ""; + keys.append(m_keys); + keys.sort(); + QStringList userKeys = QStringList() << ""; + userKeys.append(m_helper->keys()); + userKeys.sort(); + + return QPair(userKeys, keys); +} + + +void AWCustomKeysConfig::updateDialog() +{ + clearSelectors(); + auto userKeys = m_helper->getUserKeys(); + auto keys = initKeys(); + + for (auto &key : userKeys.keys()) + addSelector(keys.first, keys.second, + QPair(key, userKeys[key])); + // empty one + addSelector(keys.first, keys.second, QPair()); +} diff --git a/sources/awesome-widget/plugin/awcustomkeysconfig.h b/sources/awesome-widget/plugin/awcustomkeysconfig.h new file mode 100644 index 0000000..a6ea821 --- /dev/null +++ b/sources/awesome-widget/plugin/awcustomkeysconfig.h @@ -0,0 +1,62 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets 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. * + * * + * awesome-widgets 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 awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + + +#ifndef AWCUSTOMKEYSCONFIG_H +#define AWCUSTOMKEYSCONFIG_H + +#include + + +class AWAbstractSelector; +class AWCustomKeysHelper; +namespace Ui +{ +class AWCustomKeysConfig; +} + +class AWCustomKeysConfig : public QDialog +{ + Q_OBJECT + +public: + explicit AWCustomKeysConfig(QWidget *_parent = nullptr, + const QStringList &_keys = QStringList()); + virtual ~AWCustomKeysConfig(); + Q_INVOKABLE void showDialog(); + +private slots: + void updateUi(); + +private: + Ui::AWCustomKeysConfig *ui = nullptr; + AWCustomKeysHelper *m_helper = nullptr; + QList m_selectors; + // properties + QStringList m_keys; + // methods + void addSelector(const QStringList &_keys, const QStringList &_values, + const QPair &_current); + void clearSelectors(); + void execDialog(); + void init(); + QPair initKeys() const; + void updateDialog(); +}; + + +#endif /* AWCUSTOMKEYSCONFIG_H */ diff --git a/sources/awesome-widget/plugin/awcustomkeysconfig.ui b/sources/awesome-widget/plugin/awcustomkeysconfig.ui new file mode 100644 index 0000000..41ee1e8 --- /dev/null +++ b/sources/awesome-widget/plugin/awcustomkeysconfig.ui @@ -0,0 +1,93 @@ + + + AWCustomKeysConfig + + + + 0 + 0 + 400 + 300 + + + + + + + true + + + + + 0 + 0 + 384 + 249 + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + AWCustomKeysConfig + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + AWCustomKeysConfig + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/sources/awesome-widget/plugin/awcustomkeyshelper.cpp b/sources/awesome-widget/plugin/awcustomkeyshelper.cpp index f3ac4f9..7030b44 100644 --- a/sources/awesome-widget/plugin/awcustomkeyshelper.cpp +++ b/sources/awesome-widget/plugin/awcustomkeyshelper.cpp @@ -30,7 +30,7 @@ AWCustomKeysHelper::AWCustomKeysHelper(QObject *_parent) qCDebug(LOG_AW) << __PRETTY_FUNCTION__; m_filePath = "awesomewidgets/custom.ini"; - initKeys(); + initItems(); } @@ -40,7 +40,7 @@ AWCustomKeysHelper::~AWCustomKeysHelper() } -void AWCustomKeysHelper::initKeys() +void AWCustomKeysHelper::initItems() { m_keys.clear(); @@ -68,7 +68,7 @@ void AWCustomKeysHelper::initKeys() } -bool AWCustomKeysHelper::writeKeys( +bool AWCustomKeysHelper::writeItems( const QHash &_configuration) const { qCDebug(LOG_AW) << "Write configuration" << _configuration; @@ -91,6 +91,38 @@ bool AWCustomKeysHelper::writeKeys( } +bool AWCustomKeysHelper::removeUnusedKeys(const QStringList &_keys) const +{ + qCDebug(LOG_AW) << "Remove keys" << _keys; + + QString fileName = QString("%1/%2") + .arg(QStandardPaths::writableLocation( + QStandardPaths::GenericDataLocation)) + .arg(m_filePath); + QSettings settings(fileName, QSettings::IniFormat); + qCInfo(LOG_AW) << "Configuration file" << fileName; + + settings.beginGroup("Custom"); + QStringList foundKeys = settings.childKeys(); + for (auto &key : foundKeys) { + if (_keys.contains(key)) + continue; + settings.remove(key); + } + settings.endGroup(); + + settings.sync(); + + return (settings.status() == QSettings::NoError); +} + + +QHash AWCustomKeysHelper::getUserKeys() const +{ + return m_keys; +} + + QStringList AWCustomKeysHelper::keys() const { return m_keys.keys(); diff --git a/sources/awesome-widget/plugin/awcustomkeyshelper.h b/sources/awesome-widget/plugin/awcustomkeyshelper.h index 442b05d..6953d83 100644 --- a/sources/awesome-widget/plugin/awcustomkeyshelper.h +++ b/sources/awesome-widget/plugin/awcustomkeyshelper.h @@ -30,9 +30,12 @@ class AWCustomKeysHelper : public QObject public: explicit AWCustomKeysHelper(QObject *_parent = nullptr); virtual ~AWCustomKeysHelper(); - void initKeys(); - bool writeKeys(const QHash &_configuration) const; + // read-write methods + void initItems(); + bool writeItems(const QHash &_configuration) const; + bool removeUnusedKeys(const QStringList &_keys) const; // get + QHash getUserKeys() const; QStringList keys() const; QString source(const QString &_key) const; QStringList sources() const; diff --git a/sources/awesome-widget/plugin/awformatterconfig.cpp b/sources/awesome-widget/plugin/awformatterconfig.cpp index e9ed850..7701ff8 100644 --- a/sources/awesome-widget/plugin/awformatterconfig.cpp +++ b/sources/awesome-widget/plugin/awformatterconfig.cpp @@ -141,7 +141,7 @@ void AWFormatterConfig::execDialog() break; case 1: default: - m_helper->writeFormatters(data); + m_helper->writeItems(data); m_helper->removeUnusedFormatters(data.keys()); break; } diff --git a/sources/awesome-widget/plugin/awformatterconfigfactory.cpp b/sources/awesome-widget/plugin/awformatterconfigfactory.cpp index 33484d1..e32441f 100644 --- a/sources/awesome-widget/plugin/awformatterconfigfactory.cpp +++ b/sources/awesome-widget/plugin/awformatterconfigfactory.cpp @@ -19,6 +19,7 @@ #include "awdebug.h" #include "awformatterconfig.h" +#include "awcustomkeysconfig.h" AWFormatterConfigFactory::AWFormatterConfigFactory(QObject *_parent) @@ -34,9 +35,17 @@ AWFormatterConfigFactory::~AWFormatterConfigFactory() } -void AWFormatterConfigFactory::showDialog(const QStringList &_keys) +void AWFormatterConfigFactory::showFormatterDialog(const QStringList &_keys) { AWFormatterConfig *config = new AWFormatterConfig(nullptr, _keys); config->showDialog(); config->deleteLater(); } + + +void AWFormatterConfigFactory::showKeysDialog(const QStringList &_keys) +{ + AWCustomKeysConfig *config = new AWCustomKeysConfig(nullptr, _keys); + config->showDialog(); + config->deleteLater(); +} diff --git a/sources/awesome-widget/plugin/awformatterconfigfactory.h b/sources/awesome-widget/plugin/awformatterconfigfactory.h index e4c4c69..b8fc7fe 100644 --- a/sources/awesome-widget/plugin/awformatterconfigfactory.h +++ b/sources/awesome-widget/plugin/awformatterconfigfactory.h @@ -29,7 +29,8 @@ class AWFormatterConfigFactory : public QObject public: explicit AWFormatterConfigFactory(QObject *_parent = nullptr); virtual ~AWFormatterConfigFactory(); - Q_INVOKABLE void showDialog(const QStringList &_keys); + Q_INVOKABLE void showFormatterDialog(const QStringList &_keys); + Q_INVOKABLE void showKeysDialog(const QStringList &_keys); private: }; diff --git a/sources/awesome-widget/plugin/awformatterhelper.cpp b/sources/awesome-widget/plugin/awformatterhelper.cpp index c830974..0d902e4 100644 --- a/sources/awesome-widget/plugin/awformatterhelper.cpp +++ b/sources/awesome-widget/plugin/awformatterhelper.cpp @@ -52,6 +52,37 @@ AWFormatterHelper::~AWFormatterHelper() } +void AWFormatterHelper::initItems() +{ + installDirectories(); + initFormatters(); + initKeys(); +} + + +bool AWFormatterHelper::writeItems( + const QHash &_configuration) const +{ + qCDebug(LOG_AW) << "Write configuration" << _configuration; + + QString fileName = QString("%1/%2") + .arg(QStandardPaths::writableLocation( + QStandardPaths::GenericDataLocation)) + .arg(m_filePath); + QSettings settings(fileName, QSettings::IniFormat); + qCInfo(LOG_AW) << "Configuration file" << fileName; + + settings.beginGroup("Formatters"); + for (auto &key : _configuration.keys()) + settings.setValue(key, _configuration[key]); + settings.endGroup(); + + settings.sync(); + + return (settings.status() == QSettings::NoError); +} + + QString AWFormatterHelper::convert(const QVariant &_value, const QString &_name) const { @@ -78,14 +109,6 @@ QHash AWFormatterHelper::getFormatters() const } -void AWFormatterHelper::initItems() -{ - installDirectories(); - initFormatters(); - initKeys(); -} - - QList AWFormatterHelper::items() const { QList converted; @@ -128,29 +151,6 @@ bool AWFormatterHelper::removeUnusedFormatters(const QStringList &_keys) const } -bool AWFormatterHelper::writeFormatters( - const QHash &_configuration) const -{ - qCDebug(LOG_AW) << "Write configuration" << _configuration; - - QString fileName = QString("%1/%2") - .arg(QStandardPaths::writableLocation( - QStandardPaths::GenericDataLocation)) - .arg(m_filePath); - QSettings settings(fileName, QSettings::IniFormat); - qCInfo(LOG_AW) << "Configuration file" << fileName; - - settings.beginGroup("Formatters"); - for (auto &key : _configuration.keys()) - settings.setValue(key, _configuration[key]); - settings.endGroup(); - - settings.sync(); - - return (settings.status() == QSettings::NoError); -} - - void AWFormatterHelper::editItems() { repaintList(); diff --git a/sources/awesome-widget/plugin/awformatterhelper.h b/sources/awesome-widget/plugin/awformatterhelper.h index e70c3d5..2da1e48 100644 --- a/sources/awesome-widget/plugin/awformatterhelper.h +++ b/sources/awesome-widget/plugin/awformatterhelper.h @@ -31,14 +31,16 @@ class AWFormatterHelper : public AbstractExtItemAggregator public: explicit AWFormatterHelper(QWidget *_parent = nullptr); virtual ~AWFormatterHelper(); + // read-write methods + void initItems(); + bool writeItems(const QHash &_configuration) const; + // methods QString convert(const QVariant &_value, const QString &_name) const; QStringList definedFormatters() const; QHash getFormatters() const; - void initItems(); QList items() const; QStringList knownFormatters() const; bool removeUnusedFormatters(const QStringList &_keys) const; - bool writeFormatters(const QHash &_configuration) const; public slots: void editItems(); diff --git a/sources/awesome-widget/plugin/awkeyoperations.cpp b/sources/awesome-widget/plugin/awkeyoperations.cpp index 5dc9a0d..5dbc60f 100644 --- a/sources/awesome-widget/plugin/awkeyoperations.cpp +++ b/sources/awesome-widget/plugin/awkeyoperations.cpp @@ -346,7 +346,7 @@ void AWKeyOperations::addKeyToCache(const QString &_type, const QString &_key) void AWKeyOperations::reinitKeys() { - m_customKeys->initKeys(); + m_customKeys->initItems(); m_graphicalItems->initItems(); m_extNetRequest->initItems(); m_extQuotes->initItems(); diff --git a/sources/awesomewidgets/abstractextitemaggregator.h b/sources/awesomewidgets/abstractextitemaggregator.h index 207a43e..7916b7e 100644 --- a/sources/awesomewidgets/abstractextitemaggregator.h +++ b/sources/awesomewidgets/abstractextitemaggregator.h @@ -67,6 +67,7 @@ public: void deleteItem(); void editItem(); QString getName(); + virtual void initItems() = 0; AbstractExtItem *itemFromWidget(); void repaintList(); int uniqNumber() const; @@ -92,7 +93,6 @@ private: QString m_type; // ui methods virtual void doCreateItem() = 0; - virtual void initItems() = 0; }; diff --git a/sources/qml/AWExtensions.qml b/sources/qml/AWExtensions.qml index c2bba77..eed6e0d 100644 --- a/sources/qml/AWExtensions.qml +++ b/sources/qml/AWExtensions.qml @@ -41,15 +41,21 @@ Row { signal showMessage(string message) QtControls.Button { - width: parent.width * 3 / 10 + width: parent.width * 3 / 15 text: i18n("Edit bars") onClicked: backend.editItem("graphicalitem") } QtControls.Button { - width: parent.width * 3 / 10 + width: parent.width * 3 / 15 text: i18n("Formatters") - onClicked: awFormatter.showDialog(backend.dictKeys(true)) + onClicked: awFormatter.showFormatterDialog(backend.dictKeys(true)) + } + + QtControls.Button { + width: parent.width * 3 / 15 + text: i18n("User keys") + onClicked: awFormatter.showKeysDialog(backend.dictKeys(true)) } QtControls.Button { diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index b2788e6..ae7f9db 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -43,6 +43,7 @@ foreach (TEST_MODULE ${TEST_MODULES}) set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeycache.cpp) elseif (TEST_MODULE MATCHES "awkeys") set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awactions.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awcustomkeyshelper.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awdataaggregator.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awdataengineaggregator.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awdbusadaptor.cpp