source: vendor/trolltech/current/src/widgets/qprogressbar.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 10.8 KB
Line 
1/****************************************************************************
2** $Id: qprogressbar.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Implementation of QProgressBar class
5**
6** Created : 970521
7**
8** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
9**
10** This file is part of the widgets module of the Qt GUI Toolkit.
11**
12** This file may be distributed under the terms of the Q Public License
13** as defined by Trolltech AS of Norway and appearing in the file
14** LICENSE.QPL included in the packaging of this file.
15**
16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file.
20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software.
24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38#include "qprogressbar.h"
39#ifndef QT_NO_PROGRESSBAR
40#include "qpainter.h"
41#include "qdrawutil.h"
42#include "qpixmap.h"
43#include "qstyle.h"
44#include "../kernel/qinternal_p.h"
45#if defined(QT_ACCESSIBILITY_SUPPORT)
46#include "qaccessible.h"
47#endif
48#include <limits.h>
49
50/*!
51 \class QProgressBar qprogressbar.h
52 \brief The QProgressBar widget provides a horizontal progress bar.
53
54 \ingroup advanced
55 \mainclass
56
57 A progress bar is used to give the user an indication of the
58 progress of an operation and to reassure them that the application
59 is still running.
60
61 The progress bar uses the concept of \e steps; you give it the
62 total number of steps and the number of steps completed so far and
63 it will display the percentage of steps that have been completed.
64 You can specify the total number of steps in the constructor or
65 later with setTotalSteps(). The current number of steps is set
66 with setProgress(). The progress bar can be rewound to the
67 beginning with reset().
68
69 If the total is given as 0 the progress bar shows a busy indicator
70 instead of a percentage of steps. This is useful, for example,
71 when using QFtp or QHttp to download items when they are unable to
72 determine the size of the item being downloaded.
73
74 \sa QProgressDialog
75
76 <img src=qprogbar-m.png> <img src=qprogbar-w.png>
77
78 \sa QProgressDialog
79 \link guibooks.html#fowler GUI Design Handbook: Progress Indicator\endlink
80*/
81
82
83/*!
84 Constructs a progress bar.
85
86 The total number of steps is set to 100 by default.
87
88 The \a parent, \a name and widget flags, \a f, are passed on to
89 the QFrame::QFrame() constructor.
90
91 \sa setTotalSteps()
92*/
93
94QProgressBar::QProgressBar( QWidget *parent, const char *name, WFlags f )
95 : QFrame( parent, name, f | WNoAutoErase ),
96 total_steps( 100 ),
97 progress_val( -1 ),
98 percentage( -1 ),
99 center_indicator( TRUE ),
100 auto_indicator( TRUE ),
101 percentage_visible( TRUE ),
102 d( 0 )
103{
104 setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
105 initFrame();
106}
107
108
109/*!
110 Constructs a progress bar.
111
112 The \a totalSteps is the total number of steps that need to be
113 completed for the operation which this progress bar represents.
114 For example, if the operation is to examine 50 files, this value
115 would be 50. Before examining the first file, call setProgress(0);
116 call setProgress(50) after examining the last file.
117
118 The \a parent, \a name and widget flags, \a f, are passed to the
119 QFrame::QFrame() constructor.
120
121 \sa setTotalSteps(), setProgress()
122*/
123
124QProgressBar::QProgressBar( int totalSteps,
125 QWidget *parent, const char *name, WFlags f )
126 : QFrame( parent, name, f | WNoAutoErase ),
127 total_steps( totalSteps ),
128 progress_val( -1 ),
129 percentage( -1 ),
130 center_indicator( TRUE ),
131 auto_indicator( TRUE ),
132 percentage_visible( TRUE ),
133 d( 0 )
134{
135 setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
136 initFrame();
137}
138
139
140/*!
141 Reset the progress bar. The progress bar "rewinds" and shows no
142 progress.
143*/
144
145void QProgressBar::reset()
146{
147 progress_val = -1;
148 percentage = -1;
149 setIndicator(progress_str, progress_val, total_steps);
150 repaint( FALSE );
151}
152
153
154/*!
155 \property QProgressBar::totalSteps
156 \brief The total number of steps.
157
158 If totalSteps is 0, the progress bar will display a busy
159 indicator.
160
161 \sa totalSteps()
162*/
163
164void QProgressBar::setTotalSteps( int totalSteps )
165{
166 total_steps = totalSteps;
167
168 // Current progress is invalid if larger than total
169 if ( total_steps < progress_val )
170 progress_val = -1;
171
172 if ( isVisible() &&
173 ( setIndicator(progress_str, progress_val, total_steps) || !total_steps ) )
174 repaint( FALSE );
175}
176
177
178/*!
179 \property QProgressBar::progress
180 \brief The current amount of progress
181
182 This property is -1 if progress counting has not started.
183*/
184
185void QProgressBar::setProgress( int progress )
186{
187 if ( progress == progress_val ||
188 progress < 0 || ( ( progress > total_steps ) && total_steps ) )
189 return;
190
191 progress_val = progress;
192
193 setIndicator( progress_str, progress_val, total_steps );
194
195 repaint( FALSE );
196
197#if defined(QT_ACCESSIBILITY_SUPPORT)
198 QAccessible::updateAccessibility( this, 0, QAccessible::ValueChanged );
199#endif
200}
201
202/*!
203 \overload
204
205 Sets the amount of progress to \a progress and the total number of
206 steps to \a totalSteps.
207
208 \sa setTotalSteps()
209*/
210
211void QProgressBar::setProgress( int progress, int totalSteps )
212{
213 if ( total_steps != totalSteps )
214 setTotalSteps( totalSteps );
215 setProgress( progress );
216}
217
218/*!
219 \property QProgressBar::progressString
220 \brief the amount of progress as a string
221
222 This property is QString::null if progress counting has not started.
223*/
224
225
226/*!
227 \reimp
228*/
229QSize QProgressBar::sizeHint() const
230{
231 constPolish();
232 QFontMetrics fm = fontMetrics();
233 int cw = style().pixelMetric(QStyle::PM_ProgressBarChunkWidth, this);
234 return style().sizeFromContents(QStyle::CT_ProgressBar, this,
235 QSize( cw * 7 + fm.width( '0' ) * 4,
236 fm.height() + 8));
237}
238
239
240/*!
241 \reimp
242*/
243QSize QProgressBar::minimumSizeHint() const
244{
245 return sizeHint();
246}
247
248/*!
249 \property QProgressBar::centerIndicator
250 \brief whether the indicator string should be centered
251
252 Changing this property sets \l QProgressBar::indicatorFollowsStyle
253 to FALSE. The default is TRUE.
254*/
255
256void QProgressBar::setCenterIndicator( bool on )
257{
258 if ( !auto_indicator && on == center_indicator )
259 return;
260 auto_indicator = FALSE;
261 center_indicator = on;
262 repaint( FALSE );
263}
264
265/*!
266 \property QProgressBar::indicatorFollowsStyle
267 \brief whether the display of the indicator string should follow the GUI style
268
269 The default is TRUE.
270
271 \sa centerIndicator
272*/
273
274void QProgressBar::setIndicatorFollowsStyle( bool on )
275{
276 if ( on == auto_indicator )
277 return;
278 auto_indicator = on;
279 repaint( FALSE );
280}
281
282/*!
283 \property QProgressBar::percentageVisible
284 \brief whether the current progress value is displayed
285
286 The default is TRUE.
287
288 \sa centerIndicator, indicatorFollowsStyle
289*/
290void QProgressBar::setPercentageVisible( bool on )
291{
292 if ( on == percentage_visible )
293 return;
294 percentage_visible = on;
295 repaint( FALSE );
296}
297
298/*!
299 \reimp
300*/
301void QProgressBar::show()
302{
303 setIndicator( progress_str, progress_val, total_steps );
304 QFrame::show();
305}
306
307void QProgressBar::initFrame()
308{
309 setFrameStyle(QFrame::NoFrame);
310}
311
312/*!
313 \reimp
314*/
315void QProgressBar::styleChange( QStyle& old )
316{
317 initFrame();
318 QFrame::styleChange( old );
319}
320
321
322/*!
323 This method is called to generate the text displayed in the center
324 (or in some styles, to the left) of the progress bar.
325
326 The \a progress may be negative, indicating that the progress bar
327 is in the "reset" state before any progress is set.
328
329 The default implementation is the percentage of completion or
330 blank in the reset state. The percentage is calculated based on
331 the \a progress and \a totalSteps. You can set the \a indicator
332 text if you wish.
333
334 To allow efficient repainting of the progress bar, this method
335 should return FALSE if the string is unchanged from the last call
336 to this function.
337*/
338
339bool QProgressBar::setIndicator( QString & indicator, int progress,
340 int totalSteps )
341{
342 if ( !totalSteps )
343 return FALSE;
344 if ( progress < 0 ) {
345 indicator = QString::fromLatin1("");
346 return TRUE;
347 } else {
348 // Get the values down to something usable.
349 if ( totalSteps > INT_MAX/1000 ) {
350 progress /= 1000;
351 totalSteps /= 1000;
352 }
353
354 int np = progress * 100 / totalSteps;
355 if ( np != percentage ) {
356 percentage = np;
357 indicator.sprintf( "%d%%", np );
358 return TRUE;
359 } else {
360 return FALSE;
361 }
362 }
363}
364
365
366/*!
367 \reimp
368*/
369void QProgressBar::drawContents( QPainter *p )
370{
371 const QRect bar = contentsRect();
372
373 QSharedDoubleBuffer buffer( p, bar.x(), bar.y(), bar.width(), bar.height() );
374
375 QPoint pn = backgroundOffset();
376 buffer.painter()->setBrushOrigin( -pn.x(), -pn.y() );
377
378 const QPixmap *bpm = paletteBackgroundPixmap();
379 if ( bpm )
380 buffer.painter()->fillRect( bar, QBrush( paletteBackgroundColor(), *bpm ) );
381 else
382 buffer.painter()->fillRect( bar, paletteBackgroundColor() );
383 buffer.painter()->setFont( p->font() );
384
385 QStyle::SFlags flags = QStyle::Style_Default;
386 if (isEnabled())
387 flags |= QStyle::Style_Enabled;
388 if (hasFocus())
389 flags |= QStyle::Style_HasFocus;
390
391 style().drawControl(QStyle::CE_ProgressBarGroove, buffer.painter(), this,
392 QStyle::visualRect(style().subRect(QStyle::SR_ProgressBarGroove, this), this ),
393 colorGroup(), flags);
394
395 style().drawControl(QStyle::CE_ProgressBarContents, buffer.painter(), this,
396 QStyle::visualRect(style().subRect(QStyle::SR_ProgressBarContents, this), this ),
397 colorGroup(), flags);
398
399 if (percentageVisible())
400 style().drawControl(QStyle::CE_ProgressBarLabel, buffer.painter(), this,
401 QStyle::visualRect(style().subRect(QStyle::SR_ProgressBarLabel, this), this ),
402 colorGroup(), flags);
403}
404
405#endif
Note: See TracBrowser for help on using the repository browser.