diff --git a/sources/awesomewidgets/desktops/aw-bat-bar.desktop b/sources/awesomewidgets/desktops/aw-bat-bar.desktop index 4d4e6d0..9aa9df0 100644 --- a/sources/awesomewidgets/desktops/aw-bat-bar.desktop +++ b/sources/awesomewidgets/desktops/aw-bat-bar.desktop @@ -9,5 +9,5 @@ X-AW-Type=Horizontal X-AW-Direction=LeftToRight X-AW-Height=25 X-AW-Width=100 -X-AW-ApiVersion=2 +X-AW-ApiVersion=3 X-AW-Number=3 diff --git a/sources/awesomewidgets/desktops/aw-cpu-bar.desktop b/sources/awesomewidgets/desktops/aw-cpu-bar.desktop index e186c21..0c328a8 100644 --- a/sources/awesomewidgets/desktops/aw-cpu-bar.desktop +++ b/sources/awesomewidgets/desktops/aw-cpu-bar.desktop @@ -9,5 +9,5 @@ X-AW-Type=Horizontal X-AW-Direction=LeftToRight X-AW-Height=25 X-AW-Width=100 -X-AW-ApiVersion=2 +X-AW-ApiVersion=3 X-AW-Number=0 diff --git a/sources/awesomewidgets/desktops/aw-mem-bar.desktop b/sources/awesomewidgets/desktops/aw-mem-bar.desktop index 6e2ba18..4f4f92a 100644 --- a/sources/awesomewidgets/desktops/aw-mem-bar.desktop +++ b/sources/awesomewidgets/desktops/aw-mem-bar.desktop @@ -9,5 +9,5 @@ X-AW-Type=Horizontal X-AW-Direction=LeftToRight X-AW-Height=25 X-AW-Width=100 -X-AW-ApiVersion=2 +X-AW-ApiVersion=3 X-AW-Number=1 diff --git a/sources/awesomewidgets/desktops/aw-swap-bar.desktop b/sources/awesomewidgets/desktops/aw-swap-bar.desktop index f30bce8..95f3b9a 100644 --- a/sources/awesomewidgets/desktops/aw-swap-bar.desktop +++ b/sources/awesomewidgets/desktops/aw-swap-bar.desktop @@ -9,5 +9,5 @@ X-AW-Type=Horizontal X-AW-Direction=LeftToRight X-AW-Height=25 X-AW-Width=100 -X-AW-ApiVersion=2 +X-AW-ApiVersion=3 X-AW-Number=2 diff --git a/sources/awesomewidgets/graphicalitem.cpp b/sources/awesomewidgets/graphicalitem.cpp index 9497617..c9f921d 100644 --- a/sources/awesomewidgets/graphicalitem.cpp +++ b/sources/awesomewidgets/graphicalitem.cpp @@ -28,6 +28,8 @@ #include #include +#include + #include "awdebug.h" #include "version.h" @@ -43,6 +45,8 @@ GraphicalItem::GraphicalItem(QWidget *parent, const QString desktopName, ui->setupUi(this); translate(); + initScene(); + connect(ui->pushButton_activeColor, SIGNAL(clicked()), this, SLOT(changeColor())); connect(ui->pushButton_inactiveColor, SIGNAL(clicked()), this, SLOT(changeColor())); } @@ -52,6 +56,7 @@ GraphicalItem::~GraphicalItem() { qCDebug(LOG_LIB); + delete m_scene; delete ui; } @@ -81,81 +86,45 @@ GraphicalItem *GraphicalItem::copy(const QString _fileName, const int _number) } -QString GraphicalItem::image(const float value) const +QString GraphicalItem::image(const QVariant value) { qCDebug(LOG_LIB); qCDebug(LOG_LIB) << "Value" << value; if (m_bar == QString("none")) return QString(""); - QColor active = stringToColor(m_activeColor); - QColor inactive = stringToColor(m_inactiveColor); - float percent = value / 100.0; + m_scene->clear(); int scale[2] = { 1, 1 }; - QPen pen = QPen(); - QGraphicsScene *scene = new QGraphicsScene(); - scene->setBackgroundBrush(QBrush(Qt::NoBrush)); - QGraphicsView *view = new QGraphicsView(scene); - view->setStyleSheet(QString("background: transparent")); - view->setContentsMargins(0, 0, 0, 0); - view->setFrameShape(QFrame::NoFrame); - view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - view->resize(m_width + 5.0, m_height + 5.0); // paint switch (m_type) { case Vertical: - pen.setWidth(m_width); - // inactive - pen.setColor(inactive); - 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(active); - 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); + paintVertical(value.toFloat()); // scale scale[1] = -2 * static_cast(m_direction) + 1; break; case Circle: - QGraphicsEllipseItem *circle; - pen.setWidth(1.0); - // inactive - pen.setColor(inactive); - circle = scene->addEllipse(0.0, 0.0, m_width, m_height, pen, QBrush(inactive, Qt::SolidPattern)); - circle->setSpanAngle(- (1.0 - percent) * 360.0 * 16.0); - circle->setStartAngle(90.0 * 16.0 - percent * 360.0 * 16.0); - // active - pen.setColor(active); - circle = scene->addEllipse(0.0, 0.0, m_width, m_height, pen, QBrush(active, Qt::SolidPattern)); - circle->setSpanAngle(- percent * 360.0 * 16.0); - circle->setStartAngle(90.0 * 16.0); + paintCircle(value.toFloat()); // scale scale[0] = -2 * static_cast(m_direction) + 1; break; + case Graph: + paintGraph(value.value>()); + // direction option is not recognized by this GI type + break; + case Horizontal: default: - pen.setWidth(m_height); - // inactive - pen.setColor(inactive); - 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(active); - scene->addLine(-0.5 * m_height, 0.5 * m_height, - percent * m_width - 0.5 * m_height, 0.5 * m_height, pen); + paintHorizontal(value.toFloat()); // scale scale[0] = -2 * static_cast(m_direction) + 1; break; } // convert - QPixmap pixmap = view->grab().transformed(QTransform().scale(scale[0], scale[1])); + QPixmap pixmap = m_view->grab().transformed(QTransform().scale(scale[0], scale[1])); QByteArray byteArray; QBuffer buffer(&byteArray); pixmap.save(&buffer, "PNG"); QString url = QString("").arg(QString(byteArray.toBase64())); - delete view; - delete scene; return url; } @@ -189,7 +158,7 @@ QString GraphicalItem::tag() const { qCDebug(LOG_LIB); - return name() + m_bar; + return QString("bar%1%2").arg(number()).arg(m_bar); } @@ -213,10 +182,14 @@ QString GraphicalItem::strType() const case Circle: value = QString("Circle"); break; + case Graph: + value = QString("Graph"); + break; + case Horizontal: default: value = QString("Horizontal"); break; - }\ + } return value; } @@ -239,6 +212,7 @@ QString GraphicalItem::strDirection() const case RightToLeft: value = QString("RightToLeft"); break; + case LeftToRight: default: value = QString("LeftToRight"); break; @@ -321,6 +295,8 @@ void GraphicalItem::setStrType(const QString _type) setType(Vertical); else if (_type == QString("Circle")) setType(Circle); + else if (_type == QString("Graph")) + setType(Graph); else setType(Horizontal); } @@ -486,6 +462,114 @@ void GraphicalItem::changeColor() } +void GraphicalItem::initScene() +{ + qCDebug(LOG_LIB); + + // init scene + m_scene = new QGraphicsScene(); + if (m_type == Graph) + m_scene->setBackgroundBrush(stringToColor(m_inactiveColor)); + else + 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.0, m_height + 5.0); +} + + +void GraphicalItem::paintCircle(const float value) +{ + qCDebug(LOG_LIB); + + QPen pen; + pen.setWidth(1.0); + float percent = value / 100.0; + 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.0 - percent) * 360.0 * 16.0); + circle->setStartAngle(90.0 * 16.0 - percent * 360.0 * 16.0); + // 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.0 * 16.0); + circle->setStartAngle(90.0 * 16.0); +} + + +void GraphicalItem::paintGraph(const QList value) +{ + qCDebug(LOG_LIB); + + 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.5 * 100.0); + // paint graph + for (int i=0; iaddLine(x1, y1, x2, y2, pen); + } +} + + +void GraphicalItem::paintHorizontal(const float value) +{ + qCDebug(LOG_LIB); + + QPen pen; + float percent = value / 100.0; + + 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) +{ + qCDebug(LOG_LIB); + + QPen pen; + float percent = value / 100.0; + + 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); diff --git a/sources/awesomewidgets/graphicalitem.h b/sources/awesomewidgets/graphicalitem.h index 708f3bb..3c82b8b 100644 --- a/sources/awesomewidgets/graphicalitem.h +++ b/sources/awesomewidgets/graphicalitem.h @@ -23,6 +23,9 @@ #include "abstractextitem.h" +class QGraphicsScene; +class QGraphicsView; + namespace Ui { class GraphicalItem; } @@ -46,14 +49,15 @@ public: enum Type { Horizontal = 0, Vertical, - Circle + Circle, + Graph }; explicit GraphicalItem(QWidget *parent = nullptr, const QString desktopName = QString(), const QStringList directories = QStringList()); virtual ~GraphicalItem(); GraphicalItem *copy(const QString _fileName, const int _number); - QString image(const float value) const; + QString image(const QVariant value); // get methods QString bar() const; QString activeColor() const; @@ -89,7 +93,16 @@ private slots: private: QString m_fileName; QStringList m_dirs; + QGraphicsScene *m_scene = nullptr; + 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 diff --git a/sources/awesomewidgets/graphicalitem.ui b/sources/awesomewidgets/graphicalitem.ui index a5afb71..a2fb703 100644 --- a/sources/awesomewidgets/graphicalitem.ui +++ b/sources/awesomewidgets/graphicalitem.ui @@ -150,6 +150,11 @@ Circle + + + Graph + +