kdeui: drop support for setting custom pixmap for KRatingPainter

KRatingPainter and KRatingWidget are currently not used but holding a
reference to a pixmap that can already be (and is) obtained from the icon
(constructing QIcon from pixmap is a thing) is just redundant

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2023-08-12 14:57:01 +03:00
parent 12b8b0b3ae
commit f197e144ca
4 changed files with 201 additions and 298 deletions

View file

@ -37,14 +37,15 @@ class KRatingPainter::Private
public: public:
Private() Private()
: maxRating(10), : maxRating(10),
isEnabled( true ), isEnabled(true),
bHalfSteps(true), bHalfSteps(true),
alignment(Qt::AlignCenter), alignment(Qt::AlignCenter),
direction(Qt::LeftToRight), direction(Qt::LeftToRight),
spacing(0) { spacing(0)
{
} }
QPixmap getPixmap( int size ); QPixmap getPixmap(int size);
int maxRating; int maxRating;
QIcon icon; QIcon icon;
@ -52,23 +53,16 @@ public:
bool bHalfSteps; bool bHalfSteps;
Qt::Alignment alignment; Qt::Alignment alignment;
Qt::LayoutDirection direction; Qt::LayoutDirection direction;
QPixmap customPixmap;
int spacing; int spacing;
}; };
QPixmap KRatingPainter::Private::getPixmap(int size)
QPixmap KRatingPainter::Private::getPixmap( int size )
{ {
if ( !customPixmap.isNull() ) { QIcon _icon(icon);
return customPixmap.scaled( QSize( size, size ) ); if (_icon.isNull()) {
} _icon = KIcon("rating");
else {
QIcon _icon( icon );
if ( _icon.isNull() ) {
_icon = KIcon( "rating" );
}
return _icon.pixmap( size );
} }
return _icon.pixmap(size);
} }
@ -77,115 +71,88 @@ KRatingPainter::KRatingPainter()
{ {
} }
KRatingPainter::~KRatingPainter() KRatingPainter::~KRatingPainter()
{ {
delete d; delete d;
} }
int KRatingPainter::maxRating() const int KRatingPainter::maxRating() const
{ {
return d->maxRating; return d->maxRating;
} }
bool KRatingPainter::halfStepsEnabled() const bool KRatingPainter::halfStepsEnabled() const
{ {
return d->bHalfSteps; return d->bHalfSteps;
} }
Qt::Alignment KRatingPainter::alignment() const Qt::Alignment KRatingPainter::alignment() const
{ {
return d->alignment; return d->alignment;
} }
Qt::LayoutDirection KRatingPainter::layoutDirection() const Qt::LayoutDirection KRatingPainter::layoutDirection() const
{ {
return d->direction; return d->direction;
} }
QIcon KRatingPainter::icon() const QIcon KRatingPainter::icon() const
{ {
return d->icon; return d->icon;
} }
bool KRatingPainter::isEnabled() const bool KRatingPainter::isEnabled() const
{ {
return d->isEnabled; return d->isEnabled;
} }
QPixmap KRatingPainter::customPixmap() const
{
return d->customPixmap;
}
int KRatingPainter::spacing() const int KRatingPainter::spacing() const
{ {
return d->spacing; return d->spacing;
} }
void KRatingPainter::setMaxRating(int max)
void KRatingPainter::setMaxRating( int max )
{ {
d->maxRating = max; d->maxRating = max;
} }
void KRatingPainter::setHalfStepsEnabled(bool enabled)
void KRatingPainter::setHalfStepsEnabled( bool enabled )
{ {
d->bHalfSteps = enabled; d->bHalfSteps = enabled;
} }
void KRatingPainter::setAlignment(Qt::Alignment align)
void KRatingPainter::setAlignment( Qt::Alignment align )
{ {
d->alignment = align; d->alignment = align;
} }
void KRatingPainter::setLayoutDirection(Qt::LayoutDirection direction)
void KRatingPainter::setLayoutDirection( Qt::LayoutDirection direction )
{ {
d->direction = direction; d->direction = direction;
} }
void KRatingPainter::setIcon( const QIcon& icon ) void KRatingPainter::setIcon(const QIcon &icon)
{ {
d->icon = icon; d->icon = icon;
} }
void KRatingPainter::setEnabled(bool enabled)
void KRatingPainter::setEnabled( bool enabled )
{ {
d->isEnabled = enabled; d->isEnabled = enabled;
} }
void KRatingPainter::setSpacing(int s)
void KRatingPainter::setCustomPixmap( const QPixmap& pixmap )
{ {
d->customPixmap = pixmap; d->spacing = qMax(0, s);
} }
void KRatingPainter::paint(QPainter* painter, const QRect &rect, int rating, int hoverRating) const
void KRatingPainter::setSpacing( int s )
{ {
d->spacing = qMax( 0, s ); rating = qMin(rating, d->maxRating);
} hoverRating = qMin(hoverRating, d->maxRating);
int numUsedStars = (d->bHalfSteps ? d->maxRating / 2 : d->maxRating);
void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, int hoverRating ) const
{
rating = qMin( rating, d->maxRating );
hoverRating = qMin( hoverRating, d->maxRating );
int numUsedStars = d->bHalfSteps ? d->maxRating/2 : d->maxRating;
if ( hoverRating > 0 && hoverRating < rating ) { if ( hoverRating > 0 && hoverRating < rating ) {
int tmp = hoverRating; int tmp = hoverRating;
@ -196,10 +163,10 @@ void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, in
int usedSpacing = d->spacing; int usedSpacing = d->spacing;
// get the rating pixmaps // get the rating pixmaps
int maxHSizeOnePix = ( rect.width() - (numUsedStars-1)*usedSpacing ) / numUsedStars; int maxHSizeOnePix = ((rect.width() - (numUsedStars - 1) * usedSpacing ) / numUsedStars);
QPixmap ratingPix = d->getPixmap( qMin( rect.height(), maxHSizeOnePix ) ); QPixmap ratingPix = d->getPixmap(qMin(rect.height(), maxHSizeOnePix));
QPixmap disabledRatingPix = KIconEffect::apply( ratingPix, KIconEffect::ToGray, 1.0, QColor(), QColor(), false ); QPixmap disabledRatingPix = KIconEffect::apply(ratingPix, KIconEffect::ToGray, 1.0, QColor(), QColor(), false);
QPixmap hoverPix; QPixmap hoverPix;
// if we are disabled we become gray and more transparent // if we are disabled we become gray and more transparent
@ -208,58 +175,60 @@ void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, in
KIconEffect::semiTransparent( disabledRatingPix ); KIconEffect::semiTransparent( disabledRatingPix );
} }
bool half = d->bHalfSteps && rating%2; bool half = (d->bHalfSteps && (rating % 2));
int numRatingStars = d->bHalfSteps ? rating/2 : rating; int numRatingStars = (d->bHalfSteps ? rating / 2 : rating);
int numHoverStars = 0; int numHoverStars = 0;
bool halfHover = false; bool halfHover = false;
if ( hoverRating > 0 && rating != hoverRating && d->isEnabled ) { if (hoverRating > 0 && rating != hoverRating && d->isEnabled) {
numHoverStars = d->bHalfSteps ? hoverRating/2 : hoverRating; numHoverStars = d->bHalfSteps ? hoverRating / 2 : hoverRating;
halfHover = d->bHalfSteps && hoverRating%2; halfHover = d->bHalfSteps && (hoverRating % 2);
hoverPix = KIconEffect::apply( ratingPix, KIconEffect::ToGray, 0.5, QColor(), QColor(), false ); hoverPix = KIconEffect::apply(ratingPix, KIconEffect::ToGray, 0.5, QColor(), QColor(), false);
} }
if ( d->alignment & Qt::AlignJustify ) { if (d->alignment & Qt::AlignJustify) {
int w = rect.width(); int w = rect.width();
w -= numUsedStars * ratingPix.width(); w -= numUsedStars * ratingPix.width();
usedSpacing = w / ( numUsedStars-1 ); usedSpacing = w / (numUsedStars - 1);
} }
int ratingAreaWidth = ratingPix.width()*numUsedStars + usedSpacing*(numUsedStars-1); int ratingAreaWidth = ratingPix.width()*numUsedStars + usedSpacing*(numUsedStars-1);
int i = 0; int i = 0;
int x = rect.x(); int x = rect.x();
if ( d->alignment & Qt::AlignRight ) { if (d->alignment & Qt::AlignRight) {
x += ( rect.width() - ratingAreaWidth ); x += ( rect.width() - ratingAreaWidth);
} } else if (d->alignment & Qt::AlignHCenter) {
else if ( d->alignment & Qt::AlignHCenter ) { x += ((rect.width() - ratingAreaWidth) / 2);
x += ( rect.width() - ratingAreaWidth )/2;
} }
int xInc = ratingPix.width() + usedSpacing; int xInc = ratingPix.width() + usedSpacing;
if ( d->direction == Qt::RightToLeft ) { if (d->direction == Qt::RightToLeft) {
x = rect.width() - ratingPix.width() - x; x = (rect.width() - ratingPix.width() - x);
xInc = -xInc; xInc = -xInc;
} }
int y = rect.y(); int y = rect.y();
if( d->alignment & Qt::AlignVCenter ) { if (d->alignment & Qt::AlignVCenter) {
y += ( rect.height() / 2 - ratingPix.height() / 2 ); y += (rect.height() / 2 - ratingPix.height() / 2);
} else if (d->alignment & Qt::AlignBottom ) {
y += (rect.height() - ratingPix.height() );
} }
else if ( d->alignment & Qt::AlignBottom ) { for(; i < numRatingStars; ++i) {
y += ( rect.height() - ratingPix.height() ); painter->drawPixmap(x, y, ratingPix);
}
for(; i < numRatingStars; ++i ) {
painter->drawPixmap( x, y, ratingPix );
x += xInc; x += xInc;
} }
if( half ) { if (half) {
painter->drawPixmap( x, y, ratingPix.width()/2, ratingPix.height(), painter->drawPixmap(
d->direction == Qt::RightToLeft ? ( numHoverStars > 0 ? hoverPix : disabledRatingPix ) : ratingPix, x, y, ratingPix.width()/2, ratingPix.height(),
0, 0, ratingPix.width()/2, ratingPix.height() ); d->direction == Qt::RightToLeft ? (numHoverStars > 0 ? hoverPix : disabledRatingPix) : ratingPix,
painter->drawPixmap( x + ratingPix.width()/2, y, ratingPix.width()/2, ratingPix.height(), 0, 0, ratingPix.width()/2, ratingPix.height()
d->direction == Qt::RightToLeft ? ratingPix : ( numHoverStars > 0 ? hoverPix : disabledRatingPix ), );
ratingPix.width()/2, 0, ratingPix.width()/2, ratingPix.height() ); painter->drawPixmap(
x + ratingPix.width() / 2, y, ratingPix.width() / 2, ratingPix.height(),
d->direction == Qt::RightToLeft ? ratingPix : (numHoverStars > 0 ? hoverPix : disabledRatingPix),
ratingPix.width() / 2, 0, ratingPix.width() / 2, ratingPix.height()
);
x += xInc; x += xInc;
++i; ++i;
} }
@ -268,90 +237,86 @@ void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, in
x += xInc; x += xInc;
} }
if( halfHover ) { if( halfHover ) {
painter->drawPixmap( x, y, ratingPix.width()/2, ratingPix.height(), painter->drawPixmap(
d->direction == Qt::RightToLeft ? disabledRatingPix : hoverPix, x, y, ratingPix.width()/2, ratingPix.height(),
0, 0, ratingPix.width()/2, ratingPix.height() ); d->direction == Qt::RightToLeft ? disabledRatingPix : hoverPix,
painter->drawPixmap( x + ratingPix.width()/2, y, ratingPix.width()/2, ratingPix.height(), 0, 0, ratingPix.width()/2, ratingPix.height()
d->direction == Qt::RightToLeft ? hoverPix : disabledRatingPix, );
ratingPix.width()/2, 0, ratingPix.width()/2, ratingPix.height() ); painter->drawPixmap(
x + ratingPix.width() / 2, y, ratingPix.width() / 2, ratingPix.height(),
d->direction == Qt::RightToLeft ? hoverPix : disabledRatingPix,
ratingPix.width() / 2, 0, ratingPix.width() / 2, ratingPix.height()
);
x += xInc; x += xInc;
++i; ++i;
} }
for(; i < numUsedStars; ++i ) { for(; i < numUsedStars; ++i) {
painter->drawPixmap( x, y, disabledRatingPix ); painter->drawPixmap(x, y, disabledRatingPix);
x += xInc; x += xInc;
} }
} }
int KRatingPainter::ratingFromPosition(const QRect &rect, const QPoint &pos) const
int KRatingPainter::ratingFromPosition( const QRect& rect, const QPoint& pos ) const
{ {
int usedSpacing = d->spacing; int usedSpacing = d->spacing;
int numUsedStars = d->bHalfSteps ? d->maxRating/2 : d->maxRating; int numUsedStars = (d->bHalfSteps ? d->maxRating / 2 : d->maxRating);
int maxHSizeOnePix = ( rect.width() - (numUsedStars-1)*usedSpacing ) / numUsedStars; int maxHSizeOnePix = ((rect.width() - (numUsedStars - 1) * usedSpacing) / numUsedStars);
QPixmap ratingPix = d->getPixmap( qMin( rect.height(), maxHSizeOnePix ) ); QPixmap ratingPix = d->getPixmap(qMin(rect.height(), maxHSizeOnePix));
int ratingAreaWidth = ratingPix.width()*numUsedStars + usedSpacing*(numUsedStars-1); int ratingAreaWidth = ratingPix.width()*numUsedStars + usedSpacing*(numUsedStars-1);
QRect usedRect( rect ); QRect usedRect(rect);
if ( d->alignment & Qt::AlignRight ) { if (d->alignment & Qt::AlignRight ) {
usedRect.setLeft( rect.right() - ratingAreaWidth ); usedRect.setLeft(rect.right() - ratingAreaWidth);
} } else if (d->alignment & Qt::AlignHCenter) {
else if ( d->alignment & Qt::AlignHCenter ) { int x = (rect.width() - ratingAreaWidth) / 2;
int x = ( rect.width() - ratingAreaWidth )/2; usedRect.setLeft(rect.left() + x);
usedRect.setLeft( rect.left() + x ); usedRect.setRight(rect.right() - x);
usedRect.setRight( rect.right() - x ); } else {
} // d->alignment & Qt::AlignLeft
else { // d->alignment & Qt::AlignLeft usedRect.setRight(rect.left() + ratingAreaWidth - 1);
usedRect.setRight( rect.left() + ratingAreaWidth - 1 );
} }
if ( d->alignment & Qt::AlignBottom ) { if (d->alignment & Qt::AlignBottom) {
usedRect.setTop( rect.bottom() - ratingPix.height() + 1 ); usedRect.setTop(rect.bottom() - ratingPix.height() + 1);
} } else if (d->alignment & Qt::AlignVCenter) {
else if ( d->alignment & Qt::AlignVCenter ) {
int x = ( rect.height() - ratingPix.height() )/2; int x = ( rect.height() - ratingPix.height() )/2;
usedRect.setTop( rect.top() + x ); usedRect.setTop( rect.top() + x );
usedRect.setBottom( rect.bottom() - x ); usedRect.setBottom( rect.bottom() - x );
} } else {
else { // d->alignment & Qt::AlignTop // d->alignment & Qt::AlignTop
usedRect.setBottom( rect.top() + ratingPix.height() - 1 ); usedRect.setBottom(rect.top() + ratingPix.height() - 1);
} }
if ( usedRect.contains( pos ) ) { if (usedRect.contains(pos)) {
int x = 0; int x = 0;
if ( d->direction == Qt::RightToLeft ) { if (d->direction == Qt::RightToLeft) {
x = usedRect.right() - pos.x(); x = usedRect.right() - pos.x();
} } else {
else {
x = pos.x() - usedRect.left(); x = pos.x() - usedRect.left();
} }
double one = ( double )usedRect.width() / ( double )d->maxRating; double one = ( double )usedRect.width() / ( double )d->maxRating;
// kDebug() << "rating:" << ( int )( ( double )x/one + 0.5 ); // kDebug() << "rating:" << ( int )( ( double )x/one + 0.5 );
return ( int )( ( double )x/one + 0.5 ); return ( int )( ( double )x/one + 0.5 );
} }
else { return -1;
return -1;
}
} }
void KRatingPainter::paintRating(QPainter *painter, const QRect &rect, Qt::Alignment align, int rating, int hoverRating)
void KRatingPainter::paintRating( QPainter* painter, const QRect& rect, Qt::Alignment align, int rating, int hoverRating )
{ {
KRatingPainter rp; KRatingPainter rp;
rp.setAlignment( align ); rp.setAlignment(align);
rp.setLayoutDirection( painter->layoutDirection() ); rp.setLayoutDirection(painter->layoutDirection());
rp.paint( painter, rect, rating, hoverRating ); rp.paint(painter, rect, rating, hoverRating);
} }
int KRatingPainter::getRatingFromPosition(const QRect &rect, Qt::Alignment align, Qt::LayoutDirection direction, const QPoint &pos)
int KRatingPainter::getRatingFromPosition( const QRect& rect, Qt::Alignment align, Qt::LayoutDirection direction, const QPoint& pos )
{ {
KRatingPainter rp; KRatingPainter rp;
rp.setAlignment( align ); rp.setAlignment(align);
rp.setLayoutDirection( direction ); rp.setLayoutDirection(direction);
return rp.ratingFromPosition( rect, pos ); return rp.ratingFromPosition(rect, pos);
} }

View file

@ -99,7 +99,7 @@ public:
* The icon used to draw a star. In case a custom pixmap has been set * The icon used to draw a star. In case a custom pixmap has been set
* this value is ignored. * this value is ignored.
* *
* \sa setIcon, setCustomPixmap * \sa setIcon
*/ */
QIcon icon() const; QIcon icon() const;
@ -111,14 +111,6 @@ public:
*/ */
bool isEnabled() const; bool isEnabled() const;
/**
* The custom pixmap set to draw a star. If no custom
* pixmap has been set, an invalid pixmap is returned.
*
* \sa setCustomPixmap
*/
QPixmap customPixmap() const;
/** /**
* The spacing between rating pixmaps. * The spacing between rating pixmaps.
* *
@ -129,46 +121,41 @@ public:
/** /**
* The maximum rating. Defaults to 10. * The maximum rating. Defaults to 10.
*/ */
void setMaxRating( int max ); void setMaxRating(int max);
/** /**
* If half steps are enabled (the default) then * If half steps are enabled (the default) then
* one rating step corresponds to half a star. * one rating step corresponds to half a star.
*/ */
void setHalfStepsEnabled( bool enabled ); void setHalfStepsEnabled(bool enabled);
/** /**
* The alignment of the stars in the drawing rect. * The alignment of the stars in the drawing rect.
* All alignment flags are supported. * All alignment flags are supported.
*/ */
void setAlignment( Qt::Alignment align ); void setAlignment(Qt::Alignment align);
/** /**
* LTR or RTL * LTR or RTL
*/ */
void setLayoutDirection( Qt::LayoutDirection direction ); void setLayoutDirection(Qt::LayoutDirection direction);
/** /**
* Set a custom icon. Defaults to "rating". * Set a custom icon. Defaults to "rating".
*/ */
void setIcon( const QIcon& icon ); void setIcon(const QIcon &icon);
/** /**
* Enable or disable the rating. Default is enabled. * Enable or disable the rating. Default is enabled.
*/ */
void setEnabled( bool enabled ); void setEnabled(bool enabled);
/**
* Set a custom pixmap.
*/
void setCustomPixmap( const QPixmap& pixmap );
/** /**
* Set the spacing between rating pixmaps. Be aware that * Set the spacing between rating pixmaps. Be aware that
* for justified horizontal alignment this values may be * for justified horizontal alignment this values may be
* ignored. * ignored.
*/ */
void setSpacing( int spacing ); void setSpacing(int spacing);
/** /**
* Draw the rating. * Draw the rating.
@ -182,7 +169,7 @@ public:
* if the user would actually click as well as the difference to the * if the user would actually click as well as the difference to the
* current rating. * current rating.
*/ */
void paint( QPainter* painter, const QRect& rect, int rating, int hoverRating = -1 ) const; void paint(QPainter *painter, const QRect &rect, int rating, int hoverRating = -1) const;
/** /**
* Calculate the rating value from mouse position pos. * Calculate the rating value from mouse position pos.
@ -190,7 +177,7 @@ public:
* \return The rating corresponding to pos or -1 if pos is * \return The rating corresponding to pos or -1 if pos is
* outside of the configured rect. * outside of the configured rect.
*/ */
int ratingFromPosition( const QRect& rect, const QPoint& pos ) const; int ratingFromPosition(const QRect &rect, const QPoint &pos) const;
/** /**
* Convenience method that paints a rating into the given rect. * Convenience method that paints a rating into the given rect.
@ -200,7 +187,7 @@ public:
* \param align can be aligned vertically and horizontally. Using Qt::AlignJustify will insert spacing * \param align can be aligned vertically and horizontally. Using Qt::AlignJustify will insert spacing
* between the stars. * between the stars.
*/ */
static void paintRating( QPainter* p, const QRect& rect, Qt::Alignment align, int rating, int hoverRating = -1 ); static void paintRating(QPainter *p, const QRect &rect, Qt::Alignment align, int rating, int hoverRating = -1);
/** /**
* Get the rating that would be selected if the user clicked position pos * Get the rating that would be selected if the user clicked position pos
@ -209,7 +196,7 @@ public:
* *
* \return The new rating or -1 if pos is outside of the rating area. * \return The new rating or -1 if pos is outside of the rating area.
*/ */
static int getRatingFromPosition( const QRect& rect, Qt::Alignment align, Qt::LayoutDirection direction, const QPoint& pos ); static int getRatingFromPosition(const QRect &rect, Qt::Alignment align, Qt::LayoutDirection direction, const QPoint &pos);
private: private:
class Private; class Private;

View file

@ -23,7 +23,7 @@
#include <QtGui/QPainter> #include <QtGui/QPainter>
#include <QtGui/QPixmap> #include <QtGui/QPixmap>
#include <QtGui/qevent.h> #include <QtGui/QPaintEvent>
#include <QtGui/QImage> #include <QtGui/QImage>
#include <QtGui/QIcon> #include <QtGui/QIcon>
@ -32,8 +32,9 @@ class KRatingWidget::Private
public: public:
Private() Private()
: rating(0), : rating(0),
hoverRating(-1), hoverRating(-1),
pixSize( 16 ) { pixSize(16)
{
} }
int rating; int rating;
@ -44,12 +45,11 @@ public:
}; };
KRatingWidget::KRatingWidget(QWidget *parent)
KRatingWidget::KRatingWidget( QWidget* parent ) : QFrame(parent),
: QFrame( parent ), d(new Private())
d( new Private() )
{ {
setMouseTracking( true ); setMouseTracking(true);
} }
@ -58,212 +58,169 @@ KRatingWidget::~KRatingWidget()
delete d; delete d;
} }
void KRatingWidget::setIcon(const QIcon &icon)
void KRatingWidget::setCustomPixmap( const QPixmap& pix )
{ {
d->ratingPainter.setCustomPixmap( pix ); d->ratingPainter.setIcon(icon);
update(); update();
} }
void KRatingWidget::setPixmapSize(int size)
void KRatingWidget::setIcon( const QIcon& icon )
{
d->ratingPainter.setIcon( icon );
update();
}
void KRatingWidget::setPixmapSize( int size )
{ {
d->pixSize = size; d->pixSize = size;
updateGeometry(); updateGeometry();
} }
int KRatingWidget::spacing() const int KRatingWidget::spacing() const
{ {
return d->ratingPainter.spacing(); return d->ratingPainter.spacing();
} }
QIcon KRatingWidget::icon() const QIcon KRatingWidget::icon() const
{ {
return d->ratingPainter.icon(); return d->ratingPainter.icon();
} }
void KRatingWidget::setSpacing(int s)
void KRatingWidget::setSpacing( int s )
{ {
d->ratingPainter.setSpacing( s ); d->ratingPainter.setSpacing(s);
update(); update();
} }
Qt::Alignment KRatingWidget::alignment() const Qt::Alignment KRatingWidget::alignment() const
{ {
return d->ratingPainter.alignment(); return d->ratingPainter.alignment();
} }
void KRatingWidget::setAlignment(Qt::Alignment align)
void KRatingWidget::setAlignment( Qt::Alignment align )
{ {
d->ratingPainter.setAlignment( align ); d->ratingPainter.setAlignment(align);
update(); update();
} }
Qt::LayoutDirection KRatingWidget::layoutDirection() const Qt::LayoutDirection KRatingWidget::layoutDirection() const
{ {
return d->ratingPainter.layoutDirection(); return d->ratingPainter.layoutDirection();
} }
void KRatingWidget::setLayoutDirection(Qt::LayoutDirection direction)
void KRatingWidget::setLayoutDirection( Qt::LayoutDirection direction )
{ {
d->ratingPainter.setLayoutDirection( direction ); d->ratingPainter.setLayoutDirection(direction);
update(); update();
} }
unsigned int KRatingWidget::rating() const unsigned int KRatingWidget::rating() const
{ {
return d->rating; return d->rating;
} }
int KRatingWidget::maxRating() const int KRatingWidget::maxRating() const
{ {
return d->ratingPainter.maxRating(); return d->ratingPainter.maxRating();
} }
bool KRatingWidget::halfStepsEnabled() const bool KRatingWidget::halfStepsEnabled() const
{ {
return d->ratingPainter.halfStepsEnabled(); return d->ratingPainter.halfStepsEnabled();
} }
void KRatingWidget::setRating(int rating)
void KRatingWidget::setRating( int rating )
{ {
if ( rating != d->rating ) { if (rating != d->rating) {
d->rating = rating; d->rating = rating;
d->hoverRating = rating; d->hoverRating = rating;
emit ratingChanged( rating ); emit ratingChanged(rating);
emit ratingChanged( (unsigned int)rating ); emit ratingChanged((unsigned int)rating);
update(); update();
} }
} }
void KRatingWidget::setMaxRating(int max)
void KRatingWidget::setMaxRating( int max )
{ {
d->ratingPainter.setMaxRating( max ); d->ratingPainter.setMaxRating(max);
update(); update();
} }
void KRatingWidget::setHalfStepsEnabled(bool enabled)
void KRatingWidget::setHalfStepsEnabled( bool enabled )
{ {
d->ratingPainter.setHalfStepsEnabled( enabled ); d->ratingPainter.setHalfStepsEnabled(enabled);
update(); update();
} }
void KRatingWidget::mousePressEvent(QMouseEvent *e)
void KRatingWidget::mousePressEvent( QMouseEvent* e )
{ {
if ( e->button() == Qt::LeftButton ) { if (e->button() == Qt::LeftButton) {
const int prevRating = d->rating; const int prevRating = d->rating;
d->hoverRating = d->ratingPainter.ratingFromPosition( contentsRect(), e->pos() ); d->hoverRating = d->ratingPainter.ratingFromPosition(contentsRect(), e->pos());
if ( !( d->hoverRating % 2 ) ) { if (!(d->hoverRating % 2)) {
if ( d->hoverRating == prevRating + 1 ) { if (d->hoverRating == prevRating + 1) {
setRating( d->hoverRating - 2 ); setRating( d->hoverRating - 2 );
} else if (d->hoverRating == prevRating) {
setRating( d->hoverRating - 1);
} else {
setRating(d->hoverRating);
} }
else if ( d->hoverRating == prevRating ) { } else {
setRating( d->hoverRating - 1 ); if (d->hoverRating == prevRating - 1) {
} setRating(d->hoverRating);
else { } else if (d->hoverRating == prevRating) {
setRating( d->hoverRating ); setRating(d->hoverRating - 1);
} } else {
} setRating(d->hoverRating + 1);
else {
if ( d->hoverRating == prevRating - 1 ) {
setRating( d->hoverRating );
}
else if ( d->hoverRating == prevRating ) {
setRating( d->hoverRating - 1 );
}
else {
setRating( d->hoverRating + 1 );
} }
} }
} }
} }
void KRatingWidget::mouseMoveEvent(QMouseEvent *e)
void KRatingWidget::mouseMoveEvent( QMouseEvent* e )
{ {
// when moving the mouse we show the user what the result of clicking will be // when moving the mouse we show the user what the result of clicking will be
const int prevHoverRating = d->hoverRating; const int prevHoverRating = d->hoverRating;
d->hoverRating = d->ratingPainter.ratingFromPosition( contentsRect(), e->pos() ); d->hoverRating = d->ratingPainter.ratingFromPosition(contentsRect(), e->pos());
if ( !( d->hoverRating % 2 ) ) { if (!(d->hoverRating % 2)) {
if ( d->hoverRating == prevHoverRating + 1 ) { if (d->hoverRating == prevHoverRating + 1) {
d->hoverRating -= 2; d->hoverRating -= 2;
} } else if (d->hoverRating == prevHoverRating) {
else if ( d->hoverRating == prevHoverRating ) {
d->hoverRating -= 1; d->hoverRating -= 1;
} }
} } else {
else { if (d->hoverRating == prevHoverRating) {
if ( d->hoverRating == prevHoverRating ) {
d->hoverRating -= 1; d->hoverRating -= 1;
} } else {
else {
d->hoverRating += 1; d->hoverRating += 1;
} }
} }
if ( d->hoverRating != prevHoverRating ) { if (d->hoverRating != prevHoverRating) {
update(); update();
} }
} }
void KRatingWidget::leaveEvent(QEvent *e)
void KRatingWidget::leaveEvent( QEvent* )
{ {
Q_UNUSED(e);
d->hoverRating = -1; d->hoverRating = -1;
update(); update();
} }
void KRatingWidget::paintEvent(QPaintEvent *e)
void KRatingWidget::paintEvent( QPaintEvent* e )
{ {
QFrame::paintEvent( e ); QFrame::paintEvent(e);
QPainter p( this ); QPainter p(this);
d->ratingPainter.setEnabled( isEnabled() ); d->ratingPainter.setEnabled(isEnabled());
d->ratingPainter.paint( &p, contentsRect(), d->rating, d->hoverRating ); d->ratingPainter.paint(&p, contentsRect(), d->rating, d->hoverRating);
} }
QSize KRatingWidget::sizeHint() const QSize KRatingWidget::sizeHint() const
{ {
int numPix = d->ratingPainter.maxRating(); int numPix = d->ratingPainter.maxRating();
if( d->ratingPainter.halfStepsEnabled() ) if (d->ratingPainter.halfStepsEnabled()) {
numPix /= 2; numPix /= 2;
QSize pixSize( d->pixSize, d->pixSize );
if ( !d->ratingPainter.customPixmap().isNull() ) {
pixSize = d->ratingPainter.customPixmap().size();
} }
return QSize( pixSize.width()*numPix + spacing()*(numPix-1) + frameWidth()*2, const QSize pixSize(d->pixSize, d->pixSize);
pixSize.height() + frameWidth()*2 ); return QSize(
pixSize.width() * numPix + spacing() * (numPix - 1) + frameWidth() * 2,
pixSize.height() + frameWidth() * 2
);
} }
#include "moc_kratingwidget.cpp" #include "moc_kratingwidget.cpp"

View file

@ -40,18 +40,18 @@
class KDEUI_EXPORT KRatingWidget : public QFrame class KDEUI_EXPORT KRatingWidget : public QFrame
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY( int rating READ rating WRITE setRating ) Q_PROPERTY(int rating READ rating WRITE setRating)
Q_PROPERTY( int maxRating READ maxRating WRITE setMaxRating ) Q_PROPERTY(int maxRating READ maxRating WRITE setMaxRating)
Q_PROPERTY( Qt::Alignment alignment READ alignment WRITE setAlignment ) Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
Q_PROPERTY( bool halfStepsEnabled READ halfStepsEnabled WRITE setHalfStepsEnabled ) Q_PROPERTY(bool halfStepsEnabled READ halfStepsEnabled WRITE setHalfStepsEnabled)
Q_PROPERTY( int spacing READ spacing WRITE setSpacing ) Q_PROPERTY(int spacing READ spacing WRITE setSpacing)
Q_PROPERTY( QIcon icon READ icon WRITE setIcon ) Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
public: public:
/** /**
* Creates a new rating widget. * Creates a new rating widget.
*/ */
KRatingWidget( QWidget* parent = 0 ); KRatingWidget(QWidget *parent = nullptr);
/** /**
* Destructor * Destructor
@ -103,10 +103,9 @@ class KDEUI_EXPORT KRatingWidget : public QFrame
bool halfStepsEnabled() const; bool halfStepsEnabled() const;
/** /**
* The icon used to draw a star. In case a custom pixmap has been set * The icon used to get pixmap from and draw a star.
* this value is ignored.
* *
* \sa setIcon, setCustomPixmap * \sa setIcon
*/ */
QIcon icon() const; QIcon icon() const;
@ -114,22 +113,22 @@ class KDEUI_EXPORT KRatingWidget : public QFrame
/** /**
* This signal is emitted when the rating is changed. * This signal is emitted when the rating is changed.
*/ */
void ratingChanged( unsigned int rating ); void ratingChanged(unsigned int rating);
void ratingChanged( int rating ); void ratingChanged(int rating);
public Q_SLOTS: public Q_SLOTS:
/** /**
* Set the current rating. Calling this method will trigger the * Set the current rating. Calling this method will trigger the
* ratingChanged signal if @p rating is different from the previous rating. * ratingChanged signal if @p rating is different from the previous rating.
*/ */
void setRating( int rating ); void setRating(int rating);
/** /**
* Set the maximum allowed rating value. The default is 10 which means * Set the maximum allowed rating value. The default is 10 which means
* that a rating from 1 to 10 is selectable. If \a max is uneven steps * that a rating from 1 to 10 is selectable. If \a max is uneven steps
* are automatically only allowed full. * are automatically only allowed full.
*/ */
void setMaxRating( int max ); void setMaxRating(int max);
/** /**
* If half steps are enabled (the default) then * If half steps are enabled (the default) then
@ -140,41 +139,36 @@ class KDEUI_EXPORT KRatingWidget : public QFrame
/** /**
* Set the spacing between the pixmaps. The default is 0. * Set the spacing between the pixmaps. The default is 0.
*/ */
void setSpacing( int ); void setSpacing(int);
/** /**
* The alignment of the stars in the drawing rect. * The alignment of the stars in the drawing rect.
* All alignment flags are supported. * All alignment flags are supported.
*/ */
void setAlignment( Qt::Alignment align ); void setAlignment(Qt::Alignment align);
/** /**
* LTR or RTL * LTR or RTL
*/ */
void setLayoutDirection( Qt::LayoutDirection direction ); void setLayoutDirection(Qt::LayoutDirection direction);
/** /**
* Set a custom icon. Defaults to "rating". * Set a custom icon. Defaults to "rating".
*/ */
void setIcon( const QIcon& icon ); void setIcon(const QIcon &icon);
/**
* Set a custom pixmap.
*/
void setCustomPixmap( const QPixmap& pixmap );
/** /**
* Set the recommended size of the pixmaps. This is * Set the recommended size of the pixmaps. This is
* only used for the sizeHint. The actual size is always * only used for the sizeHint. The actual size is always
* dependent on the size of the widget itself. * dependent on the size of the widget itself.
*/ */
void setPixmapSize( int size ); void setPixmapSize(int size);
protected: protected:
void mousePressEvent( QMouseEvent* e ); void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent( QMouseEvent* e ); void mouseMoveEvent(QMouseEvent *e);
void leaveEvent( QEvent* e ); void leaveEvent(QEvent *e);
void paintEvent( QPaintEvent* e ); void paintEvent(QPaintEvent *e);
private: private:
class Private; class Private;