[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [k3b/2.0] /: Use QElapsedTimer to calculate remaining time.
From:       Johannes Obermayr <johannesobermayr () gmx ! de>
Date:       2014-11-02 15:39:55
Message-ID: E1XkxFz-00042J-1K () scm ! kde ! org
[Download RAW message or body]

Git commit a4dee50258e7678604348444a754ef2f3502b8db by Johannes Obermayr.
Committed on 30/09/2014 at 17:17.
Pushed by jobermayr into branch '2.0'.

Use QElapsedTimer to calculate remaining time.

BUGS: 330239,315463
FIXED-IN: 2.0.3
REVIEW: 120459

M  +2    -0    CMakeLists.txt
M  +24   -32   src/k3bjobprogressdialog.cpp
M  +6    -7    src/k3bjobprogressdialog.h

http://commits.kde.org/k3b/a4dee50258e7678604348444a754ef2f3502b8db

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e467321..4a0abac 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -49,6 +49,8 @@ else(K3B_BUILD_K3BSETUP)
     set(KDE_MIN_VERSION 4.3.0)
 endif(K3B_BUILD_K3BSETUP)
 
+set(QT_MIN_VERSION 4.7.0)
+
 find_package(KDE4 REQUIRED)
 include(KDE4Defaults)
 include(MacroLibrary)
diff --git a/src/k3bjobprogressdialog.cpp b/src/k3bjobprogressdialog.cpp
index 1890ccf..ba26b88 100644
--- a/src/k3bjobprogressdialog.cpp
+++ b/src/k3bjobprogressdialog.cpp
@@ -55,7 +55,6 @@
 #include <QPushButton>
 #include <QScrollBar>
 #include <QString>
-#include <QTimer>
 #include <QTreeWidget>
 #include <QVBoxLayout>
 
@@ -85,9 +84,6 @@ K3b::JobProgressDialog::JobProgressDialog( QWidget* parent,
     }
 
     m_job = 0;
-    m_timer = new QTimer( this );
-
-    connect( m_timer, SIGNAL(timeout()), this, SLOT(slotUpdateTime()) );
 }
 
 
@@ -176,10 +172,12 @@ void K3b::JobProgressDialog::setupGUI()
     m_labelTask_font.setBold( true );
     m_labelTask->setFont( m_labelTask_font );
 
-    m_labelElapsedTime = new K3b::ThemedLabel( d->progressHeaderFrame );
+    m_labelRemainingTime = new QLabel( d->progressHeaderFrame );
+    m_labelElapsedTime = new QLabel( d->progressHeaderFrame );
 
     QVBoxLayout* jobProgressLayout = new QVBoxLayout( d->progressHeaderFrame );
     jobProgressLayout->addWidget( m_labelTask );
+    jobProgressLayout->addWidget( m_labelRemainingTime );
     jobProgressLayout->addWidget( m_labelElapsedTime );
     jobProgressLayout->setContentsMargins( 10, -1, -1, -1 );
 
@@ -354,6 +352,13 @@ void K3b::JobProgressDialog::slotFinished( bool success )
     const KColorScheme colorScheme( QPalette::Normal, KColorScheme::Window );
     QPalette taskPalette( m_labelTask->palette() );
 
+    // Only show elapsed time at the end of the task
+    // setVisible( false ) would move elapsed time one line up ...
+    m_labelRemainingTime->setText( "" );
+    m_labelElapsedTime->setText( i18nc( "@info %1 is a duration formatted using \
KLocale::prettyFormatDuration", +        "Elapsed time: %1", \
KGlobal::locale()->prettyFormatDuration( m_timer.elapsed() ) ) ); +    \
m_timer.invalidate(); +
     if( success ) {
         m_pixLabel->setThemePixmap( K3b::Theme::PROGRESS_SUCCESS );
 
@@ -367,9 +372,6 @@ void K3b::JobProgressDialog::slotFinished( bool success )
         m_progressSubPercent->setValue(100);
         slotProgress(100);
 
-        // one last time update to be sure no remaining time is displayed anymore
-        slotUpdateTime();
-
         if( m_osd )
             m_osd->setText( i18n("Success.") );
 
@@ -399,7 +401,6 @@ void K3b::JobProgressDialog::slotFinished( bool success )
     showButton( Cancel, false );
     showButton( User1, true );
     showButton( User2, true );
-    m_timer->stop();
 }
 
 
@@ -525,34 +526,13 @@ void K3b::JobProgressDialog::slotStarted()
 {
     kDebug();
     d->lastProgress = 0;
-    m_timer->start( 1000 );
-    m_startTime = QDateTime::currentDateTime();
+    m_timer.start();
     m_plainCaption = k3bappcore->k3bMainWindow()->windowTitle();
 
     m_logFile.open();
 }
 
 
-void K3b::JobProgressDialog::slotUpdateTime()
-{
-    int elapsedSecs = m_startTime.secsTo( QDateTime::currentDateTime() );
-
-    QString s = i18nc( "@info %1 is a duration formatted using \
                KLocale::prettyFormatDuration",
-                       "Elapsed time: %1",
-                       KGlobal::locale()->prettyFormatDuration( elapsedSecs*1000 ) \
                );
-
-    if( d->lastProgress > 0 && d->lastProgress < 100 ) {
-        int remainingSecs = m_startTime.secsTo( m_lastProgressUpdateTime ) * \
                (100-d->lastProgress) / d->lastProgress;
-        s += " / ";
-        s += i18nc( "@info %1 is a duration formatted using \
                KLocale::prettyFormatDuration",
-                    "Remaining: %1",
-                    KGlobal::locale()->prettyFormatDuration( remainingSecs*1000 ) );
-    }
-
-    m_labelElapsedTime->setText( s );
-}
-
-
 void K3b::JobProgressDialog::slotDebuggingOutput( const QString& type, const \
QString& output )  {
     m_logCache.addOutput( type, output );
@@ -572,11 +552,23 @@ void K3b::JobProgressDialog::slotProgress( int percent )
 {
     if( percent > d->lastProgress ) {
         d->lastProgress = percent;
-        m_lastProgressUpdateTime = QDateTime::currentDateTime();
         k3bappcore->k3bMainWindow()->setPlainCaption( QString( "(%1%) %2" \
).arg(percent).arg(m_plainCaption) );  
         setCaption( QString( "(%1%) %2" ).arg(percent).arg(m_job->jobDescription()) \
);  }
+
+    if( m_timer.isValid() ) {
+	qint64 elapsed = m_timer.elapsed();
+        m_labelElapsedTime->setText( i18nc( "@info %1 is a duration formatted using \
KLocale::prettyFormatDuration", +            "Elapsed time: %1", \
KGlobal::locale()->prettyFormatDuration( elapsed ) ) ); +        // Update "Remaining \
time" max. each second (1000 ms) +        if ( elapsed - m_lastProgressUpdateTime > \
999 ) { +            m_labelRemainingTime->setText( i18nc( "@info %1 is a duration \
formatted using KLocale::prettyFormatDuration", +                "Remaining: %1", \
KGlobal::locale()->prettyFormatDuration( +                ( d->lastProgress > 0 && \
d->lastProgress < 100 ) ? elapsed * ( 100 - d->lastProgress) / d->lastProgress : 0 ) \
) ); +            m_lastProgressUpdateTime = elapsed;
+        }
+    }
 }
 
 
diff --git a/src/k3bjobprogressdialog.h b/src/k3bjobprogressdialog.h
index 224a5a5..e444ea8 100644
--- a/src/k3bjobprogressdialog.h
+++ b/src/k3bjobprogressdialog.h
@@ -23,7 +23,7 @@
 
 #include <KDialog>
 
-#include <QDateTime>
+#include <QElapsedTimer>
 
 class KSqueezedTextLabel;
 class QCloseEvent;
@@ -33,7 +33,7 @@ class QKeyEvent;
 class QLabel;
 class QProgressBar;
 class QShowEvent;
-class QTimer;
+class QElapsedTimer;
 
 namespace K3b {
     class Job;
@@ -99,7 +99,6 @@ namespace K3b {
          * \reimpl from KDialog
          */
         virtual void slotButtonClicked( int button );
-        void slotUpdateTime();
         void slotShowDebuggingOutput();
 
         void slotProgress( int );
@@ -116,7 +115,8 @@ namespace K3b {
         ThemedLabel* m_labelJob;
         ThemedLabel* m_labelJobDetails;
         ThemedLabel* m_labelTask;
-        ThemedLabel* m_labelElapsedTime;
+        QLabel* m_labelRemainingTime;
+        QLabel* m_labelElapsedTime;
         KSqueezedTextLabel* m_labelSubTask;
         QLabel* m_labelSubProcessedSize;
         QProgressBar* m_progressSubPercent;
@@ -132,9 +132,8 @@ namespace K3b {
         Private* d;
 
         Job* m_job;
-        QTimer* m_timer;
-        QDateTime m_startTime;
-        QDateTime m_lastProgressUpdateTime;
+        QElapsedTimer m_timer;
+        qint64 m_lastProgressUpdateTime;
 
         DebuggingOutputFile m_logFile;
         DebuggingOutputCache m_logCache;


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic