From 2220ad6bfe92cfa67f128982378f0a5f0480499f Mon Sep 17 00:00:00 2001 From: arcan1s Date: Thu, 5 May 2016 00:11:31 +0300 Subject: [PATCH] initial creation of custom formatters (#91) --- sources/awdebug.cpp | 1 - sources/awdebug.h | 2 + .../plugin/awabstractformatter.h | 37 ++++ .../plugin/awdatetimeformatter.cpp | 84 +++++++++ .../plugin/awdatetimeformatter.h | 46 +++++ .../plugin/awfloatformatter.cpp | 159 ++++++++++++++++++ .../awesome-widget/plugin/awfloatformatter.h | 64 +++++++ .../plugin/awformatterhelper.cpp | 118 +++++++++++++ .../awesome-widget/plugin/awformatterhelper.h | 49 ++++++ .../awesome-widget/plugin/awnoformatter.cpp | 59 +++++++ sources/awesome-widget/plugin/awnoformatter.h | 41 +++++ sources/cppcheck.cmake | 1 + 12 files changed, 660 insertions(+), 1 deletion(-) create mode 100644 sources/awesome-widget/plugin/awabstractformatter.h create mode 100644 sources/awesome-widget/plugin/awdatetimeformatter.cpp create mode 100644 sources/awesome-widget/plugin/awdatetimeformatter.h create mode 100644 sources/awesome-widget/plugin/awfloatformatter.cpp create mode 100644 sources/awesome-widget/plugin/awfloatformatter.h create mode 100644 sources/awesome-widget/plugin/awformatterhelper.cpp create mode 100644 sources/awesome-widget/plugin/awformatterhelper.h create mode 100644 sources/awesome-widget/plugin/awnoformatter.cpp create mode 100644 sources/awesome-widget/plugin/awnoformatter.h diff --git a/sources/awdebug.cpp b/sources/awdebug.cpp index 40a8d5f..d81b355 100644 --- a/sources/awdebug.cpp +++ b/sources/awdebug.cpp @@ -17,7 +17,6 @@ #include "awdebug.h" -#include "version.h" Q_LOGGING_CATEGORY(LOG_AW, "org.kde.plasma.awesomewidget", diff --git a/sources/awdebug.h b/sources/awdebug.h index 43944ce..6a3b2dd 100644 --- a/sources/awdebug.h +++ b/sources/awdebug.h @@ -21,6 +21,8 @@ #include +#include "version.h" + #ifndef LOG_FORMAT #define LOG_FORMAT \ "[%{time process}][%{if-debug}DD%{endif}%{if-info}II%{endif}%{if-" \ diff --git a/sources/awesome-widget/plugin/awabstractformatter.h b/sources/awesome-widget/plugin/awabstractformatter.h new file mode 100644 index 0000000..d873ef5 --- /dev/null +++ b/sources/awesome-widget/plugin/awabstractformatter.h @@ -0,0 +1,37 @@ +/*************************************************************************** + * 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 AWABSTRACTFORMATTER_H +#define AWABSTRACTFORMATTER_H + +#include +#include + + +class AWAbstractFormatter : public QObject +{ +public: + explicit AWAbstractFormatter(QObject *parent, const QString, const QString) + : QObject(parent){}; + explicit AWAbstractFormatter(QObject *parent) + : QObject(parent){}; + virtual ~AWAbstractFormatter(){}; + virtual QString convert(const QVariant &value) const = 0; +}; + + +#endif /* AWABSTRACTFORMATTER_H */ diff --git a/sources/awesome-widget/plugin/awdatetimeformatter.cpp b/sources/awesome-widget/plugin/awdatetimeformatter.cpp new file mode 100644 index 0000000..515dd42 --- /dev/null +++ b/sources/awesome-widget/plugin/awdatetimeformatter.cpp @@ -0,0 +1,84 @@ +/*************************************************************************** + * 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 "awdatetimeformatter.h" + +#include +#include + +#include "awdebug.h" + + +AWDateTimeFormatter::AWDateTimeFormatter(QObject *parent, + const QString filename, + const QString section) + : AWAbstractFormatter(parent, filename, section) +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; + + init(filename, section); +} + + +AWDateTimeFormatter::AWDateTimeFormatter(QObject *parent, const QString format) + : AWAbstractFormatter(parent) +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; + + setFormat(format); +} + + +AWDateTimeFormatter::~AWDateTimeFormatter() +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; +} + + +QString AWDateTimeFormatter::convert(const QVariant &value) const +{ + qCDebug(LOG_AW) << "Convert value" << value; + + return value.toDateTime().toString(m_format); +} + + +QString AWDateTimeFormatter::format() const +{ + return m_format; +} + + +void AWDateTimeFormatter::setFormat(const QString _format) +{ + qCDebug(LOG_AW) << "Set format" << _format; + + m_format = _format; +} + + +void AWDateTimeFormatter::init(const QString filename, const QString section) +{ + qCDebug(LOG_AW) << "Looking for section" << section << "in" << filename; + + QSettings settings(filename, QSettings::IniFormat); + + settings.beginGroup(section); + setFormat(settings.value(QString("Format"), QString()).toString()); + settings.endGroup(); +} diff --git a/sources/awesome-widget/plugin/awdatetimeformatter.h b/sources/awesome-widget/plugin/awdatetimeformatter.h new file mode 100644 index 0000000..df8e9d7 --- /dev/null +++ b/sources/awesome-widget/plugin/awdatetimeformatter.h @@ -0,0 +1,46 @@ +/*************************************************************************** + * 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 AWDATETIMEFORMATTER_H +#define AWDATETIMEFORMATTER_H + +#include "awabstractformatter.h" + + +class AWDateTimeFormatter : public AWAbstractFormatter +{ + Q_OBJECT + Q_PROPERTY(QString format READ format WRITE setFormat) + +public: + explicit AWDateTimeFormatter(QObject *parent, const QString filename, + const QString section); + explicit AWDateTimeFormatter(QObject *parent, const QString format); + virtual ~AWDateTimeFormatter(); + QString convert(const QVariant &value) const; + // properties + QString format() const; + void setFormat(const QString _format); + +private: + void init(const QString filename, const QString section); + // properties + QString m_format; +}; + + +#endif /* AWDATETIMEFORMATTER_H */ diff --git a/sources/awesome-widget/plugin/awfloatformatter.cpp b/sources/awesome-widget/plugin/awfloatformatter.cpp new file mode 100644 index 0000000..6663e99 --- /dev/null +++ b/sources/awesome-widget/plugin/awfloatformatter.cpp @@ -0,0 +1,159 @@ +/*************************************************************************** + * 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 "awfloatformatter.h" + +#include + +#include "awdebug.h" + + +AWFloatFormatter::AWFloatFormatter(QObject *parent, const QString filename, + const QString section) + : AWAbstractFormatter(parent, filename, section) +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; + + init(filename, section); +} + + +AWFloatFormatter::AWFloatFormatter(QObject *parent, const QChar fillChar, + const char format, const double multiplier, + const int precision, const int width) + : AWAbstractFormatter(parent) +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; + + setFillChar(fillChar); + setFormat(format); + setMultiplier(multiplier); + setPrecision(precision); + setWidth(width); +} + + +AWFloatFormatter::~AWFloatFormatter() +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; +} + + +QString AWFloatFormatter::convert(const QVariant &value) const +{ + qCDebug(LOG_AW) << "Convert value" << value; + + return QString("%1").arg(value.toDouble() * m_multiplier, m_width, m_format, + m_precision, m_fillChar); +} + + +QChar AWFloatFormatter::fillChar() const +{ + return m_fillChar; +} + + +char AWFloatFormatter::format() const +{ + return m_format; +} + + +double AWFloatFormatter::multiplier() const +{ + return m_multiplier; +} + + +int AWFloatFormatter::precision() const +{ + return m_precision; +} + + +int AWFloatFormatter::width() const +{ + return m_width; +} + + +void AWFloatFormatter::setFillChar(const QChar _fillChar) +{ + qCDebug(LOG_AW) << "Set char" << _fillChar; + + m_fillChar = _fillChar; +} + + +void AWFloatFormatter::setFormat(char _format) +{ + qCDebug(LOG_AW) << "Set format" << _format; + // http://doc.qt.io/qt-5/qstring.html#argument-formats + if ((_format != 'e') && (_format != 'E') && (_format != 'f') + && (_format != 'g') && (_format != 'G')) { + qCWarning(LOG_AW) << "Invalid format" << _format; + _format = 'f'; + } + + m_format = _format; +} + + +void AWFloatFormatter::setMultiplier(const double _multiplier) +{ + qCDebug(LOG_AW) << "Set multiplier" << _multiplier; + + m_multiplier = _multiplier; +} + + +void AWFloatFormatter::setPrecision(const int _precision) +{ + qCDebug(LOG_AW) << "Set precision" << _precision; + + m_precision = _precision; +} + + +void AWFloatFormatter::setWidth(const int _width) +{ + qCDebug(LOG_AW) << "Set width" << _width; + + m_width = _width; +} + + +void AWFloatFormatter::init(const QString filename, const QString section) +{ + qCDebug(LOG_AW) << "Looking for section" << section << "in" << filename; + + QSettings settings(filename, QSettings::IniFormat); + + settings.beginGroup(section); + setFillChar( + settings.value(QString("FillChar"), QString()).toString().at(0)); + setFormat(settings.value(QString("Format"), QString("f")) + .toString() + .at(0) + .toLatin1()); + setMultiplier(settings.value(QString("Multiplier"), 1.0).toDouble()); + setPrecision(settings.value(QString("Precision"), -1).toInt()); + setWidth(settings.value(QString("Width"), 0).toInt()); + settings.endGroup(); +} diff --git a/sources/awesome-widget/plugin/awfloatformatter.h b/sources/awesome-widget/plugin/awfloatformatter.h new file mode 100644 index 0000000..1222dd5 --- /dev/null +++ b/sources/awesome-widget/plugin/awfloatformatter.h @@ -0,0 +1,64 @@ +/*************************************************************************** + * 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 AWFLOATFORMATTER_H +#define AWFLOATFORMATTER_H + +#include "awabstractformatter.h" + + +class AWFloatFormatter : public AWAbstractFormatter +{ + Q_OBJECT + Q_PROPERTY(QChar fillChar READ fillChar WRITE setFillChar) + Q_PROPERTY(char format READ format WRITE setFormat) + Q_PROPERTY(double multiplier READ multiplier WRITE setMultiplier) + Q_PROPERTY(int precision READ precision WRITE setPrecision) + Q_PROPERTY(int width READ width WRITE setWidth) + +public: + explicit AWFloatFormatter(QObject *parent, const QString filename, + const QString section); + explicit AWFloatFormatter(QObject *parent, const QChar fillChar, + const char format, const double multiplier, + const int precision, const int width); + virtual ~AWFloatFormatter(); + QString convert(const QVariant &value) const; + // properties + QChar fillChar() const; + char format() const; + double multiplier() const; + int precision() const; + int width() const; + void setFillChar(const QChar _fillChar); + void setFormat(char _format); + void setMultiplier(const double _multiplier); + void setPrecision(const int _precision); + void setWidth(const int _width); + +private: + void init(const QString filename, const QString section); + // properties + QChar m_fillChar; + char m_format; + double m_multiplier; + int m_precision; + int m_width; +}; + + +#endif /* AWFLOATFORMATTER_H */ diff --git a/sources/awesome-widget/plugin/awformatterhelper.cpp b/sources/awesome-widget/plugin/awformatterhelper.cpp new file mode 100644 index 0000000..633cfc9 --- /dev/null +++ b/sources/awesome-widget/plugin/awformatterhelper.cpp @@ -0,0 +1,118 @@ +/*************************************************************************** + * 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 "awformatterhelper.h" + +#include +#include + +#include "awdebug.h" +#include "awdatetimeformatter.h" +#include "awfloatformatter.h" +#include "awnoformatter.h" + + +AWFormatterHelper::AWFormatterHelper(QObject *parent) + : QObject(parent) +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; + + m_genericConfig = QString("%1/awesomewidgets/general.ini") + .arg(QStandardPaths::writableLocation( + QStandardPaths::GenericDataLocation)); +#ifdef BUILD_FUTURE + init(); +#endif /* BUILD_FUTURE */ +} + + +AWFormatterHelper::~AWFormatterHelper() +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; + + m_formatters.clear(); +} + + +QString AWFormatterHelper::convert(const QVariant &value, + const QString name) const +{ + qCDebug(LOG_AW) << "Convert value" << value << "for" << name; + + return m_formatters.contains(name) ? m_formatters[name]->convert(value) + : value.toString(); +} + + +QStringList AWFormatterHelper::definedFormatters() const +{ + return m_formatters.keys(); +} + + +AWFormatterHelper::FormatterClass +AWFormatterHelper::defineFormatterClass(const QString name) const +{ + qCDebug(LOG_AW) << "Define formatter class for" << name; + + QSettings settings(m_genericConfig, QSettings::IniFormat); + + settings.beginGroup(name); + QString stringType + = settings.value(QString("Type"), QString("NoFormat")).toString(); + settings.endGroup(); + + FormatterClass formatter = FormatterClass::NoFormat; + if (stringType == QString("Float")) + formatter = FormatterClass::Float; + else if (stringType == QString("DateTime")) + formatter = FormatterClass::DateTime; + else + qCWarning(LOG_AW) << "Unknown formatter" << stringType; + + return formatter; +} + + +void AWFormatterHelper::init() +{ + QSettings settings(m_genericConfig, QSettings::IniFormat); + + settings.beginGroup(QString("Formatters")); + QStringList keys = settings.childKeys(); + for (auto key : keys) { + QString name = settings.value(key).toString(); + FormatterClass formatter = defineFormatterClass(name); + qCInfo(LOG_AW) << "Found formatter" << name << "for key" << key + << "defined as" << static_cast(formatter); + + switch (formatter) { + case FormatterClass::Float: + m_formatters[key] + = new AWFloatFormatter(this, m_genericConfig, name); + break; + case FormatterClass::DateTime: + m_formatters[key] + = new AWDateTimeFormatter(this, m_genericConfig, name); + break; + case FormatterClass::NoFormat: + m_formatters[key] = new AWNoFormatter(this, m_genericConfig, name); + break; + } + } + settings.endGroup(); +} diff --git a/sources/awesome-widget/plugin/awformatterhelper.h b/sources/awesome-widget/plugin/awformatterhelper.h new file mode 100644 index 0000000..c4012b6 --- /dev/null +++ b/sources/awesome-widget/plugin/awformatterhelper.h @@ -0,0 +1,49 @@ +/*************************************************************************** + * 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 AWFORMATTERHELPER_H +#define AWFORMATTERHELPER_H + +#include + +#include "awabstractformatter.h" + + +class AWFormatterHelper : public QObject +{ + Q_OBJECT + +public: + enum class FormatterClass { Float, DateTime, NoFormat }; + + explicit AWFormatterHelper(QObject *parent = nullptr); + virtual ~AWFormatterHelper(); + QString convert(const QVariant &value, const QString name) const; + QStringList definedFormatters() const; + +private: + AWFormatterHelper::FormatterClass + defineFormatterClass(const QString name) const; + void init(); + // properties + QString m_genericConfig; + QHash m_formatters; +}; + + +#endif /* AWFORMATTERHELPER_H */ diff --git a/sources/awesome-widget/plugin/awnoformatter.cpp b/sources/awesome-widget/plugin/awnoformatter.cpp new file mode 100644 index 0000000..f36a2b5 --- /dev/null +++ b/sources/awesome-widget/plugin/awnoformatter.cpp @@ -0,0 +1,59 @@ +/*************************************************************************** + * 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 "awnoformatter.h" + +#include "awdebug.h" + + +AWNoFormatter::AWNoFormatter(QObject *parent, const QString filename, + const QString section) + : AWAbstractFormatter(parent, filename, section) +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; + + init(filename, section); +} + + +AWNoFormatter::AWNoFormatter(QObject *parent) + : AWAbstractFormatter(parent) +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; +} + + +AWNoFormatter::~AWNoFormatter() +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; +} + + +QString AWNoFormatter::convert(const QVariant &value) const +{ + qCDebug(LOG_AW) << "Convert value" << value; + + return value.toString(); +} + + +void AWNoFormatter::init(const QString filename, const QString section) +{ + qCDebug(LOG_AW) << "Looking for section" << section << "in" << filename; + // dummy method for future references +} diff --git a/sources/awesome-widget/plugin/awnoformatter.h b/sources/awesome-widget/plugin/awnoformatter.h new file mode 100644 index 0000000..0251b50 --- /dev/null +++ b/sources/awesome-widget/plugin/awnoformatter.h @@ -0,0 +1,41 @@ +/*************************************************************************** + * 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 AWNOFORMATTER_H +#define AWNOFORMATTER_H + +#include "awabstractformatter.h" + + +class AWNoFormatter : public AWAbstractFormatter +{ + Q_OBJECT + +public: + explicit AWNoFormatter(QObject *parent, const QString filename, + const QString section); + explicit AWNoFormatter(QObject *parent); + virtual ~AWNoFormatter(); + QString convert(const QVariant &value) const; + +private: + void init(const QString filename, const QString section); + // properties +}; + + +#endif /* AWNOFORMATTER_H */ diff --git a/sources/cppcheck.cmake b/sources/cppcheck.cmake index 47fd6c8..3a38881 100644 --- a/sources/cppcheck.cmake +++ b/sources/cppcheck.cmake @@ -16,6 +16,7 @@ add_custom_target( COMMAND ${CPPCHECK_EXECUTABLE} --enable=warning,performance,portability,information,missingInclude --std=c++11 + --language=c++ --library=qt.cfg --template="[{severity}][{id}] {message} {callstack} \(On {file}:{line}\)" --verbose