2013-04-22 23:00:27 +00:00
|
|
|
#include "starwidget.h"
|
2014-10-15 13:30:51 +00:00
|
|
|
#include "metrics.h"
|
2013-04-22 23:00:27 +00:00
|
|
|
#include <QSvgRenderer>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPaintEvent>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QMouseEvent>
|
2013-05-18 23:42:59 +00:00
|
|
|
#include <unistd.h>
|
2013-12-20 13:33:22 +00:00
|
|
|
#include <QStyle>
|
|
|
|
#include <QStyleOption>
|
2014-06-19 21:45:26 +00:00
|
|
|
#include "simplewidgets.h"
|
2013-04-22 23:00:27 +00:00
|
|
|
|
2014-06-19 21:45:26 +00:00
|
|
|
QImage StarWidget::activeStar;
|
|
|
|
QImage StarWidget::inactiveStar;
|
2013-04-22 23:00:27 +00:00
|
|
|
|
2014-06-19 21:45:26 +00:00
|
|
|
const QImage& StarWidget::starActive()
|
2013-04-22 23:00:27 +00:00
|
|
|
{
|
2014-06-19 21:45:26 +00:00
|
|
|
return activeStar;
|
2013-04-22 23:00:27 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 21:45:26 +00:00
|
|
|
const QImage& StarWidget::starInactive()
|
2013-04-22 23:00:27 +00:00
|
|
|
{
|
2014-06-19 21:45:26 +00:00
|
|
|
return inactiveStar;
|
2013-04-22 23:00:27 +00:00
|
|
|
}
|
|
|
|
|
2013-04-27 15:27:27 +00:00
|
|
|
int StarWidget::currentStars() const
|
2013-04-22 23:00:27 +00:00
|
|
|
{
|
2013-04-27 15:27:27 +00:00
|
|
|
return current;
|
2013-04-22 23:00:27 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void StarWidget::mouseReleaseEvent(QMouseEvent *event)
|
2013-04-22 23:00:27 +00:00
|
|
|
{
|
2013-05-23 04:25:05 +00:00
|
|
|
if (readOnly) {
|
2013-05-18 23:42:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-15 13:30:52 +00:00
|
|
|
int starClicked = event->pos().x() / defaultIconMetrics().sz_small + 1;
|
2013-04-27 15:27:27 +00:00
|
|
|
if (starClicked > TOTALSTARS)
|
|
|
|
starClicked = TOTALSTARS;
|
2013-04-22 23:00:27 +00:00
|
|
|
|
|
|
|
if (current == starClicked)
|
|
|
|
current -= 1;
|
|
|
|
else
|
|
|
|
current = starClicked;
|
|
|
|
|
2013-05-18 23:42:59 +00:00
|
|
|
Q_EMIT valueChanged(current);
|
2013-04-22 23:00:27 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void StarWidget::paintEvent(QPaintEvent *event)
|
2013-04-22 23:00:27 +00:00
|
|
|
{
|
|
|
|
QPainter p(this);
|
2014-06-19 21:45:26 +00:00
|
|
|
QPixmap active = QPixmap::fromImage(starActive());
|
|
|
|
QPixmap inactive = QPixmap::fromImage(starInactive());
|
2013-04-22 23:00:27 +00:00
|
|
|
|
2014-10-15 13:30:52 +00:00
|
|
|
const IconMetrics& metrics = defaultIconMetrics();
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
for (int i = 0; i < current; i++)
|
2014-10-15 13:30:52 +00:00
|
|
|
p.drawPixmap(i * metrics.sz_small + metrics.spacing, 0, active);
|
2013-04-22 23:00:27 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
for (int i = current; i < TOTALSTARS; i++)
|
2014-10-15 13:30:52 +00:00
|
|
|
p.drawPixmap(i * metrics.sz_small + metrics.spacing, 0, inactive);
|
2013-12-20 13:33:22 +00:00
|
|
|
|
2014-01-16 04:50:56 +00:00
|
|
|
if (hasFocus()) {
|
2013-12-20 13:33:22 +00:00
|
|
|
QStyleOptionFocusRect option;
|
|
|
|
option.initFrom(this);
|
|
|
|
option.backgroundColor = palette().color(QPalette::Background);
|
|
|
|
style()->drawPrimitive(QStyle::PE_FrameFocusRect, &option, &p, this);
|
|
|
|
}
|
2013-04-22 23:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StarWidget::setCurrentStars(int value)
|
|
|
|
{
|
|
|
|
current = value;
|
|
|
|
update();
|
|
|
|
Q_EMIT valueChanged(current);
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
StarWidget::StarWidget(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f),
|
2013-11-30 17:18:01 +00:00
|
|
|
current(0),
|
|
|
|
readOnly(false)
|
2013-04-22 23:00:27 +00:00
|
|
|
{
|
2014-10-15 13:30:52 +00:00
|
|
|
int dim = defaultIconMetrics().sz_small;
|
2014-10-15 13:30:46 +00:00
|
|
|
|
2014-06-19 21:45:26 +00:00
|
|
|
if (activeStar.isNull()) {
|
2013-04-27 15:27:27 +00:00
|
|
|
QSvgRenderer render(QString(":star"));
|
2014-10-15 13:30:52 +00:00
|
|
|
QPixmap renderedStar(dim, dim);
|
2013-04-22 23:00:27 +00:00
|
|
|
|
2013-04-27 15:27:27 +00:00
|
|
|
renderedStar.fill(Qt::transparent);
|
|
|
|
QPainter painter(&renderedStar);
|
2013-04-22 23:00:27 +00:00
|
|
|
|
2014-10-15 13:30:52 +00:00
|
|
|
render.render(&painter, QRectF(0, 0, dim, dim));
|
2014-06-19 21:45:26 +00:00
|
|
|
activeStar = renderedStar.toImage();
|
2013-04-27 15:27:27 +00:00
|
|
|
}
|
2014-06-19 21:45:26 +00:00
|
|
|
if (inactiveStar.isNull()) {
|
|
|
|
inactiveStar = grayImage(activeStar);
|
2013-04-27 15:27:27 +00:00
|
|
|
}
|
2013-12-20 13:33:22 +00:00
|
|
|
setFocusPolicy(Qt::StrongFocus);
|
2013-04-22 23:00:27 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 21:45:26 +00:00
|
|
|
QImage grayImage(const QImage& coloredImg)
|
2013-04-22 23:00:27 +00:00
|
|
|
{
|
2014-06-19 21:45:26 +00:00
|
|
|
QImage img = coloredImg;
|
2013-04-22 23:00:27 +00:00
|
|
|
for (int i = 0; i < img.width(); ++i) {
|
|
|
|
for (int j = 0; j < img.height(); ++j) {
|
2013-04-27 15:27:27 +00:00
|
|
|
QRgb rgb = img.pixel(i, j);
|
|
|
|
if (!rgb)
|
2013-04-22 23:00:27 +00:00
|
|
|
continue;
|
2013-04-27 15:27:27 +00:00
|
|
|
|
|
|
|
QColor c(rgb);
|
|
|
|
int gray = (c.red() + c.green() + c.blue()) / 3;
|
2013-04-22 23:00:27 +00:00
|
|
|
img.setPixel(i, j, qRgb(gray, gray, gray));
|
|
|
|
}
|
|
|
|
}
|
2013-04-27 15:27:27 +00:00
|
|
|
|
2014-06-19 21:45:26 +00:00
|
|
|
return img;
|
2013-04-22 23:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize StarWidget::sizeHint() const
|
|
|
|
{
|
2014-10-15 13:30:52 +00:00
|
|
|
const IconMetrics& metrics = defaultIconMetrics();
|
|
|
|
return QSize(metrics.sz_small * TOTALSTARS + metrics.spacing * (TOTALSTARS - 1), metrics.sz_small);
|
2013-04-22 23:00:27 +00:00
|
|
|
}
|
2013-05-18 23:42:59 +00:00
|
|
|
|
|
|
|
void StarWidget::setReadOnly(bool r)
|
|
|
|
{
|
|
|
|
readOnly = r;
|
|
|
|
}
|
2013-12-20 13:33:22 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void StarWidget::focusInEvent(QFocusEvent *event)
|
2013-12-20 13:33:22 +00:00
|
|
|
{
|
|
|
|
setFocus();
|
|
|
|
QWidget::focusInEvent(event);
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void StarWidget::focusOutEvent(QFocusEvent *event)
|
2013-12-20 13:33:22 +00:00
|
|
|
{
|
|
|
|
QWidget::focusOutEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void StarWidget::keyPressEvent(QKeyEvent *event)
|
2013-12-20 13:33:22 +00:00
|
|
|
{
|
2014-01-16 04:50:56 +00:00
|
|
|
if (event->key() == Qt::Key_Up || event->key() == Qt::Key_Right) {
|
|
|
|
if (currentStars() < TOTALSTARS) {
|
2014-02-28 04:09:57 +00:00
|
|
|
setCurrentStars(currentStars() + 1);
|
2013-12-20 13:33:22 +00:00
|
|
|
}
|
2014-01-16 04:50:56 +00:00
|
|
|
} else if (event->key() == Qt::Key_Down || event->key() == Qt::Key_Left) {
|
|
|
|
if (currentStars() > 0) {
|
2014-02-28 04:09:57 +00:00
|
|
|
setCurrentStars(currentStars() - 1);
|
2013-12-20 13:33:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|