add list formatter

This commit is contained in:
Evgenii Alekseev 2016-07-03 04:10:04 +03:00
parent 3497922928
commit 17a0c61b4a
9 changed files with 597 additions and 3 deletions

View File

@ -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<AWScriptFormatter>();
case AWAbstractFormatter::FormatterClass::NoFormat:
return createItem<AWNoFormatter>();
case AWAbstractFormatter::FormatterClass::List:
return createItem<AWListFormatter>();
}
}

View File

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

View File

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

View File

@ -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 <KI18n/KLocalizedString>
#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<QWidget *>(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"));
}

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

View File

@ -0,0 +1,220 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AWListFormatter</class>
<widget class="QDialog" name="AWListFormatter">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>420</width>
<height>225</height>
</rect>
</property>
<property name="windowTitle">
<string>Configuration</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="layout_name">
<item>
<widget class="QLabel" name="label_name">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Name</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_name"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="layout_comment">
<item>
<widget class="QLabel" name="label_comment">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Comment</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_comment"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="layout_type">
<item>
<widget class="QLabel" name="label_type">
<property name="text">
<string>Type</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_typeValue">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="layout_filter">
<item>
<widget class="QLabel" name="label_filter">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Filter</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_filter"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="layout_separator">
<item>
<widget class="QLabel" name="label_separator">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Separator</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_separator"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="layout_sorted">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="checkBox_sorted">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Sort</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>AWListFormatter</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>AWListFormatter</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

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

View File

@ -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 <QtTest>
#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);

View File

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