split gi to helper and core

Also internal data format has been changed
This commit is contained in:
Evgenii Alekseev 2016-02-24 20:40:55 +03:00
parent 15d4d7667d
commit 8cae273ffb
4 changed files with 230 additions and 131 deletions

View File

@ -23,14 +23,12 @@
#include <QBuffer> #include <QBuffer>
#include <QColorDialog> #include <QColorDialog>
#include <QDir> #include <QDir>
#include <QGraphicsEllipseItem>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QGraphicsView> #include <QGraphicsView>
#include <QSettings> #include <QSettings>
#include <math.h>
#include "awdebug.h" #include "awdebug.h"
#include "graphicalitemhelper.h"
#include "version.h" #include "version.h"
@ -72,12 +70,12 @@ GraphicalItem *GraphicalItem::copy(const QString _fileName, const int _number)
GraphicalItem *item = new GraphicalItem(static_cast<QWidget *>(parent()), GraphicalItem *item = new GraphicalItem(static_cast<QWidget *>(parent()),
_fileName, directories()); _fileName, directories());
copyDefaults(item); copyDefaults(item);
item->setActiveColor(m_activeColor); item->setActiveColor(activeColor());
item->setBar(m_bar); item->setBar(m_bar);
item->setCustom(m_custom); item->setCustom(m_custom);
item->setDirection(m_direction); item->setDirection(m_direction);
item->setHeight(m_height); item->setHeight(m_height);
item->setInactiveColor(m_inactiveColor); item->setInactiveColor(inactiveColor());
item->setMaxValue(m_maxValue); item->setMaxValue(m_maxValue);
item->setMinValue(m_minValue); item->setMinValue(m_minValue);
item->setNumber(_number); item->setNumber(_number);
@ -98,22 +96,33 @@ QString GraphicalItem::image(const QVariant &value)
// paint // paint
switch (m_type) { switch (m_type) {
case Vertical: 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
scale[1] = -2 * static_cast<int>(m_direction) + 1; scale[1] = -2 * static_cast<int>(m_direction) + 1;
break; break;
case Circle: 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
scale[0] = -2 * static_cast<int>(m_direction) + 1; scale[0] = -2 * static_cast<int>(m_direction) + 1;
break; break;
case Graph: case Graph:
paintGraph(value.value<QList<float>>()); GraphicalItemHelper::paintGraph(value.value<QList<float>>(),
m_activeColor, m_width, m_height,
m_scene);
// direction option is not recognized by this GI type // direction option is not recognized by this GI type
break; break;
case Horizontal: case Horizontal:
default: 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
scale[0] = -2 * static_cast<int>(m_direction) + 1; scale[0] = -2 * static_cast<int>(m_direction) + 1;
break; break;
@ -140,13 +149,13 @@ QString GraphicalItem::bar() const
QString GraphicalItem::activeColor() const QString GraphicalItem::activeColor() const
{ {
return m_activeColor; return GraphicalItemHelper::colorToString(m_activeColor);
} }
QString GraphicalItem::inactiveColor() const 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; 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; 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()); setMaxValue(settings.value(QString("X-AW-Max"), m_maxValue).toFloat());
setMinValue(settings.value(QString("X-AW-Min"), m_minValue).toFloat()); setMinValue(settings.value(QString("X-AW-Min"), m_minValue).toFloat());
setActiveColor( setActiveColor(
settings.value(QString("X-AW-ActiveColor"), m_activeColor) settings.value(QString("X-AW-ActiveColor"), activeColor())
.toString()); .toString());
setInactiveColor( setInactiveColor(
settings.value(QString("X-AW-InactiveColor"), m_inactiveColor) settings.value(QString("X-AW-InactiveColor"), inactiveColor())
.toString()); .toString());
setStrType(settings.value(QString("X-AW-Type"), strType()).toString()); setStrType(settings.value(QString("X-AW-Type"), strType()).toString());
setStrDirection( setStrDirection(
@ -433,8 +442,8 @@ int GraphicalItem::showConfiguration(const QVariant args)
} }
ui->doubleSpinBox_max->setValue(m_maxValue); ui->doubleSpinBox_max->setValue(m_maxValue);
ui->doubleSpinBox_min->setValue(m_minValue); ui->doubleSpinBox_min->setValue(m_minValue);
ui->pushButton_activeColor->setText(m_activeColor); ui->pushButton_activeColor->setText(activeColor());
ui->pushButton_inactiveColor->setText(m_inactiveColor); ui->pushButton_inactiveColor->setText(inactiveColor());
ui->comboBox_type->setCurrentIndex(static_cast<int>(m_type)); ui->comboBox_type->setCurrentIndex(static_cast<int>(m_type));
ui->comboBox_direction->setCurrentIndex(static_cast<int>(m_direction)); ui->comboBox_direction->setCurrentIndex(static_cast<int>(m_direction));
ui->spinBox_height->setValue(m_height); 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-Custom"), m_custom);
settings.setValue(QString("X-AW-Max"), m_maxValue); settings.setValue(QString("X-AW-Max"), m_maxValue);
settings.setValue(QString("X-AW-Min"), m_minValue); settings.setValue(QString("X-AW-Min"), m_minValue);
settings.setValue(QString("X-AW-ActiveColor"), m_activeColor); settings.setValue(QString("X-AW-ActiveColor"), activeColor());
settings.setValue(QString("X-AW-InactiveColor"), m_inactiveColor); settings.setValue(QString("X-AW-InactiveColor"), inactiveColor());
settings.setValue(QString("X-AW-Type"), strType()); settings.setValue(QString("X-AW-Type"), strType());
settings.setValue(QString("X-AW-Direction"), strDirection()); settings.setValue(QString("X-AW-Direction"), strDirection());
settings.setValue(QString("X-AW-Height"), m_height); settings.setValue(QString("X-AW-Height"), m_height);
@ -491,8 +500,8 @@ void GraphicalItem::writeConfiguration() const
void GraphicalItem::changeColor() void GraphicalItem::changeColor()
{ {
QColor color QColor color = GraphicalItemHelper::stringToColor(
= stringToColor((static_cast<QPushButton *>(sender()))->text()); (static_cast<QPushButton *>(sender()))->text());
QColor newColor = QColorDialog::getColor(color, this, tr("Select color"), QColor newColor = QColorDialog::getColor(color, this, tr("Select color"),
QColorDialog::ShowAlphaChannel); QColorDialog::ShowAlphaChannel);
if (!newColor.isValid()) if (!newColor.isValid())
@ -524,7 +533,7 @@ void GraphicalItem::initScene()
// init scene // init scene
m_scene = new QGraphicsScene(); m_scene = new QGraphicsScene();
if (m_type == Graph) if (m_type == Graph)
m_scene->setBackgroundBrush(stringToColor(m_inactiveColor)); m_scene->setBackgroundBrush(m_inactiveColor);
else else
m_scene->setBackgroundBrush(QBrush(Qt::NoBrush)); m_scene->setBackgroundBrush(QBrush(Qt::NoBrush));
// init view // 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<float> value)
{
QPen pen;
pen.setColor(stringToColor(m_activeColor));
// default norms
float normX
= static_cast<float>(m_width) / static_cast<float>(value.count());
float normY = static_cast<float>(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() void GraphicalItem::translate()
{ {
ui->label_name->setText(i18n("Name")); ui->label_name->setText(i18n("Name"));

View File

@ -74,10 +74,10 @@ public:
// set methods // set methods
void setBar(const QString _bar = QString("cpu")); void setBar(const QString _bar = QString("cpu"));
void setActiveColor(const QString _color = QString("0,0,0,130")); 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 setInactiveColor(const QString _color = QString("255,255,255,130"));
void setMinValue(const float _value); void setMinValue(const float _value = 0.0);
void setMaxValue(const float _value); void setMaxValue(const float _value = 100.0);
void setType(const Type _type = Horizontal); void setType(const Type _type = Horizontal);
void setStrType(const QString _type = QString("Horizontal")); void setStrType(const QString _type = QString("Horizontal"));
void setDirection(const Direction _direction = LeftToRight); void setDirection(const Direction _direction = LeftToRight);
@ -101,19 +101,12 @@ private:
QGraphicsView *m_view = nullptr; QGraphicsView *m_view = nullptr;
Ui::GraphicalItem *ui; Ui::GraphicalItem *ui;
void initScene(); void initScene();
// paint methods
void paintCircle(const float value);
void paintGraph(const QList<float> value);
void paintHorizontal(const float value);
void paintVertical(const float value);
// additional method
QColor stringToColor(const QString _color) const;
void translate(); void translate();
// properties // properties
QString m_bar = QString("cpu"); QString m_bar = QString("cpu");
bool m_custom = false; bool m_custom = false;
QString m_activeColor = QString("0,0,0,130"); QColor m_activeColor = QColor(0, 0, 0, 130);
QString m_inactiveColor = QString("255,255,255,130"); QColor m_inactiveColor = QColor(255, 255, 255, 130);
float m_minValue = 0.0f; float m_minValue = 0.0f;
float m_maxValue = 100.0f; float m_maxValue = 100.0f;
Type m_type = Horizontal; Type m_type = Horizontal;

View File

@ -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 <QGraphicsEllipseItem>
#include <QGraphicsScene>
#include <math.h>
#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<float> &value,
const QColor &active, const int &width,
const int &height, QGraphicsScene *scene)
{
QPen pen;
pen.setColor(active);
// default norms
float normX = static_cast<float>(width) / static_cast<float>(value.count());
float normY = static_cast<float>(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;
}

View File

@ -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 <QColor>
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<float> &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 */