From 004a97800c2b84bf05f4c3a3f81ae34848de7cc2 Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Wed, 2 Nov 2016 23:23:30 +0300 Subject: [PATCH] add json formatter --- .../plugin/awformatterhelper.cpp | 11 +- .../awesomewidgets/awabstractformatter.cpp | 5 + sources/awesomewidgets/awabstractformatter.h | 3 +- sources/awesomewidgets/awjsonformatter.cpp | 201 ++++++++++++++++++ sources/awesomewidgets/awjsonformatter.h | 62 ++++++ sources/awesomewidgets/awjsonformatter.ui | 167 +++++++++++++++ 6 files changed, 447 insertions(+), 2 deletions(-) create mode 100644 sources/awesomewidgets/awjsonformatter.cpp create mode 100644 sources/awesomewidgets/awjsonformatter.h create mode 100644 sources/awesomewidgets/awjsonformatter.ui diff --git a/sources/awesome-widget/plugin/awformatterhelper.cpp b/sources/awesome-widget/plugin/awformatterhelper.cpp index 147588c..3f92cc6 100644 --- a/sources/awesome-widget/plugin/awformatterhelper.cpp +++ b/sources/awesome-widget/plugin/awformatterhelper.cpp @@ -26,6 +26,7 @@ #include "awdatetimeformatter.h" #include "awdebug.h" #include "awfloatformatter.h" +#include "awjsonformatter.h" #include "awlistformatter.h" #include "awnoformatter.h" #include "awscriptformatter.h" @@ -169,6 +170,8 @@ AWFormatterHelper::defineFormatterClass(const QString stringType) const formatter = AWAbstractFormatter::FormatterClass::Script; else if (stringType == QString("String")) formatter = AWAbstractFormatter::FormatterClass::String; + else if (stringType == QString("Json")) + formatter = AWAbstractFormatter::FormatterClass::Json; else qCWarning(LOG_AW) << "Unknown formatter" << stringType; @@ -216,6 +219,9 @@ void AWFormatterHelper::initFormatters() m_formattersClasses[name] = new AWStringFormatter(this, filePath); break; + case AWAbstractFormatter::FormatterClass::Json: + m_formattersClasses[name] = new AWJsonFormatter(this, filePath); + break; case AWAbstractFormatter::FormatterClass::NoFormat: m_formattersClasses[name] = new AWNoFormatter(this, filePath); break; @@ -296,7 +302,8 @@ void AWFormatterHelper::doCreateItem() QStringList selection = QStringList() << QString("NoFormat") << QString("DateTime") << QString("Float") << QString("List") - << QString("Script") << QString("String"); + << QString("Script") << QString("String") + << QString("Json"); bool ok; QString select = QInputDialog::getItem( this, i18n("Select type"), i18n("Type:"), selection, 0, false, &ok); @@ -319,6 +326,8 @@ void AWFormatterHelper::doCreateItem() return createItem(); case AWAbstractFormatter::FormatterClass::String: return createItem(); + case AWAbstractFormatter::FormatterClass::Json: + return createItem(); case AWAbstractFormatter::FormatterClass::NoFormat: return createItem(); } diff --git a/sources/awesomewidgets/awabstractformatter.cpp b/sources/awesomewidgets/awabstractformatter.cpp index af9e55e..b97a4e6 100644 --- a/sources/awesomewidgets/awabstractformatter.cpp +++ b/sources/awesomewidgets/awabstractformatter.cpp @@ -69,6 +69,9 @@ QString AWAbstractFormatter::strType() const case FormatterClass::String: value = QString("String"); break; + case FormatterClass::Json: + value = QString("Json"); + break; case FormatterClass::NoFormat: value = QString("NoFormat"); break; @@ -98,6 +101,8 @@ void AWAbstractFormatter::setStrType(const QString _type) m_type = FormatterClass::Script; else if (_type == QString("String")) m_type = FormatterClass::String; + else if (_type == QString("Json")) + m_type = FormatterClass::Json; else m_type = FormatterClass::NoFormat; } diff --git a/sources/awesomewidgets/awabstractformatter.h b/sources/awesomewidgets/awabstractformatter.h index 883314d..12db5e6 100644 --- a/sources/awesomewidgets/awabstractformatter.h +++ b/sources/awesomewidgets/awabstractformatter.h @@ -34,7 +34,8 @@ public: List, Script, String, - NoFormat + NoFormat, + Json }; explicit AWAbstractFormatter(QWidget *parent, diff --git a/sources/awesomewidgets/awjsonformatter.cpp b/sources/awesomewidgets/awjsonformatter.cpp new file mode 100644 index 0000000..2b3fa38 --- /dev/null +++ b/sources/awesomewidgets/awjsonformatter.cpp @@ -0,0 +1,201 @@ +/*************************************************************************** + * 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 "awjsonformatter.h" +#include "ui_awjsonformatter.h" + +#include + +#include + +#include "awdebug.h" + + +AWJsonFormatter::AWJsonFormatter(QWidget *parent, const QString filePath) + : AWAbstractFormatter(parent, filePath) + , ui(new Ui::AWJsonFormatter) +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; + + if (!filePath.isEmpty()) + readConfiguration(); + ui->setupUi(this); + translate(); +} + + +AWJsonFormatter::~AWJsonFormatter() +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; + + delete ui; +} + + +QString AWJsonFormatter::convert(const QVariant &_value) const +{ + qCDebug(LOG_LIB) << "Convert value" << _value; + + QVariant converted = _value; + for (auto &element : m_splittedPath) + converted = getFromJson(converted, element); + + return converted.toString(); +} + + +AWJsonFormatter *AWJsonFormatter::copy(const QString _fileName, + const int _number) +{ + qCDebug(LOG_LIB) << "File" << _fileName << "with number" << _number; + + AWJsonFormatter *item + = new AWJsonFormatter(static_cast(parent()), _fileName); + AWAbstractFormatter::copyDefaults(item); + item->setNumber(_number); + item->setPath(path()); + + return item; +} + + +QString AWJsonFormatter::path() const +{ + return m_path; +} + + +void AWJsonFormatter::setPath(const QString _path) +{ + qCDebug(LOG_LIB) << "Path" << _path; + + m_path = _path; + initPath(); +} + + +void AWJsonFormatter::readConfiguration() +{ + AWAbstractFormatter::readConfiguration(); + + QSettings settings(fileName(), QSettings::IniFormat); + + settings.beginGroup(QString("Desktop Entry")); + setPath(settings.value(QString("X-AW-Path"), path()).toString()); + settings.endGroup(); + + bumpApi(AWEFAPI); +} + + +int AWJsonFormatter::showConfiguration(const QVariant args) +{ + Q_UNUSED(args) + + ui->lineEdit_name->setText(name()); + ui->lineEdit_comment->setText(comment()); + ui->label_typeValue->setText(QString("NoFormat")); + ui->lineEdit_path->setText(path()); + + int ret = exec(); + if (ret != 1) + return ret; + setName(ui->lineEdit_name->text()); + setComment(ui->lineEdit_comment->text()); + setApiVersion(AWEFAPI); + setStrType(ui->label_typeValue->text()); + setPath(ui->lineEdit_path->text()); + + writeConfiguration(); + return ret; +} + + +void AWJsonFormatter::writeConfiguration() const +{ + AWAbstractFormatter::writeConfiguration(); + + QSettings settings(writtableConfig(), QSettings::IniFormat); + qCInfo(LOG_LIB) << "Configuration file" << settings.fileName(); + + settings.beginGroup(QString("Desktop Entry")); + settings.setValue(QString("X-AW-Path"), path()); + settings.endGroup(); + + settings.sync(); +} + + +QVariant AWJsonFormatter::getFromJson(const QVariant &value, + const QVariant &element) const +{ + qCDebug(LOG_LIB) << "Looking for element" << element << "in" << value; + + if (element.type() == QVariant::String) { + return getFromMap(value, element.toString()); + } else if (element.type() == QVariant::Int) { + return getFromList(value, element.toInt()); + } else { + qCWarning(LOG_LIB) << "Unknown type" << element.typeName(); + return value; + } +} + + +QVariant AWJsonFormatter::getFromList(const QVariant &value, + const int index) const +{ + qCDebug(LOG_LIB) << "Looking for index" << index << "in" << value; + + return value.toList()[index]; +} + + +QVariant AWJsonFormatter::getFromMap(const QVariant &value, + const QString &key) const +{ + qCDebug(LOG_LIB) << "Looking for key" << key << "in" << value; + + return value.toMap()[key]; +} + + +void AWJsonFormatter::initPath() +{ + m_splittedPath.clear(); + QStringList splittedByDot = m_path.split(QRegExp(QString("(\\.|\\[|\\])")), + QString::SkipEmptyParts); + + for (auto &element : splittedByDot) { + bool ok; + int number = element.toInt(&ok); + if (ok) + m_splittedPath.append(number); + else + m_splittedPath.append(element); + } +} + + +void AWJsonFormatter::translate() +{ + ui->label_name->setText(i18n("Name")); + ui->label_comment->setText(i18n("Comment")); + ui->label_type->setText(i18n("Type")); + ui->label_path->setText(i18n("Path")); +} diff --git a/sources/awesomewidgets/awjsonformatter.h b/sources/awesomewidgets/awjsonformatter.h new file mode 100644 index 0000000..338d2d8 --- /dev/null +++ b/sources/awesomewidgets/awjsonformatter.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 AWJSONFORMATTER_H +#define AWJSONFORMATTER_H + +#include "awabstractformatter.h" + + +namespace Ui +{ +class AWJsonFormatter; +} + +class AWJsonFormatter : public AWAbstractFormatter +{ + Q_OBJECT + Q_PROPERTY(QString path READ path WRITE setPath) + +public: + explicit AWJsonFormatter(QWidget *parent, + const QString filePath = QString()); + virtual ~AWJsonFormatter(); + QString convert(const QVariant &_value) const; + AWJsonFormatter *copy(const QString _fileName, const int _number); + // properties + QString path() const; + void setPath(const QString _path); + +public slots: + void readConfiguration(); + int showConfiguration(const QVariant args = QVariant()); + void writeConfiguration() const; + +private: + Ui::AWJsonFormatter *ui = nullptr; + QVariant getFromJson(const QVariant &value, const QVariant &element) const; + QVariant getFromList(const QVariant &value, const int index) const; + QVariant getFromMap(const QVariant &value, const QString &key) const; + void initPath(); + void translate(); + // properties + QString m_path; + QVariantList m_splittedPath; +}; + + +#endif /* AWJSONFORMATTER_H */ diff --git a/sources/awesomewidgets/awjsonformatter.ui b/sources/awesomewidgets/awjsonformatter.ui new file mode 100644 index 0000000..f648fb4 --- /dev/null +++ b/sources/awesomewidgets/awjsonformatter.ui @@ -0,0 +1,167 @@ + + + AWJsonFormatter + + + + 0 + 0 + 420 + 128 + + + + Configuration + + + + + + + + + 0 + 0 + + + + Name + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + 0 + 0 + + + + Comment + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + Type + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + + + + + 0 + 0 + + + + Path + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + AWJsonFormatter + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + AWJsonFormatter + reject() + + + 316 + 260 + + + 286 + 274 + + + + +