123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- /**
- * @file ProgressBarQt.cpp
- * @brief Show a Progress-Bar with time estimation
- * @author Michael Koch
- * @date 25/03/2010
- */
- #include "core/image/ImageT.h"
- #include "core/vector/VectorT.h"
- #include "core/vector/MatrixT.h"
- #include <core/imagedisplay/ImageDisplay.h>
- #include <core/imagedisplay/QtFramework.h>
- #include <iostream>
- #include <sys/time.h>
- #include "vislearning/baselib/ProgressBarQt.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- ProgressBarQt::ProgressBarQt ( const std::string & _name, bool _useGraphics )
- : useGraphics ( _useGraphics )
- {
- name = _name;
- step = 0;
- display_on = false;
- working = true;
- if ( graphicIsAvailable() )
- {
- if ( qApp == NULL )
- {
- QtFramework::instance();
- }
- dialogwindow = new QWidget;
- progressdialog = new QProgressDialog ( "Process at work ...", "Cancel",
- 0, 100 );
- layout = new QGridLayout ( dialogwindow, 1, 1 );
- layout->addWidget ( progressdialog, 0, 0 );
- dialogwindow->setLayout ( layout );
- }
- reset ( _name );
- }
- ProgressBarQt::~ProgressBarQt()
- {
- }
- void ProgressBarQt::timediff2str ( char *text, long time )
- {
- int seconds;
- int minutes;
- int hours;
- int milliseconds;
- milliseconds = time % 100;
- time /= 100;
- seconds = time % 60;
- time /= 60;
- minutes = time % 60;
- time /= 60;
- hours = time;
- if ( hours != 0 )
- {
- sprintf ( text, "%dh %dm %d.%d s", hours, minutes, seconds, milliseconds );
- }
- else if ( minutes != 0 )
- {
- sprintf ( text, "%dm %d.%d s", minutes, seconds, milliseconds );
- }
- else
- {
- sprintf ( text, "%d.%d s", seconds, milliseconds );
- }
- }
- void ProgressBarQt::displayTimeStat ( int posy, char *text, long time )
- {
- // Text (char *str,int x0,int y0,int val,int exp,Image img);
- char disptext[200];
- char timetext[200];
- timediff2str ( timetext, time );
- sprintf ( disptext, "%s %s", text, timetext );
- }
- double ProgressBarQt::getCurrentTime()
- {
- struct timeval curtime;
- gettimeofday ( &curtime, NULL );
- return curtime.tv_sec * 100.0 + curtime.tv_usec / 10000.0;
- }
- void ProgressBarQt::show()
- {
- if ( !display_on )
- {
- display_on = true;
- }
- if ( graphicIsAvailable() )
- {
- if ( qApp == NULL )
- {
- QtFramework::instance();
- }
- dialogwindow->show();
- }
- }
- void ProgressBarQt::hide()
- {
- if ( display_on )
- {
- // Show ( OFF, display, name );
- display_on = false;
- }
- if ( graphicIsAvailable() )
- {
- dialogwindow->hide();
- }
- }
- void ProgressBarQt::stop()
- {
- working = false;
- }
- void ProgressBarQt::reset ( const std::string & _name )
- {
- name = _name;
- step = 0;
- elapsed_time = 0;
- avg_time_step = 0;
- start_time = getCurrentTime();
- }
- void ProgressBarQt::update ( int count )
- {
- step++;
- double progress = step / ( double ) count;
- elapsed_time = getCurrentTime() - start_time;
- avg_time_step = elapsed_time / step;
- estimated_time = ( count - step ) * avg_time_step;
- size_t mod;
- if ( avg_time_step > 50.0 )
- mod = 1;
- else
- mod = ( size_t ) ( 50.0 / avg_time_step ) + 1;
- if ( ( mod <= 1 ) || ( step % mod == 0 ) )
- {
- char percent_text[10];
- sprintf ( percent_text, "%4.2f %%", step * 100.0 / count );
- displayTimeStat ( 0, "Elapsed Time : ", ( long int ) elapsed_time );
- displayTimeStat ( 1, "Estimated Time : ", ( long int ) estimated_time );
- displayTimeStat ( 2, "Avg. Time per step : ", ( long int ) avg_time_step );
- char eltime[200];
- char estime[200];
- char avgtime[200];
- timediff2str ( eltime, ( long int ) elapsed_time );
- timediff2str ( estime, ( long int ) estimated_time );
- timediff2str ( avgtime, ( long int ) avg_time_step );
- fprintf (
- stderr,
- "[PROGRESS] %s %s (elapsed time %s, estimated time %s, avg %s), \n",
- name.c_str(), percent_text, eltime, estime, avgtime );
- //set progress value.
- if ( graphicIsAvailable() )
- {
- progressdialog->setValue ( progress * 100 );
- }
- }
- }
- bool ProgressBarQt::graphicIsAvailable()
- {
- return ( useGraphics && ( getenv ( "DISPLAY" ) != NULL ) );
- }
|