mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-04-25 07:57:19 +00:00
add script formatter
This commit is contained in:
parent
13f2d560d0
commit
d57e54b714
@ -24,6 +24,7 @@
|
|||||||
#include "awdatetimeformatter.h"
|
#include "awdatetimeformatter.h"
|
||||||
#include "awfloatformatter.h"
|
#include "awfloatformatter.h"
|
||||||
#include "awnoformatter.h"
|
#include "awnoformatter.h"
|
||||||
|
#include "awscriptformatter.h"
|
||||||
|
|
||||||
|
|
||||||
AWFormatterHelper::AWFormatterHelper(QObject *parent)
|
AWFormatterHelper::AWFormatterHelper(QObject *parent)
|
||||||
@ -77,10 +78,12 @@ AWFormatterHelper::defineFormatterClass(const QString name) const
|
|||||||
settings.endGroup();
|
settings.endGroup();
|
||||||
|
|
||||||
FormatterClass formatter = FormatterClass::NoFormat;
|
FormatterClass formatter = FormatterClass::NoFormat;
|
||||||
if (stringType == QString("Float"))
|
if (stringType == QString("DateTime"))
|
||||||
formatter = FormatterClass::Float;
|
|
||||||
else if (stringType == QString("DateTime"))
|
|
||||||
formatter = FormatterClass::DateTime;
|
formatter = FormatterClass::DateTime;
|
||||||
|
else if (stringType == QString("Float"))
|
||||||
|
formatter = FormatterClass::Float;
|
||||||
|
else if (stringType == QString("Script"))
|
||||||
|
formatter = FormatterClass::Script;
|
||||||
else
|
else
|
||||||
qCWarning(LOG_AW) << "Unknown formatter" << stringType;
|
qCWarning(LOG_AW) << "Unknown formatter" << stringType;
|
||||||
|
|
||||||
@ -101,13 +104,17 @@ void AWFormatterHelper::init()
|
|||||||
<< "defined as" << static_cast<int>(formatter);
|
<< "defined as" << static_cast<int>(formatter);
|
||||||
|
|
||||||
switch (formatter) {
|
switch (formatter) {
|
||||||
|
case FormatterClass::DateTime:
|
||||||
|
m_formatters[key]
|
||||||
|
= new AWDateTimeFormatter(this, m_genericConfig, name);
|
||||||
|
break;
|
||||||
case FormatterClass::Float:
|
case FormatterClass::Float:
|
||||||
m_formatters[key]
|
m_formatters[key]
|
||||||
= new AWFloatFormatter(this, m_genericConfig, name);
|
= new AWFloatFormatter(this, m_genericConfig, name);
|
||||||
break;
|
break;
|
||||||
case FormatterClass::DateTime:
|
case FormatterClass::Script:
|
||||||
m_formatters[key]
|
m_formatters[key]
|
||||||
= new AWDateTimeFormatter(this, m_genericConfig, name);
|
= new AWScriptFormatter(this, m_genericConfig, name);
|
||||||
break;
|
break;
|
||||||
case FormatterClass::NoFormat:
|
case FormatterClass::NoFormat:
|
||||||
m_formatters[key] = new AWNoFormatter(this, m_genericConfig, name);
|
m_formatters[key] = new AWNoFormatter(this, m_genericConfig, name);
|
||||||
|
@ -29,7 +29,7 @@ class AWFormatterHelper : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum class FormatterClass { Float, DateTime, NoFormat };
|
enum class FormatterClass { DateTime, Float, Script, NoFormat };
|
||||||
|
|
||||||
explicit AWFormatterHelper(QObject *parent = nullptr);
|
explicit AWFormatterHelper(QObject *parent = nullptr);
|
||||||
virtual ~AWFormatterHelper();
|
virtual ~AWFormatterHelper();
|
||||||
|
151
sources/awesome-widget/plugin/awscriptformatter.cpp
Normal file
151
sources/awesome-widget/plugin/awscriptformatter.cpp
Normal file
@ -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 <QJSEngine>
|
||||||
|
#include <QSettings>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
59
sources/awesome-widget/plugin/awscriptformatter.h
Normal file
59
sources/awesome-widget/plugin/awscriptformatter.h
Normal 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/ *
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
#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 */
|
Loading…
Reference in New Issue
Block a user