diff --git a/sources/awesome-widget/plugin/awformatterhelper.cpp b/sources/awesome-widget/plugin/awformatterhelper.cpp index 649cd0b..2dc9598 100644 --- a/sources/awesome-widget/plugin/awformatterhelper.cpp +++ b/sources/awesome-widget/plugin/awformatterhelper.cpp @@ -145,20 +145,21 @@ void AWFormatterHelper::editItems() } -AWFormatterHelper::FormatterClass +AWAbstractFormatter::FormatterClass AWFormatterHelper::defineFormatterClass(const QString stringType) const { qCDebug(LOG_AW) << "Define formatter class for" << stringType; - FormatterClass formatter = FormatterClass::NoFormat; + AWAbstractFormatter::FormatterClass formatter + = AWAbstractFormatter::FormatterClass::NoFormat; if (stringType == QString("DateTime")) - formatter = FormatterClass::DateTime; + formatter = AWAbstractFormatter::FormatterClass::DateTime; else if (stringType == QString("Float")) - formatter = FormatterClass::Float; + formatter = AWAbstractFormatter::FormatterClass::Float; else if (stringType == QString("NoFormat")) ; else if (stringType == QString("Script")) - formatter = FormatterClass::Script; + formatter = AWAbstractFormatter::FormatterClass::Script; else qCWarning(LOG_AW) << "Unknown formatter" << stringType; @@ -187,19 +188,19 @@ void AWFormatterHelper::initFormatters() switch (metadata.second) { - case FormatterClass::DateTime: + case AWAbstractFormatter::FormatterClass::DateTime: m_formattersClasses[name] = new AWDateTimeFormatter(this, filePath); break; - case FormatterClass::Float: + case AWAbstractFormatter::FormatterClass::Float: m_formattersClasses[name] = new AWFloatFormatter(this, filePath); break; - case FormatterClass::Script: + case AWAbstractFormatter::FormatterClass::Script: m_formattersClasses[name] = new AWScriptFormatter(this, filePath); break; - case FormatterClass::NoFormat: + case AWAbstractFormatter::FormatterClass::NoFormat: m_formattersClasses[name] = new AWNoFormatter(this, filePath); break; } @@ -258,7 +259,7 @@ void AWFormatterHelper::installDirectories() } -QPair +QPair AWFormatterHelper::readMetadata(const QString filePath) const { qCDebug(LOG_AW) << "Read initial parameters from" << filePath; @@ -268,10 +269,10 @@ AWFormatterHelper::readMetadata(const QString filePath) const QString name = settings.value(QString("Name"), filePath).toString(); QString type = settings.value(QString("X-AW-Type"), QString("NoFormat")).toString(); - FormatterClass formatter = defineFormatterClass(type); + AWAbstractFormatter::FormatterClass formatter = defineFormatterClass(type); settings.endGroup(); - return QPair(name, formatter); + return QPair(name, formatter); } @@ -289,15 +290,16 @@ void AWFormatterHelper::doCreateItem() } qCInfo(LOG_AW) << "Selected type" << select; - FormatterClass formatter = defineFormatterClass(select); + AWAbstractFormatter::FormatterClass formatter + = defineFormatterClass(select); switch (formatter) { - case FormatterClass::DateTime: + case AWAbstractFormatter::FormatterClass::DateTime: return createItem(); - case FormatterClass::Float: + case AWAbstractFormatter::FormatterClass::Float: return createItem(); - case FormatterClass::Script: + case AWAbstractFormatter::FormatterClass::Script: return createItem(); - case FormatterClass::NoFormat: + case AWAbstractFormatter::FormatterClass::NoFormat: return createItem(); } } diff --git a/sources/awesome-widget/plugin/awformatterhelper.h b/sources/awesome-widget/plugin/awformatterhelper.h index 5b9f349..5ba16d6 100644 --- a/sources/awesome-widget/plugin/awformatterhelper.h +++ b/sources/awesome-widget/plugin/awformatterhelper.h @@ -21,6 +21,8 @@ #include "abstractextitemaggregator.h" +#include "awabstractformatter.h" + class AWAbstractFormatter; @@ -29,8 +31,6 @@ class AWFormatterHelper : public AbstractExtItemAggregator Q_OBJECT public: - enum class FormatterClass { DateTime, Float, Script, NoFormat }; - explicit AWFormatterHelper(QWidget *parent = nullptr); virtual ~AWFormatterHelper(); QString convert(const QVariant &value, const QString &name) const; @@ -46,12 +46,12 @@ public slots: private: // methods - AWFormatterHelper::FormatterClass + AWAbstractFormatter::FormatterClass defineFormatterClass(const QString stringType) const; void initFormatters(); void initKeys(); void installDirectories(); - QPair + QPair readMetadata(const QString filePath) const; // parent methods void doCreateItem(); diff --git a/sources/awesomewidgets/awabstractformatter.cpp b/sources/awesomewidgets/awabstractformatter.cpp index 18e39ea..123a245 100644 --- a/sources/awesomewidgets/awabstractformatter.cpp +++ b/sources/awesomewidgets/awabstractformatter.cpp @@ -46,20 +46,58 @@ void AWAbstractFormatter::copyDefaults(AbstractExtItem *_other) const QString AWAbstractFormatter::uniq() const { - return QString("%1(%2)").arg(name()).arg(m_type); + return QString("%1(%2)").arg(name()).arg(strType()); } -QString AWAbstractFormatter::type() const +QString AWAbstractFormatter::strType() const +{ + QString value; + switch (m_type) { + case FormatterClass::DateTime: + value = QString("DateTime"); + break; + case FormatterClass::Float: + value = QString("Float"); + break; + case FormatterClass::Script: + value = QString("Script"); + break; + case FormatterClass::NoFormat: + value = QString("NoFormat"); + break; + } + + return value; +} + + +AWAbstractFormatter::FormatterClass AWAbstractFormatter::type() const { return m_type; } -void AWAbstractFormatter::setType(const QString _type) +void AWAbstractFormatter::setStrType(const QString _type) { qCDebug(LOG_LIB) << "Type" << _type; + if (_type == QString("DateTime")) + m_type = FormatterClass::DateTime; + else if (_type == QString("Float")) + m_type = FormatterClass::Float; + else if (_type == QString("Script")) + m_type = FormatterClass::Script; + else + m_type = FormatterClass::NoFormat; +} + + +void AWAbstractFormatter::setType( + const AWAbstractFormatter::FormatterClass _type) +{ + qCDebug(LOG_LIB) << "Type" << static_cast(_type); + m_type = _type; } @@ -71,7 +109,7 @@ void AWAbstractFormatter::readConfiguration() QSettings settings(fileName(), QSettings::IniFormat); settings.beginGroup(QString("Desktop Entry")); - setType(settings.value(QString("X-AW-Type"), m_type).toString()); + setStrType(settings.value(QString("X-AW-Type"), strType()).toString()); settings.endGroup(); } @@ -84,7 +122,7 @@ void AWAbstractFormatter::writeConfiguration() const qCInfo(LOG_LIB) << "Configuration file" << settings.fileName(); settings.beginGroup(QString("Desktop Entry")); - settings.setValue(QString("X-AW-Type"), m_type); + settings.setValue(QString("X-AW-Type"), strType()); settings.endGroup(); settings.sync(); diff --git a/sources/awesomewidgets/awabstractformatter.h b/sources/awesomewidgets/awabstractformatter.h index 3fcd8e4..9bb5315 100644 --- a/sources/awesomewidgets/awabstractformatter.h +++ b/sources/awesomewidgets/awabstractformatter.h @@ -24,9 +24,12 @@ class AWAbstractFormatter : public AbstractExtItem { Q_OBJECT - Q_PROPERTY(QString type READ type WRITE setType) + Q_PROPERTY(FormatterClass type READ type WRITE setType) + Q_PROPERTY(QString strType READ strType WRITE setStrType) public: + enum class FormatterClass { DateTime, Float, Script, NoFormat }; + explicit AWAbstractFormatter(QWidget *parent, const QString filePath = QString()); virtual ~AWAbstractFormatter(); @@ -34,8 +37,10 @@ public: void copyDefaults(AbstractExtItem *_other) const; QString uniq() const; // properties - QString type() const; - void setType(const QString _type = QString("NoFormat")); + QString strType() const; + FormatterClass type() const; + void setStrType(const QString type); + void setType(const FormatterClass _type = FormatterClass::NoFormat); public slots: virtual void readConfiguration(); @@ -44,7 +49,7 @@ public slots: private: // properties - QString m_type = QString("NoFormat"); + FormatterClass m_type = FormatterClass::NoFormat; }; diff --git a/sources/awesomewidgets/awdatetimeformatter.cpp b/sources/awesomewidgets/awdatetimeformatter.cpp index 0a99eb2..dfaa437 100644 --- a/sources/awesomewidgets/awdatetimeformatter.cpp +++ b/sources/awesomewidgets/awdatetimeformatter.cpp @@ -115,7 +115,7 @@ int AWDateTimeFormatter::showConfiguration(const QVariant args) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); - setType(ui->label_typeValue->text()); + setStrType(ui->label_typeValue->text()); setFormat(ui->lineEdit_format->text()); writeConfiguration(); diff --git a/sources/awesomewidgets/awfloatformatter.cpp b/sources/awesomewidgets/awfloatformatter.cpp index 088bd51..53a0f5a 100644 --- a/sources/awesomewidgets/awfloatformatter.cpp +++ b/sources/awesomewidgets/awfloatformatter.cpp @@ -212,7 +212,7 @@ int AWFloatFormatter::showConfiguration(const QVariant args) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); - setType(ui->label_typeValue->text()); + setStrType(ui->label_typeValue->text()); setFormat(ui->comboBox_format->currentText().at(0).toLatin1()); setPrecision(ui->spinBox_precision->value()); setCount(ui->spinBox_width->value()); diff --git a/sources/awesomewidgets/awnoformatter.cpp b/sources/awesomewidgets/awnoformatter.cpp index 1a6aa81..e1e3ddb 100644 --- a/sources/awesomewidgets/awnoformatter.cpp +++ b/sources/awesomewidgets/awnoformatter.cpp @@ -79,7 +79,7 @@ int AWNoFormatter::showConfiguration(const QVariant args) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); - setType(ui->label_typeValue->text()); + setStrType(ui->label_typeValue->text()); writeConfiguration(); return ret; diff --git a/sources/awesomewidgets/awscriptformatter.cpp b/sources/awesomewidgets/awscriptformatter.cpp index 3bc23cd..e06ef23 100644 --- a/sources/awesomewidgets/awscriptformatter.cpp +++ b/sources/awesomewidgets/awscriptformatter.cpp @@ -174,7 +174,7 @@ int AWScriptFormatter::showConfiguration(const QVariant args) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); - setType(ui->label_typeValue->text()); + setStrType(ui->label_typeValue->text()); setAppendCode(ui->checkBox_appendCode->checkState() == Qt::Checked); setHasReturn(ui->checkBox_hasReturn->checkState() == Qt::Checked); setCode(ui->textEdit_code->toPlainText()); diff --git a/sources/awesomewidgets/extscript.cpp b/sources/awesomewidgets/extscript.cpp index c7823f2..fd5179a 100644 --- a/sources/awesomewidgets/extscript.cpp +++ b/sources/awesomewidgets/extscript.cpp @@ -168,13 +168,13 @@ void ExtScript::setStrRedirect(const QString _redirect) qCDebug(LOG_LIB) << "Redirect" << _redirect; if (_redirect == QString("stdout2sdterr")) - m_redirect = Redirect::stdout2stderr; + setRedirect(Redirect::stdout2stderr); else if (_redirect == QString("stderr2sdtout")) - m_redirect = Redirect::stderr2stdout; + setRedirect(Redirect::stderr2stdout); else if (_redirect == QString("swap")) - m_redirect = Redirect::swap; + setRedirect(Redirect::swap); else - m_redirect = Redirect::nothing; + setRedirect(Redirect::nothing); } @@ -317,7 +317,7 @@ int ExtScript::showConfiguration(const QVariant args) setExecutable(ui->lineEdit_command->text()); setPrefix(ui->lineEdit_prefix->text()); setActive(ui->checkBox_active->checkState() == Qt::Checked); - setStrRedirect(ui->comboBox_redirect->currentText()); + setRedirect(static_cast(ui->comboBox_redirect->currentIndex())); setInterval(ui->spinBox_interval->value()); // filters updateFilter(QString("color"), diff --git a/sources/awesomewidgets/extscript.h b/sources/awesomewidgets/extscript.h index a5fb1b7..1dca548 100644 --- a/sources/awesomewidgets/extscript.h +++ b/sources/awesomewidgets/extscript.h @@ -37,7 +37,7 @@ class ExtScript : public AbstractExtItem Q_PROPERTY(Redirect redirect READ redirect WRITE setRedirect) public: - enum class Redirect { stdout2stderr, nothing, stderr2stdout, swap }; + enum class Redirect { stdout2stderr = 0, nothing = 1, stderr2stdout = 2, swap = 3 }; explicit ExtScript(QWidget *parent, const QString filePath = QString()); virtual ~ExtScript(); diff --git a/sources/awesomewidgets/graphicalitem.cpp b/sources/awesomewidgets/graphicalitem.cpp index 52354ef..313f022 100644 --- a/sources/awesomewidgets/graphicalitem.cpp +++ b/sources/awesomewidgets/graphicalitem.cpp @@ -43,8 +43,6 @@ GraphicalItem::GraphicalItem(QWidget *parent, const QString filePath) ui->setupUi(this); translate(); - initScene(); - connect(ui->checkBox_custom, SIGNAL(stateChanged(int)), this, SLOT(changeValue(int))); connect(ui->comboBox_type, SIGNAL(currentIndexChanged(int)), this, @@ -141,6 +139,31 @@ QString GraphicalItem::image(const QVariant &value) } +void GraphicalItem::initScene() +{ + // cleanup + delete m_helper; + delete m_scene; + + // init scene + m_scene = new QGraphicsScene(); + m_scene->setBackgroundBrush(QBrush(Qt::NoBrush)); + // init view + m_view = new QGraphicsView(m_scene); + m_view->setStyleSheet(QString("background: transparent")); + m_view->setContentsMargins(0, 0, 0, 0); + m_view->setFrameShape(QFrame::NoFrame); + m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + m_view->resize(m_width + 5, m_height + 5); + + // init helper + m_helper = new GraphicalItemHelper(this, m_scene); + m_helper->setParameters(m_activeColor, m_inactiveColor, m_width, m_height, + m_count); +} + + QString GraphicalItem::bar() const { return m_bar; @@ -433,6 +456,7 @@ void GraphicalItem::readConfiguration() settings.endGroup(); bumpApi(AWGIAPI); + initScene(); } @@ -489,8 +513,8 @@ int GraphicalItem::showConfiguration(const QVariant args) setMinValue(ui->doubleSpinBox_min->value()); setActiveColor(ui->lineEdit_activeColor->text()); setInactiveColor(ui->lineEdit_inactiveColor->text()); - setStrType(ui->comboBox_type->currentText()); - setStrDirection(ui->comboBox_direction->currentText()); + setType(static_cast(ui->comboBox_type->currentIndex())); + setDirection(static_cast(ui->comboBox_direction->currentIndex())); setItemHeight(ui->spinBox_height->value()); setItemWidth(ui->spinBox_width->value()); @@ -590,27 +614,6 @@ void GraphicalItem::changeValue(const int state) } -void GraphicalItem::initScene() -{ - // init scene - m_scene = new QGraphicsScene(); - m_scene->setBackgroundBrush(QBrush(Qt::NoBrush)); - // init view - m_view = new QGraphicsView(m_scene); - m_view->setStyleSheet(QString("background: transparent")); - m_view->setContentsMargins(0, 0, 0, 0); - m_view->setFrameShape(QFrame::NoFrame); - m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - m_view->resize(m_width + 5, m_height + 5); - - // init helper - m_helper = new GraphicalItemHelper(this, m_scene); - m_helper->setParameters(m_activeColor, m_inactiveColor, m_width, m_height, - m_count); -} - - void GraphicalItem::translate() { ui->label_name->setText(i18n("Name")); diff --git a/sources/awesomewidgets/graphicalitem.h b/sources/awesomewidgets/graphicalitem.h index b5a6c30..b7bc656 100644 --- a/sources/awesomewidgets/graphicalitem.h +++ b/sources/awesomewidgets/graphicalitem.h @@ -50,12 +50,13 @@ class GraphicalItem : public AbstractExtItem public: enum class Direction { LeftToRight = 0, RightToLeft = 1 }; - enum class Type { Horizontal, Vertical, Circle, Graph, Bars }; + enum class Type { Horizontal = 0, Vertical = 1, Circle = 2, Graph = 3, Bars = 4 }; explicit GraphicalItem(QWidget *parent, const QString filePath = QString()); virtual ~GraphicalItem(); GraphicalItem *copy(const QString _fileName, const int _number); QString image(const QVariant &value); + void initScene(); // get methods QString bar() const; QString activeColor() const; @@ -105,7 +106,6 @@ private: QGraphicsScene *m_scene = nullptr; QGraphicsView *m_view = nullptr; Ui::GraphicalItem *ui = nullptr; - void initScene(); void translate(); // properties QString m_bar = QString("cpu"); diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index fd94701..45f5831 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -21,7 +21,7 @@ set(LIBRARY_TEST_SET ${SUBPROJECT}-awtest ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Q ## modules set(TEST_MODULES abstractextitem extquotes extscript extupgrade extweather - datetimeformatter floatformatter noformatter scriptformatter) + abstractformatter datetimeformatter floatformatter noformatter scriptformatter) foreach (TEST_MODULE ${TEST_MODULES}) set(${TEST_MODULE}_HEADERS test${TEST_MODULE}.h) set(${TEST_MODULE}_SOURCES test${TEST_MODULE}.cpp) diff --git a/sources/test/testabstractformatter.cpp b/sources/test/testabstractformatter.cpp new file mode 100644 index 0000000..a8c6905 --- /dev/null +++ b/sources/test/testabstractformatter.cpp @@ -0,0 +1,67 @@ +/*************************************************************************** + * 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 "testabstractformatter.h" + +#include + +#include "awnoformatter.h" +#include "awtestlibrary.h" + + +void TestAbstractFormatter::initTestCase() +{ + formatter = new AWNoFormatter(nullptr); +} + + +void TestAbstractFormatter::cleanupTestCase() +{ + delete formatter; +} + + +void TestAbstractFormatter::test_values() +{ +} + + +void TestAbstractFormatter::test_type() +{ + QString type = AWTestLibrary::randomString(); + QEXPECT_FAIL("", "Will fail because of invalid format", Continue); + formatter->setStrType(type); + QCOMPARE(formatter->strType(), type); + + formatter->setStrType(QString("NoFormat")); + QCOMPARE(formatter->strType(), QString("NoFormat")); +} + + +void TestAbstractFormatter::test_copy() +{ + AWNoFormatter *newFormatter = formatter->copy(QString("/dev/null"), 1); + + QCOMPARE(newFormatter->type(), formatter->type()); + QCOMPARE(newFormatter->name(), formatter->name()); + + delete newFormatter; +} + + +QTEST_MAIN(TestAbstractFormatter); diff --git a/sources/test/testabstractformatter.h b/sources/test/testabstractformatter.h new file mode 100644 index 0000000..ec94f93 --- /dev/null +++ b/sources/test/testabstractformatter.h @@ -0,0 +1,45 @@ +/*************************************************************************** + * 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 TESTABSTRACTFORMATTER_H +#define TESTABSTRACTFORMATTER_H + +#include + + +class AWNoFormatter; + +class TestAbstractFormatter : public QObject +{ + Q_OBJECT + +private slots: + // initialization + void initTestCase(); + void cleanupTestCase(); + // test + void test_values(); + void test_type(); + void test_copy(); + +private: + AWNoFormatter *formatter = nullptr; +}; + + +#endif /* TESTABSTRACTFORMATTER_H */ diff --git a/sources/test/testdatetimeformatter.cpp b/sources/test/testdatetimeformatter.cpp index c785338..41a1554 100644 --- a/sources/test/testdatetimeformatter.cpp +++ b/sources/test/testdatetimeformatter.cpp @@ -27,7 +27,8 @@ void TestAWDateTimeFormatter::initTestCase() { - format = AWTestLibrary::randomSelect(QString(TIME_KEYS).split(QChar(','))).join(QChar(' ')); + format = AWTestLibrary::randomSelect(QString(TIME_KEYS).split(QChar(','))) + .join(QChar(' ')); formatter = new AWDateTimeFormatter(nullptr); formatter->setFormat(format);