| 1 | /* |
| 2 | Copyright 2010, David Sansome <[email protected]> |
| 3 | Copyright (C) 2005-2014 Mario Stephan <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU Lesser General Public License as published |
| 7 | by the Free Software Foundation; either version 2.1 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "ratingwidget.h" |
| 20 | |
| 21 | #include <QMouseEvent> |
| 22 | #include <QStylePainter> |
| 23 | #include <QtDebug> |
| 24 | |
| 25 | const int RatingPainter::kStarCount; |
| 26 | const int RatingPainter::kStarSize; |
| 27 | |
| 28 | RatingPainter::RatingPainter() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | QRect RatingPainter::Contents(const QRect& rect) |
| 33 | { |
| 34 | const int width = kStarSize * kStarCount; |
| 35 | return QRect(rect.x(), rect.y(), width, rect.height()); |
| 36 | } |
| 37 | |
| 38 | double RatingPainter::RatingForPos(const QPoint& pos, const QRect& rect) |
| 39 | { |
| 40 | const QRect contents = Contents(rect); |
| 41 | const double raw = double(pos.x() - contents.left()) / contents.width(); |
| 42 | |
| 43 | // Round to the nearest 0.1 |
| 44 | return double(int(raw * kStarCount * 2 + 0.5)) / (kStarCount * 2); |
| 45 | } |
| 46 | |
| 47 | void RatingPainter::Paint(QPainter* painter, const QRect& rect, float rating) const |
| 48 | { |
| 49 | Q_UNUSED(rect); |
| 50 | |
| 51 | // save some time here |
| 52 | if (rating == 0) |
| 53 | return; |
| 54 | |
| 55 | rating *= kStarCount; |
| 56 | QPixmap on(":/star-on.png"); |
| 57 | QPixmap off(":/star-off.png"); |
| 58 | |
| 59 | // Draw the stars |
| 60 | int x = kStarSize / 2; |
| 61 | for (int i = 0; i < kStarCount; i++, x += kStarSize) { |
| 62 | const QRect t_rect(x, 0, kStarSize, kStarSize); |
| 63 | |
| 64 | if (rating - 0.25 <= i) { |
| 65 | // Totally empty |
| 66 | painter->drawPixmap(t_rect, off); |
| 67 | } else if (rating - 0.75 <= i) { |
| 68 | // Half full |
| 69 | const QRect target_left(t_rect.x(), t_rect.y(), kStarSize / 2, kStarSize); |
| 70 | const QRect target_right(t_rect.x() + kStarSize / 2, t_rect.y(), kStarSize / 2, kStarSize); |
| 71 | const QRect source_left(0, 0, kStarSize / 2, kStarSize); |
| 72 | const QRect source_right(kStarSize / 2, 0, kStarSize / 2, kStarSize); |
| 73 | painter->drawPixmap(target_left, on, source_left); |
| 74 | painter->drawPixmap(target_right, off, source_right); |
| 75 | } else { |
| 76 | // Totally full |
| 77 | painter->drawPixmap(t_rect, on); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | RatingWidget::RatingWidget(QWidget* parent) |
| 83 | : QWidget(parent) |
| 84 | , rating_(0.0) |
| 85 | , hover_rating_(-1.0) |
| 86 | { |
| 87 | setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 88 | setMouseTracking(true); |
| 89 | } |
| 90 | |
| 91 | QSize RatingWidget::sizeHint() const |
| 92 | { |
| 93 | return QSize(RatingPainter::kStarSize * (RatingPainter::kStarCount + 2), |
| 94 | RatingPainter::kStarSize); |
| 95 | } |
| 96 | |
| 97 | void RatingWidget::setRating(float rating) |
| 98 | { |
| 99 | rating_ = rating; |
| 100 | update(); |
| 101 | } |
| 102 | |
| 103 | void RatingWidget::paintEvent(QPaintEvent* e) |
| 104 | { |
| 105 | Q_UNUSED(e); |
| 106 | QStylePainter p(this); |
| 107 | |
| 108 | // Draw the stars |
| 109 | painter_.Paint(&p, rect(), hover_rating_ == -1.0 ? rating_ : hover_rating_); |
| 110 | } |
| 111 | |
| 112 | void RatingWidget::mousePressEvent(QMouseEvent* e) |
| 113 | { |
| 114 | rating_ = RatingPainter::RatingForPos(e->pos(), rect()); |
| 115 | emit RatingChanged(rating_); |
| 116 | } |
| 117 | |
| 118 | void RatingWidget::mouseMoveEvent(QMouseEvent* e) |
| 119 | { |
| 120 | hover_rating_ = RatingPainter::RatingForPos(e->pos(), rect()); |
| 121 | update(); |
| 122 | } |
| 123 | |
| 124 | void RatingWidget::leaveEvent(QEvent*) |
| 125 | { |
| 126 | hover_rating_ = -1.0; |
| 127 | update(); |
| 128 | } |