initial creation of custom formatters (#91)

This commit is contained in:
Evgenii Alekseev 2016-05-05 00:11:31 +03:00
parent 97f2e78308
commit 2220ad6bfe
12 changed files with 660 additions and 1 deletions

View File

@ -17,7 +17,6 @@
#include "awdebug.h" #include "awdebug.h"
#include "version.h"
Q_LOGGING_CATEGORY(LOG_AW, "org.kde.plasma.awesomewidget", Q_LOGGING_CATEGORY(LOG_AW, "org.kde.plasma.awesomewidget",

View File

@ -21,6 +21,8 @@
#include <QLoggingCategory> #include <QLoggingCategory>
#include "version.h"
#ifndef LOG_FORMAT #ifndef LOG_FORMAT
#define LOG_FORMAT \ #define LOG_FORMAT \
"[%{time process}][%{if-debug}DD%{endif}%{if-info}II%{endif}%{if-" \ "[%{time process}][%{if-debug}DD%{endif}%{if-info}II%{endif}%{if-" \

View File

@ -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 <QObject>
#include <QVariant>
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 */

View File

@ -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 <QDateTime>
#include <QSettings>
#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();
}

View File

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

View File

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

View File

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

View File

@ -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 <QSettings>
#include <QStandardPaths>
#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<int>(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();
}

View File

@ -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 <QObject>
#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<QString, AWAbstractFormatter *> m_formatters;
};
#endif /* AWFORMATTERHELPER_H */

View File

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

View File

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

View File

@ -16,6 +16,7 @@ add_custom_target(
COMMAND ${CPPCHECK_EXECUTABLE} COMMAND ${CPPCHECK_EXECUTABLE}
--enable=warning,performance,portability,information,missingInclude --enable=warning,performance,portability,information,missingInclude
--std=c++11 --std=c++11
--language=c++
--library=qt.cfg --library=qt.cfg
--template="[{severity}][{id}] {message} {callstack} \(On {file}:{line}\)" --template="[{severity}][{id}] {message} {callstack} \(On {file}:{line}\)"
--verbose --verbose