From 52b1255d3f18c272d90ff8024bf1e0ae4b4be599 Mon Sep 17 00:00:00 2001 From: arcan1s Date: Mon, 14 Mar 2016 12:04:38 +0300 Subject: [PATCH] move graph data store to graphical item helper It is required by custom graphs. X-AW-Count parameter (which is only recognized by Graph item type) has been introduced --- sources/awesome-widget/plugin/awkeys.cpp | 20 +++---- sources/awesomewidgets/graphicalitem.cpp | 53 ++++++++++++++++--- sources/awesomewidgets/graphicalitem.h | 5 ++ sources/awesomewidgets/graphicalitem.ui | 28 +++++++++- .../awesomewidgets/graphicalitemhelper.cpp | 32 ++++++++--- sources/awesomewidgets/graphicalitemhelper.h | 8 ++- 6 files changed, 114 insertions(+), 32 deletions(-) diff --git a/sources/awesome-widget/plugin/awkeys.cpp b/sources/awesome-widget/plugin/awkeys.cpp index 5a1438f..71b4e6e 100644 --- a/sources/awesome-widget/plugin/awkeys.cpp +++ b/sources/awesome-widget/plugin/awkeys.cpp @@ -312,20 +312,14 @@ QString AWKeys::parsePattern(QString pattern) const // bars for (auto bar : m_foundBars) { GraphicalItem *item = keyOperator->giByKey(bar); - if (item->type() == GraphicalItem::Graph) { + if (item->isCustom()) + pattern.replace( + QString("$%1").arg(bar), + item->image(AWPatternFunctions::expandLambdas( + item->bar(), aggregator, values, item->usedKeys()))); + else pattern.replace(QString("$%1").arg(bar), - item->image(QVariant::fromValue>( - dataAggregator->getData(item->bar())))); - } else { - if (item->isCustom()) - pattern.replace( - QString("$%1").arg(bar), - item->image(AWPatternFunctions::expandLambdas( - item->bar(), aggregator, values, item->usedKeys()))); - else - pattern.replace(QString("$%1").arg(bar), - item->image(values[item->bar()])); - } + item->image(values[item->bar()])); } // prepare strings diff --git a/sources/awesomewidgets/graphicalitem.cpp b/sources/awesomewidgets/graphicalitem.cpp index 1d9fba3..4c54294 100644 --- a/sources/awesomewidgets/graphicalitem.cpp +++ b/sources/awesomewidgets/graphicalitem.cpp @@ -47,6 +47,8 @@ GraphicalItem::GraphicalItem(QWidget *parent, const QString desktopName, connect(ui->checkBox_custom, SIGNAL(stateChanged(int)), this, SLOT(changeValue(int))); + connect(ui->comboBox_type, SIGNAL(currentIndexChanged(int)), this, + SLOT(changeCountState(int))); connect(ui->pushButton_activeColor, SIGNAL(clicked()), this, SLOT(changeColor())); connect(ui->pushButton_inactiveColor, SIGNAL(clicked()), this, @@ -73,6 +75,7 @@ GraphicalItem *GraphicalItem::copy(const QString _fileName, const int _number) copyDefaults(item); item->setActiveColor(activeColor()); item->setBar(m_bar); + item->setCount(m_count); item->setCustom(m_custom); item->setDirection(m_direction); item->setHeight(m_height); @@ -93,29 +96,28 @@ QString GraphicalItem::image(const QVariant &value) m_scene->clear(); int scale[2] = {1, 1}; + float converted + = m_helper->getPercents(value.toFloat(), m_minValue, m_maxValue); // paint switch (m_type) { case Vertical: - m_helper->paintVertical( - m_helper->getPercents(value.toFloat(), m_minValue, m_maxValue)); + m_helper->paintVertical(converted); // scale scale[1] = -2 * static_cast(m_direction) + 1; break; case Circle: - m_helper->paintCircle( - m_helper->getPercents(value.toFloat(), m_minValue, m_maxValue)); + m_helper->paintCircle(converted); // scale scale[0] = -2 * static_cast(m_direction) + 1; break; case Graph: - m_helper->paintGraph(value.value>()); + m_helper->paintGraph(converted); // direction option is not recognized by this GI type break; case Horizontal: default: - m_helper->paintHorizontal( - m_helper->getPercents(value.toFloat(), m_minValue, m_maxValue)); + m_helper->paintHorizontal(converted); // scale scale[0] = -2 * static_cast(m_direction) + 1; break; @@ -152,6 +154,12 @@ QString GraphicalItem::inactiveColor() const } +int GraphicalItem::count() const +{ + return m_count; +} + + bool GraphicalItem::isCustom() const { return m_custom; @@ -262,6 +270,16 @@ void GraphicalItem::setActiveColor(const QString _color) } +void GraphicalItem::setCount(const int _count) +{ + qCDebug(LOG_LIB) << "Count" << _count; + if (_count <= 1) + return; + + m_count = _count; +} + + void GraphicalItem::setCustom(const bool _custom) { qCDebug(LOG_LIB) << "Use custom tag" << _custom; @@ -379,6 +397,7 @@ void GraphicalItem::readConfiguration() QSettings::IniFormat); settings.beginGroup(QString("Desktop Entry")); + setCount(settings.value(QString("X-AW-Count"), m_count).toInt()); setCustom(settings.value(QString("X-AW-Custom"), m_custom).toBool()); setBar(settings.value(QString("X-AW-Value"), m_bar).toString()); setMaxValue(settings.value(QString("X-AW-Max"), m_maxValue).toFloat()); @@ -435,6 +454,7 @@ int GraphicalItem::showConfiguration(const QVariant args) } ui->doubleSpinBox_max->setValue(m_maxValue); ui->doubleSpinBox_min->setValue(m_minValue); + ui->spinBox_count->setValue(m_count); ui->pushButton_activeColor->setText(activeColor()); ui->pushButton_inactiveColor->setText(inactiveColor()); ui->comboBox_type->setCurrentIndex(static_cast(m_type)); @@ -442,12 +462,17 @@ int GraphicalItem::showConfiguration(const QVariant args) ui->spinBox_height->setValue(m_height); ui->spinBox_width->setValue(m_width); + // update UI + changeCountState(ui->comboBox_type->currentIndex()); + changeValue(ui->checkBox_custom->checkState()); + int ret = exec(); if (ret != 1) return ret; setName(ui->lineEdit_name->text()); setComment(ui->lineEdit_comment->text()); setApiVersion(AWGIAPI); + setCount(ui->spinBox_count->value()); setCustom(ui->checkBox_custom->isChecked()); setBar(m_custom ? ui->lineEdit_customValue->text() : ui->comboBox_value->currentText()); @@ -476,6 +501,7 @@ void GraphicalItem::writeConfiguration() const settings.beginGroup(QString("Desktop Entry")); settings.setValue(QString("X-AW-Value"), m_bar); + settings.setValue(QString("X-AW-Count"), m_count); settings.setValue(QString("X-AW-Custom"), m_custom); settings.setValue(QString("X-AW-Max"), m_maxValue); settings.setValue(QString("X-AW-Min"), m_minValue); @@ -512,6 +538,15 @@ void GraphicalItem::changeColor() } +void GraphicalItem::changeCountState(const int state) +{ + qCDebug(LOG_LIB) << "Current state is" << state; + + // 3 is magic number. Actually 3 is Graph mode + ui->widget_count->setHidden(state != 3); +} + + void GraphicalItem::changeValue(const int state) { qCDebug(LOG_LIB) << "Current state is" << state; @@ -540,7 +575,8 @@ void GraphicalItem::initScene() // init helper m_helper = new GraphicalItemHelper(this, m_scene); - m_helper->setParameters(m_activeColor, m_inactiveColor, m_width, m_height); + m_helper->setParameters(m_activeColor, m_inactiveColor, m_width, m_height, + m_count); } @@ -548,6 +584,7 @@ void GraphicalItem::translate() { ui->label_name->setText(i18n("Name")); ui->label_comment->setText(i18n("Comment")); + ui->label_count->setText(i18n("Points count")); ui->checkBox_custom->setText(i18n("Use custom formula")); ui->label_value->setText(i18n("Value")); ui->label_customValue->setText(i18n("Value")); diff --git a/sources/awesomewidgets/graphicalitem.h b/sources/awesomewidgets/graphicalitem.h index c051279..270eb17 100644 --- a/sources/awesomewidgets/graphicalitem.h +++ b/sources/awesomewidgets/graphicalitem.h @@ -37,6 +37,7 @@ class GraphicalItem : public AbstractExtItem Q_OBJECT Q_PROPERTY(QString bar READ bar WRITE setBar) Q_PROPERTY(QString activeColor READ activeColor WRITE setActiveColor) + Q_PROPERTY(int count READ count WRITE setCount) Q_PROPERTY(bool custom READ isCustom WRITE setCustom) Q_PROPERTY(QString inactiveColor READ inactiveColor WRITE setInactiveColor) Q_PROPERTY(Type type READ type WRITE setType) @@ -61,6 +62,7 @@ public: QString bar() const; QString activeColor() const; QString inactiveColor() const; + int count() const; bool isCustom() const; float minValue() const; float maxValue() const; @@ -75,6 +77,7 @@ public: // set methods void setBar(const QString _bar = QString("cpu")); void setActiveColor(const QString _color = QString("0,0,0,130")); + void setCount(const int _count = 100); void setCustom(const bool _custom = false); void setInactiveColor(const QString _color = QString("255,255,255,130")); void setMinValue(const float _value = 0.0); @@ -95,6 +98,7 @@ public slots: private slots: void changeColor(); + void changeCountState(const int state); void changeValue(const int state); private: @@ -106,6 +110,7 @@ private: void translate(); // properties QString m_bar = QString("cpu"); + int m_count = 100; bool m_custom = false; QColor m_activeColor = QColor(0, 0, 0, 130); QColor m_inactiveColor = QColor(255, 255, 255, 130); diff --git a/sources/awesomewidgets/graphicalitem.ui b/sources/awesomewidgets/graphicalitem.ui index a14676e..3000f49 100644 --- a/sources/awesomewidgets/graphicalitem.ui +++ b/sources/awesomewidgets/graphicalitem.ui @@ -7,7 +7,7 @@ 0 0 416 - 461 + 481 @@ -187,6 +187,32 @@ + + + + + + + Points count + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + 2000 + + + 25 + + + + + + diff --git a/sources/awesomewidgets/graphicalitemhelper.cpp b/sources/awesomewidgets/graphicalitemhelper.cpp index fd84efa..b7dadc2 100644 --- a/sources/awesomewidgets/graphicalitemhelper.cpp +++ b/sources/awesomewidgets/graphicalitemhelper.cpp @@ -42,15 +42,17 @@ GraphicalItemHelper::~GraphicalItemHelper() void GraphicalItemHelper::setParameters(const QColor active, const QColor inactive, const int width, - const int height) + const int height, const int count) { qCDebug(LOG_LIB) << "Use active color" << active << ", inactive" << inactive - << ", width" << width << ", height" << height; + << ", width" << width << ", height" << height << ", count" + << count; m_activeColor = active; m_inactiveColor = inactive; m_width = width; m_height = height; + m_count = count; } @@ -77,24 +79,25 @@ void GraphicalItemHelper::paintCircle(const float &percent) } -void GraphicalItemHelper::paintGraph(const QList &value) +void GraphicalItemHelper::paintGraph(const float &value) { qCDebug(LOG_LIB) << "Paint with value" << value; + storeValue(value); QPen pen; pen.setColor(m_activeColor); // default norms float normX - = static_cast(m_width) / static_cast(value.count()); - float normY = static_cast(m_height) / (1.5f * 100.0f); + = static_cast(m_width) / static_cast(m_values.count()); + float normY = static_cast(m_height) / 1.5f; // paint graph - for (int i = 0; i < value.count() - 1; i++) { + for (int i = 0; i < m_values.count() - 1; i++) { // some magic here float x1 = i * normX; - float y1 = -fabs(value.at(i)) * normY + 5.0f; + float y1 = -fabs(m_values.at(i)) * normY + 5.0f; float x2 = (i + 1) * normX; - float y2 = -fabs(value.at(i + 1)) * normY + 5.0f; + float y2 = -fabs(m_values.at(i + 1)) * normY + 5.0f; m_scene->addLine(x1, y1, x2, y2, pen); } } @@ -173,3 +176,16 @@ QColor GraphicalItemHelper::stringToColor(const QString &color) return qColor; } + + +void GraphicalItemHelper::storeValue(const float &value) +{ + qCDebug(LOG_LIB) << "Save value to array" << value; + + if (m_values.count() == 0) + m_values.append(0.0); + else if (m_values.count() > m_count) + m_values.removeFirst(); + + m_values.append(value); +} diff --git a/sources/awesomewidgets/graphicalitemhelper.h b/sources/awesomewidgets/graphicalitemhelper.h index 5b326fd..9e6157a 100644 --- a/sources/awesomewidgets/graphicalitemhelper.h +++ b/sources/awesomewidgets/graphicalitemhelper.h @@ -32,10 +32,10 @@ public: virtual ~GraphicalItemHelper(); // parameters void setParameters(const QColor active, const QColor inactive, - const int width, const int height); + const int width, const int height, const int count); // paint methods void paintCircle(const float &percent); - void paintGraph(const QList &value); + void paintGraph(const float &value); void paintHorizontal(const float &percent); void paintVertical(const float &percent); // additional conversion methods @@ -44,11 +44,15 @@ public: QColor stringToColor(const QString &color); private: + void storeValue(const float &value); QGraphicsScene *m_scene = nullptr; + int m_count; QColor m_activeColor; QColor m_inactiveColor; int m_width; int m_height; + // list of values which will be used to store data for graph type only + QList m_values; };