From d57e54b714b54fda4c6231901de1fbefbc2fe190 Mon Sep 17 00:00:00 2001 From: arcan1s Date: Sat, 7 May 2016 00:10:06 +0300 Subject: [PATCH] add script formatter --- .../plugin/awformatterhelper.cpp | 17 +- .../awesome-widget/plugin/awformatterhelper.h | 2 +- .../plugin/awscriptformatter.cpp | 151 ++++++++++++++++++ .../awesome-widget/plugin/awscriptformatter.h | 59 +++++++ 4 files changed, 223 insertions(+), 6 deletions(-) create mode 100644 sources/awesome-widget/plugin/awscriptformatter.cpp create mode 100644 sources/awesome-widget/plugin/awscriptformatter.h diff --git a/sources/awesome-widget/plugin/awformatterhelper.cpp b/sources/awesome-widget/plugin/awformatterhelper.cpp index 633cfc9..aa6cd4f 100644 --- a/sources/awesome-widget/plugin/awformatterhelper.cpp +++ b/sources/awesome-widget/plugin/awformatterhelper.cpp @@ -24,6 +24,7 @@ #include "awdatetimeformatter.h" #include "awfloatformatter.h" #include "awnoformatter.h" +#include "awscriptformatter.h" AWFormatterHelper::AWFormatterHelper(QObject *parent) @@ -77,10 +78,12 @@ AWFormatterHelper::defineFormatterClass(const QString name) const settings.endGroup(); FormatterClass formatter = FormatterClass::NoFormat; - if (stringType == QString("Float")) - formatter = FormatterClass::Float; - else if (stringType == QString("DateTime")) + if (stringType == QString("DateTime")) formatter = FormatterClass::DateTime; + else if (stringType == QString("Float")) + formatter = FormatterClass::Float; + else if (stringType == QString("Script")) + formatter = FormatterClass::Script; else qCWarning(LOG_AW) << "Unknown formatter" << stringType; @@ -101,13 +104,17 @@ void AWFormatterHelper::init() << "defined as" << static_cast(formatter); switch (formatter) { + case FormatterClass::DateTime: + m_formatters[key] + = new AWDateTimeFormatter(this, m_genericConfig, name); + break; case FormatterClass::Float: m_formatters[key] = new AWFloatFormatter(this, m_genericConfig, name); break; - case FormatterClass::DateTime: + case FormatterClass::Script: m_formatters[key] - = new AWDateTimeFormatter(this, m_genericConfig, name); + = new AWScriptFormatter(this, m_genericConfig, name); break; case FormatterClass::NoFormat: m_formatters[key] = new AWNoFormatter(this, m_genericConfig, name); diff --git a/sources/awesome-widget/plugin/awformatterhelper.h b/sources/awesome-widget/plugin/awformatterhelper.h index c4012b6..784f5f9 100644 --- a/sources/awesome-widget/plugin/awformatterhelper.h +++ b/sources/awesome-widget/plugin/awformatterhelper.h @@ -29,7 +29,7 @@ class AWFormatterHelper : public QObject Q_OBJECT public: - enum class FormatterClass { Float, DateTime, NoFormat }; + enum class FormatterClass { DateTime, Float, Script, NoFormat }; explicit AWFormatterHelper(QObject *parent = nullptr); virtual ~AWFormatterHelper(); diff --git a/sources/awesome-widget/plugin/awscriptformatter.cpp b/sources/awesome-widget/plugin/awscriptformatter.cpp new file mode 100644 index 0000000..0f056d2 --- /dev/null +++ b/sources/awesome-widget/plugin/awscriptformatter.cpp @@ -0,0 +1,151 @@ +/*************************************************************************** + * 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 "awscriptformatter.h" + +#include +#include + +#include "awdebug.h" + + +AWScriptFormatter::AWScriptFormatter(QObject *parent, const QString filename, + const QString section) + : AWAbstractFormatter(parent, filename, section) +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; + + init(filename, section); + initProgram(); +} + + +AWScriptFormatter::AWScriptFormatter(QObject *parent, const bool appendCode, + const QString code, const bool hasReturn) + : AWAbstractFormatter(parent) +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; + + setAppendCode(appendCode); + setCode(code); + setHasReturn(hasReturn); + initProgram(); +} + + +AWScriptFormatter::~AWScriptFormatter() +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; +} + + +QString AWScriptFormatter::convert(const QVariant &value) const +{ + qCDebug(LOG_AW) << "Convert value" << value; + + // init engine + QJSEngine engine; + QJSValue fn = engine.evaluate(m_program); + QJSValueList args = QJSValueList() << value.toString(); + QJSValue result = fn.call(args); + + if (result.isError()) { + qCWarning(LOG_AW) << "Uncaught exception at line" + << result.property("lineNumber").toInt() << ":" + << result.toString(); + return QString(); + } else { + return result.toString(); + } +} + + +bool AWScriptFormatter::appendCode() const +{ + return m_appendCode; +} + + +QString AWScriptFormatter::code() const +{ + return m_code; +} + + +bool AWScriptFormatter::hasReturn() const +{ + return m_hasReturn; +} + + +QString AWScriptFormatter::program() const +{ + return m_program; +} + + +void AWScriptFormatter::setAppendCode(const bool _appendCode) +{ + qCDebug(LOG_AW) << "Set append code" << _appendCode; + + m_appendCode = _appendCode; +} + + +void AWScriptFormatter::setCode(const QString _code) +{ + qCDebug(LOG_AW) << "Set code" << _code; + + m_code = _code; +} + + +void AWScriptFormatter::setHasReturn(const bool _hasReturn) +{ + qCDebug(LOG_AW) << "Set has return" << _hasReturn; + + m_hasReturn = _hasReturn; +} + + +void AWScriptFormatter::init(const QString filename, const QString section) +{ + qCDebug(LOG_AW) << "Looking for section" << section << "in" << filename; + + QSettings settings(filename, QSettings::IniFormat); + + settings.beginGroup(section); + setAppendCode(settings.value(QString("AppendCode"), true).toBool()); + setCode(settings.value(QString("Code"), QString()).toString()); + setHasReturn(settings.value(QString("HasReturn"), false).toBool()); + settings.endGroup(); +} + + +void AWScriptFormatter::initProgram() +{ + if (m_appendCode) + m_program + = QString("(function(value) { %1%2 })") + .arg(m_code) + .arg(m_hasReturn ? QString("") : QString("; return output;")); + else + m_program = m_code; + + qCInfo(LOG_AW) << "Create JS engine with code" << m_program; +} diff --git a/sources/awesome-widget/plugin/awscriptformatter.h b/sources/awesome-widget/plugin/awscriptformatter.h new file mode 100644 index 0000000..1749e90 --- /dev/null +++ b/sources/awesome-widget/plugin/awscriptformatter.h @@ -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/ * + ***************************************************************************/ + +#ifndef AWSCRIPTFORMATTER_H +#define AWSCRIPTFORMATTER_H + +#include "awabstractformatter.h" + + +class AWScriptFormatter : public AWAbstractFormatter +{ + Q_OBJECT + Q_PROPERTY(bool appendCode READ appendCode WRITE setAppendCode) + Q_PROPERTY(QString code READ code WRITE setCode) + Q_PROPERTY(bool hasReturn READ hasReturn WRITE setHasReturn) + Q_PROPERTY(QString program READ program) + +public: + explicit AWScriptFormatter(QObject *parent, const QString filename, + const QString section); + explicit AWScriptFormatter(QObject *parent, const bool appendCode, + const QString code, const bool hasReturn); + virtual ~AWScriptFormatter(); + QString convert(const QVariant &value) const; + // properties + bool appendCode() const; + QString code() const; + bool hasReturn() const; + QString program() const; + void setAppendCode(const bool _appendCode); + void setCode(const QString _code); + void setHasReturn(const bool _hasReturn); + +private: + void init(const QString filename, const QString section); + void initProgram(); + // properties + bool m_appendCode; + QString m_code; + bool m_hasReturn; + QString m_program; +}; + + +#endif /* AWSCRIPTFORMATTER_H */