Add a rating widget, use it in the smart playlist dialog, refactor the playlist delegate to use it.

This commit is contained in:
David Sansome 2010-10-26 19:59:55 +00:00
parent 9b5b4ef4c6
commit fb224608ae
9 changed files with 267 additions and 73 deletions

View File

@ -195,6 +195,7 @@ set(SOURCES
widgets/prettyimage.cpp
widgets/prettyimageview.cpp
widgets/progressitemdelegate.cpp
widgets/ratingwidget.cpp
widgets/sliderwidget.cpp
widgets/spinbox.cpp
widgets/stickyslider.cpp
@ -349,6 +350,7 @@ set(HEADERS
widgets/prettyimage.h
widgets/prettyimageview.h
widgets/progressitemdelegate.h
widgets/ratingwidget.h
widgets/sliderwidget.h
widgets/spinbox.h
widgets/stickyslider.h

View File

@ -42,9 +42,6 @@ const float QueuedItemDelegate::kQueueOpacityLowerBound = 0.4;
const int PlaylistDelegateBase::kMinHeight = 19;
const int RatingItemDelegate::kStarCount;
const int RatingItemDelegate::kStarSize;
QueuedItemDelegate::QueuedItemDelegate(QObject *parent, int indicator_column)
: QStyledItemDelegate(parent),
indicator_column_(indicator_column)
@ -292,56 +289,6 @@ QWidget* TextItemDelegate::createEditor(
RatingItemDelegate::RatingItemDelegate(QObject* parent)
: PlaylistDelegateBase(parent)
{
// 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 ; i<kStarCount*2+1 ; ++i) {
const float rating = float(i) / 2.0;
// Clear the pixmap
stars_[i] = QPixmap(kStarSize * kStarCount, kStarSize);
stars_[i].fill(Qt::transparent);
QPainter p(&stars_[i]);
// Draw the stars
int x = 0;
for (int i=0 ; i<kStarCount ; ++i, x+=kStarSize) {
const QRect rect(x, 0, kStarSize, kStarSize);
if (rating - 0.25 <= i) {
// Totally empty
p.drawPixmap(rect, off);
} else if (rating - 0.75 <= i) {
// Half full
const QRect target_left(rect.x(), rect.y(), kStarSize/2, kStarSize);
const QRect target_right(rect.x() + kStarSize/2, rect.y(), kStarSize/2, kStarSize);
const QRect source_left(0, 0, kStarSize/2, kStarSize);
const QRect source_right(kStarSize/2, 0, kStarSize/2, kStarSize);
p.drawPixmap(target_left, on, source_left);
p.drawPixmap(target_right, off, source_right);
} else {
// Totally full
p.drawPixmap(rect, on);
}
}
}
}
QRect RatingItemDelegate::ContentRect(const QRect& total) {
const int width = total.height() * kStarCount;
const int x = total.x() + (total.width() - width) / 2;
return QRect(x, total.y(), width, total.height());
}
double RatingItemDelegate::RatingForPos(const QPoint& pos, const QRect& total_rect) {
const QRect contents = ContentRect(total_rect);
const double raw = double(pos.x() - contents.left()) / contents.width();
// Round to the nearest 0.1
return double(int(raw * kStarCount * 2 + 0.5)) / (kStarCount * 2);
}
void RatingItemDelegate::paint(
@ -356,23 +303,18 @@ void RatingItemDelegate::paint(
if (!index.data(Playlist::Role_CanSetRating).toBool())
return;
QSize size(qMin(kStarSize*kStarCount, option.rect.width()),
qMin(kStarSize, option.rect.height()));
QPoint pos(option.rect.center() - QPoint(size.width() / 2, size.height() / 2));
const bool hover = mouse_over_index_ == index;
const double rating = hover ? double(mouse_over_pos_.x() - pos.x()) / kStarSize
: index.data().toDouble() * kStarCount;
const double rating =
(hover ? RatingPainter::RatingForPos(mouse_over_pos_, option.rect)
: index.data().toDouble());
// Draw the stars
const int star = qBound(0, int(rating*2.0 + 0.5), kStarCount*2);
painter->drawPixmap(QRect(pos, size), stars_[star], QRect(QPoint(0,0), size));
painter_.Paint(painter, option.rect, rating);
}
QSize RatingItemDelegate::sizeHint(
const QStyleOptionViewItem& option, const QModelIndex& index) const {
QSize size = PlaylistDelegateBase::sizeHint(option, index);
size.setWidth(size.height() * kStarCount);
size.setWidth(size.height() * RatingPainter::kStarCount);
return size;
}
@ -382,7 +324,7 @@ QString RatingItemDelegate::displayText(
return QString();
// Round to the nearest 0.5
const double rating = float(int(value.toDouble() * kStarCount * 2 + 0.5)) / 2;
const double rating = float(int(value.toDouble() * RatingPainter::kStarCount * 2 + 0.5)) / 2;
return QString::number(rating, 'f', 1);
}

View File

@ -19,6 +19,7 @@
#include "playlist.h"
#include "library/library.h"
#include "widgets/ratingwidget.h"
#include <QStyledItemDelegate>
#include <QTreeView>
@ -117,14 +118,8 @@ public:
bool is_mouse_over() const { return mouse_over_index_.isValid(); }
QModelIndex mouse_over_index() const { return mouse_over_index_; }
static QRect ContentRect(const QRect& total);
static double RatingForPos(const QPoint& pos, const QRect& total_rect);
static const int kStarCount = 5;
static const int kStarSize = 15;
private:
QPixmap stars_[kStarCount*2+1];
RatingPainter painter_;
QModelIndex mouse_over_index_;
QPoint mouse_over_pos_;

View File

@ -523,7 +523,7 @@ void PlaylistView::mousePressEvent(QMouseEvent* event) {
if (event->button() == Qt::LeftButton && index.isValid() &&
index.data(Playlist::Role_CanSetRating).toBool()) {
// Calculate which star was clicked
double new_rating = RatingItemDelegate::RatingForPos(
double new_rating = RatingPainter::RatingForPos(
event->pos(), visualRect(index));
emit SongRatingSet(index, new_rating);
} else {

View File

@ -104,7 +104,9 @@ void SmartPlaylistSearchTermWidget::FieldChanged(int index) {
// Populate the operator combo box
ui_->op->clear();
foreach (SmartPlaylistSearchTerm::Operator op, SmartPlaylistSearchTerm::OperatorsForType(type)) {
const int i = ui_->op->count();
ui_->op->addItem(SmartPlaylistSearchTerm::OperatorText(type, op));
ui_->op->setItemData(i, op);
}
// Show the correct value editor
@ -113,7 +115,7 @@ void SmartPlaylistSearchTermWidget::FieldChanged(int index) {
case SmartPlaylistSearchTerm::Type_Time: page = ui_->page_time; break;
case SmartPlaylistSearchTerm::Type_Number: page = ui_->page_number; break;
case SmartPlaylistSearchTerm::Type_Date: page = ui_->page_date; break;
case SmartPlaylistSearchTerm::Type_Rating: page = ui_->page_number; break; // TODO
case SmartPlaylistSearchTerm::Type_Rating: page = ui_->page_rating; break;
case SmartPlaylistSearchTerm::Type_Text: page = ui_->page_text; break;
}
ui_->value_stack->setCurrentWidget(page);
@ -190,6 +192,31 @@ float SmartPlaylistSearchTermWidget::overlay_opacity() const {
return overlay_ ? overlay_->opacity() : 0.0;
}
SmartPlaylistSearchTerm SmartPlaylistSearchTermWidget::Term() const {
const int field = ui_->field->itemData(ui_->field->currentIndex()).toInt();
const int op = ui_->op->itemData(ui_->op->currentIndex()).toInt();
SmartPlaylistSearchTerm ret;
ret.field_ = SmartPlaylistSearchTerm::Field(field);
ret.operator_ = SmartPlaylistSearchTerm::Operator(op);
// The value depends on the data type
const QWidget* value_page = ui_->value_stack->currentWidget();
if (value_page == ui_->page_text) {
ret.value_ = ui_->value_text->text();
} else if (value_page == ui_->page_number) {
ret.value_ = ui_->value_number->value();
} else if (value_page == ui_->page_date) {
ret.value_ = ui_->value_date->dateTime().toTime_t();
} else if (value_page == ui_->page_time) {
ret.value_ = QTime(0,0).secsTo(ui_->value_time->time());
} else if (value_page == ui_->page_rating) {
ret.value_ = ui_->value_rating->rating();
}
return ret;
}
SmartPlaylistSearchTermWidget::Overlay::Overlay(SmartPlaylistSearchTermWidget* parent)

View File

@ -17,6 +17,8 @@
#ifndef SMARTPLAYLISTSEARCHTERMWIDGET_H
#define SMARTPLAYLISTSEARCHTERMWIDGET_H
#include "smartplaylistsearchterm.h"
#include <QPushButton>
#include <QWidget>
@ -40,6 +42,8 @@ public:
float overlay_opacity() const;
void set_overlay_opacity(float opacity);
SmartPlaylistSearchTerm Term() const;
signals:
void Clicked();
void RemoveClicked();

View File

@ -78,6 +78,19 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="page_rating">
<layout class="QVBoxLayout" name="verticalLayout_6">
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="RatingWidget" name="value_rating" native="true"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_time">
<layout class="QVBoxLayout" name="verticalLayout_5">
<property name="spacing">
@ -164,6 +177,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>RatingWidget</class>
<extends>QWidget</extends>
<header>widgets/ratingwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -0,0 +1,141 @@
/* This file is part of Clementine.
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 <http://www.gnu.org/licenses/>.
*/
#include "ratingwidget.h"
#include <QMouseEvent>
#include <QStyleOptionFrameV3>
#include <QStylePainter>
#include <QtDebug>
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 ; i<kStarCount*2+1 ; ++i) {
const float rating = float(i) / 2.0;
// Clear the pixmap
stars_[i] = QPixmap(kStarSize * kStarCount, kStarSize);
stars_[i].fill(Qt::transparent);
QPainter p(&stars_[i]);
// Draw the stars
int x = 0;
for (int i=0 ; i<kStarCount ; ++i, x+=kStarSize) {
const QRect rect(x, 0, kStarSize, kStarSize);
if (rating - 0.25 <= i) {
// Totally empty
p.drawPixmap(rect, off);
} else if (rating - 0.75 <= i) {
// Half full
const QRect target_left(rect.x(), rect.y(), kStarSize/2, kStarSize);
const QRect target_right(rect.x() + kStarSize/2, rect.y(), kStarSize/2, kStarSize);
const QRect source_left(0, 0, kStarSize/2, kStarSize);
const QRect source_right(kStarSize/2, 0, kStarSize/2, kStarSize);
p.drawPixmap(target_left, on, source_left);
p.drawPixmap(target_right, off, source_right);
} else {
// Totally full
p.drawPixmap(rect, on);
}
}
}
}
QRect RatingPainter::Contents(const QRect& rect) {
const int width = kStarSize * kStarCount;
const int x = rect.x() + (rect.width() - width) / 2;
return QRect(x, rect.y(), width, rect.height());
}
double RatingPainter::RatingForPos(const QPoint& pos, const QRect& rect) {
const QRect contents = Contents(rect);
const double raw = double(pos.x() - contents.left()) / contents.width();
// Round to the nearest 0.1
return double(int(raw * kStarCount * 2 + 0.5)) / (kStarCount * 2);
}
void RatingPainter::Paint(QPainter* painter, const QRect& rect, float rating) const {
QSize size(qMin(kStarSize*kStarCount, rect.width()),
qMin(kStarSize, rect.height()));
QPoint pos(rect.center() - QPoint(size.width() / 2, size.height() / 2));
rating *= kStarCount;
// Draw the stars
const int star = qBound(0, int(rating*2.0 + 0.5), kStarCount*2);
painter->drawPixmap(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::MinimumExpanding, QSizePolicy::MinimumExpanding);
setMouseTracking(true);
}
QSize RatingWidget::sizeHint() const {
return QSize(RatingPainter::kStarSize * RatingPainter::kStarCount,
RatingPainter::kStarSize);
}
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());
}
void RatingWidget::mouseMoveEvent(QMouseEvent* e) {
hover_rating_ = RatingPainter::RatingForPos(e->pos(), rect());
update();
}
void RatingWidget::leaveEvent(QEvent*) {
hover_rating_ = -1.0;
update();
}

View File

@ -0,0 +1,62 @@
/* This file is part of Clementine.
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 <http://www.gnu.org/licenses/>.
*/
#ifndef RATINGWIDGET_H
#define RATINGWIDGET_H
#include <QFrame>
#include <QPixmap>
class RatingPainter {
public:
RatingPainter();
static const int kStarCount = 5;
static const int kStarSize = 15;
static QRect Contents(const QRect& rect);
static double RatingForPos(const QPoint& pos, const QRect& rect);
void Paint(QPainter* painter, const QRect& rect, float rating) const;
private:
QPixmap stars_[kStarCount*2+1];
};
class RatingWidget : public QWidget {
Q_OBJECT
Q_PROPERTY(float rating READ rating WRITE set_rating);
public:
RatingWidget(QWidget* parent = 0);
QSize sizeHint() const;
float rating() const { return rating_; }
void set_rating(float rating);
protected:
void paintEvent(QPaintEvent*);
void mousePressEvent(QMouseEvent* e);
void mouseMoveEvent(QMouseEvent* e);
void leaveEvent(QEvent*);
private:
RatingPainter painter_;
float rating_;
float hover_rating_;
};
#endif // RATINGWIDGET_H