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

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • 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 
30 class KRatingWidget::Private
31 {
32 public:
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 
48 KRatingWidget::KRatingWidget( QWidget* parent )
49  : QFrame( parent ),
50  d( new Private() )
51 {
52  setMouseTracking( true );
53 }
54 
55 
56 KRatingWidget::~KRatingWidget()
57 {
58  delete d;
59 }
60 
61 
62 #ifndef KDE_NO_DEPRECATED
63 void KRatingWidget::setPixmap( const QPixmap& pix )
64 {
65  setCustomPixmap( pix );
66 }
67 #endif
68 
69 
70 void KRatingWidget::setCustomPixmap( const QPixmap& pix )
71 {
72  d->ratingPainter.setCustomPixmap( pix );
73  update();
74 }
75 
76 
77 void KRatingWidget::setIcon( const QIcon& icon )
78 {
79  d->ratingPainter.setIcon( icon );
80  update();
81 }
82 
83 
84 void KRatingWidget::setPixmapSize( int size )
85 {
86  d->pixSize = size;
87  updateGeometry();
88 }
89 
90 
91 int KRatingWidget::spacing() const
92 {
93  return d->ratingPainter.spacing();
94 }
95 
96 
97 QIcon KRatingWidget::icon() const
98 {
99  return d->ratingPainter.icon();
100 }
101 
102 
103 void KRatingWidget::setSpacing( int s )
104 {
105  d->ratingPainter.setSpacing( s );
106  update();
107 }
108 
109 
110 Qt::Alignment KRatingWidget::alignment() const
111 {
112  return d->ratingPainter.alignment();
113 }
114 
115 
116 void KRatingWidget::setAlignment( Qt::Alignment align )
117 {
118  d->ratingPainter.setAlignment( align );
119  update();
120 }
121 
122 
123 Qt::LayoutDirection KRatingWidget::layoutDirection() const
124 {
125  return d->ratingPainter.layoutDirection();
126 }
127 
128 
129 void KRatingWidget::setLayoutDirection( Qt::LayoutDirection direction )
130 {
131  d->ratingPainter.setLayoutDirection( direction );
132  update();
133 }
134 
135 
136 unsigned int KRatingWidget::rating() const
137 {
138  return d->rating;
139 }
140 
141 
142 int KRatingWidget::maxRating() const
143 {
144  return d->ratingPainter.maxRating();
145 }
146 
147 
148 bool KRatingWidget::halfStepsEnabled() const
149 {
150  return d->ratingPainter.halfStepsEnabled();
151 }
152 
153 
154 #ifndef KDE_NO_DEPRECATED
155 void KRatingWidget::setRating( unsigned int rating )
156 {
157  setRating( (int)rating );
158 }
159 #endif
160 
161 
162 void 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
175 void KRatingWidget::setMaxRating( unsigned int max )
176 {
177  setMaxRating( (int)max );
178 }
179 #endif
180 
181 
182 void KRatingWidget::setMaxRating( int max )
183 {
184  d->ratingPainter.setMaxRating( max );
185  update();
186 }
187 
188 
189 void KRatingWidget::setHalfStepsEnabled( bool enabled )
190 {
191  d->ratingPainter.setHalfStepsEnabled( enabled );
192  update();
193 }
194 
195 
196 #ifndef KDE_NO_DEPRECATED
197 void KRatingWidget::setOnlyPaintFullSteps( bool fs )
198 {
199  setHalfStepsEnabled( !fs );
200 }
201 #endif
202 
203 
204 void 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 
235 void 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 
262 void KRatingWidget::leaveEvent( QEvent* )
263 {
264  d->hoverRating = -1;
265  update();
266 }
267 
268 
269 void 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 
278 QSize 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 
294 void KRatingWidget::resizeEvent( QResizeEvent* e )
295 {
296  QFrame::resizeEvent( e );
297 }
298 
299 #include "kratingwidget.moc"
QEvent
KRatingWidget::setLayoutDirection
void setLayoutDirection(Qt::LayoutDirection direction)
LTR or RTL.
Definition: kratingwidget.cpp:129
QResizeEvent
QWidget
KRatingWidget::setRating
void setRating(int rating)
Set the current rating.
Definition: kratingwidget.cpp:162
QSize::width
int width() const
KRatingWidget::setAlignment
void setAlignment(Qt::Alignment align)
The alignment of the stars in the drawing rect.
Definition: kratingwidget.cpp:116
QWidget::updateGeometry
void updateGeometry()
KRatingWidget::mousePressEvent
void mousePressEvent(QMouseEvent *e)
Definition: kratingwidget.cpp:204
QWidget::contentsRect
QRect contentsRect() const
KRatingWidget::leaveEvent
void leaveEvent(QEvent *e)
Definition: kratingwidget.cpp:262
KRatingWidget::setIcon
void setIcon(const QIcon &icon)
Set a custom icon.
Definition: kratingwidget.cpp:77
KRatingWidget::rating
unsigned int rating() const
KRatingWidget::setPixmapSize
void setPixmapSize(int size)
Set the recommended size of the pixmaps.
Definition: kratingwidget.cpp:84
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
QMouseEvent
Qt::Alignment
typedef Alignment
KRatingWidget::setMaxRating
void setMaxRating(int max)
Set the maximum allowed rating value.
Definition: kratingwidget.cpp:182
QWidget::update
void update()
KRatingWidget::icon
QIcon icon() const
The icon used to draw a star.
QWidget::size
QSize size() const
KRatingWidget::setSpacing
void setSpacing(int)
Set the spacing between the pixmaps.
Definition: kratingwidget.cpp:103
KRatingWidget::~KRatingWidget
~KRatingWidget()
Destructor.
Definition: kratingwidget.cpp:56
QWidget::isEnabled
bool isEnabled() const
KRatingWidget::sizeHint
QSize sizeHint() const
Definition: kratingwidget.cpp:278
QFrame::paintEvent
virtual void paintEvent(QPaintEvent *)
KRatingWidget::KRatingWidget
KRatingWidget(QWidget *parent=0)
Creates a new rating widget.
Definition: kratingwidget.cpp:48
KRatingWidget::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *e)
Definition: kratingwidget.cpp:235
QMouseEvent::button
Qt::MouseButton button() const
KRatingWidget::spacing
int spacing() const
The spacing between the rating stars.
QPainter
KRatingWidget::alignment
Qt::Alignment alignment() const
The alignment of the stars.
KRatingPainter
Utility class that draws a row of stars for a rating value.
Definition: kratingpainter.h:50
KRatingWidget::setCustomPixmap
void setCustomPixmap(const QPixmap &pixmap)
Set a custom pixmap.
Definition: kratingwidget.cpp:70
KRatingWidget::resizeEvent
void resizeEvent(QResizeEvent *e)
Definition: kratingwidget.cpp:294
KRatingWidget::ratingChanged
void ratingChanged(unsigned int rating)
This signal is emitted when the rating is changed.
KRatingWidget::layoutDirection
Qt::LayoutDirection layoutDirection() const
The layout direction.
Definition: kratingwidget.cpp:123
QPixmap
KRatingWidget::paintEvent
void paintEvent(QPaintEvent *e)
Definition: kratingwidget.cpp:269
QSize
QFrame
QFrame::frameWidth
int frameWidth() const
KRatingWidget::halfStepsEnabled
bool halfStepsEnabled() const
If half steps are enabled one star equals to 2 rating points and uneven rating values result in half-...
QSize::height
int height() const
KRatingWidget::maxRating
int maxRating() const
QWidget::setMouseTracking
void setMouseTracking(bool enable)
KRatingWidget::setOnlyPaintFullSteps
void setOnlyPaintFullSteps(bool)
Definition: kratingwidget.cpp:197
kratingwidget.h
QMouseEvent::pos
const QPoint & pos() const
kratingpainter.h
QPaintEvent
QWidget::resizeEvent
virtual void resizeEvent(QResizeEvent *event)
KRatingWidget::setPixmap
void setPixmap(const QPixmap &)
Set the pixap to be used to display a rating step.
Definition: kratingwidget.cpp:63
QIcon
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:00 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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