Change the Star Widget to use QImage instead of QPixmap

Also, clean a few calls, this should make the widget a tiny
bit faster. This patch also moves the grayImage function from
the star widget to the global scope, so I can use it on the
Calendar widget.

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-06-19 18:45:26 -03:00 committed by Dirk Hohndel
parent f3f03e2ee8
commit be462ae1a6
5 changed files with 30 additions and 25 deletions

View file

@ -47,10 +47,13 @@ void StarWidgetsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o
int deltaY = option.rect.height() / 2 - StarWidget::starActive().height() / 2; int deltaY = option.rect.height() / 2 - StarWidget::starActive().height() / 2;
painter->save(); painter->save();
painter->setRenderHint(QPainter::Antialiasing, true); painter->setRenderHint(QPainter::Antialiasing, true);
const QPixmap active = QPixmap::fromImage(StarWidget::starActive());
const QPixmap inactive = QPixmap::fromImage(StarWidget::starInactive());
for (int i = 0; i < rating; i++) for (int i = 0; i < rating; i++)
painter->drawPixmap(option.rect.x() + i * IMG_SIZE + SPACING, option.rect.y() + deltaY, StarWidget::starActive()); painter->drawPixmap(option.rect.x() + i * IMG_SIZE + SPACING, option.rect.y() + deltaY, active);
for (int i = rating; i < TOTALSTARS; i++) for (int i = rating; i < TOTALSTARS; i++)
painter->drawPixmap(option.rect.x() + i * IMG_SIZE + SPACING, option.rect.y() + deltaY, StarWidget::starInactive()); painter->drawPixmap(option.rect.x() + i * IMG_SIZE + SPACING, option.rect.y() + deltaY, inactive);
painter->restore(); painter->restore();
} }

View file

@ -332,7 +332,8 @@ void DateWidget::paintEvent(QPaintEvent *event)
static int pixRedTag = 18; /* calculated using a ruler. */ static int pixRedTag = 18; /* calculated using a ruler. */
QPainter painter(this); QPainter painter(this);
painter.drawPixmap(QPoint(0,0), pix);
painter.drawPixmap(QPoint(0,0), isEnabled() ? pix : QPixmap::fromImage(grayImage(pix.toImage())));
QString month = mDate.toString("MMM"); QString month = mDate.toString("MMM");
QString year = mDate.toString("yyyy"); QString year = mDate.toString("yyyy");

View file

@ -111,5 +111,6 @@ private:
}; };
bool isGnome3Session(); bool isGnome3Session();
QImage grayImage(const QImage& coloredImg);
#endif // SIMPLEWIDGETS_H #endif // SIMPLEWIDGETS_H

View file

@ -7,18 +7,19 @@
#include <unistd.h> #include <unistd.h>
#include <QStyle> #include <QStyle>
#include <QStyleOption> #include <QStyleOption>
#include "simplewidgets.h"
QPixmap *StarWidget::activeStar = 0; QImage StarWidget::activeStar;
QPixmap *StarWidget::inactiveStar = 0; QImage StarWidget::inactiveStar;
QPixmap StarWidget::starActive() const QImage& StarWidget::starActive()
{ {
return (*activeStar); return activeStar;
} }
QPixmap StarWidget::starInactive() const QImage& StarWidget::starInactive()
{ {
return (*inactiveStar); return inactiveStar;
} }
int StarWidget::currentStars() const int StarWidget::currentStars() const
@ -48,12 +49,14 @@ void StarWidget::mouseReleaseEvent(QMouseEvent *event)
void StarWidget::paintEvent(QPaintEvent *event) void StarWidget::paintEvent(QPaintEvent *event)
{ {
QPainter p(this); QPainter p(this);
QPixmap active = QPixmap::fromImage(starActive());
QPixmap inactive = QPixmap::fromImage(starInactive());
for (int i = 0; i < current; i++) for (int i = 0; i < current; i++)
p.drawPixmap(i * IMG_SIZE + SPACING, 0, starActive()); p.drawPixmap(i * IMG_SIZE + SPACING, 0, active);
for (int i = current; i < TOTALSTARS; i++) for (int i = current; i < TOTALSTARS; i++)
p.drawPixmap(i * IMG_SIZE + SPACING, 0, starInactive()); p.drawPixmap(i * IMG_SIZE + SPACING, 0, inactive);
if (hasFocus()) { if (hasFocus()) {
QStyleOptionFocusRect option; QStyleOptionFocusRect option;
@ -74,8 +77,7 @@ StarWidget::StarWidget(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f),
current(0), current(0),
readOnly(false) readOnly(false)
{ {
if (!activeStar) { if (activeStar.isNull()) {
activeStar = new QPixmap();
QSvgRenderer render(QString(":star")); QSvgRenderer render(QString(":star"));
QPixmap renderedStar(IMG_SIZE, IMG_SIZE); QPixmap renderedStar(IMG_SIZE, IMG_SIZE);
@ -83,18 +85,17 @@ StarWidget::StarWidget(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f),
QPainter painter(&renderedStar); QPainter painter(&renderedStar);
render.render(&painter, QRectF(0, 0, IMG_SIZE, IMG_SIZE)); render.render(&painter, QRectF(0, 0, IMG_SIZE, IMG_SIZE));
(*activeStar) = renderedStar; activeStar = renderedStar.toImage();
} }
if (!inactiveStar) { if (inactiveStar.isNull()) {
inactiveStar = new QPixmap(); inactiveStar = grayImage(activeStar);
(*inactiveStar) = grayImage(activeStar);
} }
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
} }
QPixmap StarWidget::grayImage(QPixmap *coloredImg) QImage grayImage(const QImage& coloredImg)
{ {
QImage img = coloredImg->toImage(); QImage img = coloredImg;
for (int i = 0; i < img.width(); ++i) { for (int i = 0; i < img.width(); ++i) {
for (int j = 0; j < img.height(); ++j) { for (int j = 0; j < img.height(); ++j) {
QRgb rgb = img.pixel(i, j); QRgb rgb = img.pixel(i, j);
@ -107,7 +108,7 @@ QPixmap StarWidget::grayImage(QPixmap *coloredImg)
} }
} }
return QPixmap::fromImage(img); return img;
} }
QSize StarWidget::sizeHint() const QSize StarWidget::sizeHint() const

View file

@ -17,8 +17,8 @@ public:
/*reimp*/ QSize sizeHint() const; /*reimp*/ QSize sizeHint() const;
static QPixmap starActive(); static const QImage& starActive();
static QPixmap starInactive(); static const QImage& starInactive();
signals: signals:
void valueChanged(int stars); void valueChanged(int stars);
@ -39,9 +39,8 @@ private:
int current; int current;
bool readOnly; bool readOnly;
static QPixmap *activeStar; static QImage activeStar;
static QPixmap *inactiveStar; static QImage inactiveStar;
QPixmap grayImage(QPixmap *coloredImg);
}; };
#endif // STARWIDGET_H #endif // STARWIDGET_H