/* This file is part of Clementine. Copyright 2010, David Sansome Clementine is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Clementine is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Clementine. If not, see . */ #include "ratingwidget.h" #include #include #include #include const int RatingPainter::kStarCount; const int RatingPainter::kStarSize; RatingPainter::RatingPainter() { // Load the base pixmaps QPixmap on(":/star-on.png"); QPixmap off(":/star-off.png"); // Generate the 10 states, better to do it now than on the fly for (int i=0 ; idrawPixmap(QRect(pos, size), stars_[star], QRect(QPoint(0,0), size)); } RatingWidget::RatingWidget(QWidget* parent) : QWidget(parent), rating_(0.0), hover_rating_(-1.0) { setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); setMouseTracking(true); } QSize RatingWidget::sizeHint() const { const int frame_width = 1 + style()->pixelMetric(QStyle::PM_DefaultFrameWidth); return QSize(RatingPainter::kStarSize * (RatingPainter::kStarCount+2) + frame_width*2, RatingPainter::kStarSize + frame_width*2); } void RatingWidget::set_rating(float rating) { rating_ = rating; update(); } void RatingWidget::paintEvent(QPaintEvent* e) { QStylePainter p(this); // Draw the background QStyleOptionFrameV3 opt; opt.initFrom(this); opt.state |= QStyle::State_Sunken; opt.frameShape = QFrame::StyledPanel; opt.lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, this); opt.midLineWidth = 0; p.drawPrimitive(QStyle::PE_PanelLineEdit, opt); // Draw the stars painter_.Paint(&p, rect(), hover_rating_ == -1.0 ? rating_ : hover_rating_); } void RatingWidget::mousePressEvent(QMouseEvent* e) { rating_ = RatingPainter::RatingForPos(e->pos(), rect()); emit RatingChanged(rating_); } void RatingWidget::mouseMoveEvent(QMouseEvent* e) { hover_rating_ = RatingPainter::RatingForPos(e->pos(), rect()); update(); } void RatingWidget::leaveEvent(QEvent*) { hover_rating_ = -1.0; update(); }