kdelibs/kdeui/widgets/kratingwidget.cpp

227 lines
5.3 KiB
C++
Raw Normal View History

2014-11-13 01:04:59 +02:00
/*
* This file is part of the Nepomuk KDE project.
* Copyright (C) 2006-2007 Sebastian Trueg <trueg@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kratingwidget.h"
#include "kratingpainter.h"
#include <QtGui/QPainter>
#include <QtGui/QPixmap>
#include <QtGui/QPaintEvent>
2014-11-13 01:04:59 +02:00
#include <QtGui/QImage>
#include <QtGui/QIcon>
class KRatingWidget::Private
{
public:
Private()
: rating(0),
hoverRating(-1),
pixSize(16)
{
2014-11-13 01:04:59 +02:00
}
int rating;
int hoverRating;
int pixSize;
KRatingPainter ratingPainter;
};
KRatingWidget::KRatingWidget(QWidget *parent)
: QFrame(parent),
d(new Private())
2014-11-13 01:04:59 +02:00
{
setMouseTracking(true);
2014-11-13 01:04:59 +02:00
}
KRatingWidget::~KRatingWidget()
{
delete d;
}
void KRatingWidget::setIcon(const QIcon &icon)
2014-11-13 01:04:59 +02:00
{
d->ratingPainter.setIcon(icon);
2014-11-13 01:04:59 +02:00
update();
}
void KRatingWidget::setPixmapSize(int size)
2014-11-13 01:04:59 +02:00
{
d->pixSize = size;
updateGeometry();
}
int KRatingWidget::spacing() const
{
return d->ratingPainter.spacing();
}
QIcon KRatingWidget::icon() const
{
return d->ratingPainter.icon();
}
void KRatingWidget::setSpacing(int s)
2014-11-13 01:04:59 +02:00
{
d->ratingPainter.setSpacing(s);
2014-11-13 01:04:59 +02:00
update();
}
Qt::Alignment KRatingWidget::alignment() const
{
return d->ratingPainter.alignment();
}
void KRatingWidget::setAlignment(Qt::Alignment align)
2014-11-13 01:04:59 +02:00
{
d->ratingPainter.setAlignment(align);
2014-11-13 01:04:59 +02:00
update();
}
Qt::LayoutDirection KRatingWidget::layoutDirection() const
{
return d->ratingPainter.layoutDirection();
}
void KRatingWidget::setLayoutDirection(Qt::LayoutDirection direction)
2014-11-13 01:04:59 +02:00
{
d->ratingPainter.setLayoutDirection(direction);
2014-11-13 01:04:59 +02:00
update();
}
unsigned int KRatingWidget::rating() const
{
return d->rating;
}
int KRatingWidget::maxRating() const
{
return d->ratingPainter.maxRating();
}
bool KRatingWidget::halfStepsEnabled() const
{
return d->ratingPainter.halfStepsEnabled();
}
void KRatingWidget::setRating(int rating)
2014-11-13 01:04:59 +02:00
{
if (rating != d->rating) {
2014-11-13 01:04:59 +02:00
d->rating = rating;
d->hoverRating = rating;
emit ratingChanged(rating);
emit ratingChanged((unsigned int)rating);
2014-11-13 01:04:59 +02:00
update();
}
}
void KRatingWidget::setMaxRating(int max)
2014-11-13 01:04:59 +02:00
{
d->ratingPainter.setMaxRating(max);
2014-11-13 01:04:59 +02:00
update();
}
void KRatingWidget::setHalfStepsEnabled(bool enabled)
2014-11-13 01:04:59 +02:00
{
d->ratingPainter.setHalfStepsEnabled(enabled);
2014-11-13 01:04:59 +02:00
update();
}
void KRatingWidget::mousePressEvent(QMouseEvent *e)
2014-11-13 01:04:59 +02:00
{
if (e->button() == Qt::LeftButton) {
2014-11-13 01:04:59 +02:00
const int prevRating = d->rating;
d->hoverRating = d->ratingPainter.ratingFromPosition(contentsRect(), e->pos());
if (!(d->hoverRating % 2)) {
if (d->hoverRating == prevRating + 1) {
2014-11-13 01:04:59 +02:00
setRating( d->hoverRating - 2 );
} else if (d->hoverRating == prevRating) {
setRating( d->hoverRating - 1);
} else {
setRating(d->hoverRating);
2014-11-13 01:04:59 +02:00
}
} else {
if (d->hoverRating == prevRating - 1) {
setRating(d->hoverRating);
} else if (d->hoverRating == prevRating) {
setRating(d->hoverRating - 1);
} else {
setRating(d->hoverRating + 1);
2014-11-13 01:04:59 +02:00
}
}
}
}
void KRatingWidget::mouseMoveEvent(QMouseEvent *e)
2014-11-13 01:04:59 +02:00
{
// when moving the mouse we show the user what the result of clicking will be
const int prevHoverRating = d->hoverRating;
d->hoverRating = d->ratingPainter.ratingFromPosition(contentsRect(), e->pos());
if (!(d->hoverRating % 2)) {
if (d->hoverRating == prevHoverRating + 1) {
2014-11-13 01:04:59 +02:00
d->hoverRating -= 2;
} else if (d->hoverRating == prevHoverRating) {
2014-11-13 01:04:59 +02:00
d->hoverRating -= 1;
}
} else {
if (d->hoverRating == prevHoverRating) {
2014-11-13 01:04:59 +02:00
d->hoverRating -= 1;
} else {
2014-11-13 01:04:59 +02:00
d->hoverRating += 1;
}
}
if (d->hoverRating != prevHoverRating) {
2014-11-13 01:04:59 +02:00
update();
}
}
void KRatingWidget::leaveEvent(QEvent *e)
2014-11-13 01:04:59 +02:00
{
Q_UNUSED(e);
2014-11-13 01:04:59 +02:00
d->hoverRating = -1;
update();
}
void KRatingWidget::paintEvent(QPaintEvent *e)
2014-11-13 01:04:59 +02:00
{
QFrame::paintEvent(e);
QPainter p(this);
d->ratingPainter.setEnabled(isEnabled());
d->ratingPainter.paint(&p, contentsRect(), d->rating, d->hoverRating);
2014-11-13 01:04:59 +02:00
}
QSize KRatingWidget::sizeHint() const
{
int numPix = d->ratingPainter.maxRating();
if (d->ratingPainter.halfStepsEnabled()) {
2014-11-13 01:04:59 +02:00
numPix /= 2;
}
const QSize pixSize(d->pixSize, d->pixSize);
return QSize(
pixSize.width() * numPix + spacing() * (numPix - 1) + frameWidth() * 2,
pixSize.height() + frameWidth() * 2
);
2014-11-13 01:04:59 +02:00
}
#include "moc_kratingwidget.cpp"