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 #include <kiconeffect.h> 00031 #include <kiconloader.h> 00032 #include <klocale.h> 00033 #include <kstandarddirs.h> 00034 00035 class KRatingWidget::Private 00036 { 00037 public: 00038 Private() 00039 : rating(0), 00040 hoverRating(-1), 00041 pixSize( 16 ) { 00042 } 00043 00044 int rating; 00045 int hoverRating; 00046 int pixSize; 00047 00048 KRatingPainter ratingPainter; 00049 }; 00050 00051 00052 00053 KRatingWidget::KRatingWidget( QWidget* parent ) 00054 : QFrame( parent ), 00055 d( new Private() ) 00056 { 00057 setMouseTracking( true ); 00058 } 00059 00060 00061 KRatingWidget::~KRatingWidget() 00062 { 00063 delete d; 00064 } 00065 00066 00067 #ifndef KDE_NO_DEPRECATED 00068 void KRatingWidget::setPixmap( const QPixmap& pix ) 00069 { 00070 setCustomPixmap( pix ); 00071 } 00072 #endif 00073 00074 00075 void KRatingWidget::setCustomPixmap( const QPixmap& pix ) 00076 { 00077 d->ratingPainter.setCustomPixmap( pix ); 00078 update(); 00079 } 00080 00081 00082 void KRatingWidget::setIcon( const QIcon& icon ) 00083 { 00084 d->ratingPainter.setIcon( icon ); 00085 update(); 00086 } 00087 00088 00089 void KRatingWidget::setPixmapSize( int size ) 00090 { 00091 d->pixSize = size; 00092 updateGeometry(); 00093 } 00094 00095 00096 int KRatingWidget::spacing() const 00097 { 00098 return d->ratingPainter.spacing(); 00099 } 00100 00101 00102 QIcon KRatingWidget::icon() const 00103 { 00104 return d->ratingPainter.icon(); 00105 } 00106 00107 00108 void KRatingWidget::setSpacing( int s ) 00109 { 00110 d->ratingPainter.setSpacing( s ); 00111 update(); 00112 } 00113 00114 00115 Qt::Alignment KRatingWidget::alignment() const 00116 { 00117 return d->ratingPainter.alignment(); 00118 } 00119 00120 00121 void KRatingWidget::setAlignment( Qt::Alignment align ) 00122 { 00123 d->ratingPainter.setAlignment( align ); 00124 update(); 00125 } 00126 00127 00128 Qt::LayoutDirection KRatingWidget::layoutDirection() const 00129 { 00130 return d->ratingPainter.layoutDirection(); 00131 } 00132 00133 00134 void KRatingWidget::setLayoutDirection( Qt::LayoutDirection direction ) 00135 { 00136 d->ratingPainter.setLayoutDirection( direction ); 00137 update(); 00138 } 00139 00140 00141 unsigned int KRatingWidget::rating() const 00142 { 00143 return d->rating; 00144 } 00145 00146 00147 int KRatingWidget::maxRating() const 00148 { 00149 return d->ratingPainter.maxRating(); 00150 } 00151 00152 00153 bool KRatingWidget::halfStepsEnabled() const 00154 { 00155 return d->ratingPainter.halfStepsEnabled(); 00156 } 00157 00158 00159 #ifndef KDE_NO_DEPRECATED 00160 void KRatingWidget::setRating( unsigned int rating ) 00161 { 00162 setRating( (int)rating ); 00163 } 00164 #endif 00165 00166 00167 void KRatingWidget::setRating( int rating ) 00168 { 00169 if ( rating != d->rating ) { 00170 d->rating = rating; 00171 d->hoverRating = rating; 00172 emit ratingChanged( rating ); 00173 emit ratingChanged( (unsigned int)rating ); 00174 update(); 00175 } 00176 } 00177 00178 00179 #ifndef KDE_NO_DEPRECATED 00180 void KRatingWidget::setMaxRating( unsigned int max ) 00181 { 00182 setMaxRating( (int)max ); 00183 } 00184 #endif 00185 00186 00187 void KRatingWidget::setMaxRating( int max ) 00188 { 00189 d->ratingPainter.setMaxRating( max ); 00190 update(); 00191 } 00192 00193 00194 void KRatingWidget::setHalfStepsEnabled( bool enabled ) 00195 { 00196 d->ratingPainter.setHalfStepsEnabled( enabled ); 00197 update(); 00198 } 00199 00200 00201 #ifndef KDE_NO_DEPRECATED 00202 void KRatingWidget::setOnlyPaintFullSteps( bool fs ) 00203 { 00204 setHalfStepsEnabled( !fs ); 00205 } 00206 #endif 00207 00208 00209 void KRatingWidget::mousePressEvent( QMouseEvent* e ) 00210 { 00211 if ( e->button() == Qt::LeftButton ) { 00212 d->hoverRating = d->ratingPainter.ratingFromPosition( contentsRect(), e->pos() ); 00213 setRating( d->hoverRating ); 00214 } 00215 } 00216 00217 00218 void KRatingWidget::mouseMoveEvent( QMouseEvent* e ) 00219 { 00220 // when moving the mouse we show the user what the result of clicking will be 00221 const int prevHoverRating = d->hoverRating; 00222 d->hoverRating = d->ratingPainter.ratingFromPosition( contentsRect(), e->pos() ); 00223 if ( d->hoverRating != prevHoverRating ) { 00224 update(); 00225 } 00226 if ( d->hoverRating >= 0 && e->buttons() & Qt::LeftButton ) { 00227 setRating( d->hoverRating ); 00228 } 00229 } 00230 00231 00232 void KRatingWidget::leaveEvent( QEvent* ) 00233 { 00234 d->hoverRating = -1; 00235 update(); 00236 } 00237 00238 00239 void KRatingWidget::paintEvent( QPaintEvent* e ) 00240 { 00241 QFrame::paintEvent( e ); 00242 QPainter p( this ); 00243 d->ratingPainter.setEnabled( isEnabled() ); 00244 d->ratingPainter.paint( &p, contentsRect(), d->rating, d->hoverRating ); 00245 } 00246 00247 00248 QSize KRatingWidget::sizeHint() const 00249 { 00250 int numPix = d->ratingPainter.maxRating(); 00251 if( d->ratingPainter.halfStepsEnabled() ) 00252 numPix /= 2; 00253 00254 QSize pixSize( d->pixSize, d->pixSize ); 00255 if ( !d->ratingPainter.customPixmap().isNull() ) { 00256 pixSize = d->ratingPainter.customPixmap().size(); 00257 } 00258 00259 return QSize( pixSize.width()*numPix + spacing()*(numPix-1) + frameWidth()*2, 00260 pixSize.height() + frameWidth()*2 ); 00261 } 00262 00263 00264 void KRatingWidget::resizeEvent( QResizeEvent* e ) 00265 { 00266 QFrame::resizeEvent( e ); 00267 } 00268 00269 #include "kratingwidget.moc"
KDE 4.6 API Reference