23 #include <QtGui/QPainter> 24 #include <QtGui/QPixmap> 25 #include <QtGui/QIcon> 26 #include <QtCore/QRect> 27 #include <QtCore/QPoint> 35 class KRatingPainter::Private
43 direction(Qt::LeftToRight),
47 QPixmap getPixmap(
int size );
54 Qt::LayoutDirection direction;
60 QPixmap KRatingPainter::Private::getPixmap(
int size )
62 if ( !customPixmap.isNull() ) {
63 return customPixmap.scaled( QSize( size, size ) );
67 if ( _icon.isNull() ) {
68 _icon =
KIcon(
"rating" );
70 return _icon.pixmap( size );
125 return d->customPixmap;
143 d->bHalfSteps = enabled;
149 d->alignment = align;
155 d->direction = direction;
167 d->isEnabled = enabled;
173 d->customPixmap = pixmap;
179 d->spacing = qMax( 0, s );
185 rating = qMin( rating, d->maxRating );
186 hoverRating = qMin( hoverRating, d->maxRating );
188 int numUsedStars = d->bHalfSteps ? d->maxRating/2 : d->maxRating;
190 if ( hoverRating > 0 && hoverRating < rating ) {
191 int tmp = hoverRating;
192 hoverRating = rating;
196 int usedSpacing = d->spacing;
199 int maxHSizeOnePix = ( rect.width() - (numUsedStars-1)*usedSpacing ) / numUsedStars;
200 QPixmap ratingPix = d->getPixmap( qMin( rect.height(), maxHSizeOnePix ) );
207 if ( !d->isEnabled ) {
208 ratingPix = disabledRatingPix;
212 bool half = d->bHalfSteps && rating%2;
213 int numRatingStars = d->bHalfSteps ? rating/2 : rating;
215 int numHoverStars = 0;
216 bool halfHover =
false;
217 if ( hoverRating > 0 && rating != hoverRating && d->isEnabled ) {
218 numHoverStars = d->bHalfSteps ? hoverRating/2 : hoverRating;
219 halfHover = d->bHalfSteps && hoverRating%2;
223 if ( d->alignment & Qt::AlignJustify ) {
224 int w = rect.width();
225 w -= numUsedStars * ratingPix.width();
226 usedSpacing = w / ( numUsedStars-1 );
229 int ratingAreaWidth = ratingPix.width()*numUsedStars + usedSpacing*(numUsedStars-1);
233 if ( d->alignment & Qt::AlignRight ) {
234 x += ( rect.width() - ratingAreaWidth );
236 else if ( d->alignment & Qt::AlignHCenter ) {
237 x += ( rect.width() - ratingAreaWidth )/2;
240 int xInc = ratingPix.width() + usedSpacing;
241 if ( d->direction == Qt::RightToLeft ) {
242 x = rect.width() - ratingPix.width() - x;
247 if( d->alignment & Qt::AlignVCenter ) {
248 y += ( rect.height() / 2 - ratingPix.height() / 2 );
250 else if ( d->alignment & Qt::AlignBottom ) {
251 y += ( rect.height() - ratingPix.height() );
253 for(; i < numRatingStars; ++i ) {
254 painter->drawPixmap( x, y, ratingPix );
258 painter->drawPixmap( x, y, ratingPix.width()/2, ratingPix.height(),
259 d->direction == Qt::RightToLeft ? ( numHoverStars > 0 ? hoverPix : disabledRatingPix ) : ratingPix,
260 0, 0, ratingPix.width()/2, ratingPix.height() );
261 painter->drawPixmap( x + ratingPix.width()/2, y, ratingPix.width()/2, ratingPix.height(),
262 d->direction == Qt::RightToLeft ? ratingPix : ( numHoverStars > 0 ? hoverPix : disabledRatingPix ),
263 ratingPix.width()/2, 0, ratingPix.width()/2, ratingPix.height() );
267 for(; i < numHoverStars; ++i ) {
268 painter->drawPixmap( x, y, hoverPix );
272 painter->drawPixmap( x, y, ratingPix.width()/2, ratingPix.height(),
273 d->direction == Qt::RightToLeft ? disabledRatingPix : hoverPix,
274 0, 0, ratingPix.width()/2, ratingPix.height() );
275 painter->drawPixmap( x + ratingPix.width()/2, y, ratingPix.width()/2, ratingPix.height(),
276 d->direction == Qt::RightToLeft ? hoverPix : disabledRatingPix,
277 ratingPix.width()/2, 0, ratingPix.width()/2, ratingPix.height() );
281 for(; i < numUsedStars; ++i ) {
282 painter->drawPixmap( x, y, disabledRatingPix );
290 int usedSpacing = d->spacing;
291 int numUsedStars = d->bHalfSteps ? d->maxRating/2 : d->maxRating;
292 int maxHSizeOnePix = ( rect.width() - (numUsedStars-1)*usedSpacing ) / numUsedStars;
293 QPixmap ratingPix = d->getPixmap( qMin( rect.height(), maxHSizeOnePix ) );
295 int ratingAreaWidth = ratingPix.width()*numUsedStars + usedSpacing*(numUsedStars-1);
297 QRect usedRect( rect );
298 if ( d->alignment & Qt::AlignRight ) {
299 usedRect.setLeft( rect.right() - ratingAreaWidth );
301 else if ( d->alignment & Qt::AlignHCenter ) {
302 int x = ( rect.width() - ratingAreaWidth )/2;
303 usedRect.setLeft( rect.left() + x );
304 usedRect.setRight( rect.right() - x );
307 usedRect.setRight( rect.left() + ratingAreaWidth - 1 );
310 if ( d->alignment & Qt::AlignBottom ) {
311 usedRect.setTop( rect.bottom() - ratingPix.height() + 1 );
313 else if ( d->alignment & Qt::AlignVCenter ) {
314 int x = ( rect.height() - ratingPix.height() )/2;
315 usedRect.setTop( rect.top() + x );
316 usedRect.setBottom( rect.bottom() - x );
319 usedRect.setBottom( rect.top() + ratingPix.height() - 1 );
322 if ( usedRect.contains( pos ) ) {
324 if ( d->direction == Qt::RightToLeft ) {
325 x = usedRect.right() - pos.x();
328 x = pos.x() - usedRect.left();
331 double one = ( double )usedRect.width() / ( double )d->maxRating;
335 return (
int )( ( double )x/one + 0.5 );
348 rp.
paint( painter, rect, rating, hoverRating );
static void paintRating(QPainter *p, const QRect &rect, Qt::Alignment align, int rating, int hoverRating=-1)
Convenience method that paints a rating into the given rect.
void setSpacing(int spacing)
Set the spacing between rating pixmaps.
bool halfStepsEnabled() const
If half steps are enabled one star equals to 2 rating points and uneven rating values result in half-...
void setEnabled(bool enabled)
Enable or disable the rating.
static void semiTransparent(QImage &image)
Renders an image semi-transparent.
QPixmap customPixmap() const
The custom pixmap set to draw a star.
static KIconLoader * global()
Returns the global icon loader initialized with the global KComponentData.
void setIcon(const QIcon &icon)
Set a custom icon.
void setMaxRating(int max)
The maximum rating.
~KRatingPainter()
Destructor.
Qt::Alignment alignment() const
The alignment of the stars.
void setCustomPixmap(const QPixmap &pixmap)
Set a custom pixmap.
QIcon icon() const
The icon used to draw a star.
void setAlignment(Qt::Alignment align)
The alignment of the stars in the drawing rect.
QImage apply(const QImage &src, int group, int state) const
Applies an effect to an image.
void setHalfStepsEnabled(bool enabled)
If half steps are enabled (the default) then one rating step corresponds to half a star.
Utility class that draws a row of stars for a rating value.
A wrapper around QIcon that provides KDE icon features.
KRatingPainter()
Create a new KRatingPainter.
Qt::LayoutDirection layoutDirection() const
The layout direction.
bool isEnabled() const
The rating can be painted in a disabled state where no color is used and hover ratings are ignored.
static int getRatingFromPosition(const QRect &rect, Qt::Alignment align, Qt::LayoutDirection direction, const QPoint &pos)
Get the rating that would be selected if the user clicked position pos within rect if the rating has ...
void setLayoutDirection(Qt::LayoutDirection direction)
LTR or RTL.
KIconEffect * iconEffect() const
Returns a pointer to the KIconEffect object used by the icon loader.
int maxRating() const
The maximum rating, i.e.
void paint(QPainter *painter, const QRect &rect, int rating, int hoverRating=-1) const
Draw the rating.
int ratingFromPosition(const QRect &rect, const QPoint &pos) const
Calculate the rating value from mouse position pos.
Applies effects to icons.
int spacing() const
The spacing between rating pixmaps.