From 8cae273ffb1a7ec2f36dd7492612a28eb9646bed Mon Sep 17 00:00:00 2001 From: arcan1s Date: Wed, 24 Feb 2016 20:40:55 +0300 Subject: [PATCH] split gi to helper and core Also internal data format has been changed --- sources/awesomewidgets/graphicalitem.cpp | 150 ++++-------------- sources/awesomewidgets/graphicalitem.h | 17 +- .../awesomewidgets/graphicalitemhelper.cpp | 147 +++++++++++++++++ sources/awesomewidgets/graphicalitemhelper.h | 47 ++++++ 4 files changed, 230 insertions(+), 131 deletions(-) create mode 100644 sources/awesomewidgets/graphicalitemhelper.cpp create mode 100644 sources/awesomewidgets/graphicalitemhelper.h diff --git a/sources/awesomewidgets/graphicalitem.cpp b/sources/awesomewidgets/graphicalitem.cpp index 6ceec17..1d2d5b4 100644 --- a/sources/awesomewidgets/graphicalitem.cpp +++ b/sources/awesomewidgets/graphicalitem.cpp @@ -23,14 +23,12 @@ #include #include #include -#include #include #include #include -#include - #include "awdebug.h" +#include "graphicalitemhelper.h" #include "version.h" @@ -72,12 +70,12 @@ GraphicalItem *GraphicalItem::copy(const QString _fileName, const int _number) GraphicalItem *item = new GraphicalItem(static_cast(parent()), _fileName, directories()); copyDefaults(item); - item->setActiveColor(m_activeColor); + item->setActiveColor(activeColor()); item->setBar(m_bar); item->setCustom(m_custom); item->setDirection(m_direction); item->setHeight(m_height); - item->setInactiveColor(m_inactiveColor); + item->setInactiveColor(inactiveColor()); item->setMaxValue(m_maxValue); item->setMinValue(m_minValue); item->setNumber(_number); @@ -98,22 +96,33 @@ QString GraphicalItem::image(const QVariant &value) // paint switch (m_type) { case Vertical: - paintVertical(value.toFloat()); + GraphicalItemHelper::paintVertical( + GraphicalItemHelper::getPercents(value.toFloat(), m_minValue, + m_maxValue), + m_inactiveColor, m_activeColor, m_width, m_height, m_scene); // scale scale[1] = -2 * static_cast(m_direction) + 1; break; case Circle: - paintCircle(value.toFloat()); + GraphicalItemHelper::paintCircle( + GraphicalItemHelper::getPercents(value.toFloat(), m_minValue, + m_maxValue), + m_inactiveColor, m_activeColor, m_width, m_height, m_scene); // scale scale[0] = -2 * static_cast(m_direction) + 1; break; case Graph: - paintGraph(value.value>()); + GraphicalItemHelper::paintGraph(value.value>(), + m_activeColor, m_width, m_height, + m_scene); // direction option is not recognized by this GI type break; case Horizontal: default: - paintHorizontal(value.toFloat()); + GraphicalItemHelper::paintHorizontal( + GraphicalItemHelper::getPercents(value.toFloat(), m_minValue, + m_maxValue), + m_inactiveColor, m_activeColor, m_width, m_height, m_scene); // scale scale[0] = -2 * static_cast(m_direction) + 1; break; @@ -140,13 +149,13 @@ QString GraphicalItem::bar() const QString GraphicalItem::activeColor() const { - return m_activeColor; + return GraphicalItemHelper::colorToString(m_activeColor); } QString GraphicalItem::inactiveColor() const { - return m_inactiveColor; + return GraphicalItemHelper::colorToString(m_inactiveColor); } @@ -256,7 +265,7 @@ void GraphicalItem::setActiveColor(const QString _color) { qCDebug(LOG_LIB) << "Color" << _color; - m_activeColor = _color; + m_activeColor = GraphicalItemHelper::stringToColor(_color); } @@ -272,7 +281,7 @@ void GraphicalItem::setInactiveColor(const QString _color) { qCDebug(LOG_LIB) << "Color" << _color; - m_inactiveColor = _color; + m_inactiveColor = GraphicalItemHelper::stringToColor(_color); } @@ -382,10 +391,10 @@ void GraphicalItem::readConfiguration() setMaxValue(settings.value(QString("X-AW-Max"), m_maxValue).toFloat()); setMinValue(settings.value(QString("X-AW-Min"), m_minValue).toFloat()); setActiveColor( - settings.value(QString("X-AW-ActiveColor"), m_activeColor) + settings.value(QString("X-AW-ActiveColor"), activeColor()) .toString()); setInactiveColor( - settings.value(QString("X-AW-InactiveColor"), m_inactiveColor) + settings.value(QString("X-AW-InactiveColor"), inactiveColor()) .toString()); setStrType(settings.value(QString("X-AW-Type"), strType()).toString()); setStrDirection( @@ -433,8 +442,8 @@ int GraphicalItem::showConfiguration(const QVariant args) } ui->doubleSpinBox_max->setValue(m_maxValue); ui->doubleSpinBox_min->setValue(m_minValue); - ui->pushButton_activeColor->setText(m_activeColor); - ui->pushButton_inactiveColor->setText(m_inactiveColor); + ui->pushButton_activeColor->setText(activeColor()); + ui->pushButton_inactiveColor->setText(inactiveColor()); ui->comboBox_type->setCurrentIndex(static_cast(m_type)); ui->comboBox_direction->setCurrentIndex(static_cast(m_direction)); ui->spinBox_height->setValue(m_height); @@ -477,8 +486,8 @@ void GraphicalItem::writeConfiguration() const settings.setValue(QString("X-AW-Custom"), m_custom); settings.setValue(QString("X-AW-Max"), m_maxValue); settings.setValue(QString("X-AW-Min"), m_minValue); - settings.setValue(QString("X-AW-ActiveColor"), m_activeColor); - settings.setValue(QString("X-AW-InactiveColor"), m_inactiveColor); + settings.setValue(QString("X-AW-ActiveColor"), activeColor()); + settings.setValue(QString("X-AW-InactiveColor"), inactiveColor()); settings.setValue(QString("X-AW-Type"), strType()); settings.setValue(QString("X-AW-Direction"), strDirection()); settings.setValue(QString("X-AW-Height"), m_height); @@ -491,8 +500,8 @@ void GraphicalItem::writeConfiguration() const void GraphicalItem::changeColor() { - QColor color - = stringToColor((static_cast(sender()))->text()); + QColor color = GraphicalItemHelper::stringToColor( + (static_cast(sender()))->text()); QColor newColor = QColorDialog::getColor(color, this, tr("Select color"), QColorDialog::ShowAlphaChannel); if (!newColor.isValid()) @@ -524,7 +533,7 @@ void GraphicalItem::initScene() // init scene m_scene = new QGraphicsScene(); if (m_type == Graph) - m_scene->setBackgroundBrush(stringToColor(m_inactiveColor)); + m_scene->setBackgroundBrush(m_inactiveColor); else m_scene->setBackgroundBrush(QBrush(Qt::NoBrush)); // init view @@ -538,103 +547,6 @@ void GraphicalItem::initScene() } -void GraphicalItem::paintCircle(const float value) -{ - QPen pen; - pen.setWidth(1); - float percent = (value - m_minValue) / (m_maxValue - m_minValue); - QGraphicsEllipseItem *circle; - - QColor inactive = stringToColor(m_inactiveColor); - QColor active = stringToColor(m_activeColor); - - // inactive - pen.setColor(inactive); - circle = m_scene->addEllipse(0.0, 0.0, m_width, m_height, pen, - QBrush(inactive, Qt::SolidPattern)); - circle->setSpanAngle(-(1.0f - percent) * 360.0f * 16.0f); - circle->setStartAngle(90.0f * 16.0f - percent * 360.0f * 16.0f); - // active - pen.setColor(active); - circle = m_scene->addEllipse(0.0, 0.0, m_width, m_height, pen, - QBrush(active, Qt::SolidPattern)); - circle->setSpanAngle(-percent * 360.0f * 16.0f); - circle->setStartAngle(90.0f * 16.0f); -} - - -void GraphicalItem::paintGraph(const QList value) -{ - QPen pen; - pen.setColor(stringToColor(m_activeColor)); - - // default norms - float normX - = static_cast(m_width) / static_cast(value.count()); - float normY = static_cast(m_height) / (1.5f * 100.0f); - // paint graph - for (int i = 0; i < value.count() - 1; i++) { - // some magic here - float x1 = i * normX; - float y1 = -fabs(value.at(i)) * normY + 5.0f; - float x2 = (i + 1) * normX; - float y2 = -fabs(value.at(i + 1)) * normY + 5.0f; - m_scene->addLine(x1, y1, x2, y2, pen); - } -} - - -void GraphicalItem::paintHorizontal(const float value) -{ - QPen pen; - float percent = (value - m_minValue) / (m_maxValue - m_minValue); - - pen.setWidth(m_height); - // inactive - pen.setColor(stringToColor(m_inactiveColor)); - m_scene->addLine(percent * m_width + 0.5 * m_height, 0.5 * m_height, - m_width + 0.5 * m_height, 0.5 * m_height, pen); - // active - pen.setColor(stringToColor(m_activeColor)); - m_scene->addLine(-0.5 * m_height, 0.5 * m_height, - percent * m_width - 0.5 * m_height, 0.5 * m_height, pen); -} - - -void GraphicalItem::paintVertical(const float value) -{ - QPen pen; - float percent = (value - m_minValue) / (m_maxValue - m_minValue); - - pen.setWidth(m_width); - // inactive - pen.setColor(stringToColor(m_inactiveColor)); - m_scene->addLine(0.5 * m_width, -0.5 * m_width, 0.5 * m_width, - (1.0 - percent) * m_height - 0.5 * m_width, pen); - // active - pen.setColor(stringToColor(m_activeColor)); - m_scene->addLine(0.5 * m_width, (1.0 - percent) * m_height + 0.5 * m_width, - 0.5 * m_width, m_height + 0.5 * m_width, pen); -} - - -QColor GraphicalItem::stringToColor(const QString _color) const -{ - qCDebug(LOG_LIB) << "Color" << _color; - - QColor qcolor; - QStringList listColor = _color.split(QChar(',')); - while (listColor.count() < 4) - listColor.append(QString("0")); - qcolor.setRed(listColor.at(0).toInt()); - qcolor.setGreen(listColor.at(1).toInt()); - qcolor.setBlue(listColor.at(2).toInt()); - qcolor.setAlpha(listColor.at(3).toInt()); - - return qcolor; -} - - void GraphicalItem::translate() { ui->label_name->setText(i18n("Name")); diff --git a/sources/awesomewidgets/graphicalitem.h b/sources/awesomewidgets/graphicalitem.h index ee83c00..91cb71b 100644 --- a/sources/awesomewidgets/graphicalitem.h +++ b/sources/awesomewidgets/graphicalitem.h @@ -74,10 +74,10 @@ public: // set methods void setBar(const QString _bar = QString("cpu")); void setActiveColor(const QString _color = QString("0,0,0,130")); - void setCustom(const bool _custom); + void setCustom(const bool _custom = false); void setInactiveColor(const QString _color = QString("255,255,255,130")); - void setMinValue(const float _value); - void setMaxValue(const float _value); + void setMinValue(const float _value = 0.0); + void setMaxValue(const float _value = 100.0); void setType(const Type _type = Horizontal); void setStrType(const QString _type = QString("Horizontal")); void setDirection(const Direction _direction = LeftToRight); @@ -101,19 +101,12 @@ private: QGraphicsView *m_view = nullptr; Ui::GraphicalItem *ui; void initScene(); - // paint methods - void paintCircle(const float value); - void paintGraph(const QList value); - void paintHorizontal(const float value); - void paintVertical(const float value); - // additional method - QColor stringToColor(const QString _color) const; void translate(); // properties QString m_bar = QString("cpu"); bool m_custom = false; - QString m_activeColor = QString("0,0,0,130"); - QString m_inactiveColor = QString("255,255,255,130"); + QColor m_activeColor = QColor(0, 0, 0, 130); + QColor m_inactiveColor = QColor(255, 255, 255, 130); float m_minValue = 0.0f; float m_maxValue = 100.0f; Type m_type = Horizontal; diff --git a/sources/awesomewidgets/graphicalitemhelper.cpp b/sources/awesomewidgets/graphicalitemhelper.cpp new file mode 100644 index 0000000..9fcd788 --- /dev/null +++ b/sources/awesomewidgets/graphicalitemhelper.cpp @@ -0,0 +1,147 @@ +/*************************************************************************** + * 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 "graphicalitemhelper.h" + +#include +#include + +#include + +#include "awdebug.h" + + +void GraphicalItemHelper::paintCircle(const float &percent, + const QColor &inactive, + const QColor &active, const int &width, + const int &height, QGraphicsScene *scene) +{ + QPen pen; + pen.setWidth(1); + QGraphicsEllipseItem *circle; + + // inactive + pen.setColor(inactive); + circle = scene->addEllipse(0.0, 0.0, width, height, pen, + QBrush(inactive, Qt::SolidPattern)); + circle->setSpanAngle(-(1.0f - percent) * 360.0f * 16.0f); + circle->setStartAngle(90.0f * 16.0f - percent * 360.0f * 16.0f); + // active + pen.setColor(active); + circle = scene->addEllipse(0.0, 0.0, width, height, pen, + QBrush(active, Qt::SolidPattern)); + circle->setSpanAngle(-percent * 360.0f * 16.0f); + circle->setStartAngle(90.0f * 16.0f); +} + + +void GraphicalItemHelper::paintGraph(const QList &value, + const QColor &active, const int &width, + const int &height, QGraphicsScene *scene) +{ + QPen pen; + pen.setColor(active); + + // default norms + float normX = static_cast(width) / static_cast(value.count()); + float normY = static_cast(height) / (1.5f * 100.0f); + // paint graph + for (int i = 0; i < value.count() - 1; i++) { + // some magic here + float x1 = i * normX; + float y1 = -fabs(value.at(i)) * normY + 5.0f; + float x2 = (i + 1) * normX; + float y2 = -fabs(value.at(i + 1)) * normY + 5.0f; + scene->addLine(x1, y1, x2, y2, pen); + } +} + + +void GraphicalItemHelper::paintHorizontal(const float &percent, + const QColor &inactive, + const QColor &active, + const int &width, const int &height, + QGraphicsScene *scene) +{ + QPen pen; + pen.setWidth(height); + // inactive + pen.setColor(inactive); + scene->addLine(percent * width + 0.5 * height, 0.5 * height, + width + 0.5 * height, 0.5 * height, pen); + // active + pen.setColor(active); + scene->addLine(-0.5 * height, 0.5 * height, percent * width - 0.5 * height, + 0.5 * height, pen); +} + + +void GraphicalItemHelper::paintVertical(const float &percent, + const QColor &inactive, + const QColor &active, const int &width, + const int &height, + QGraphicsScene *scene) +{ + QPen pen; + pen.setWidth(width); + // inactive + pen.setColor(inactive); + scene->addLine(0.5 * width, -0.5 * width, 0.5 * width, + (1.0 - percent) * height - 0.5 * width, pen); + // active + pen.setColor(active); + scene->addLine(0.5 * width, (1.0 - percent) * height + 0.5 * width, + 0.5 * width, height + 0.5 * width, pen); +} + + +QString GraphicalItemHelper::colorToString(const QColor &color) +{ + qCDebug(LOG_LIB) << "Color" << color; + + return QString("%1,%2,%3,%4") + .arg(color.red()) + .arg(color.green()) + .arg(color.blue()) + .arg(color.alpha()); +} + + +float GraphicalItemHelper::getPercents(const float &value, const float &min, + const float &max) +{ + qCDebug(LOG_LIB) << "Get percent value from" << value; + + return (value - min) / (max - min); +} + + +QColor GraphicalItemHelper::stringToColor(const QString &color) +{ + qCDebug(LOG_LIB) << "Color" << color; + + QColor qColor; + QStringList listColor = color.split(QChar(',')); + while (listColor.count() < 4) + listColor.append(QString("0")); + qColor.setRed(listColor.at(0).toInt()); + qColor.setGreen(listColor.at(1).toInt()); + qColor.setBlue(listColor.at(2).toInt()); + qColor.setAlpha(listColor.at(3).toInt()); + + return qColor; +} diff --git a/sources/awesomewidgets/graphicalitemhelper.h b/sources/awesomewidgets/graphicalitemhelper.h new file mode 100644 index 0000000..f76c41f --- /dev/null +++ b/sources/awesomewidgets/graphicalitemhelper.h @@ -0,0 +1,47 @@ +/*************************************************************************** + * 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 GRAPHICALITEMHELPER_H +#define GRAPHICALITEMHELPER_H + +#include + + +class QGraphicsScene; + +namespace GraphicalItemHelper +{ +// paint methods +void paintCircle(const float &percent, const QColor &inactive, + const QColor &active, const int &width, const int &height, + QGraphicsScene *scene); +void paintGraph(const QList &value, const QColor &active, + const int &width, const int &height, QGraphicsScene *scene); +void paintHorizontal(const float &percent, const QColor &inactive, + const QColor &active, const int &width, const int &height, + QGraphicsScene *scene); +void paintVertical(const float &percent, const QColor &inactive, + const QColor &active, const int &width, const int &height, + QGraphicsScene *scene); +// additional conversion methods +QString colorToString(const QColor &color); +float getPercents(const float &value, const float &min, const float &max); +QColor stringToColor(const QString &color); +}; + + +#endif /* GRAPHICALITEMHELPER_H */