summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrerich Raabe <raabe@kde.org>2014-09-16 03:14:25 +0200
committerSlávek Banko <slavek.banko@axis.cz>2014-09-16 03:41:12 +0200
commit9f882f9de741d76b8f45b0dc23d4fe65d6e09d78 (patch)
tree506caec59585b1e056b8509d203fa356e1368f10
parentb716176cec88f5fa604b637a947b44b9ff165d5a (diff)
downloadqt3-9f882f9de741d76b8f45b0dc23d4fe65d6e09d78.tar.gz
qt3-9f882f9de741d76b8f45b0dc23d4fe65d6e09d78.zip
Add repaint optimization to TQProgressBar
This optimization makes TQProgressBar::setProgress() only repaint itself if stepping to the new progress would cause a graphical change. This means that for a width W and a total number of steps S, it will repaint itself 'W' times (every 'S/W' steps) instead of 'S' times (every step) as it is right now.
-rw-r--r--src/widgets/qprogressbar.cpp55
-rw-r--r--src/widgets/qprogressbar.h2
2 files changed, 54 insertions, 3 deletions
diff --git a/src/widgets/qprogressbar.cpp b/src/widgets/qprogressbar.cpp
index 99dedb0..1219419 100644
--- a/src/widgets/qprogressbar.cpp
+++ b/src/widgets/qprogressbar.cpp
@@ -51,6 +51,14 @@
#endif
#include <limits.h>
+class QProgressBarPrivate
+{
+ public:
+ QProgressBarPrivate() : last_painted_progress( 0 ) { }
+
+ int last_painted_progress;
+};
+
/*!
\class QProgressBar qprogressbar.h
\brief The QProgressBar widget provides a horizontal progress bar.
@@ -103,7 +111,7 @@ QProgressBar::QProgressBar( QWidget *parent, const char *name, WFlags f )
center_indicator( TRUE ),
auto_indicator( TRUE ),
percentage_visible( TRUE ),
- d( 0 ),
+ d( new QProgressBarPrivate ),
m_orientation( Horizontal )
{
setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
@@ -135,7 +143,7 @@ QProgressBar::QProgressBar( int totalSteps,
center_indicator( TRUE ),
auto_indicator( TRUE ),
percentage_visible( TRUE ),
- d( 0 ),
+ d( new QProgressBarPrivate ),
m_orientation( Horizontal )
{
setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
@@ -144,6 +152,16 @@ QProgressBar::QProgressBar( int totalSteps,
/*!
+ Destroys the object and frees any allocated ressources.
+*/
+
+QProgressBar::~QProgressBar()
+{
+ delete d;
+}
+
+
+/*!
Reset the progress bar. The progress bar "rewinds" and shows no
progress.
*/
@@ -194,11 +212,16 @@ void QProgressBar::setProgress( int progress )
progress < 0 || ( ( progress > total_steps ) && total_steps ) )
return;
+ const bool needRepaint = isVisible() && requireRepaint( progress );
+
progress_val = progress;
setIndicator( progress_str, progress_val, total_steps );
- repaint( FALSE );
+ if ( needRepaint ) {
+ repaint( FALSE );
+ d->last_painted_progress = progress;
+ }
#if defined(QT_ACCESSIBILITY_SUPPORT)
QAccessible::updateAccessibility( this, 0, QAccessible::ValueChanged );
@@ -330,6 +353,32 @@ void QProgressBar::styleChange( QStyle& old )
QFrame::styleChange( old );
}
+/*!
+ This method returns whether changing the progress to the \a newValue
+ would require a repaint of the progress bar. This allows efficient
+ repainting.
+*/
+bool QProgressBar::requireRepaint( int newProgress ) const
+{
+ if ( newProgress == progress_val ||
+ newProgress == d->last_painted_progress ) {
+ return false;
+ }
+
+ const int width = contentsRect().width();
+ if ( width == 0 ) {
+ return false;
+ }
+
+ float progressPerPixel = 1.0;
+ if ( total_steps > width ) {
+ progressPerPixel = float( total_steps ) / float( width );
+ }
+
+ const int delta = d->last_painted_progress - newProgress;
+ return QABS( delta ) >= progressPerPixel;
+}
+
Qt::Orientation QProgressBar::orientation() const
{
return m_orientation;
diff --git a/src/widgets/qprogressbar.h b/src/widgets/qprogressbar.h
index ac69914..3ba2388 100644
--- a/src/widgets/qprogressbar.h
+++ b/src/widgets/qprogressbar.h
@@ -65,6 +65,7 @@ class Q_EXPORT QProgressBar : public QFrame
public:
QProgressBar( QWidget* parent=0, const char* name=0, WFlags f=0 );
QProgressBar( int totalSteps, QWidget* parent=0, const char* name=0, WFlags f=0 );
+ virtual ~QProgressBar();
int totalSteps() const;
int progress() const;
@@ -95,6 +96,7 @@ protected:
virtual bool setIndicator( QString & progress_str, int progress,
int totalSteps );
void styleChange( QStyle& );
+ bool requireRepaint( int newProgress ) const;
private:
int total_steps;