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 d->hoverRating = d->ratingPainter.ratingFromPosition( contentsRect(), e->pos() ); 00208 setRating( d->hoverRating ); 00209 } 00210 } 00211 00212 00213 void KRatingWidget::mouseMoveEvent( QMouseEvent* e ) 00214 { 00215 // when moving the mouse we show the user what the result of clicking will be 00216 const int prevHoverRating = d->hoverRating; 00217 d->hoverRating = d->ratingPainter.ratingFromPosition( contentsRect(), e->pos() ); 00218 if ( d->hoverRating != prevHoverRating ) { 00219 update(); 00220 } 00221 if ( d->hoverRating >= 0 && e->buttons() & Qt::LeftButton ) { 00222 setRating( d->hoverRating ); 00223 } 00224 } 00225 00226 00227 void KRatingWidget::leaveEvent( QEvent* ) 00228 { 00229 d->hoverRating = -1; 00230 update(); 00231 } 00232 00233 00234 void KRatingWidget::paintEvent( QPaintEvent* e ) 00235 { 00236 QFrame::paintEvent( e ); 00237 QPainter p( this ); 00238 d->ratingPainter.setEnabled( isEnabled() ); 00239 d->ratingPainter.paint( &p, contentsRect(), d->rating, d->hoverRating ); 00240 } 00241 00242 00243 QSize KRatingWidget::sizeHint() const 00244 { 00245 int numPix = d->ratingPainter.maxRating(); 00246 if( d->ratingPainter.halfStepsEnabled() ) 00247 numPix /= 2; 00248 00249 QSize pixSize( d->pixSize, d->pixSize ); 00250 if ( !d->ratingPainter.customPixmap().isNull() ) { 00251 pixSize = d->ratingPainter.customPixmap().size(); 00252 } 00253 00254 return QSize( pixSize.width()*numPix + spacing()*(numPix-1) + frameWidth()*2, 00255 pixSize.height() + frameWidth()*2 ); 00256 } 00257 00258 00259 void KRatingWidget::resizeEvent( QResizeEvent* e ) 00260 { 00261 QFrame::resizeEvent( e ); 00262 } 00263 00264 #include "kratingwidget.moc"
KDE 4.7 API Reference