• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames

kgameprogress.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 1996 Martynas Kunigelis martynas.kunigelis@vm.ktu.lt
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00022 #include "kgameprogress.h"
00023 
00024 #include <QPaintEvent>
00025 #include <QPainter>
00026 #include <QPixmap>
00027 #include <QRegExp>
00028 #include <QStyle>
00029 #include <QFrame>
00030 #include <QApplication>
00031 #include <QtGui/QAbstractSlider>
00032 
00033 #include <kglobalsettings.h>
00034 
00035 class KGameProgress::KGameProgressPrivate
00036 {
00037 public:
00038     KGameProgressPrivate(KGameProgress *qq)
00039         : q(qq)
00040     {
00041     }
00042 
00043     KGameProgress *q;
00044 
00045     void initialize();
00046     int recalcValue(int);
00047     void drawText(QPainter *);
00048     void adjustStyle();
00049 
00050     QPixmap *bar_pixmap;
00051     bool use_supplied_bar_color;
00052     QColor bar_color;
00053     QColor bar_text_color;
00054     QColor text_color;
00055     QRect fr;
00056     BarStyle bar_style;
00057     bool text_enabled;
00058     QString format_;
00059     QAbstractSlider *slider;
00060 };
00061 
00062 
00063 KGameProgress::KGameProgress(QWidget *parent)
00064     : QFrame(parent), d(new KGameProgressPrivate(this))
00065 {
00066     d->initialize();
00067     d->slider->setOrientation(Qt::Horizontal);
00068 }
00069 
00070 KGameProgress::KGameProgress(Qt::Orientation orientation, QWidget *parent)
00071     : QFrame(parent), d(new KGameProgressPrivate(this))
00072 {
00073     d->initialize();
00074     d->slider->setOrientation(orientation);
00075 }
00076 
00077 KGameProgress::~KGameProgress()
00078 {
00079     delete d->bar_pixmap;
00080     delete d;
00081 }
00082 
00083 void KGameProgress::advance(int offset)
00084 {
00085     setValue(value() + offset);
00086 }
00087 
00088 void KGameProgress::KGameProgressPrivate::initialize()
00089 {
00090     slider = new QAbstractSlider(q);
00091 
00092     slider->setMinimum(0);
00093     slider->setMaximum(100);
00094     slider->setValue(0);
00095 
00096     format_ = "%p%";
00097     use_supplied_bar_color = false;
00098     bar_pixmap = 0;
00099     bar_style = Solid;
00100     text_enabled = true;
00101     connect(slider, SIGNAL(valueChanged(int)), q, SLOT(valueChange(int)));
00102     connect(KGlobalSettings::self(), SIGNAL(appearanceChanged()), q, SLOT(paletteChange()));
00103     q->paletteChange();
00104 }
00105 
00106 void KGameProgress::paletteChange()
00107 {
00108     QPalette p = qApp->palette();
00109     if (!d->use_supplied_bar_color)
00110         d->bar_color = p.color( QPalette::Active, QPalette::Highlight );
00111     d->bar_text_color = p.color( QPalette::Active, QPalette::HighlightedText );
00112     d->text_color = p.color( QPalette::Active, QPalette::Text );
00113     setPalette(p);
00114 
00115     d->adjustStyle();
00116 }
00117 
00118 
00119 void KGameProgress::setBarPixmap(const QPixmap &pixmap)
00120 {
00121     if (pixmap.isNull())
00122         return;
00123     if (d->bar_pixmap)
00124         delete d->bar_pixmap;
00125 
00126     d->bar_pixmap = new QPixmap(pixmap);
00127 }
00128 
00129 void KGameProgress::setBarColor(const QColor &color)
00130 {
00131     d->bar_color = color;
00132     d->use_supplied_bar_color = true;
00133     if (d->bar_pixmap) {
00134         delete d->bar_pixmap;
00135         d->bar_pixmap = 0;
00136     }
00137 }
00138 
00139 void KGameProgress::setBarStyle(BarStyle style)
00140 {
00141     if (d->bar_style != style) {
00142         d->bar_style = style;
00143         update();
00144     }
00145 }
00146 
00147 void KGameProgress::setOrientation(Qt::Orientation orientation)
00148 {
00149     if (this->orientation() != orientation) {
00150         setOrientation(orientation);
00151         update();
00152     }
00153 }
00154 
00155 void KGameProgress::setValue(int value)
00156 {
00157     d->slider->setValue( value );
00158 }
00159 
00160 void KGameProgress::setMinimum(int value)
00161 {
00162     d->slider->setMinimum( value );
00163 }
00164 
00165 void KGameProgress::setMaximum(int value)
00166 {
00167     d->slider->setMaximum( value );
00168 }
00169 
00170 void KGameProgress::setTextEnabled(bool enable)
00171 {
00172     d->text_enabled = enable;
00173 }
00174 
00175 QColor KGameProgress::barColor() const
00176 {
00177     return d->bar_color;
00178 }
00179 
00180 const QPixmap * KGameProgress::barPixmap() const
00181 {
00182     return d->bar_pixmap;
00183 }
00184 
00185 int KGameProgress::value() const
00186 {
00187     return d->slider->value();
00188 }
00189 
00190 int KGameProgress::minimum() const
00191 {
00192     return d->slider->minimum();
00193 }
00194 
00195 int KGameProgress::maximum() const
00196 {
00197     return d->slider->maximum();
00198 }
00199 
00200 bool KGameProgress::textEnabled() const
00201 {
00202     return d->text_enabled;
00203 }
00204 
00205 QSize KGameProgress::sizeHint() const
00206 {
00207     QSize s( size() );
00208 
00209     if(orientation() == Qt::Vertical) {
00210         s.setWidth(24);
00211     } else {
00212         s.setHeight(24);
00213     }
00214 
00215     return s;
00216 }
00217 
00218 QSize KGameProgress::minimumSizeHint() const
00219 {
00220     return sizeHint();
00221 }
00222 
00223 QSizePolicy KGameProgress::sizePolicy() const
00224 {
00225     if ( orientation()==Qt::Vertical )
00226         return QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Expanding );
00227     else
00228         return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00229 }
00230 
00231 Qt::Orientation KGameProgress::orientation() const
00232 {
00233     return d->slider->orientation();
00234 }
00235 
00236 KGameProgress::BarStyle KGameProgress::barStyle() const
00237 {
00238     return d->bar_style;
00239 }
00240 
00241 int KGameProgress::KGameProgressPrivate::recalcValue(int range)
00242 {
00243     int abs_value = q->value() - q->minimum();
00244     int abs_range = q->maximum() - q->minimum();
00245     return abs_range ? range * abs_value / abs_range : 0;
00246 }
00247 
00248 void KGameProgress::valueChange(int newValue)
00249 {
00250     Q_UNUSED(newValue);
00251 
00252     repaint(contentsRect());
00253     emit percentageChanged(d->recalcValue(100));
00254 }
00255 
00256 void KGameProgress::styleChange(QStyle&)
00257 {
00258     d->adjustStyle();
00259 }
00260 
00261 void KGameProgress::KGameProgressPrivate::adjustStyle()
00262 {
00264 /*  switch (style()->styleHint(QStyle::SH_GUIStyle)) {
00265         case Qt::WindowsStyle:
00266             setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
00267             break;
00268         case Qt::MotifStyle:
00269         default:
00270             setFrameStyle(QFrame::Panel | QFrame::Sunken);
00271             setLineWidth( 2 );
00272             break;
00273     }*/
00274     q->update();
00275 }
00276 
00277 void KGameProgress::paletteChange( const QPalette &p )
00278 {
00279     // This never gets called for global color changes 
00280     // because we call setPalette() ourselves.
00281     QFrame::paletteChange(p);
00282 }
00283 
00284 void KGameProgress::KGameProgressPrivate::drawText(QPainter *p)
00285 {
00286     QRect r(q->contentsRect());
00287     //QColor c(bar_color.rgb() ^ backgroundColor().rgb());
00288 
00289     // Rik: Replace the tags '%p', '%v' and '%m' with the current percentage,
00290     // the current value and the maximum value respectively.
00291     QString s(format_);
00292 
00293     s.replace(QRegExp(QString::fromLatin1("%p")), QString::number(recalcValue(100)));
00294     s.replace(QRegExp(QString::fromLatin1("%v")), QString::number(q->value()));
00295     s.replace(QRegExp(QString::fromLatin1("%m")), QString::number(q->maximum()));
00296 
00297     p->setPen(text_color);
00298     QFont font = p->font();
00299     font.setBold(true);
00300     p->setFont(font);
00301     //p->setRasterOp(XorROP);
00302     p->drawText(r, Qt::AlignCenter, s);
00303     p->setClipRegion( fr );
00304     p->setPen(bar_text_color);
00305     p->drawText(r, Qt::AlignCenter, s);
00306 }
00307 
00308 void KGameProgress::paintEvent( QPaintEvent *e )
00309 {
00310     //paint the frame
00311     QFrame::paintEvent(e);
00312 
00313     //now for the inside of the Widget
00314     QPainter p(this);
00315     
00316     QRect cr = contentsRect(), er = cr;
00317     d->fr = cr;
00318         QBrush fb(d->bar_color), eb(palette().color(backgroundRole()));
00319 
00320     if (d->bar_pixmap)
00321         fb.setTexture(*d->bar_pixmap);
00322 
00323         QPixmap bkgnd_pix = palette().brush(backgroundRole()).texture();
00324     if (!bkgnd_pix.isNull())
00325         eb.setTexture(bkgnd_pix);
00326 
00327     switch (d->bar_style) {
00328         case Solid:
00329             if (orientation() == Qt::Horizontal) {
00330                 d->fr.setWidth(d->recalcValue(cr.width()));
00331                 er.setLeft(d->fr.right() + 1);
00332             } else {
00333                 d->fr.setTop(cr.bottom() - d->recalcValue(cr.height()));
00334                 er.setBottom(d->fr.top() - 1);
00335             }
00336 
00337             p.setBrushOrigin(cr.topLeft());
00338             p.fillRect(d->fr, fb);
00339 
00340             p.fillRect(er, eb);
00341 
00342             break;
00343 
00344         case Blocked:
00345             const int margin = 2;
00346             int max, num, dx, dy;
00347             if (orientation() == Qt::Horizontal) {
00348                 d->fr.setHeight(cr.height() - 2 * margin);
00349                 d->fr.setWidth((int)(0.67 * d->fr.height()));
00350                 d->fr.moveTopLeft(QPoint(cr.left() + margin, cr.top() + margin));
00351                 dx = d->fr.width() + margin;
00352                 dy = 0;
00353                 max = (cr.width() - margin) / (d->fr.width() + margin) + 1;
00354                 num = d->recalcValue(max);
00355             } else {
00356                 d->fr.setWidth(cr.width() - 2 * margin);
00357                 d->fr.setHeight((int)(0.67 * d->fr.width()));
00358                 d->fr.moveBottomLeft(QPoint(cr.left() + margin, cr.bottom() - margin));
00359                 dx = 0;
00360                 dy = - (d->fr.height() + margin);
00361                 max = (cr.height() - margin) / (d->fr.height() + margin) + 1;
00362                 num = d->recalcValue(max);
00363             }
00364             p.setClipRect(cr.x() + margin, cr.y() + margin,
00365                            cr.width() - margin, cr.height() - margin);
00366             for (int i = 0; i < num; i++) {
00367                 p.setBrushOrigin(d->fr.topLeft());
00368                 p.fillRect(d->fr, fb);
00369                 d->fr.translate(dx, dy);
00370             }
00371             
00372             if (num != max) {
00373                 if (orientation() == Qt::Horizontal)
00374                     er.setLeft(d->fr.right() + 1);
00375                 else
00376                     er.setBottom(d->fr.bottom() + 1);
00377                 if (!er.isNull()) {
00378                     p.setBrushOrigin(cr.topLeft());
00379                     p.fillRect(er, eb);
00380                 }
00381             }
00382 
00383             break;
00384     }
00385 
00386     if (d->text_enabled && d->bar_style != Blocked)
00387         d->drawText(&p);
00388 }
00389 
00390 void KGameProgress::setFormat(const QString & format)
00391 {
00392     d->format_ = format;
00393 }
00394 
00395 QString KGameProgress::format() const
00396 {
00397     return d->format_;
00398 }
00399 
00400 #include "kgameprogress.moc"

libkdegames

Skip menu "libkdegames"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal