• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

  • libtdegames
kgameprogress.cpp
1 /* This file is part of the TDE libraries
2  Copyright (C) 1996 Martynas Kunigelis
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
22 #include <tqpainter.h>
23 #include <tqpixmap.h>
24 #include <tqstring.h>
25 #include <tqregexp.h>
26 #include <tqstyle.h>
27 
28 #include "kgameprogress.h"
29 
30 #include <tdeapplication.h>
31 
32 KGameProgress::KGameProgress(TQWidget *parent, const char *name)
33  : TQFrame(parent, name),
34  TQRangeControl(0, 100, 1, 10, 0),
35  orient(TQt::Horizontal)
36 {
37  initialize();
38 }
39 
40 KGameProgress::KGameProgress(TQt::Orientation orientation, TQWidget *parent, const char *name)
41  : TQFrame(parent, name),
42  TQRangeControl(0, 100, 1, 10, 0),
43  orient(orientation)
44 {
45  initialize();
46 }
47 
48 KGameProgress::KGameProgress(int minValue, int maxValue, int value,
49  TQt::Orientation orientation, TQWidget *parent, const char *name)
50  : TQFrame(parent, name),
51  TQRangeControl(minValue, maxValue, 1, 10, value),
52  orient(orientation)
53 {
54  initialize();
55 }
56 
57 KGameProgress::~KGameProgress()
58 {
59  delete bar_pixmap;
60 }
61 
62 void KGameProgress::advance(int offset)
63 {
64  setValue(value() + offset);
65 }
66 
67 void KGameProgress::initialize()
68 {
69  format_ = "%p%";
70  use_supplied_bar_color = false;
71  bar_pixmap = 0;
72  bar_style = Solid;
73  text_enabled = true;
74  setBackgroundMode( PaletteBackground );
75  connect(kapp, TQ_SIGNAL(appearanceChanged()), this, TQ_SLOT(paletteChange()));
76  paletteChange();
77 }
78 
79 void KGameProgress::paletteChange()
80 {
81  TQPalette p = kapp->palette();
82  const TQColorGroup &colorGroup = p.active();
83  if (!use_supplied_bar_color)
84  bar_color = colorGroup.highlight();
85  bar_text_color = colorGroup.highlightedText();
86  text_color = colorGroup.text();
87  setPalette(p);
88 
89  adjustStyle();
90 }
91 
92 
93 void KGameProgress::setBarPixmap(const TQPixmap &pixmap)
94 {
95  if (pixmap.isNull())
96  return;
97  if (bar_pixmap)
98  delete bar_pixmap;
99 
100  bar_pixmap = new TQPixmap(pixmap);
101 }
102 
103 void KGameProgress::setBarColor(const TQColor &color)
104 {
105  bar_color = color;
106  use_supplied_bar_color = true;
107  if (bar_pixmap) {
108  delete bar_pixmap;
109  bar_pixmap = 0;
110  }
111 }
112 
113 void KGameProgress::setBarStyle(BarStyle style)
114 {
115  if (bar_style != style) {
116  bar_style = style;
117  update();
118  }
119 }
120 
121 void KGameProgress::setOrientation(TQt::Orientation orientation)
122 {
123  if (orient != orientation) {
124  orient = orientation;
125  update();
126  }
127 }
128 
129 void KGameProgress::setValue(int value)
130 {
131  TQRangeControl::setValue(value);
132 }
133 
134 void KGameProgress::setTextEnabled(bool enable)
135 {
136  text_enabled = enable;
137 }
138 
139 const TQColor & KGameProgress::barColor() const
140 {
141  return bar_color;
142 }
143 
144 const TQPixmap * KGameProgress::barPixmap() const
145 {
146  return bar_pixmap;
147 }
148 
149 bool KGameProgress::textEnabled() const
150 {
151  return text_enabled;
152 }
153 
154 TQSize KGameProgress::sizeHint() const
155 {
156  TQSize s( size() );
157 
158  if(orientation() == TQt::Vertical) {
159  s.setWidth(24);
160  } else {
161  s.setHeight(24);
162  }
163 
164  return s;
165 }
166 
167 TQSize KGameProgress::minimumSizeHint() const
168 {
169  return sizeHint();
170 }
171 
172 TQSizePolicy KGameProgress::sizePolicy() const
173 {
174  if ( orientation()==TQt::Vertical )
175  return TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Expanding );
176  else
177  return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed );
178 }
179 
180 KGameProgress::Orientation KGameProgress::orientation() const
181 {
182  return orient;
183 }
184 
185 KGameProgress::BarStyle KGameProgress::barStyle() const
186 {
187  return bar_style;
188 }
189 
190 int KGameProgress::recalcValue(int range)
191 {
192  int abs_value = value() - minValue();
193  int abs_range = maxValue() - minValue();
194  return abs_range ? range * abs_value / abs_range : 0;
195 }
196 
197 void KGameProgress::valueChange()
198 {
199  repaint(contentsRect(), false);
200  emit percentageChanged(recalcValue(100));
201 }
202 
203 void KGameProgress::rangeChange()
204 {
205  repaint(contentsRect(), false);
206  emit percentageChanged(recalcValue(100));
207 }
208 
209 void KGameProgress::styleChange(TQStyle&)
210 {
211  adjustStyle();
212 }
213 
214 void KGameProgress::adjustStyle()
215 {
216  switch (style().styleHint(TQStyle::SH_GUIStyle)) {
217  case WindowsStyle:
218  setFrameStyle(TQFrame::WinPanel | TQFrame::Sunken);
219  break;
220  case MotifStyle:
221  default:
222  setFrameStyle(TQFrame::Panel | TQFrame::Sunken);
223  setLineWidth( 2 );
224  break;
225  }
226  update();
227 }
228 
229 void KGameProgress::paletteChange( const TQPalette &p )
230 {
231  // This never gets called for global color changes
232  // because we call setPalette() ourselves.
233  TQFrame::paletteChange(p);
234 }
235 
236 void KGameProgress::drawText(TQPainter *p)
237 {
238  TQRect r(contentsRect());
239  //TQColor c(bar_color.rgb() ^ backgroundColor().rgb());
240 
241  // Rik: Replace the tags '%p', '%v' and '%m' with the current percentage,
242  // the current value and the maximum value respectively.
243  TQString s(format_);
244 
245  s.replace(TQRegExp(TQString::fromLatin1("%p")), TQString::number(recalcValue(100)));
246  s.replace(TQRegExp(TQString::fromLatin1("%v")), TQString::number(value()));
247  s.replace(TQRegExp(TQString::fromLatin1("%m")), TQString::number(maxValue()));
248 
249  p->setPen(text_color);
250  TQFont font = p->font();
251  font.setBold(true);
252  p->setFont(font);
253  //p->setRasterOp(XorROP);
254  p->drawText(r, AlignCenter, s);
255  p->setClipRegion( fr );
256  p->setPen(bar_text_color);
257  p->drawText(r, AlignCenter, s);
258 }
259 
260 void KGameProgress::drawContents(TQPainter *p)
261 {
262  TQRect cr = contentsRect(), er = cr;
263  fr = cr;
264  TQBrush fb(bar_color), eb(backgroundColor());
265 
266  if (bar_pixmap)
267  fb.setPixmap(*bar_pixmap);
268 
269  if (backgroundPixmap())
270  eb.setPixmap(*backgroundPixmap());
271 
272  switch (bar_style) {
273  case Solid:
274  if (orient ==TQt::Horizontal) {
275  fr.setWidth(recalcValue(cr.width()));
276  er.setLeft(fr.right() + 1);
277  } else {
278  fr.setTop(cr.bottom() - recalcValue(cr.height()));
279  er.setBottom(fr.top() - 1);
280  }
281 
282  p->setBrushOrigin(cr.topLeft());
283  p->fillRect(fr, fb);
284 
285  p->fillRect(er, eb);
286 
287  break;
288 
289  case Blocked:
290  const int margin = 2;
291  int max, num, dx, dy;
292  if (orient ==TQt::Horizontal) {
293  fr.setHeight(cr.height() - 2 * margin);
294  fr.setWidth((int)(0.67 * fr.height()));
295  fr.moveTopLeft(TQPoint(cr.left() + margin, cr.top() + margin));
296  dx = fr.width() + margin;
297  dy = 0;
298  max = (cr.width() - margin) / (fr.width() + margin) + 1;
299  num = recalcValue(max);
300  } else {
301  fr.setWidth(cr.width() - 2 * margin);
302  fr.setHeight((int)(0.67 * fr.width()));
303  fr.moveBottomLeft(TQPoint(cr.left() + margin, cr.bottom() - margin));
304  dx = 0;
305  dy = - (fr.height() + margin);
306  max = (cr.height() - margin) / (fr.height() + margin) + 1;
307  num = recalcValue(max);
308  }
309  p->setClipRect(cr.x() + margin, cr.y() + margin,
310  cr.width() - margin, cr.height() - margin);
311  for (int i = 0; i < num; i++) {
312  p->setBrushOrigin(fr.topLeft());
313  p->fillRect(fr, fb);
314  fr.moveBy(dx, dy);
315  }
316 
317  if (num != max) {
318  if (orient ==TQt::Horizontal)
319  er.setLeft(fr.right() + 1);
320  else
321  er.setBottom(fr.bottom() + 1);
322  if (!er.isNull()) {
323  p->setBrushOrigin(cr.topLeft());
324  p->fillRect(er, eb);
325  }
326  }
327 
328  break;
329  }
330 
331  if (text_enabled && bar_style != Blocked)
332  drawText(p);
333 }
334 
335 void KGameProgress::setFormat(const TQString & format)
336 {
337  format_ = format;
338 }
339 
340 TQString KGameProgress::format() const
341 {
342  return format_;
343 }
344 
345 #include "kgameprogress.moc"
KGameProgress::setBarStyle
void setBarStyle(BarStyle style)
Set the progress bar style.
Definition: kgameprogress.cpp:113
KGameProgress::barPixmap
const TQPixmap * barPixmap() const
Retrieve the bar pixmap.
Definition: kgameprogress.cpp:144
KGameProgress::~KGameProgress
~KGameProgress()
Destruct the progress bar.
Definition: kgameprogress.cpp:57
KGameProgress::percentageChanged
void percentageChanged(int)
Emitted when the state of the progress bar changes.
KGameProgress::barColor
const TQColor & barColor() const
Retrieve the bar color.
Definition: kgameprogress.cpp:139
KGameProgress::advance
void advance(int prog)
Advance the progress bar by prog.
Definition: kgameprogress.cpp:62
KGameProgress::setValue
void setValue(int value)
Set the current value of the progress bar to value.
Definition: kgameprogress.cpp:129
KGameProgress::BarStyle
BarStyle
Possible values for bar style.
Definition: kgameprogress.h:69
KGameProgress::setTextEnabled
void setTextEnabled(bool)
If this is set to true, the progress text will be displayed.
Definition: kgameprogress.cpp:134
KGameProgress::format
TQString format() const
Retrieve the current format for printing status text.
Definition: kgameprogress.cpp:340
KGameProgress::setBarColor
void setBarColor(const TQColor &)
Set the color of the progress bar.
Definition: kgameprogress.cpp:103
KGameProgress::value
int value() const
Retrive the current status.
Definition: kgameprogress.h:147
KGameProgress::KGameProgress
KGameProgress(TQWidget *parent=0, const char *name=0)
Construct a horizontal progress bar.
Definition: kgameprogress.cpp:32
KGameProgress::barStyle
BarStyle barStyle() const
Retrieve the bar style.
Definition: kgameprogress.cpp:185
KGameProgress::setBarPixmap
void setBarPixmap(const TQPixmap &)
Set a pixmap to be shown in the progress bar.
Definition: kgameprogress.cpp:93
KGameProgress::orientation
Orientation orientation() const
Retrive the orientation of the progress bar.
Definition: kgameprogress.cpp:180
KGameProgress::setOrientation
void setOrientation(Orientation)
Set the orientation of the progress bar.
Definition: kgameprogress.cpp:121
KGameProgress::textEnabled
bool textEnabled() const
Returns true if progress text will be displayed, false otherwise.
Definition: kgameprogress.cpp:149
KGameProgress::setFormat
void setFormat(const TQString &format)
Set the format of the text to use to display status.
Definition: kgameprogress.cpp:335

libtdegames

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

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.9.1
This website is maintained by Timothy Pearson.