diff --git a/sources/awesome-widget/plugin/awformatterhelper.cpp b/sources/awesome-widget/plugin/awformatterhelper.cpp index 2dc9598..45ac3e0 100644 --- a/sources/awesome-widget/plugin/awformatterhelper.cpp +++ b/sources/awesome-widget/plugin/awformatterhelper.cpp @@ -26,6 +26,7 @@ #include "awdatetimeformatter.h" #include "awdebug.h" #include "awfloatformatter.h" +#include "awlistformatter.h" #include "awnoformatter.h" #include "awscriptformatter.h" @@ -160,6 +161,8 @@ AWFormatterHelper::defineFormatterClass(const QString stringType) const ; else if (stringType == QString("Script")) formatter = AWAbstractFormatter::FormatterClass::Script; + else if (stringType == QString("List")) + formatter = AWAbstractFormatter::FormatterClass::List; else qCWarning(LOG_AW) << "Unknown formatter" << stringType; @@ -203,6 +206,9 @@ void AWFormatterHelper::initFormatters() case AWAbstractFormatter::FormatterClass::NoFormat: m_formattersClasses[name] = new AWNoFormatter(this, filePath); break; + case AWAbstractFormatter::FormatterClass::List: + m_formattersClasses[name] = new AWListFormatter(this, filePath); + break; } } } @@ -280,7 +286,8 @@ void AWFormatterHelper::doCreateItem() { QStringList selection = QStringList() << QString("NoFormat") << QString("DateTime") - << QString("Float") << QString("Script"); + << QString("Float") << QString("Script") + << QString("List"); bool ok; QString select = QInputDialog::getItem( this, i18n("Select type"), i18n("Type:"), selection, 0, false, &ok); @@ -301,6 +308,8 @@ void AWFormatterHelper::doCreateItem() return createItem(); case AWAbstractFormatter::FormatterClass::NoFormat: return createItem(); + case AWAbstractFormatter::FormatterClass::List: + return createItem(); } } diff --git a/sources/awesomewidgets/awabstractformatter.cpp b/sources/awesomewidgets/awabstractformatter.cpp index 123a245..4d3c58f 100644 --- a/sources/awesomewidgets/awabstractformatter.cpp +++ b/sources/awesomewidgets/awabstractformatter.cpp @@ -66,6 +66,9 @@ QString AWAbstractFormatter::strType() const case FormatterClass::NoFormat: value = QString("NoFormat"); break; + case FormatterClass::List: + value = QString("List"); + break; } return value; @@ -88,6 +91,8 @@ void AWAbstractFormatter::setStrType(const QString _type) m_type = FormatterClass::Float; else if (_type == QString("Script")) m_type = FormatterClass::Script; + else if (_type == QString("List")) + m_type = FormatterClass::List; else m_type = FormatterClass::NoFormat; } diff --git a/sources/awesomewidgets/awabstractformatter.h b/sources/awesomewidgets/awabstractformatter.h index 9bb5315..c62c34c 100644 --- a/sources/awesomewidgets/awabstractformatter.h +++ b/sources/awesomewidgets/awabstractformatter.h @@ -28,7 +28,7 @@ class AWAbstractFormatter : public AbstractExtItem Q_PROPERTY(QString strType READ strType WRITE setStrType) public: - enum class FormatterClass { DateTime, Float, Script, NoFormat }; + enum class FormatterClass { DateTime, Float, Script, NoFormat, List }; explicit AWAbstractFormatter(QWidget *parent, const QString filePath = QString()); diff --git a/sources/awesomewidgets/awlistformatter.cpp b/sources/awesomewidgets/awlistformatter.cpp new file mode 100644 index 0000000..c513598 --- /dev/null +++ b/sources/awesomewidgets/awlistformatter.cpp @@ -0,0 +1,155 @@ +/*************************************************************************** + * 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 "awlistformatter.h" +#include "ui_awlistformatter.h" + +#include + +#include "awdebug.h" + + +AWListFormatter::AWListFormatter(QWidget *parent, const QString filePath) + : AWAbstractFormatter(parent, filePath) + , ui(new Ui::AWListFormatter) +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; + + if (!filePath.isEmpty()) + readConfiguration(); + ui->setupUi(this); + translate(); +} + + +AWListFormatter::~AWListFormatter() +{ + qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; + + delete ui; +} + + +QString AWListFormatter::convert(const QVariant &_value) const +{ + qCDebug(LOG_LIB) << "Convert value" << _value; + + QStringList output = _value.toStringList(); + if (isSorted()) + output.sort(); + + return output.filter(m_regex).join(separator()); +} + + +AWListFormatter *AWListFormatter::copy(const QString _fileName, + const int _number) +{ + qCDebug(LOG_LIB) << "File" << _fileName << "with number" << _number; + + AWListFormatter *item + = new AWListFormatter(static_cast(parent()), _fileName); + AWAbstractFormatter::copyDefaults(item); + item->setFilter(filter()); + item->setSeparator(separator()); + item->setSorted(isSorted()); + item->setNumber(_number); + + return item; +} + + +QString AWListFormatter::filter() const +{ + return m_filter; +} + + +bool AWListFormatter::isSorted() const +{ + return m_sorted; +} + + +QString AWListFormatter::separator() const +{ + return m_separator; +} + + +void AWListFormatter::setFilter(const QString _filter) +{ + qCDebug(LOG_LIB) << "Filter" << _filter; + + m_filter = _filter; + m_regex = QRegExp(m_filter); +} + + +void AWListFormatter::setSeparator(const QString _separator) +{ + qCDebug(LOG_LIB) << "Separtor" << _separator; + + m_separator = _separator; +} + + +void AWListFormatter::setSorted(const bool _sorted) +{ + qCDebug(LOG_LIB) << "Sorting" << _sorted; + + m_sorted = _sorted; +} + + +int AWListFormatter::showConfiguration(const QVariant args) +{ + Q_UNUSED(args) + + ui->lineEdit_name->setText(name()); + ui->lineEdit_comment->setText(comment()); + ui->label_typeValue->setText(QString("NoFormat")); + ui->lineEdit_filter->setText(filter()); + ui->lineEdit_separator->setText(separator()); + ui->checkBox_sorted->setCheckState(isSorted() ? Qt::Checked + : Qt::Unchecked); + + int ret = exec(); + if (ret != 1) + return ret; + setName(ui->lineEdit_name->text()); + setComment(ui->lineEdit_comment->text()); + setStrType(ui->label_typeValue->text()); + setFilter(ui->lineEdit_filter->text()); + setSeparator(ui->lineEdit_separator->text()); + setSorted(ui->checkBox_sorted->checkState() == Qt::Checked); + + writeConfiguration(); + return ret; +} + + +void AWListFormatter::translate() +{ + ui->label_name->setText(i18n("Name")); + ui->label_comment->setText(i18n("Comment")); + ui->label_type->setText(i18n("Type")); + ui->label_filter->setText(i18n("Filter")); + ui->label_separator->setText(i18n("Separator")); + ui->checkBox_sorted->setText(i18n("Sort")); +} diff --git a/sources/awesomewidgets/awlistformatter.h b/sources/awesomewidgets/awlistformatter.h new file mode 100644 index 0000000..ebc384d --- /dev/null +++ b/sources/awesomewidgets/awlistformatter.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 AWLISTFORMATTER_H +#define AWLISTFORMATTER_H + +#include "awabstractformatter.h" + + +namespace Ui +{ +class AWListFormatter; +} + +class AWListFormatter : public AWAbstractFormatter +{ + Q_OBJECT + Q_PROPERTY(QString filter READ filter WRITE setFilter) + Q_PROPERTY(QString separator READ separator WRITE setSeparator) + Q_PROPERTY(bool sorted READ isSorted WRITE setSorted) + +public: + explicit AWListFormatter(QWidget *parent, + const QString filePath = QString()); + virtual ~AWListFormatter(); + QString convert(const QVariant &_value) const; + AWListFormatter *copy(const QString _fileName, const int _number); + // properties + QString filter() const; + bool isSorted() const; + QString separator() const; + void setFilter(const QString _filter); + void setSeparator(const QString _separator); + void setSorted(const bool _sorted); + +public slots: + int showConfiguration(const QVariant args = QVariant()); + +private: + Ui::AWListFormatter *ui = nullptr; + void translate(); + // properties + QString m_filter = QString(); + QString m_separator = QString(); + bool m_sorted = false; + QRegExp m_regex; +}; + + +#endif /* AWLISTFORMATTER_H */ diff --git a/sources/awesomewidgets/awlistformatter.ui b/sources/awesomewidgets/awlistformatter.ui new file mode 100644 index 0000000..bdebe21 --- /dev/null +++ b/sources/awesomewidgets/awlistformatter.ui @@ -0,0 +1,220 @@ + + + AWListFormatter + + + + 0 + 0 + 420 + 225 + + + + Configuration + + + + + + + + + 0 + 0 + + + + Name + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + 0 + 0 + + + + Comment + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + Type + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + + + + + 0 + 0 + + + + Filter + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + 0 + 0 + + + + Separator + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 0 + 0 + + + + Sort + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + AWListFormatter + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + AWListFormatter + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index 0197ad9..ed1dd2c 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -27,7 +27,7 @@ set(LIBRARY_TEST_SET ${SUBPROJECT}-awtest ${PROJECT_LIBRARY} ${PROJECT_MONITORSO ## modules set(TEST_MODULES abstractextitem extquotes extscript extupgrade extweather - abstractformatter datetimeformatter floatformatter noformatter scriptformatter + abstractformatter datetimeformatter floatformatter listformatter noformatter scriptformatter extitemaggregator batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource playersource processessource awconfighelper awkeycache awkeys awpatternfunctions awupdatehelper diff --git a/sources/test/testlistformatter.cpp b/sources/test/testlistformatter.cpp new file mode 100644 index 0000000..9522dae --- /dev/null +++ b/sources/test/testlistformatter.cpp @@ -0,0 +1,93 @@ +/*************************************************************************** + * 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 "testlistformatter.h" + +#include + +#include "awlistformatter.h" +#include "awtestlibrary.h" + + +void TestAWListFormatter::initTestCase() +{ + separator = AWTestLibrary::randomString(10); + + formatter = new AWListFormatter(nullptr); + formatter->setSeparator(separator); +} + + +void TestAWListFormatter::cleanupTestCase() +{ + delete formatter; +} + + +void TestAWListFormatter::test_values() +{ + QCOMPARE(formatter->filter(), QString()); + QCOMPARE(formatter->separator(), separator); + QCOMPARE(formatter->isSorted(), false); +} + + +void TestAWListFormatter::test_conversion() +{ + QStringList value = AWTestLibrary::randomStringList(); + QCOMPARE(formatter->convert(value), value.join(separator)); +} + + +void TestAWListFormatter::test_sorted() +{ + formatter->setSorted(true); + QStringList value = AWTestLibrary::randomStringList(); + QString received = formatter->convert(value); + + value.sort(); + QCOMPARE(received, value.join(separator)); +} + + +void TestAWListFormatter::test_filter() +{ + QStringList value = AWTestLibrary::randomStringList(); + QStringList filters = AWTestLibrary::randomSelect(value); + value.sort(); + formatter->setFilter(QString("(^%1$)").arg(filters.join(QString("$|^")))); + + QCOMPARE(formatter->convert(value).split(separator).count(), + filters.count()); +} + + +void TestAWListFormatter::test_copy() +{ + AWListFormatter *newFormatter = formatter->copy(QString("/dev/null"), 1); + + QCOMPARE(newFormatter->number(), 1); + QCOMPARE(newFormatter->filter(), formatter->filter()); + QCOMPARE(newFormatter->separator(), formatter->separator()); + QCOMPARE(newFormatter->isSorted(), formatter->isSorted()); + + delete newFormatter; +} + + +QTEST_MAIN(TestAWListFormatter); diff --git a/sources/test/testlistformatter.h b/sources/test/testlistformatter.h new file mode 100644 index 0000000..6f4e16e --- /dev/null +++ b/sources/test/testlistformatter.h @@ -0,0 +1,48 @@ +/*************************************************************************** + * 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 TESTLISTFORMATTER_H +#define TESTLISTFORMATTER_H + +#include + + +class AWListFormatter; + +class TestAWListFormatter : public QObject +{ + Q_OBJECT + +private slots: + // initialization + void initTestCase(); + void cleanupTestCase(); + // test + void test_values(); + void test_conversion(); + void test_sorted(); + void test_filter(); + void test_copy(); + +private: + AWListFormatter *formatter = nullptr; + QString separator; +}; + + +#endif /* TESTLISTFORMATTER_H */