• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • widgets
kratingpainter.cpp
Go to the documentation of this file.
1/*
2 This file is part of the Nepomuk KDE project.
3 Copyright (C) 2007-2008 Sebastian Trueg <trueg@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19 */
20
21#include "kratingpainter.h"
22
23#include <QtGui/QPainter>
24#include <QtGui/QPixmap>
25#include <QtGui/QIcon>
26#include <QtCore/QRect>
27#include <QtCore/QPoint>
28
29#include <kicon.h>
30#include <kiconeffect.h>
31#include <kiconloader.h>
32#include <kdebug.h>
33
34
35class KRatingPainter::Private
36{
37public:
38 Private()
39 : maxRating(10),
40 isEnabled( true ),
41 bHalfSteps(true),
42 alignment(Qt::AlignCenter),
43 direction(Qt::LeftToRight),
44 spacing(0) {
45 }
46
47 QPixmap getPixmap( int size );
48
49 int maxRating;
50 QIcon icon;
51 bool isEnabled;
52 bool bHalfSteps;
53 Qt::Alignment alignment;
54 Qt::LayoutDirection direction;
55 QPixmap customPixmap;
56 int spacing;
57};
58
59
60QPixmap KRatingPainter::Private::getPixmap( int size )
61{
62 if ( !customPixmap.isNull() ) {
63 return customPixmap.scaled( QSize( size, size ) );
64 }
65 else {
66 QIcon _icon( icon );
67 if ( _icon.isNull() ) {
68 _icon = KIcon( "rating" );
69 }
70 return _icon.pixmap( size );
71 }
72}
73
74
75KRatingPainter::KRatingPainter()
76 : d(new Private())
77{
78}
79
80
81KRatingPainter::~KRatingPainter()
82{
83 delete d;
84}
85
86
87int KRatingPainter::maxRating() const
88{
89 return d->maxRating;
90}
91
92
93bool KRatingPainter::halfStepsEnabled() const
94{
95 return d->bHalfSteps;
96}
97
98
99Qt::Alignment KRatingPainter::alignment() const
100{
101 return d->alignment;
102}
103
104
105Qt::LayoutDirection KRatingPainter::layoutDirection() const
106{
107 return d->direction;
108}
109
110
111QIcon KRatingPainter::icon() const
112{
113 return d->icon;
114}
115
116
117bool KRatingPainter::isEnabled() const
118{
119 return d->isEnabled;
120}
121
122
123QPixmap KRatingPainter::customPixmap() const
124{
125 return d->customPixmap;
126}
127
128
129int KRatingPainter::spacing() const
130{
131 return d->spacing;
132}
133
134
135void KRatingPainter::setMaxRating( int max )
136{
137 d->maxRating = max;
138}
139
140
141void KRatingPainter::setHalfStepsEnabled( bool enabled )
142{
143 d->bHalfSteps = enabled;
144}
145
146
147void KRatingPainter::setAlignment( Qt::Alignment align )
148{
149 d->alignment = align;
150}
151
152
153void KRatingPainter::setLayoutDirection( Qt::LayoutDirection direction )
154{
155 d->direction = direction;
156}
157
158
159void KRatingPainter::setIcon( const QIcon& icon )
160{
161 d->icon = icon;
162}
163
164
165void KRatingPainter::setEnabled( bool enabled )
166{
167 d->isEnabled = enabled;
168}
169
170
171void KRatingPainter::setCustomPixmap( const QPixmap& pixmap )
172{
173 d->customPixmap = pixmap;
174}
175
176
177void KRatingPainter::setSpacing( int s )
178{
179 d->spacing = qMax( 0, s );
180}
181
182
183void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, int hoverRating ) const
184{
185 rating = qMin( rating, d->maxRating );
186 hoverRating = qMin( hoverRating, d->maxRating );
187
188 int numUsedStars = d->bHalfSteps ? d->maxRating/2 : d->maxRating;
189
190 if ( hoverRating > 0 && hoverRating < rating ) {
191 int tmp = hoverRating;
192 hoverRating = rating;
193 rating = tmp;
194 }
195
196 int usedSpacing = d->spacing;
197
198 // get the rating pixmaps
199 int maxHSizeOnePix = ( rect.width() - (numUsedStars-1)*usedSpacing ) / numUsedStars;
200 QPixmap ratingPix = d->getPixmap( qMin( rect.height(), maxHSizeOnePix ) );
201
202 KIconEffect *iconEffect = KIconLoader::global()->iconEffect();
203 QPixmap disabledRatingPix = iconEffect->apply( ratingPix, KIconEffect::ToGray, 1.0, QColor(), QColor(), false );
204 QPixmap hoverPix;
205
206 // if we are disabled we become gray and more transparent
207 if ( !d->isEnabled ) {
208 ratingPix = disabledRatingPix;
209 KIconEffect::semiTransparent( disabledRatingPix );
210 }
211
212 bool half = d->bHalfSteps && rating%2;
213 int numRatingStars = d->bHalfSteps ? rating/2 : rating;
214
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;
220 hoverPix = iconEffect->apply( ratingPix, KIconEffect::ToGray, 0.5, QColor(), QColor(), false );
221 }
222
223 if ( d->alignment & Qt::AlignJustify ) {
224 int w = rect.width();
225 w -= numUsedStars * ratingPix.width();
226 usedSpacing = w / ( numUsedStars-1 );
227 }
228
229 int ratingAreaWidth = ratingPix.width()*numUsedStars + usedSpacing*(numUsedStars-1);
230
231 int i = 0;
232 int x = rect.x();
233 if ( d->alignment & Qt::AlignRight ) {
234 x += ( rect.width() - ratingAreaWidth );
235 }
236 else if ( d->alignment & Qt::AlignHCenter ) {
237 x += ( rect.width() - ratingAreaWidth )/2;
238 }
239
240 int xInc = ratingPix.width() + usedSpacing;
241 if ( d->direction == Qt::RightToLeft ) {
242 x = rect.width() - ratingPix.width() - x;
243 xInc = -xInc;
244 }
245
246 int y = rect.y();
247 if( d->alignment & Qt::AlignVCenter ) {
248 y += ( rect.height() / 2 - ratingPix.height() / 2 );
249 }
250 else if ( d->alignment & Qt::AlignBottom ) {
251 y += ( rect.height() - ratingPix.height() );
252 }
253 for(; i < numRatingStars; ++i ) {
254 painter->drawPixmap( x, y, ratingPix );
255 x += xInc;
256 }
257 if( half ) {
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() );
264 x += xInc;
265 ++i;
266 }
267 for(; i < numHoverStars; ++i ) {
268 painter->drawPixmap( x, y, hoverPix );
269 x += xInc;
270 }
271 if( halfHover ) {
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() );
278 x += xInc;
279 ++i;
280 }
281 for(; i < numUsedStars; ++i ) {
282 painter->drawPixmap( x, y, disabledRatingPix );
283 x += xInc;
284 }
285}
286
287
288int KRatingPainter::ratingFromPosition( const QRect& rect, const QPoint& pos ) const
289{
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 ) );
294
295 int ratingAreaWidth = ratingPix.width()*numUsedStars + usedSpacing*(numUsedStars-1);
296
297 QRect usedRect( rect );
298 if ( d->alignment & Qt::AlignRight ) {
299 usedRect.setLeft( rect.right() - ratingAreaWidth );
300 }
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 );
305 }
306 else { // d->alignment & Qt::AlignLeft
307 usedRect.setRight( rect.left() + ratingAreaWidth - 1 );
308 }
309
310 if ( d->alignment & Qt::AlignBottom ) {
311 usedRect.setTop( rect.bottom() - ratingPix.height() + 1 );
312 }
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 );
317 }
318 else { // d->alignment & Qt::AlignTop
319 usedRect.setBottom( rect.top() + ratingPix.height() - 1 );
320 }
321
322 if ( usedRect.contains( pos ) ) {
323 int x = 0;
324 if ( d->direction == Qt::RightToLeft ) {
325 x = usedRect.right() - pos.x();
326 }
327 else {
328 x = pos.x() - usedRect.left();
329 }
330
331 double one = ( double )usedRect.width() / ( double )d->maxRating;
332
333// kDebug() << "rating:" << ( int )( ( double )x/one + 0.5 );
334
335 return ( int )( ( double )x/one + 0.5 );
336 }
337 else {
338 return -1;
339 }
340}
341
342
343void KRatingPainter::paintRating( QPainter* painter, const QRect& rect, Qt::Alignment align, int rating, int hoverRating )
344{
345 KRatingPainter rp;
346 rp.setAlignment( align );
347 rp.setLayoutDirection( painter->layoutDirection() );
348 rp.paint( painter, rect, rating, hoverRating );
349}
350
351
352int KRatingPainter::getRatingFromPosition( const QRect& rect, Qt::Alignment align, Qt::LayoutDirection direction, const QPoint& pos )
353{
354 KRatingPainter rp;
355 rp.setAlignment( align );
356 rp.setLayoutDirection( direction );
357 return rp.ratingFromPosition( rect, pos );
358}
KIconEffect
Applies effects to icons.
Definition: kiconeffect.h:48
KIconEffect::ToGray
@ ToGray
Definition: kiconeffect.h:72
KIconEffect::semiTransparent
static void semiTransparent(QImage &image)
Renders an image semi-transparent.
Definition: kiconeffect.cpp:527
KIconEffect::apply
QImage apply(const QImage &src, int group, int state) const
Applies an effect to an image.
Definition: kiconeffect.cpp:196
KIconLoader::global
static KIconLoader * global()
Returns the global icon loader initialized with the global KComponentData.
KIconLoader::iconEffect
KIconEffect * iconEffect() const
Returns a pointer to the KIconEffect object used by the icon loader.
Definition: kiconloader.cpp:1487
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:41
KRatingPainter
Utility class that draws a row of stars for a rating value.
Definition: kratingpainter.h:51
KRatingPainter::maxRating
int maxRating() const
The maximum rating, i.e.
Definition: kratingpainter.cpp:87
KRatingPainter::alignment
Qt::Alignment alignment() const
The alignment of the stars.
Definition: kratingpainter.cpp:99
KRatingPainter::setMaxRating
void setMaxRating(int max)
The maximum rating.
Definition: kratingpainter.cpp:135
KRatingPainter::setEnabled
void setEnabled(bool enabled)
Enable or disable the rating.
Definition: kratingpainter.cpp:165
KRatingPainter::paintRating
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.
Definition: kratingpainter.cpp:343
KRatingPainter::setCustomPixmap
void setCustomPixmap(const QPixmap &pixmap)
Set a custom pixmap.
Definition: kratingpainter.cpp:171
KRatingPainter::ratingFromPosition
int ratingFromPosition(const QRect &rect, const QPoint &pos) const
Calculate the rating value from mouse position pos.
Definition: kratingpainter.cpp:288
KRatingPainter::icon
QIcon icon() const
The icon used to draw a star.
Definition: kratingpainter.cpp:111
KRatingPainter::setAlignment
void setAlignment(Qt::Alignment align)
The alignment of the stars in the drawing rect.
Definition: kratingpainter.cpp:147
KRatingPainter::spacing
int spacing() const
The spacing between rating pixmaps.
Definition: kratingpainter.cpp:129
KRatingPainter::layoutDirection
Qt::LayoutDirection layoutDirection() const
The layout direction.
Definition: kratingpainter.cpp:105
KRatingPainter::setLayoutDirection
void setLayoutDirection(Qt::LayoutDirection direction)
LTR or RTL.
Definition: kratingpainter.cpp:153
KRatingPainter::KRatingPainter
KRatingPainter()
Create a new KRatingPainter.
Definition: kratingpainter.cpp:75
KRatingPainter::halfStepsEnabled
bool halfStepsEnabled() const
If half steps are enabled one star equals to 2 rating points and uneven rating values result in half-...
Definition: kratingpainter.cpp:93
KRatingPainter::isEnabled
bool isEnabled() const
The rating can be painted in a disabled state where no color is used and hover ratings are ignored.
Definition: kratingpainter.cpp:117
KRatingPainter::setSpacing
void setSpacing(int spacing)
Set the spacing between rating pixmaps.
Definition: kratingpainter.cpp:177
KRatingPainter::getRatingFromPosition
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 ...
Definition: kratingpainter.cpp:352
KRatingPainter::setHalfStepsEnabled
void setHalfStepsEnabled(bool enabled)
If half steps are enabled (the default) then one rating step corresponds to half a star.
Definition: kratingpainter.cpp:141
KRatingPainter::customPixmap
QPixmap customPixmap() const
The custom pixmap set to draw a star.
Definition: kratingpainter.cpp:123
KRatingPainter::paint
void paint(QPainter *painter, const QRect &rect, int rating, int hoverRating=-1) const
Draw the rating.
Definition: kratingpainter.cpp:183
KRatingPainter::setIcon
void setIcon(const QIcon &icon)
Set a custom icon.
Definition: kratingpainter.cpp:159
KRatingPainter::~KRatingPainter
~KRatingPainter()
Destructor.
Definition: kratingpainter.cpp:81
kdebug.h
kicon.h
kiconeffect.h
kiconloader.h
kratingpainter.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal