mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-23 18:32:49 +00:00
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:
parent
12b8b0b3ae
commit
f197e144ca
4 changed files with 201 additions and 298 deletions
|
@ -37,14 +37,15 @@ class KRatingPainter::Private
|
|||
public:
|
||||
Private()
|
||||
: maxRating(10),
|
||||
isEnabled( true ),
|
||||
bHalfSteps(true),
|
||||
alignment(Qt::AlignCenter),
|
||||
direction(Qt::LeftToRight),
|
||||
spacing(0) {
|
||||
isEnabled(true),
|
||||
bHalfSteps(true),
|
||||
alignment(Qt::AlignCenter),
|
||||
direction(Qt::LeftToRight),
|
||||
spacing(0)
|
||||
{
|
||||
}
|
||||
|
||||
QPixmap getPixmap( int size );
|
||||
QPixmap getPixmap(int size);
|
||||
|
||||
int maxRating;
|
||||
QIcon icon;
|
||||
|
@ -52,23 +53,16 @@ public:
|
|||
bool bHalfSteps;
|
||||
Qt::Alignment alignment;
|
||||
Qt::LayoutDirection direction;
|
||||
QPixmap customPixmap;
|
||||
int spacing;
|
||||
};
|
||||
|
||||
|
||||
QPixmap KRatingPainter::Private::getPixmap( int size )
|
||||
QPixmap KRatingPainter::Private::getPixmap(int size)
|
||||
{
|
||||
if ( !customPixmap.isNull() ) {
|
||||
return customPixmap.scaled( QSize( size, size ) );
|
||||
}
|
||||
else {
|
||||
QIcon _icon( icon );
|
||||
if ( _icon.isNull() ) {
|
||||
_icon = KIcon( "rating" );
|
||||
}
|
||||
return _icon.pixmap( size );
|
||||
QIcon _icon(icon);
|
||||
if (_icon.isNull()) {
|
||||
_icon = KIcon("rating");
|
||||
}
|
||||
return _icon.pixmap(size);
|
||||
}
|
||||
|
||||
|
||||
|
@ -77,115 +71,88 @@ KRatingPainter::KRatingPainter()
|
|||
{
|
||||
}
|
||||
|
||||
|
||||
KRatingPainter::~KRatingPainter()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
|
||||
int KRatingPainter::maxRating() const
|
||||
{
|
||||
return d->maxRating;
|
||||
}
|
||||
|
||||
|
||||
bool KRatingPainter::halfStepsEnabled() const
|
||||
{
|
||||
return d->bHalfSteps;
|
||||
}
|
||||
|
||||
|
||||
Qt::Alignment KRatingPainter::alignment() const
|
||||
{
|
||||
return d->alignment;
|
||||
}
|
||||
|
||||
|
||||
Qt::LayoutDirection KRatingPainter::layoutDirection() const
|
||||
{
|
||||
return d->direction;
|
||||
}
|
||||
|
||||
|
||||
QIcon KRatingPainter::icon() const
|
||||
{
|
||||
return d->icon;
|
||||
}
|
||||
|
||||
|
||||
bool KRatingPainter::isEnabled() const
|
||||
{
|
||||
return d->isEnabled;
|
||||
}
|
||||
|
||||
|
||||
QPixmap KRatingPainter::customPixmap() const
|
||||
{
|
||||
return d->customPixmap;
|
||||
}
|
||||
|
||||
|
||||
int KRatingPainter::spacing() const
|
||||
{
|
||||
return d->spacing;
|
||||
}
|
||||
|
||||
|
||||
void KRatingPainter::setMaxRating( int max )
|
||||
void KRatingPainter::setMaxRating(int max)
|
||||
{
|
||||
d->maxRating = max;
|
||||
}
|
||||
|
||||
|
||||
void KRatingPainter::setHalfStepsEnabled( bool enabled )
|
||||
void KRatingPainter::setHalfStepsEnabled(bool enabled)
|
||||
{
|
||||
d->bHalfSteps = enabled;
|
||||
}
|
||||
|
||||
|
||||
void KRatingPainter::setAlignment( Qt::Alignment align )
|
||||
void KRatingPainter::setAlignment(Qt::Alignment align)
|
||||
{
|
||||
d->alignment = align;
|
||||
}
|
||||
|
||||
|
||||
void KRatingPainter::setLayoutDirection( Qt::LayoutDirection direction )
|
||||
void KRatingPainter::setLayoutDirection(Qt::LayoutDirection direction)
|
||||
{
|
||||
d->direction = direction;
|
||||
}
|
||||
|
||||
|
||||
void KRatingPainter::setIcon( const QIcon& icon )
|
||||
void KRatingPainter::setIcon(const QIcon &icon)
|
||||
{
|
||||
d->icon = icon;
|
||||
}
|
||||
|
||||
|
||||
void KRatingPainter::setEnabled( bool enabled )
|
||||
void KRatingPainter::setEnabled(bool enabled)
|
||||
{
|
||||
d->isEnabled = enabled;
|
||||
}
|
||||
|
||||
|
||||
void KRatingPainter::setCustomPixmap( const QPixmap& pixmap )
|
||||
void KRatingPainter::setSpacing(int s)
|
||||
{
|
||||
d->customPixmap = pixmap;
|
||||
d->spacing = qMax(0, s);
|
||||
}
|
||||
|
||||
|
||||
void KRatingPainter::setSpacing( int s )
|
||||
void KRatingPainter::paint(QPainter* painter, const QRect &rect, int rating, int hoverRating) const
|
||||
{
|
||||
d->spacing = qMax( 0, s );
|
||||
}
|
||||
rating = qMin(rating, d->maxRating);
|
||||
hoverRating = qMin(hoverRating, 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;
|
||||
int numUsedStars = (d->bHalfSteps ? d->maxRating / 2 : d->maxRating);
|
||||
|
||||
if ( hoverRating > 0 && hoverRating < rating ) {
|
||||
int tmp = hoverRating;
|
||||
|
@ -196,10 +163,10 @@ void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, in
|
|||
int usedSpacing = d->spacing;
|
||||
|
||||
// get the rating pixmaps
|
||||
int maxHSizeOnePix = ( rect.width() - (numUsedStars-1)*usedSpacing ) / numUsedStars;
|
||||
QPixmap ratingPix = d->getPixmap( qMin( rect.height(), maxHSizeOnePix ) );
|
||||
int maxHSizeOnePix = ((rect.width() - (numUsedStars - 1) * usedSpacing ) / numUsedStars);
|
||||
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;
|
||||
|
||||
// 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 );
|
||||
}
|
||||
|
||||
bool half = d->bHalfSteps && rating%2;
|
||||
int numRatingStars = d->bHalfSteps ? rating/2 : rating;
|
||||
bool half = (d->bHalfSteps && (rating % 2));
|
||||
int numRatingStars = (d->bHalfSteps ? rating / 2 : rating);
|
||||
|
||||
int numHoverStars = 0;
|
||||
bool halfHover = false;
|
||||
if ( hoverRating > 0 && rating != hoverRating && d->isEnabled ) {
|
||||
numHoverStars = d->bHalfSteps ? hoverRating/2 : hoverRating;
|
||||
halfHover = d->bHalfSteps && hoverRating%2;
|
||||
hoverPix = KIconEffect::apply( ratingPix, KIconEffect::ToGray, 0.5, QColor(), QColor(), false );
|
||||
if (hoverRating > 0 && rating != hoverRating && d->isEnabled) {
|
||||
numHoverStars = d->bHalfSteps ? hoverRating / 2 : hoverRating;
|
||||
halfHover = d->bHalfSteps && (hoverRating % 2);
|
||||
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();
|
||||
w -= numUsedStars * ratingPix.width();
|
||||
usedSpacing = w / ( numUsedStars-1 );
|
||||
usedSpacing = w / (numUsedStars - 1);
|
||||
}
|
||||
|
||||
int ratingAreaWidth = ratingPix.width()*numUsedStars + usedSpacing*(numUsedStars-1);
|
||||
|
||||
int i = 0;
|
||||
int x = rect.x();
|
||||
if ( d->alignment & Qt::AlignRight ) {
|
||||
x += ( rect.width() - ratingAreaWidth );
|
||||
}
|
||||
else if ( d->alignment & Qt::AlignHCenter ) {
|
||||
x += ( rect.width() - ratingAreaWidth )/2;
|
||||
if (d->alignment & Qt::AlignRight) {
|
||||
x += ( rect.width() - ratingAreaWidth);
|
||||
} else if (d->alignment & Qt::AlignHCenter) {
|
||||
x += ((rect.width() - ratingAreaWidth) / 2);
|
||||
}
|
||||
|
||||
int xInc = ratingPix.width() + usedSpacing;
|
||||
if ( d->direction == Qt::RightToLeft ) {
|
||||
x = rect.width() - ratingPix.width() - x;
|
||||
if (d->direction == Qt::RightToLeft) {
|
||||
x = (rect.width() - ratingPix.width() - x);
|
||||
xInc = -xInc;
|
||||
}
|
||||
|
||||
int y = rect.y();
|
||||
if( d->alignment & Qt::AlignVCenter ) {
|
||||
y += ( rect.height() / 2 - ratingPix.height() / 2 );
|
||||
if (d->alignment & Qt::AlignVCenter) {
|
||||
y += (rect.height() / 2 - ratingPix.height() / 2);
|
||||
} else if (d->alignment & Qt::AlignBottom ) {
|
||||
y += (rect.height() - ratingPix.height() );
|
||||
}
|
||||
else if ( d->alignment & Qt::AlignBottom ) {
|
||||
y += ( rect.height() - ratingPix.height() );
|
||||
}
|
||||
for(; i < numRatingStars; ++i ) {
|
||||
painter->drawPixmap( x, y, ratingPix );
|
||||
for(; i < numRatingStars; ++i) {
|
||||
painter->drawPixmap(x, y, ratingPix);
|
||||
x += xInc;
|
||||
}
|
||||
if( half ) {
|
||||
painter->drawPixmap( x, y, ratingPix.width()/2, ratingPix.height(),
|
||||
d->direction == Qt::RightToLeft ? ( numHoverStars > 0 ? hoverPix : disabledRatingPix ) : ratingPix,
|
||||
0, 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() );
|
||||
if (half) {
|
||||
painter->drawPixmap(
|
||||
x, y, ratingPix.width()/2, ratingPix.height(),
|
||||
d->direction == Qt::RightToLeft ? (numHoverStars > 0 ? hoverPix : disabledRatingPix) : ratingPix,
|
||||
0, 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;
|
||||
++i;
|
||||
}
|
||||
|
@ -268,90 +237,86 @@ void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, in
|
|||
x += xInc;
|
||||
}
|
||||
if( halfHover ) {
|
||||
painter->drawPixmap( x, y, ratingPix.width()/2, ratingPix.height(),
|
||||
d->direction == Qt::RightToLeft ? disabledRatingPix : hoverPix,
|
||||
0, 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() );
|
||||
painter->drawPixmap(
|
||||
x, y, ratingPix.width()/2, ratingPix.height(),
|
||||
d->direction == Qt::RightToLeft ? disabledRatingPix : hoverPix,
|
||||
0, 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;
|
||||
++i;
|
||||
}
|
||||
for(; i < numUsedStars; ++i ) {
|
||||
painter->drawPixmap( x, y, disabledRatingPix );
|
||||
for(; i < numUsedStars; ++i) {
|
||||
painter->drawPixmap(x, y, disabledRatingPix);
|
||||
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 numUsedStars = d->bHalfSteps ? d->maxRating/2 : d->maxRating;
|
||||
int maxHSizeOnePix = ( rect.width() - (numUsedStars-1)*usedSpacing ) / numUsedStars;
|
||||
QPixmap ratingPix = d->getPixmap( qMin( rect.height(), maxHSizeOnePix ) );
|
||||
int numUsedStars = (d->bHalfSteps ? d->maxRating / 2 : d->maxRating);
|
||||
int maxHSizeOnePix = ((rect.width() - (numUsedStars - 1) * usedSpacing) / numUsedStars);
|
||||
QPixmap ratingPix = d->getPixmap(qMin(rect.height(), maxHSizeOnePix));
|
||||
|
||||
int ratingAreaWidth = ratingPix.width()*numUsedStars + usedSpacing*(numUsedStars-1);
|
||||
|
||||
QRect usedRect( rect );
|
||||
if ( d->alignment & Qt::AlignRight ) {
|
||||
usedRect.setLeft( rect.right() - ratingAreaWidth );
|
||||
}
|
||||
else if ( d->alignment & Qt::AlignHCenter ) {
|
||||
int x = ( rect.width() - ratingAreaWidth )/2;
|
||||
usedRect.setLeft( rect.left() + x );
|
||||
usedRect.setRight( rect.right() - x );
|
||||
}
|
||||
else { // d->alignment & Qt::AlignLeft
|
||||
usedRect.setRight( rect.left() + ratingAreaWidth - 1 );
|
||||
QRect usedRect(rect);
|
||||
if (d->alignment & Qt::AlignRight ) {
|
||||
usedRect.setLeft(rect.right() - ratingAreaWidth);
|
||||
} else if (d->alignment & Qt::AlignHCenter) {
|
||||
int x = (rect.width() - ratingAreaWidth) / 2;
|
||||
usedRect.setLeft(rect.left() + x);
|
||||
usedRect.setRight(rect.right() - x);
|
||||
} else {
|
||||
// d->alignment & Qt::AlignLeft
|
||||
usedRect.setRight(rect.left() + ratingAreaWidth - 1);
|
||||
}
|
||||
|
||||
if ( d->alignment & Qt::AlignBottom ) {
|
||||
usedRect.setTop( rect.bottom() - ratingPix.height() + 1 );
|
||||
}
|
||||
else if ( d->alignment & Qt::AlignVCenter ) {
|
||||
if (d->alignment & Qt::AlignBottom) {
|
||||
usedRect.setTop(rect.bottom() - ratingPix.height() + 1);
|
||||
} else if (d->alignment & Qt::AlignVCenter) {
|
||||
int x = ( rect.height() - ratingPix.height() )/2;
|
||||
usedRect.setTop( rect.top() + x );
|
||||
usedRect.setBottom( rect.bottom() - x );
|
||||
}
|
||||
else { // d->alignment & Qt::AlignTop
|
||||
usedRect.setBottom( rect.top() + ratingPix.height() - 1 );
|
||||
} else {
|
||||
// d->alignment & Qt::AlignTop
|
||||
usedRect.setBottom(rect.top() + ratingPix.height() - 1);
|
||||
}
|
||||
|
||||
if ( usedRect.contains( pos ) ) {
|
||||
if (usedRect.contains(pos)) {
|
||||
int x = 0;
|
||||
if ( d->direction == Qt::RightToLeft ) {
|
||||
if (d->direction == Qt::RightToLeft) {
|
||||
x = usedRect.right() - pos.x();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
x = pos.x() - usedRect.left();
|
||||
}
|
||||
|
||||
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 );
|
||||
}
|
||||
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;
|
||||
rp.setAlignment( align );
|
||||
rp.setLayoutDirection( painter->layoutDirection() );
|
||||
rp.paint( painter, rect, rating, hoverRating );
|
||||
rp.setAlignment(align);
|
||||
rp.setLayoutDirection(painter->layoutDirection());
|
||||
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;
|
||||
rp.setAlignment( align );
|
||||
rp.setLayoutDirection( direction );
|
||||
return rp.ratingFromPosition( rect, pos );
|
||||
rp.setAlignment(align);
|
||||
rp.setLayoutDirection(direction);
|
||||
return rp.ratingFromPosition(rect, pos);
|
||||
}
|
||||
|
|
|
@ -99,7 +99,7 @@ public:
|
|||
* The icon used to draw a star. In case a custom pixmap has been set
|
||||
* this value is ignored.
|
||||
*
|
||||
* \sa setIcon, setCustomPixmap
|
||||
* \sa setIcon
|
||||
*/
|
||||
QIcon icon() const;
|
||||
|
||||
|
@ -111,14 +111,6 @@ public:
|
|||
*/
|
||||
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.
|
||||
*
|
||||
|
@ -129,46 +121,41 @@ public:
|
|||
/**
|
||||
* The maximum rating. Defaults to 10.
|
||||
*/
|
||||
void setMaxRating( int max );
|
||||
void setMaxRating(int max);
|
||||
|
||||
/**
|
||||
* If half steps are enabled (the default) then
|
||||
* 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.
|
||||
* All alignment flags are supported.
|
||||
*/
|
||||
void setAlignment( Qt::Alignment align );
|
||||
void setAlignment(Qt::Alignment align);
|
||||
|
||||
/**
|
||||
* LTR or RTL
|
||||
*/
|
||||
void setLayoutDirection( Qt::LayoutDirection direction );
|
||||
void setLayoutDirection(Qt::LayoutDirection direction);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
void setEnabled( bool enabled );
|
||||
|
||||
/**
|
||||
* Set a custom pixmap.
|
||||
*/
|
||||
void setCustomPixmap( const QPixmap& pixmap );
|
||||
void setEnabled(bool enabled);
|
||||
|
||||
/**
|
||||
* Set the spacing between rating pixmaps. Be aware that
|
||||
* for justified horizontal alignment this values may be
|
||||
* ignored.
|
||||
*/
|
||||
void setSpacing( int spacing );
|
||||
void setSpacing(int spacing);
|
||||
|
||||
/**
|
||||
* Draw the rating.
|
||||
|
@ -182,7 +169,7 @@ public:
|
|||
* if the user would actually click as well as the difference to the
|
||||
* 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.
|
||||
|
@ -190,7 +177,7 @@ public:
|
|||
* \return The rating corresponding to pos or -1 if pos is
|
||||
* 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.
|
||||
|
@ -200,7 +187,7 @@ public:
|
|||
* \param align can be aligned vertically and horizontally. Using Qt::AlignJustify will insert spacing
|
||||
* 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
|
||||
|
@ -209,7 +196,7 @@ public:
|
|||
*
|
||||
* \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:
|
||||
class Private;
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
|
||||
#include <QtGui/QPainter>
|
||||
#include <QtGui/QPixmap>
|
||||
#include <QtGui/qevent.h>
|
||||
#include <QtGui/QPaintEvent>
|
||||
#include <QtGui/QImage>
|
||||
#include <QtGui/QIcon>
|
||||
|
||||
|
@ -32,8 +32,9 @@ class KRatingWidget::Private
|
|||
public:
|
||||
Private()
|
||||
: rating(0),
|
||||
hoverRating(-1),
|
||||
pixSize( 16 ) {
|
||||
hoverRating(-1),
|
||||
pixSize(16)
|
||||
{
|
||||
}
|
||||
|
||||
int rating;
|
||||
|
@ -44,12 +45,11 @@ public:
|
|||
};
|
||||
|
||||
|
||||
|
||||
KRatingWidget::KRatingWidget( QWidget* parent )
|
||||
: QFrame( parent ),
|
||||
d( new Private() )
|
||||
KRatingWidget::KRatingWidget(QWidget *parent)
|
||||
: QFrame(parent),
|
||||
d(new Private())
|
||||
{
|
||||
setMouseTracking( true );
|
||||
setMouseTracking(true);
|
||||
}
|
||||
|
||||
|
||||
|
@ -58,212 +58,169 @@ KRatingWidget::~KRatingWidget()
|
|||
delete d;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void KRatingWidget::setCustomPixmap( const QPixmap& pix )
|
||||
void KRatingWidget::setIcon(const QIcon &icon)
|
||||
{
|
||||
d->ratingPainter.setCustomPixmap( pix );
|
||||
d->ratingPainter.setIcon(icon);
|
||||
update();
|
||||
}
|
||||
|
||||
|
||||
void KRatingWidget::setIcon( const QIcon& icon )
|
||||
{
|
||||
d->ratingPainter.setIcon( icon );
|
||||
update();
|
||||
}
|
||||
|
||||
|
||||
void KRatingWidget::setPixmapSize( int size )
|
||||
void KRatingWidget::setPixmapSize(int size)
|
||||
{
|
||||
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 )
|
||||
void KRatingWidget::setSpacing(int s)
|
||||
{
|
||||
d->ratingPainter.setSpacing( s );
|
||||
d->ratingPainter.setSpacing(s);
|
||||
update();
|
||||
}
|
||||
|
||||
|
||||
Qt::Alignment KRatingWidget::alignment() const
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
Qt::LayoutDirection KRatingWidget::layoutDirection() const
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
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 )
|
||||
void KRatingWidget::setRating(int rating)
|
||||
{
|
||||
if ( rating != d->rating ) {
|
||||
if (rating != d->rating) {
|
||||
d->rating = rating;
|
||||
d->hoverRating = rating;
|
||||
emit ratingChanged( rating );
|
||||
emit ratingChanged( (unsigned int)rating );
|
||||
emit ratingChanged(rating);
|
||||
emit ratingChanged((unsigned int)rating);
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void KRatingWidget::setMaxRating( int max )
|
||||
void KRatingWidget::setMaxRating(int max)
|
||||
{
|
||||
d->ratingPainter.setMaxRating( max );
|
||||
d->ratingPainter.setMaxRating(max);
|
||||
update();
|
||||
}
|
||||
|
||||
|
||||
void KRatingWidget::setHalfStepsEnabled( bool enabled )
|
||||
void KRatingWidget::setHalfStepsEnabled(bool enabled)
|
||||
{
|
||||
d->ratingPainter.setHalfStepsEnabled( enabled );
|
||||
d->ratingPainter.setHalfStepsEnabled(enabled);
|
||||
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;
|
||||
d->hoverRating = d->ratingPainter.ratingFromPosition( contentsRect(), e->pos() );
|
||||
if ( !( d->hoverRating % 2 ) ) {
|
||||
if ( d->hoverRating == prevRating + 1 ) {
|
||||
d->hoverRating = d->ratingPainter.ratingFromPosition(contentsRect(), e->pos());
|
||||
if (!(d->hoverRating % 2)) {
|
||||
if (d->hoverRating == prevRating + 1) {
|
||||
setRating( d->hoverRating - 2 );
|
||||
} else if (d->hoverRating == prevRating) {
|
||||
setRating( d->hoverRating - 1);
|
||||
} else {
|
||||
setRating(d->hoverRating);
|
||||
}
|
||||
else if ( d->hoverRating == prevRating ) {
|
||||
setRating( d->hoverRating - 1 );
|
||||
}
|
||||
else {
|
||||
setRating( d->hoverRating );
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( d->hoverRating == prevRating - 1 ) {
|
||||
setRating( d->hoverRating );
|
||||
}
|
||||
else if ( d->hoverRating == prevRating ) {
|
||||
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
|
||||
const int prevHoverRating = d->hoverRating;
|
||||
d->hoverRating = d->ratingPainter.ratingFromPosition( contentsRect(), e->pos() );
|
||||
if ( !( d->hoverRating % 2 ) ) {
|
||||
if ( d->hoverRating == prevHoverRating + 1 ) {
|
||||
d->hoverRating = d->ratingPainter.ratingFromPosition(contentsRect(), e->pos());
|
||||
if (!(d->hoverRating % 2)) {
|
||||
if (d->hoverRating == prevHoverRating + 1) {
|
||||
d->hoverRating -= 2;
|
||||
}
|
||||
else if ( d->hoverRating == prevHoverRating ) {
|
||||
} else if (d->hoverRating == prevHoverRating) {
|
||||
d->hoverRating -= 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( d->hoverRating == prevHoverRating ) {
|
||||
} else {
|
||||
if (d->hoverRating == prevHoverRating) {
|
||||
d->hoverRating -= 1;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
d->hoverRating += 1;
|
||||
}
|
||||
}
|
||||
if ( d->hoverRating != prevHoverRating ) {
|
||||
if (d->hoverRating != prevHoverRating) {
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void KRatingWidget::leaveEvent( QEvent* )
|
||||
void KRatingWidget::leaveEvent(QEvent *e)
|
||||
{
|
||||
Q_UNUSED(e);
|
||||
d->hoverRating = -1;
|
||||
update();
|
||||
}
|
||||
|
||||
|
||||
void KRatingWidget::paintEvent( QPaintEvent* e )
|
||||
void KRatingWidget::paintEvent(QPaintEvent *e)
|
||||
{
|
||||
QFrame::paintEvent( e );
|
||||
QPainter p( this );
|
||||
d->ratingPainter.setEnabled( isEnabled() );
|
||||
d->ratingPainter.paint( &p, contentsRect(), d->rating, d->hoverRating );
|
||||
QFrame::paintEvent(e);
|
||||
QPainter p(this);
|
||||
d->ratingPainter.setEnabled(isEnabled());
|
||||
d->ratingPainter.paint(&p, contentsRect(), d->rating, d->hoverRating);
|
||||
}
|
||||
|
||||
|
||||
QSize KRatingWidget::sizeHint() const
|
||||
{
|
||||
int numPix = d->ratingPainter.maxRating();
|
||||
if( d->ratingPainter.halfStepsEnabled() )
|
||||
if (d->ratingPainter.halfStepsEnabled()) {
|
||||
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,
|
||||
pixSize.height() + frameWidth()*2 );
|
||||
const QSize pixSize(d->pixSize, d->pixSize);
|
||||
return QSize(
|
||||
pixSize.width() * numPix + spacing() * (numPix - 1) + frameWidth() * 2,
|
||||
pixSize.height() + frameWidth() * 2
|
||||
);
|
||||
}
|
||||
|
||||
#include "moc_kratingwidget.cpp"
|
||||
|
|
|
@ -40,18 +40,18 @@
|
|||
class KDEUI_EXPORT KRatingWidget : public QFrame
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY( int rating READ rating WRITE setRating )
|
||||
Q_PROPERTY( int maxRating READ maxRating WRITE setMaxRating )
|
||||
Q_PROPERTY( Qt::Alignment alignment READ alignment WRITE setAlignment )
|
||||
Q_PROPERTY( bool halfStepsEnabled READ halfStepsEnabled WRITE setHalfStepsEnabled )
|
||||
Q_PROPERTY( int spacing READ spacing WRITE setSpacing )
|
||||
Q_PROPERTY( QIcon icon READ icon WRITE setIcon )
|
||||
Q_PROPERTY(int rating READ rating WRITE setRating)
|
||||
Q_PROPERTY(int maxRating READ maxRating WRITE setMaxRating)
|
||||
Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
|
||||
Q_PROPERTY(bool halfStepsEnabled READ halfStepsEnabled WRITE setHalfStepsEnabled)
|
||||
Q_PROPERTY(int spacing READ spacing WRITE setSpacing)
|
||||
Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
|
||||
|
||||
public:
|
||||
/**
|
||||
* Creates a new rating widget.
|
||||
*/
|
||||
KRatingWidget( QWidget* parent = 0 );
|
||||
KRatingWidget(QWidget *parent = nullptr);
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
|
@ -103,10 +103,9 @@ class KDEUI_EXPORT KRatingWidget : public QFrame
|
|||
bool halfStepsEnabled() const;
|
||||
|
||||
/**
|
||||
* The icon used to draw a star. In case a custom pixmap has been set
|
||||
* this value is ignored.
|
||||
* The icon used to get pixmap from and draw a star.
|
||||
*
|
||||
* \sa setIcon, setCustomPixmap
|
||||
* \sa setIcon
|
||||
*/
|
||||
QIcon icon() const;
|
||||
|
||||
|
@ -114,22 +113,22 @@ class KDEUI_EXPORT KRatingWidget : public QFrame
|
|||
/**
|
||||
* This signal is emitted when the rating is changed.
|
||||
*/
|
||||
void ratingChanged( unsigned int rating );
|
||||
void ratingChanged( int rating );
|
||||
void ratingChanged(unsigned int rating);
|
||||
void ratingChanged(int rating);
|
||||
|
||||
public Q_SLOTS:
|
||||
public Q_SLOTS:
|
||||
/**
|
||||
* Set the current rating. Calling this method will trigger the
|
||||
* 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
|
||||
* that a rating from 1 to 10 is selectable. If \a max is uneven steps
|
||||
* are automatically only allowed full.
|
||||
*/
|
||||
void setMaxRating( int max );
|
||||
void setMaxRating(int max);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
void setSpacing( int );
|
||||
void setSpacing(int);
|
||||
|
||||
/**
|
||||
* The alignment of the stars in the drawing rect.
|
||||
* All alignment flags are supported.
|
||||
*/
|
||||
void setAlignment( Qt::Alignment align );
|
||||
void setAlignment(Qt::Alignment align);
|
||||
|
||||
/**
|
||||
* LTR or RTL
|
||||
*/
|
||||
void setLayoutDirection( Qt::LayoutDirection direction );
|
||||
void setLayoutDirection(Qt::LayoutDirection direction);
|
||||
|
||||
/**
|
||||
* Set a custom icon. Defaults to "rating".
|
||||
*/
|
||||
void setIcon( const QIcon& icon );
|
||||
|
||||
/**
|
||||
* Set a custom pixmap.
|
||||
*/
|
||||
void setCustomPixmap( const QPixmap& pixmap );
|
||||
void setIcon(const QIcon &icon);
|
||||
|
||||
/**
|
||||
* Set the recommended size of the pixmaps. This is
|
||||
* only used for the sizeHint. The actual size is always
|
||||
* dependent on the size of the widget itself.
|
||||
*/
|
||||
void setPixmapSize( int size );
|
||||
void setPixmapSize(int size);
|
||||
|
||||
protected:
|
||||
void mousePressEvent( QMouseEvent* e );
|
||||
void mouseMoveEvent( QMouseEvent* e );
|
||||
void leaveEvent( QEvent* e );
|
||||
void paintEvent( QPaintEvent* e );
|
||||
void mousePressEvent(QMouseEvent *e);
|
||||
void mouseMoveEvent(QMouseEvent *e);
|
||||
void leaveEvent(QEvent *e);
|
||||
void paintEvent(QPaintEvent *e);
|
||||
|
||||
private:
|
||||
class Private;
|
||||
|
|
Loading…
Add table
Reference in a new issue