mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-04-24 23:47:20 +00:00
Improve performance with image generation
To avoid pen creation on each update class properties is used.
This commit is contained in:
parent
88f0ebfe96
commit
7e42c8cb49
@ -299,8 +299,8 @@ void ExtWeather::writeConfiguration() const
|
||||
|
||||
void ExtWeather::weatherReplyReceived(QNetworkReply *reply)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Return code" << reply->error() << "with messa";
|
||||
qCDebug(LOG_LIB) << "Reply error message" << reply->errorString();
|
||||
qCDebug(LOG_LIB) << "Return code" << reply->error() << "with message"
|
||||
<< reply->errorString();
|
||||
|
||||
isRunning = false;
|
||||
QJsonParseError error;
|
||||
|
@ -48,21 +48,31 @@ void GraphicalItemHelper::setParameters(const QString active,
|
||||
<< ", width" << width << ", height" << height << ", count"
|
||||
<< count;
|
||||
|
||||
// put images to pens if any otherwise set pen colors
|
||||
// Images resize to content here as well
|
||||
if (active.startsWith(QString("/"))) {
|
||||
qCInfo(LOG_LIB) << "Found path, trying to load Pixmap from" << active;
|
||||
m_activeImage = QPixmap(active);
|
||||
if (m_activeImage.isNull())
|
||||
QPixmap pixmap = QPixmap(active);
|
||||
if (pixmap.isNull()) {
|
||||
qCInfo(LOG_LIB) << "Invalid pixmap found" << active;
|
||||
m_activePen.setColor(QColor(0, 0, 0, 130));
|
||||
} else {
|
||||
m_activeColor = stringToColor(active);
|
||||
m_activePen.setBrush(QBrush(pixmap.scaled(width, height)));
|
||||
}
|
||||
} else {
|
||||
m_activePen.setColor(stringToColor(active));
|
||||
}
|
||||
if (inactive.startsWith(QString("/"))) {
|
||||
qCInfo(LOG_LIB) << "Found path, trying to load Pixmap from" << inactive;
|
||||
m_inactiveImage = QPixmap(inactive);
|
||||
if (m_inactiveImage.isNull())
|
||||
QPixmap pixmap = QPixmap(inactive);
|
||||
if (pixmap.isNull()) {
|
||||
qCInfo(LOG_LIB) << "Invalid pixmap found" << inactive;
|
||||
m_inactivePen.setColor(QColor(255, 255, 255, 130));
|
||||
} else {
|
||||
m_inactiveColor = stringToColor(inactive);
|
||||
m_inactivePen.setBrush(QBrush(pixmap.scaled(width, height)));
|
||||
}
|
||||
} else {
|
||||
m_inactivePen.setColor(stringToColor(inactive));
|
||||
}
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
@ -74,22 +84,20 @@ void GraphicalItemHelper::paintCircle(const float &percent)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Paint with percent" << percent;
|
||||
|
||||
QPen pen;
|
||||
pen.setWidth(1);
|
||||
m_activePen.setWidth(1);
|
||||
m_inactivePen.setWidth(1);
|
||||
QGraphicsEllipseItem *circle;
|
||||
// 16 is because of qt. From Qt documentation:
|
||||
// Returns the start angle for an ellipse segment in 16ths of a degree
|
||||
|
||||
// inactive
|
||||
pen.setColor(m_inactiveColor);
|
||||
circle = m_scene->addEllipse(0.0, 0.0, m_width, m_height, pen,
|
||||
QBrush(m_inactiveColor, Qt::SolidPattern));
|
||||
circle = m_scene->addEllipse(0.0, 0.0, m_width, m_height, m_inactivePen,
|
||||
m_inactivePen.brush());
|
||||
circle->setSpanAngle(-(1.0f - percent) * 360.0f * 16.0f);
|
||||
circle->setStartAngle(90.0f * 16.0f - percent * 360.0f * 16.0f);
|
||||
// active
|
||||
pen.setColor(m_activeColor);
|
||||
circle = m_scene->addEllipse(0.0, 0.0, m_width, m_height, pen,
|
||||
QBrush(m_activeColor, Qt::SolidPattern));
|
||||
circle = m_scene->addEllipse(0.0, 0.0, m_width, m_height, m_activePen,
|
||||
m_activePen.brush());
|
||||
circle->setSpanAngle(-percent * 360.0f * 16.0f);
|
||||
circle->setStartAngle(90 * 16);
|
||||
}
|
||||
@ -100,15 +108,9 @@ void GraphicalItemHelper::paintGraph(const float &value)
|
||||
qCDebug(LOG_LIB) << "Paint with value" << value;
|
||||
|
||||
// refresh background image
|
||||
if (m_inactiveImage.isNull())
|
||||
m_scene->setBackgroundBrush(QBrush(m_inactiveColor));
|
||||
else
|
||||
m_scene->setBackgroundBrush(
|
||||
QBrush(m_inactiveImage.scaled(m_width, m_height)));
|
||||
m_scene->setBackgroundBrush(m_inactivePen.brush());
|
||||
|
||||
storeValue(value);
|
||||
QPen pen;
|
||||
pen.setColor(m_activeColor);
|
||||
|
||||
// default norms
|
||||
float normX
|
||||
@ -121,7 +123,7 @@ void GraphicalItemHelper::paintGraph(const float &value)
|
||||
float y1 = -fabs(m_values.at(i)) * normY + 0.5f;
|
||||
float x2 = (i + 1) * normX;
|
||||
float y2 = -fabs(m_values.at(i + 1)) * normY + 0.5f;
|
||||
m_scene->addLine(x1, y1, x2, y2, pen);
|
||||
m_scene->addLine(x1, y1, x2, y2, m_activePen);
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,16 +132,15 @@ void GraphicalItemHelper::paintHorizontal(const float &percent)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Paint with percent" << percent;
|
||||
|
||||
QPen pen;
|
||||
pen.setWidth(m_height);
|
||||
m_activePen.setWidth(m_height);
|
||||
m_inactivePen.setWidth(m_height);
|
||||
// inactive
|
||||
pen.setColor(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);
|
||||
m_width + 0.5 * m_height, 0.5 * m_height, m_inactivePen);
|
||||
// active
|
||||
pen.setColor(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);
|
||||
percent * m_width - 0.5 * m_height, 0.5 * m_height,
|
||||
m_activePen);
|
||||
}
|
||||
|
||||
|
||||
@ -147,28 +148,14 @@ void GraphicalItemHelper::paintVertical(const float &percent)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Paint with percent" << percent;
|
||||
|
||||
QPen pen;
|
||||
pen.setWidth(m_width);
|
||||
m_activePen.setWidth(m_height);
|
||||
m_inactivePen.setWidth(m_height);
|
||||
// inactive
|
||||
pen.setColor(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);
|
||||
(1.0 - percent) * m_height - 0.5 * m_width, m_inactivePen);
|
||||
// active
|
||||
pen.setColor(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);
|
||||
}
|
||||
|
||||
|
||||
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());
|
||||
0.5 * m_width, m_height + 0.5 * m_width, m_activePen);
|
||||
}
|
||||
|
||||
|
||||
|
@ -19,8 +19,8 @@
|
||||
#define GRAPHICALITEMHELPER_H
|
||||
|
||||
#include <QColor>
|
||||
#include <QPixmap>
|
||||
#include <QObject>
|
||||
#include <QPen>
|
||||
|
||||
|
||||
class QGraphicsScene;
|
||||
@ -40,7 +40,6 @@ public:
|
||||
void paintHorizontal(const float &percent);
|
||||
void paintVertical(const float &percent);
|
||||
// additional conversion methods
|
||||
QString colorToString(const QColor &color);
|
||||
float getPercents(const float &value, const float &min, const float &max);
|
||||
QColor stringToColor(const QString &color);
|
||||
|
||||
@ -48,10 +47,8 @@ private:
|
||||
void storeValue(const float &value);
|
||||
QGraphicsScene *m_scene = nullptr;
|
||||
int m_count = 100;
|
||||
QColor m_activeColor = QColor(0, 0, 0, 130);
|
||||
QColor m_inactiveColor = QColor(255, 255, 255, 130);
|
||||
QPixmap m_activeImage;
|
||||
QPixmap m_inactiveImage;
|
||||
QPen m_activePen;
|
||||
QPen m_inactivePen;
|
||||
int m_width = 100;
|
||||
int m_height = 100;
|
||||
// list of values which will be used to store data for graph type only
|
||||
|
Loading…
Reference in New Issue
Block a user