00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00022 #include <qpainter.h>
00023 #include <qpixmap.h>
00024 #include <qstring.h>
00025 #include <qregexp.h>
00026 #include <qstyle.h>
00027
00028 #include "kgameprogress.h"
00029
00030 #include <kapplication.h>
00031
00032 KGameProgress::KGameProgress(QWidget *parent, const char *name)
00033 : QFrame(parent, name),
00034 QRangeControl(0, 100, 1, 10, 0),
00035 orient(Horizontal)
00036 {
00037 initialize();
00038 }
00039
00040 KGameProgress::KGameProgress(Orientation orientation, QWidget *parent, const char *name)
00041 : QFrame(parent, name),
00042 QRangeControl(0, 100, 1, 10, 0),
00043 orient(orientation)
00044 {
00045 initialize();
00046 }
00047
00048 KGameProgress::KGameProgress(int minValue, int maxValue, int value,
00049 Orientation orientation, QWidget *parent, const char *name)
00050 : QFrame(parent, name),
00051 QRangeControl(minValue, maxValue, 1, 10, value),
00052 orient(orientation)
00053 {
00054 initialize();
00055 }
00056
00057 KGameProgress::~KGameProgress()
00058 {
00059 delete bar_pixmap;
00060 }
00061
00062 void KGameProgress::advance(int offset)
00063 {
00064 setValue(value() + offset);
00065 }
00066
00067 void KGameProgress::initialize()
00068 {
00069 format_ = "%p%";
00070 use_supplied_bar_color = false;
00071 bar_pixmap = 0;
00072 bar_style = Solid;
00073 text_enabled = TRUE;
00074 setBackgroundMode( PaletteBackground );
00075 connect(kapp, SIGNAL(appearanceChanged()), this, SLOT(paletteChange()));
00076 paletteChange();
00077 }
00078
00079 void KGameProgress::paletteChange()
00080 {
00081 QPalette p = kapp->palette();
00082 const QColorGroup &colorGroup = p.active();
00083 if (!use_supplied_bar_color)
00084 bar_color = colorGroup.highlight();
00085 bar_text_color = colorGroup.highlightedText();
00086 text_color = colorGroup.text();
00087 setPalette(p);
00088
00089 adjustStyle();
00090 }
00091
00092
00093 void KGameProgress::setBarPixmap(const QPixmap &pixmap)
00094 {
00095 if (pixmap.isNull())
00096 return;
00097 if (bar_pixmap)
00098 delete bar_pixmap;
00099
00100 bar_pixmap = new QPixmap(pixmap);
00101 }
00102
00103 void KGameProgress::setBarColor(const QColor &color)
00104 {
00105 bar_color = color;
00106 use_supplied_bar_color = true;
00107 if (bar_pixmap) {
00108 delete bar_pixmap;
00109 bar_pixmap = 0;
00110 }
00111 }
00112
00113 void KGameProgress::setBarStyle(BarStyle style)
00114 {
00115 if (bar_style != style) {
00116 bar_style = style;
00117 update();
00118 }
00119 }
00120
00121 void KGameProgress::setOrientation(Orientation orientation)
00122 {
00123 if (orient != orientation) {
00124 orient = orientation;
00125 update();
00126 }
00127 }
00128
00129 void KGameProgress::setValue(int value)
00130 {
00131 QRangeControl::setValue(value);
00132 }
00133
00134 void KGameProgress::setTextEnabled(bool enable)
00135 {
00136 text_enabled = enable;
00137 }
00138
00139 const QColor & KGameProgress::barColor() const
00140 {
00141 return bar_color;
00142 }
00143
00144 const QPixmap * KGameProgress::barPixmap() const
00145 {
00146 return bar_pixmap;
00147 }
00148
00149 bool KGameProgress::textEnabled() const
00150 {
00151 return text_enabled;
00152 }
00153
00154 QSize KGameProgress::sizeHint() const
00155 {
00156 QSize s( size() );
00157
00158 if(orientation() == KGameProgress::Vertical) {
00159 s.setWidth(24);
00160 } else {
00161 s.setHeight(24);
00162 }
00163
00164 return s;
00165 }
00166
00167 QSize KGameProgress::minimumSizeHint() const
00168 {
00169 return sizeHint();
00170 }
00171
00172 QSizePolicy KGameProgress::sizePolicy() const
00173 {
00174 if ( orientation()==KGameProgress::Vertical )
00175 return QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Expanding );
00176 else
00177 return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00178 }
00179
00180 KGameProgress::Orientation KGameProgress::orientation() const
00181 {
00182 return orient;
00183 }
00184
00185 KGameProgress::BarStyle KGameProgress::barStyle() const
00186 {
00187 return bar_style;
00188 }
00189
00190 int KGameProgress::recalcValue(int range)
00191 {
00192 int abs_value = value() - minValue();
00193 int abs_range = maxValue() - minValue();
00194 return abs_range ? range * abs_value / abs_range : 0;
00195 }
00196
00197 void KGameProgress::valueChange()
00198 {
00199 repaint(contentsRect(), FALSE);
00200 emit percentageChanged(recalcValue(100));
00201 }
00202
00203 void KGameProgress::rangeChange()
00204 {
00205 repaint(contentsRect(), FALSE);
00206 emit percentageChanged(recalcValue(100));
00207 }
00208
00209 void KGameProgress::styleChange(QStyle&)
00210 {
00211 adjustStyle();
00212 }
00213
00214 void KGameProgress::adjustStyle()
00215 {
00216 switch (style().styleHint(QStyle::SH_GUIStyle)) {
00217 case WindowsStyle:
00218 setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
00219 break;
00220 case MotifStyle:
00221 default:
00222 setFrameStyle(QFrame::Panel | QFrame::Sunken);
00223 setLineWidth( 2 );
00224 break;
00225 }
00226 update();
00227 }
00228
00229 void KGameProgress::paletteChange( const QPalette &p )
00230 {
00231
00232
00233 QFrame::paletteChange(p);
00234 }
00235
00236 void KGameProgress::drawText(QPainter *p)
00237 {
00238 QRect r(contentsRect());
00239
00240
00241
00242
00243 QString s(format_);
00244
00245 s.replace(QRegExp(QString::fromLatin1("%p")), QString::number(recalcValue(100)));
00246 s.replace(QRegExp(QString::fromLatin1("%v")), QString::number(value()));
00247 s.replace(QRegExp(QString::fromLatin1("%m")), QString::number(maxValue()));
00248
00249 p->setPen(text_color);
00250 QFont font = p->font();
00251 font.setBold(true);
00252 p->setFont(font);
00253
00254 p->drawText(r, AlignCenter, s);
00255 p->setClipRegion( fr );
00256 p->setPen(bar_text_color);
00257 p->drawText(r, AlignCenter, s);
00258 }
00259
00260 void KGameProgress::drawContents(QPainter *p)
00261 {
00262 QRect cr = contentsRect(), er = cr;
00263 fr = cr;
00264 QBrush fb(bar_color), eb(backgroundColor());
00265
00266 if (bar_pixmap)
00267 fb.setPixmap(*bar_pixmap);
00268
00269 if (backgroundPixmap())
00270 eb.setPixmap(*backgroundPixmap());
00271
00272 switch (bar_style) {
00273 case Solid:
00274 if (orient == Horizontal) {
00275 fr.setWidth(recalcValue(cr.width()));
00276 er.setLeft(fr.right() + 1);
00277 } else {
00278 fr.setTop(cr.bottom() - recalcValue(cr.height()));
00279 er.setBottom(fr.top() - 1);
00280 }
00281
00282 p->setBrushOrigin(cr.topLeft());
00283 p->fillRect(fr, fb);
00284
00285 p->fillRect(er, eb);
00286
00287 break;
00288
00289 case Blocked:
00290 const int margin = 2;
00291 int max, num, dx, dy;
00292 if (orient == Horizontal) {
00293 fr.setHeight(cr.height() - 2 * margin);
00294 fr.setWidth((int)(0.67 * fr.height()));
00295 fr.moveTopLeft(QPoint(cr.left() + margin, cr.top() + margin));
00296 dx = fr.width() + margin;
00297 dy = 0;
00298 max = (cr.width() - margin) / (fr.width() + margin) + 1;
00299 num = recalcValue(max);
00300 } else {
00301 fr.setWidth(cr.width() - 2 * margin);
00302 fr.setHeight((int)(0.67 * fr.width()));
00303 fr.moveBottomLeft(QPoint(cr.left() + margin, cr.bottom() - margin));
00304 dx = 0;
00305 dy = - (fr.height() + margin);
00306 max = (cr.height() - margin) / (fr.height() + margin) + 1;
00307 num = recalcValue(max);
00308 }
00309 p->setClipRect(cr.x() + margin, cr.y() + margin,
00310 cr.width() - margin, cr.height() - margin);
00311 for (int i = 0; i < num; i++) {
00312 p->setBrushOrigin(fr.topLeft());
00313 p->fillRect(fr, fb);
00314 fr.moveBy(dx, dy);
00315 }
00316
00317 if (num != max) {
00318 if (orient == Horizontal)
00319 er.setLeft(fr.right() + 1);
00320 else
00321 er.setBottom(fr.bottom() + 1);
00322 if (!er.isNull()) {
00323 p->setBrushOrigin(cr.topLeft());
00324 p->fillRect(er, eb);
00325 }
00326 }
00327
00328 break;
00329 }
00330
00331 if (text_enabled && bar_style != Blocked)
00332 drawText(p);
00333 }
00334
00335 void KGameProgress::setFormat(const QString & format)
00336 {
00337 format_ = format;
00338 }
00339
00340 QString KGameProgress::format() const
00341 {
00342 return format_;
00343 }
00344
00345 #include "kgameprogress.moc"