kgameprogress.cpp
00001 /* This file is part of the TDE libraries 00002 Copyright (C) 1996 Martynas Kunigelis 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 <tqpainter.h> 00023 #include <tqpixmap.h> 00024 #include <tqstring.h> 00025 #include <tqregexp.h> 00026 #include <tqstyle.h> 00027 00028 #include "kgameprogress.h" 00029 00030 #include <tdeapplication.h> 00031 00032 KGameProgress::KGameProgress(TQWidget *parent, const char *name) 00033 : TQFrame(parent, name), 00034 TQRangeControl(0, 100, 1, 10, 0), 00035 orient(Qt::Horizontal) 00036 { 00037 initialize(); 00038 } 00039 00040 KGameProgress::KGameProgress(Qt::Orientation orientation, TQWidget *parent, const char *name) 00041 : TQFrame(parent, name), 00042 TQRangeControl(0, 100, 1, 10, 0), 00043 orient(orientation) 00044 { 00045 initialize(); 00046 } 00047 00048 KGameProgress::KGameProgress(int minValue, int maxValue, int value, 00049 Qt::Orientation orientation, TQWidget *parent, const char *name) 00050 : TQFrame(parent, name), 00051 TQRangeControl(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, TQT_SIGNAL(appearanceChanged()), this, TQT_SLOT(paletteChange())); 00076 paletteChange(); 00077 } 00078 00079 void KGameProgress::paletteChange() 00080 { 00081 TQPalette p = kapp->palette(); 00082 const TQColorGroup &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 TQPixmap &pixmap) 00094 { 00095 if (pixmap.isNull()) 00096 return; 00097 if (bar_pixmap) 00098 delete bar_pixmap; 00099 00100 bar_pixmap = new TQPixmap(pixmap); 00101 } 00102 00103 void KGameProgress::setBarColor(const TQColor &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(Qt::Orientation orientation) 00122 { 00123 if (orient != orientation) { 00124 orient = orientation; 00125 update(); 00126 } 00127 } 00128 00129 void KGameProgress::setValue(int value) 00130 { 00131 TQRangeControl::setValue(value); 00132 } 00133 00134 void KGameProgress::setTextEnabled(bool enable) 00135 { 00136 text_enabled = enable; 00137 } 00138 00139 const TQColor & KGameProgress::barColor() const 00140 { 00141 return bar_color; 00142 } 00143 00144 const TQPixmap * KGameProgress::barPixmap() const 00145 { 00146 return bar_pixmap; 00147 } 00148 00149 bool KGameProgress::textEnabled() const 00150 { 00151 return text_enabled; 00152 } 00153 00154 TQSize KGameProgress::sizeHint() const 00155 { 00156 TQSize s( size() ); 00157 00158 if(orientation() == Qt::Vertical) { 00159 s.setWidth(24); 00160 } else { 00161 s.setHeight(24); 00162 } 00163 00164 return s; 00165 } 00166 00167 TQSize KGameProgress::minimumSizeHint() const 00168 { 00169 return sizeHint(); 00170 } 00171 00172 TQSizePolicy KGameProgress::sizePolicy() const 00173 { 00174 if ( orientation()==Qt::Vertical ) 00175 return TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Expanding ); 00176 else 00177 return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::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(TQStyle&) 00210 { 00211 adjustStyle(); 00212 } 00213 00214 void KGameProgress::adjustStyle() 00215 { 00216 switch (style().styleHint(TQStyle::SH_GUIStyle)) { 00217 case WindowsStyle: 00218 setFrameStyle(TQFrame::WinPanel | TQFrame::Sunken); 00219 break; 00220 case MotifStyle: 00221 default: 00222 setFrameStyle(TQFrame::Panel | TQFrame::Sunken); 00223 setLineWidth( 2 ); 00224 break; 00225 } 00226 update(); 00227 } 00228 00229 void KGameProgress::paletteChange( const TQPalette &p ) 00230 { 00231 // This never gets called for global color changes 00232 // because we call setPalette() ourselves. 00233 TQFrame::paletteChange(p); 00234 } 00235 00236 void KGameProgress::drawText(TQPainter *p) 00237 { 00238 TQRect r(contentsRect()); 00239 //TQColor c(bar_color.rgb() ^ backgroundColor().rgb()); 00240 00241 // Rik: Replace the tags '%p', '%v' and '%m' with the current percentage, 00242 // the current value and the maximum value respectively. 00243 TQString s(format_); 00244 00245 s.replace(TQRegExp(TQString::fromLatin1("%p")), TQString::number(recalcValue(100))); 00246 s.replace(TQRegExp(TQString::fromLatin1("%v")), TQString::number(value())); 00247 s.replace(TQRegExp(TQString::fromLatin1("%m")), TQString::number(maxValue())); 00248 00249 p->setPen(text_color); 00250 TQFont font = p->font(); 00251 font.setBold(true); 00252 p->setFont(font); 00253 //p->setRasterOp(XorROP); 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(TQPainter *p) 00261 { 00262 TQRect cr = contentsRect(), er = cr; 00263 fr = cr; 00264 TQBrush 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 ==Qt::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 ==Qt::Horizontal) { 00293 fr.setHeight(cr.height() - 2 * margin); 00294 fr.setWidth((int)(0.67 * fr.height())); 00295 fr.moveTopLeft(TQPoint(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(TQPoint(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 ==Qt::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 TQString & format) 00336 { 00337 format_ = format; 00338 } 00339 00340 TQString KGameProgress::format() const 00341 { 00342 return format_; 00343 } 00344 00345 #include "kgameprogress.moc"