KDEUI
kratingwidget.cpp
Go to the documentation of this file.
00001 /* 00002 * This file is part of the Nepomuk KDE project. 00003 * Copyright (C) 2006-2007 Sebastian Trueg <trueg@kde.org> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kratingwidget.h" 00022 #include "kratingpainter.h" 00023 00024 #include <QtGui/QPainter> 00025 #include <QtGui/QPixmap> 00026 #include <QtGui/QKeyEvent> 00027 #include <QtGui/QImage> 00028 #include <QtGui/QIcon> 00029 00030 class KRatingWidget::Private 00031 { 00032 public: 00033 Private() 00034 : rating(0), 00035 hoverRating(-1), 00036 pixSize( 16 ) { 00037 } 00038 00039 int rating; 00040 int hoverRating; 00041 int pixSize; 00042 00043 KRatingPainter ratingPainter; 00044 }; 00045 00046 00047 00048 KRatingWidget::KRatingWidget( QWidget* parent ) 00049 : QFrame( parent ), 00050 d( new Private() ) 00051 { 00052 setMouseTracking( true ); 00053 } 00054 00055 00056 KRatingWidget::~KRatingWidget() 00057 { 00058 delete d; 00059 } 00060 00061 00062 #ifndef KDE_NO_DEPRECATED 00063 void KRatingWidget::setPixmap( const QPixmap& pix ) 00064 { 00065 setCustomPixmap( pix ); 00066 } 00067 #endif 00068 00069 00070 void KRatingWidget::setCustomPixmap( const QPixmap& pix ) 00071 { 00072 d->ratingPainter.setCustomPixmap( pix ); 00073 update(); 00074 } 00075 00076 00077 void KRatingWidget::setIcon( const QIcon& icon ) 00078 { 00079 d->ratingPainter.setIcon( icon ); 00080 update(); 00081 } 00082 00083 00084 void KRatingWidget::setPixmapSize( int size ) 00085 { 00086 d->pixSize = size; 00087 updateGeometry(); 00088 } 00089 00090 00091 int KRatingWidget::spacing() const 00092 { 00093 return d->ratingPainter.spacing(); 00094 } 00095 00096 00097 QIcon KRatingWidget::icon() const 00098 { 00099 return d->ratingPainter.icon(); 00100 } 00101 00102 00103 void KRatingWidget::setSpacing( int s ) 00104 { 00105 d->ratingPainter.setSpacing( s ); 00106 update(); 00107 } 00108 00109 00110 Qt::Alignment KRatingWidget::alignment() const 00111 { 00112 return d->ratingPainter.alignment(); 00113 } 00114 00115 00116 void KRatingWidget::setAlignment( Qt::Alignment align ) 00117 { 00118 d->ratingPainter.setAlignment( align ); 00119 update(); 00120 } 00121 00122 00123 Qt::LayoutDirection KRatingWidget::layoutDirection() const 00124 { 00125 return d->ratingPainter.layoutDirection(); 00126 } 00127 00128 00129 void KRatingWidget::setLayoutDirection( Qt::LayoutDirection direction ) 00130 { 00131 d->ratingPainter.setLayoutDirection( direction ); 00132 update(); 00133 } 00134 00135 00136 unsigned int KRatingWidget::rating() const 00137 { 00138 return d->rating; 00139 } 00140 00141 00142 int KRatingWidget::maxRating() const 00143 { 00144 return d->ratingPainter.maxRating(); 00145 } 00146 00147 00148 bool KRatingWidget::halfStepsEnabled() const 00149 { 00150 return d->ratingPainter.halfStepsEnabled(); 00151 } 00152 00153 00154 #ifndef KDE_NO_DEPRECATED 00155 void KRatingWidget::setRating( unsigned int rating ) 00156 { 00157 setRating( (int)rating ); 00158 } 00159 #endif 00160 00161 00162 void KRatingWidget::setRating( int rating ) 00163 { 00164 if ( rating != d->rating ) { 00165 d->rating = rating; 00166 d->hoverRating = rating; 00167 emit ratingChanged( rating ); 00168 emit ratingChanged( (unsigned int)rating ); 00169 update(); 00170 } 00171 } 00172 00173 00174 #ifndef KDE_NO_DEPRECATED 00175 void KRatingWidget::setMaxRating( unsigned int max ) 00176 { 00177 setMaxRating( (int)max ); 00178 } 00179 #endif 00180 00181 00182 void KRatingWidget::setMaxRating( int max ) 00183 { 00184 d->ratingPainter.setMaxRating( max ); 00185 update(); 00186 } 00187 00188 00189 void KRatingWidget::setHalfStepsEnabled( bool enabled ) 00190 { 00191 d->ratingPainter.setHalfStepsEnabled( enabled ); 00192 update(); 00193 } 00194 00195 00196 #ifndef KDE_NO_DEPRECATED 00197 void KRatingWidget::setOnlyPaintFullSteps( bool fs ) 00198 { 00199 setHalfStepsEnabled( !fs ); 00200 } 00201 #endif 00202 00203 00204 void KRatingWidget::mousePressEvent( QMouseEvent* e ) 00205 { 00206 if ( e->button() == Qt::LeftButton ) { 00207 const int prevRating = d->rating; 00208 d->hoverRating = d->ratingPainter.ratingFromPosition( contentsRect(), e->pos() ); 00209 if ( !( d->hoverRating % 2 ) ) { 00210 if ( d->hoverRating == prevRating + 1 ) { 00211 setRating( d->hoverRating - 2 ); 00212 } 00213 else if ( d->hoverRating == prevRating ) { 00214 setRating( d->hoverRating - 1 ); 00215 } 00216 else { 00217 setRating( d->hoverRating ); 00218 } 00219 } 00220 else { 00221 if ( d->hoverRating == prevRating - 1 ) { 00222 setRating( d->hoverRating ); 00223 } 00224 else if ( d->hoverRating == prevRating ) { 00225 setRating( d->hoverRating - 1 ); 00226 } 00227 else { 00228 setRating( d->hoverRating + 1 ); 00229 } 00230 } 00231 } 00232 } 00233 00234 00235 void KRatingWidget::mouseMoveEvent( QMouseEvent* e ) 00236 { 00237 // when moving the mouse we show the user what the result of clicking will be 00238 const int prevHoverRating = d->hoverRating; 00239 d->hoverRating = d->ratingPainter.ratingFromPosition( contentsRect(), e->pos() ); 00240 if ( !( d->hoverRating % 2 ) ) { 00241 if ( d->hoverRating == prevHoverRating + 1 ) { 00242 d->hoverRating -= 2; 00243 } 00244 else if ( d->hoverRating == prevHoverRating ) { 00245 d->hoverRating -= 1; 00246 } 00247 } 00248 else { 00249 if ( d->hoverRating == prevHoverRating ) { 00250 d->hoverRating -= 1; 00251 } 00252 else { 00253 d->hoverRating += 1; 00254 } 00255 } 00256 if ( d->hoverRating != prevHoverRating ) { 00257 update(); 00258 } 00259 } 00260 00261 00262 void KRatingWidget::leaveEvent( QEvent* ) 00263 { 00264 d->hoverRating = -1; 00265 update(); 00266 } 00267 00268 00269 void KRatingWidget::paintEvent( QPaintEvent* e ) 00270 { 00271 QFrame::paintEvent( e ); 00272 QPainter p( this ); 00273 d->ratingPainter.setEnabled( isEnabled() ); 00274 d->ratingPainter.paint( &p, contentsRect(), d->rating, d->hoverRating ); 00275 } 00276 00277 00278 QSize KRatingWidget::sizeHint() const 00279 { 00280 int numPix = d->ratingPainter.maxRating(); 00281 if( d->ratingPainter.halfStepsEnabled() ) 00282 numPix /= 2; 00283 00284 QSize pixSize( d->pixSize, d->pixSize ); 00285 if ( !d->ratingPainter.customPixmap().isNull() ) { 00286 pixSize = d->ratingPainter.customPixmap().size(); 00287 } 00288 00289 return QSize( pixSize.width()*numPix + spacing()*(numPix-1) + frameWidth()*2, 00290 pixSize.height() + frameWidth()*2 ); 00291 } 00292 00293 00294 void KRatingWidget::resizeEvent( QResizeEvent* e ) 00295 { 00296 QFrame::resizeEvent( e ); 00297 } 00298 00299 #include "kratingwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:53:05 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:53:05 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.