add custom keys configuration

This commit is contained in:
Evgenii Alekseev 2017-07-10 01:43:21 +03:00
parent 7a00dce7c5
commit 7ce3e8b1cf
14 changed files with 428 additions and 46 deletions

View File

@ -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 <KI18n/KLocalizedString>
#include <QPushButton>
#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<QString, QString> current
= static_cast<AWAbstractSelector *>(sender())->current();
int index
= m_selectors.indexOf(static_cast<AWAbstractSelector *>(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<QString, QString>());
}
}
void AWCustomKeysConfig::addSelector(const QStringList &_keys,
const QStringList &_values,
const QPair<QString, QString> &_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<QString, QString> data;
for (auto &selector : m_selectors) {
QPair<QString, QString> 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<QStringList, QStringList> 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<QStringList, QStringList>(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<QString, QString>(key, userKeys[key]));
// empty one
addSelector(keys.first, keys.second, QPair<QString, QString>());
}

View File

@ -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 <QDialog>
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<AWAbstractSelector *> m_selectors;
// properties
QStringList m_keys;
// methods
void addSelector(const QStringList &_keys, const QStringList &_values,
const QPair<QString, QString> &_current);
void clearSelectors();
void execDialog();
void init();
QPair<QStringList, QStringList> initKeys() const;
void updateDialog();
};
#endif /* AWCUSTOMKEYSCONFIG_H */

View File

@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AWCustomKeysConfig</class>
<widget class="QDialog" name="AWCustomKeysConfig">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>384</width>
<height>249</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>AWCustomKeysConfig</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>AWCustomKeysConfig</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -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<QString, QString> &_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<QString, QString> AWCustomKeysHelper::getUserKeys() const
{
return m_keys;
}
QStringList AWCustomKeysHelper::keys() const
{
return m_keys.keys();

View File

@ -30,9 +30,12 @@ class AWCustomKeysHelper : public QObject
public:
explicit AWCustomKeysHelper(QObject *_parent = nullptr);
virtual ~AWCustomKeysHelper();
void initKeys();
bool writeKeys(const QHash<QString, QString> &_configuration) const;
// read-write methods
void initItems();
bool writeItems(const QHash<QString, QString> &_configuration) const;
bool removeUnusedKeys(const QStringList &_keys) const;
// get
QHash<QString, QString> getUserKeys() const;
QStringList keys() const;
QString source(const QString &_key) const;
QStringList sources() const;

View File

@ -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;
}

View File

@ -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();
}

View File

@ -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:
};

View File

@ -52,6 +52,37 @@ AWFormatterHelper::~AWFormatterHelper()
}
void AWFormatterHelper::initItems()
{
installDirectories();
initFormatters();
initKeys();
}
bool AWFormatterHelper::writeItems(
const QHash<QString, QString> &_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<QString, QString> AWFormatterHelper::getFormatters() const
}
void AWFormatterHelper::initItems()
{
installDirectories();
initFormatters();
initKeys();
}
QList<AbstractExtItem *> AWFormatterHelper::items() const
{
QList<AbstractExtItem *> converted;
@ -128,29 +151,6 @@ bool AWFormatterHelper::removeUnusedFormatters(const QStringList &_keys) const
}
bool AWFormatterHelper::writeFormatters(
const QHash<QString, QString> &_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();

View File

@ -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<QString, QString> &_configuration) const;
// methods
QString convert(const QVariant &_value, const QString &_name) const;
QStringList definedFormatters() const;
QHash<QString, QString> getFormatters() const;
void initItems();
QList<AbstractExtItem *> items() const;
QStringList knownFormatters() const;
bool removeUnusedFormatters(const QStringList &_keys) const;
bool writeFormatters(const QHash<QString, QString> &_configuration) const;
public slots:
void editItems();

View File

@ -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();

View File

@ -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;
};

View File

@ -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 {

View File

@ -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