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