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

KDEUI

  • kdeui
  • widgets
kratingwidget.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the Nepomuk KDE project.
3 * Copyright (C) 2006-2007 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 "kratingwidget.h"
22#include "kratingpainter.h"
23
24#include <QtGui/QPainter>
25#include <QtGui/QPixmap>
26#include <QtGui/QKeyEvent>
27#include <QtGui/QImage>
28#include <QtGui/QIcon>
29
30class KRatingWidget::Private
31{
32public:
33 Private()
34 : rating(0),
35 hoverRating(-1),
36 pixSize( 16 ) {
37 }
38
39 int rating;
40 int hoverRating;
41 int pixSize;
42
43 KRatingPainter ratingPainter;
44};
45
46
47
48KRatingWidget::KRatingWidget( QWidget* parent )
49 : QFrame( parent ),
50 d( new Private() )
51{
52 setMouseTracking( true );
53}
54
55
56KRatingWidget::~KRatingWidget()
57{
58 delete d;
59}
60
61
62#ifndef KDE_NO_DEPRECATED
63void KRatingWidget::setPixmap( const QPixmap& pix )
64{
65 setCustomPixmap( pix );
66}
67#endif
68
69
70void KRatingWidget::setCustomPixmap( const QPixmap& pix )
71{
72 d->ratingPainter.setCustomPixmap( pix );
73 update();
74}
75
76
77void KRatingWidget::setIcon( const QIcon& icon )
78{
79 d->ratingPainter.setIcon( icon );
80 update();
81}
82
83
84void KRatingWidget::setPixmapSize( int size )
85{
86 d->pixSize = size;
87 updateGeometry();
88}
89
90
91int KRatingWidget::spacing() const
92{
93 return d->ratingPainter.spacing();
94}
95
96
97QIcon KRatingWidget::icon() const
98{
99 return d->ratingPainter.icon();
100}
101
102
103void KRatingWidget::setSpacing( int s )
104{
105 d->ratingPainter.setSpacing( s );
106 update();
107}
108
109
110Qt::Alignment KRatingWidget::alignment() const
111{
112 return d->ratingPainter.alignment();
113}
114
115
116void KRatingWidget::setAlignment( Qt::Alignment align )
117{
118 d->ratingPainter.setAlignment( align );
119 update();
120}
121
122
123Qt::LayoutDirection KRatingWidget::layoutDirection() const
124{
125 return d->ratingPainter.layoutDirection();
126}
127
128
129void KRatingWidget::setLayoutDirection( Qt::LayoutDirection direction )
130{
131 d->ratingPainter.setLayoutDirection( direction );
132 update();
133}
134
135
136unsigned int KRatingWidget::rating() const
137{
138 return d->rating;
139}
140
141
142int KRatingWidget::maxRating() const
143{
144 return d->ratingPainter.maxRating();
145}
146
147
148bool KRatingWidget::halfStepsEnabled() const
149{
150 return d->ratingPainter.halfStepsEnabled();
151}
152
153
154#ifndef KDE_NO_DEPRECATED
155void KRatingWidget::setRating( unsigned int rating )
156{
157 setRating( (int)rating );
158}
159#endif
160
161
162void KRatingWidget::setRating( int rating )
163{
164 if ( rating != d->rating ) {
165 d->rating = rating;
166 d->hoverRating = rating;
167 emit ratingChanged( rating );
168 emit ratingChanged( (unsigned int)rating );
169 update();
170 }
171}
172
173
174#ifndef KDE_NO_DEPRECATED
175void KRatingWidget::setMaxRating( unsigned int max )
176{
177 setMaxRating( (int)max );
178}
179#endif
180
181
182void KRatingWidget::setMaxRating( int max )
183{
184 d->ratingPainter.setMaxRating( max );
185 update();
186}
187
188
189void KRatingWidget::setHalfStepsEnabled( bool enabled )
190{
191 d->ratingPainter.setHalfStepsEnabled( enabled );
192 update();
193}
194
195
196#ifndef KDE_NO_DEPRECATED
197void KRatingWidget::setOnlyPaintFullSteps( bool fs )
198{
199 setHalfStepsEnabled( !fs );
200}
201#endif
202
203
204void KRatingWidget::mousePressEvent( QMouseEvent* e )
205{
206 if ( e->button() == Qt::LeftButton ) {
207 const int prevRating = d->rating;
208 d->hoverRating = d->ratingPainter.ratingFromPosition( contentsRect(), e->pos() );
209 if ( !( d->hoverRating % 2 ) ) {
210 if ( d->hoverRating == prevRating + 1 ) {
211 setRating( d->hoverRating - 2 );
212 }
213 else if ( d->hoverRating == prevRating ) {
214 setRating( d->hoverRating - 1 );
215 }
216 else {
217 setRating( d->hoverRating );
218 }
219 }
220 else {
221 if ( d->hoverRating == prevRating - 1 ) {
222 setRating( d->hoverRating );
223 }
224 else if ( d->hoverRating == prevRating ) {
225 setRating( d->hoverRating - 1 );
226 }
227 else {
228 setRating( d->hoverRating + 1 );
229 }
230 }
231 }
232}
233
234
235void KRatingWidget::mouseMoveEvent( QMouseEvent* e )
236{
237 // when moving the mouse we show the user what the result of clicking will be
238 const int prevHoverRating = d->hoverRating;
239 d->hoverRating = d->ratingPainter.ratingFromPosition( contentsRect(), e->pos() );
240 if ( !( d->hoverRating % 2 ) ) {
241 if ( d->hoverRating == prevHoverRating + 1 ) {
242 d->hoverRating -= 2;
243 }
244 else if ( d->hoverRating == prevHoverRating ) {
245 d->hoverRating -= 1;
246 }
247 }
248 else {
249 if ( d->hoverRating == prevHoverRating ) {
250 d->hoverRating -= 1;
251 }
252 else {
253 d->hoverRating += 1;
254 }
255 }
256 if ( d->hoverRating != prevHoverRating ) {
257 update();
258 }
259}
260
261
262void KRatingWidget::leaveEvent( QEvent* )
263{
264 d->hoverRating = -1;
265 update();
266}
267
268
269void KRatingWidget::paintEvent( QPaintEvent* e )
270{
271 QFrame::paintEvent( e );
272 QPainter p( this );
273 d->ratingPainter.setEnabled( isEnabled() );
274 d->ratingPainter.paint( &p, contentsRect(), d->rating, d->hoverRating );
275}
276
277
278QSize KRatingWidget::sizeHint() const
279{
280 int numPix = d->ratingPainter.maxRating();
281 if( d->ratingPainter.halfStepsEnabled() )
282 numPix /= 2;
283
284 QSize pixSize( d->pixSize, d->pixSize );
285 if ( !d->ratingPainter.customPixmap().isNull() ) {
286 pixSize = d->ratingPainter.customPixmap().size();
287 }
288
289 return QSize( pixSize.width()*numPix + spacing()*(numPix-1) + frameWidth()*2,
290 pixSize.height() + frameWidth()*2 );
291}
292
293
294void KRatingWidget::resizeEvent( QResizeEvent* e )
295{
296 QFrame::resizeEvent( e );
297}
298
299#include "kratingwidget.moc"
KRatingPainter
Utility class that draws a row of stars for a rating value.
Definition: kratingpainter.h:51
KRatingWidget::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *e)
Definition: kratingwidget.cpp:235
KRatingWidget::setAlignment
void setAlignment(Qt::Alignment align)
The alignment of the stars in the drawing rect.
Definition: kratingwidget.cpp:116
KRatingWidget::setMaxRating
void setMaxRating(int max)
Set the maximum allowed rating value.
Definition: kratingwidget.cpp:182
KRatingWidget::paintEvent
void paintEvent(QPaintEvent *e)
Definition: kratingwidget.cpp:269
KRatingWidget::~KRatingWidget
~KRatingWidget()
Destructor.
Definition: kratingwidget.cpp:56
KRatingWidget::setRating
void setRating(int rating)
Set the current rating.
Definition: kratingwidget.cpp:162
KRatingWidget::setPixmap
void setPixmap(const QPixmap &)
Set the pixap to be used to display a rating step.
Definition: kratingwidget.cpp:63
KRatingWidget::ratingChanged
void ratingChanged(unsigned int rating)
This signal is emitted when the rating is changed.
KRatingWidget::setOnlyPaintFullSteps
void setOnlyPaintFullSteps(bool)
Definition: kratingwidget.cpp:197
KRatingWidget::setPixmapSize
void setPixmapSize(int size)
Set the recommended size of the pixmaps.
Definition: kratingwidget.cpp:84
KRatingWidget::setSpacing
void setSpacing(int)
Set the spacing between the pixmaps.
Definition: kratingwidget.cpp:103
KRatingWidget::sizeHint
QSize sizeHint() const
Definition: kratingwidget.cpp:278
KRatingWidget::setCustomPixmap
void setCustomPixmap(const QPixmap &pixmap)
Set a custom pixmap.
Definition: kratingwidget.cpp:70
KRatingWidget::layoutDirection
Qt::LayoutDirection layoutDirection() const
The layout direction.
Definition: kratingwidget.cpp:123
KRatingWidget::icon
QIcon icon
Definition: kratingwidget.h:48
KRatingWidget::KRatingWidget
KRatingWidget(QWidget *parent=0)
Creates a new rating widget.
Definition: kratingwidget.cpp:48
KRatingWidget::halfStepsEnabled
bool halfStepsEnabled
Definition: kratingwidget.h:46
KRatingWidget::leaveEvent
void leaveEvent(QEvent *e)
Definition: kratingwidget.cpp:262
KRatingWidget::rating
int rating
Definition: kratingwidget.h:43
KRatingWidget::spacing
int spacing
Definition: kratingwidget.h:47
KRatingWidget::mousePressEvent
void mousePressEvent(QMouseEvent *e)
Definition: kratingwidget.cpp:204
KRatingWidget::maxRating
int maxRating
Definition: kratingwidget.h:44
KRatingWidget::setLayoutDirection
void setLayoutDirection(Qt::LayoutDirection direction)
LTR or RTL.
Definition: kratingwidget.cpp:129
KRatingWidget::resizeEvent
void resizeEvent(QResizeEvent *e)
Definition: kratingwidget.cpp:294
KRatingWidget::setIcon
void setIcon(const QIcon &icon)
Set a custom icon.
Definition: kratingwidget.cpp:77
KRatingWidget::alignment
Qt::Alignment alignment
Definition: kratingwidget.h:45
KRatingWidget::setHalfStepsEnabled
void setHalfStepsEnabled(bool enabled)
If half steps are enabled (the default) then one rating step corresponds to half a star.
Definition: kratingwidget.cpp:189
QFrame
QWidget
kratingpainter.h
kratingwidget.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