Improve performance with image generation

To avoid pen creation on each update class properties is used.
This commit is contained in:
Evgenii Alekseev 2016-03-24 15:53:07 +03:00
parent 88f0ebfe96
commit 7e42c8cb49
3 changed files with 38 additions and 54 deletions

View File

@ -299,8 +299,8 @@ void ExtWeather::writeConfiguration() const
void ExtWeather::weatherReplyReceived(QNetworkReply *reply) void ExtWeather::weatherReplyReceived(QNetworkReply *reply)
{ {
qCDebug(LOG_LIB) << "Return code" << reply->error() << "with messa"; qCDebug(LOG_LIB) << "Return code" << reply->error() << "with message"
qCDebug(LOG_LIB) << "Reply error message" << reply->errorString(); << reply->errorString();
isRunning = false; isRunning = false;
QJsonParseError error; QJsonParseError error;

View File

@ -48,21 +48,31 @@ void GraphicalItemHelper::setParameters(const QString active,
<< ", width" << width << ", height" << height << ", count" << ", width" << width << ", height" << height << ", count"
<< count; << count;
// put images to pens if any otherwise set pen colors
// Images resize to content here as well
if (active.startsWith(QString("/"))) { if (active.startsWith(QString("/"))) {
qCInfo(LOG_LIB) << "Found path, trying to load Pixmap from" << active; qCInfo(LOG_LIB) << "Found path, trying to load Pixmap from" << active;
m_activeImage = QPixmap(active); QPixmap pixmap = QPixmap(active);
if (m_activeImage.isNull()) if (pixmap.isNull()) {
qCInfo(LOG_LIB) << "Invalid pixmap found" << active; qCInfo(LOG_LIB) << "Invalid pixmap found" << active;
m_activePen.setColor(QColor(0, 0, 0, 130));
} else {
m_activePen.setBrush(QBrush(pixmap.scaled(width, height)));
}
} else { } else {
m_activeColor = stringToColor(active); m_activePen.setColor(stringToColor(active));
} }
if (inactive.startsWith(QString("/"))) { if (inactive.startsWith(QString("/"))) {
qCInfo(LOG_LIB) << "Found path, trying to load Pixmap from" << inactive; qCInfo(LOG_LIB) << "Found path, trying to load Pixmap from" << inactive;
m_inactiveImage = QPixmap(inactive); QPixmap pixmap = QPixmap(inactive);
if (m_inactiveImage.isNull()) if (pixmap.isNull()) {
qCInfo(LOG_LIB) << "Invalid pixmap found" << inactive; qCInfo(LOG_LIB) << "Invalid pixmap found" << inactive;
m_inactivePen.setColor(QColor(255, 255, 255, 130));
} else {
m_inactivePen.setBrush(QBrush(pixmap.scaled(width, height)));
}
} else { } else {
m_inactiveColor = stringToColor(inactive); m_inactivePen.setColor(stringToColor(inactive));
} }
m_width = width; m_width = width;
m_height = height; m_height = height;
@ -74,22 +84,20 @@ void GraphicalItemHelper::paintCircle(const float &percent)
{ {
qCDebug(LOG_LIB) << "Paint with percent" << percent; qCDebug(LOG_LIB) << "Paint with percent" << percent;
QPen pen; m_activePen.setWidth(1);
pen.setWidth(1); m_inactivePen.setWidth(1);
QGraphicsEllipseItem *circle; QGraphicsEllipseItem *circle;
// 16 is because of qt. From Qt documentation: // 16 is because of qt. From Qt documentation:
// Returns the start angle for an ellipse segment in 16ths of a degree // Returns the start angle for an ellipse segment in 16ths of a degree
// inactive // inactive
pen.setColor(m_inactiveColor); circle = m_scene->addEllipse(0.0, 0.0, m_width, m_height, m_inactivePen,
circle = m_scene->addEllipse(0.0, 0.0, m_width, m_height, pen, m_inactivePen.brush());
QBrush(m_inactiveColor, Qt::SolidPattern));
circle->setSpanAngle(-(1.0f - percent) * 360.0f * 16.0f); circle->setSpanAngle(-(1.0f - percent) * 360.0f * 16.0f);
circle->setStartAngle(90.0f * 16.0f - percent * 360.0f * 16.0f); circle->setStartAngle(90.0f * 16.0f - percent * 360.0f * 16.0f);
// active // active
pen.setColor(m_activeColor); circle = m_scene->addEllipse(0.0, 0.0, m_width, m_height, m_activePen,
circle = m_scene->addEllipse(0.0, 0.0, m_width, m_height, pen, m_activePen.brush());
QBrush(m_activeColor, Qt::SolidPattern));
circle->setSpanAngle(-percent * 360.0f * 16.0f); circle->setSpanAngle(-percent * 360.0f * 16.0f);
circle->setStartAngle(90 * 16); circle->setStartAngle(90 * 16);
} }
@ -100,15 +108,9 @@ void GraphicalItemHelper::paintGraph(const float &value)
qCDebug(LOG_LIB) << "Paint with value" << value; qCDebug(LOG_LIB) << "Paint with value" << value;
// refresh background image // refresh background image
if (m_inactiveImage.isNull()) m_scene->setBackgroundBrush(m_inactivePen.brush());
m_scene->setBackgroundBrush(QBrush(m_inactiveColor));
else
m_scene->setBackgroundBrush(
QBrush(m_inactiveImage.scaled(m_width, m_height)));
storeValue(value); storeValue(value);
QPen pen;
pen.setColor(m_activeColor);
// default norms // default norms
float normX float normX
@ -121,7 +123,7 @@ void GraphicalItemHelper::paintGraph(const float &value)
float y1 = -fabs(m_values.at(i)) * normY + 0.5f; float y1 = -fabs(m_values.at(i)) * normY + 0.5f;
float x2 = (i + 1) * normX; float x2 = (i + 1) * normX;
float y2 = -fabs(m_values.at(i + 1)) * normY + 0.5f; 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; qCDebug(LOG_LIB) << "Paint with percent" << percent;
QPen pen; m_activePen.setWidth(m_height);
pen.setWidth(m_height); m_inactivePen.setWidth(m_height);
// inactive // inactive
pen.setColor(m_inactiveColor);
m_scene->addLine(percent * m_width + 0.5 * m_height, 0.5 * m_height, 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 // active
pen.setColor(m_activeColor);
m_scene->addLine(-0.5 * m_height, 0.5 * m_height, 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; qCDebug(LOG_LIB) << "Paint with percent" << percent;
QPen pen; m_activePen.setWidth(m_height);
pen.setWidth(m_width); m_inactivePen.setWidth(m_height);
// inactive // inactive
pen.setColor(m_inactiveColor);
m_scene->addLine(0.5 * m_width, -0.5 * m_width, 0.5 * m_width, 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 // active
pen.setColor(m_activeColor);
m_scene->addLine(0.5 * m_width, (1.0 - percent) * m_height + 0.5 * m_width, 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); 0.5 * m_width, m_height + 0.5 * m_width, m_activePen);
}
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());
} }

View File

@ -19,8 +19,8 @@
#define GRAPHICALITEMHELPER_H #define GRAPHICALITEMHELPER_H
#include <QColor> #include <QColor>
#include <QPixmap>
#include <QObject> #include <QObject>
#include <QPen>
class QGraphicsScene; class QGraphicsScene;
@ -40,7 +40,6 @@ public:
void paintHorizontal(const float &percent); void paintHorizontal(const float &percent);
void paintVertical(const float &percent); void paintVertical(const float &percent);
// additional conversion methods // additional conversion methods
QString colorToString(const QColor &color);
float getPercents(const float &value, const float &min, const float &max); float getPercents(const float &value, const float &min, const float &max);
QColor stringToColor(const QString &color); QColor stringToColor(const QString &color);
@ -48,10 +47,8 @@ private:
void storeValue(const float &value); void storeValue(const float &value);
QGraphicsScene *m_scene = nullptr; QGraphicsScene *m_scene = nullptr;
int m_count = 100; int m_count = 100;
QColor m_activeColor = QColor(0, 0, 0, 130); QPen m_activePen;
QColor m_inactiveColor = QColor(255, 255, 255, 130); QPen m_inactivePen;
QPixmap m_activeImage;
QPixmap m_inactiveImage;
int m_width = 100; int m_width = 100;
int m_height = 100; int m_height = 100;
// list of values which will be used to store data for graph type only // list of values which will be used to store data for graph type only