ProgressBarQt.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @file ProgressBarQt.cpp
  3. * @brief Show a Progress-Bar with time estimation
  4. * @author Michael Koch
  5. * @date 25/03/2010
  6. */
  7. #include "core/image/ImageT.h"
  8. #include "core/vector/VectorT.h"
  9. #include "core/vector/MatrixT.h"
  10. #include <core/imagedisplay/ImageDisplay.h>
  11. #include <core/imagedisplay/QtFramework.h>
  12. #include <iostream>
  13. #ifndef WIN32
  14. #include <sys/time.h>
  15. #endif
  16. #include "vislearning/baselib/ProgressBarQt.h"
  17. using namespace OBJREC;
  18. using namespace std;
  19. using namespace NICE;
  20. ProgressBarQt::ProgressBarQt ( const std::string & _name, bool _useGraphics )
  21. : useGraphics ( _useGraphics )
  22. {
  23. name = _name;
  24. step = 0;
  25. display_on = false;
  26. working = true;
  27. if ( graphicIsAvailable() )
  28. {
  29. if ( qApp == NULL )
  30. {
  31. QtFramework::instance();
  32. }
  33. dialogwindow = new QWidget;
  34. progressdialog = new QProgressDialog ( "Process at work ...", "Cancel",
  35. 0, 100 );
  36. layout = new QGridLayout ( dialogwindow );
  37. layout->addWidget ( progressdialog, 0, 0 );
  38. dialogwindow->setLayout ( layout );
  39. }
  40. reset ( _name );
  41. }
  42. ProgressBarQt::~ProgressBarQt()
  43. {
  44. if( this->dialogwindow != NULL)
  45. {
  46. delete this->dialogwindow;
  47. this->dialogwindow = NULL;
  48. }
  49. }
  50. void ProgressBarQt::timediff2str(char *text, double dtime)
  51. {
  52. int time = int (dtime / 60);
  53. double seconds = dtime - time*60;
  54. int minutes;
  55. int hours;
  56. minutes = time % 60;
  57. time /= 60;
  58. hours = time;
  59. if (hours != 0)
  60. {
  61. sprintf(text, "%dh %dm %5.2fs", hours, minutes, seconds);
  62. }
  63. else if (minutes != 0)
  64. {
  65. sprintf(text, "%dm %5.2fs", minutes, seconds);
  66. }
  67. else
  68. {
  69. sprintf(text, "%fs", seconds);
  70. }
  71. }
  72. void ProgressBarQt::displayTimeStat(int posy, char *text, double time)
  73. {
  74. // Text (char *str,int x0,int y0,int val,int exp,Image img);
  75. char disptext[200];
  76. char timetext[200];
  77. timediff2str ( timetext, time );
  78. sprintf ( disptext, "%s %s", text, timetext );
  79. }
  80. double ProgressBarQt::getCurrentTime()
  81. {
  82. #ifdef WIN32
  83. fthrow(Exception,"function not yet ported, use boost::time_date here");
  84. #else
  85. struct timeval curtime;
  86. gettimeofday ( &curtime, NULL );
  87. //return curtime.tv_sec * 100.0 + curtime.tv_usec / 10000.0;
  88. return curtime.tv_sec + curtime.tv_usec * 1e-6;
  89. #endif
  90. }
  91. void ProgressBarQt::show()
  92. {
  93. if ( !display_on )
  94. {
  95. display_on = true;
  96. }
  97. if ( graphicIsAvailable() )
  98. {
  99. if ( qApp == NULL )
  100. {
  101. QtFramework::instance();
  102. }
  103. dialogwindow->show();
  104. }
  105. }
  106. void ProgressBarQt::hide()
  107. {
  108. if (display_on)
  109. {
  110. display_on = false;
  111. }
  112. if (graphicIsAvailable())
  113. {
  114. dialogwindow->hide();
  115. }
  116. }
  117. void ProgressBarQt::stop()
  118. {
  119. working = false;
  120. }
  121. void ProgressBarQt::reset ( const std::string & _name )
  122. {
  123. name = _name;
  124. step = 0;
  125. elapsed_time = 0;
  126. avg_time_step = 0;
  127. start_time = getCurrentTime();
  128. }
  129. void ProgressBarQt::update ( int count )
  130. {
  131. step++;
  132. double progress = step / (double) count;
  133. elapsed_time = getCurrentTime() - start_time;
  134. avg_time_step = elapsed_time / step;
  135. estimated_time = (count - step) * avg_time_step;
  136. size_t mod;
  137. if (avg_time_step > 1.0)
  138. mod = 1;
  139. else
  140. mod = (size_t) (1.0 / avg_time_step) + 1;
  141. if ((mod <= 1) || (step % mod == 0))
  142. {
  143. char percent_text[10];
  144. sprintf(percent_text, "%4.2f %%", step * 100.0 / count);
  145. displayTimeStat(0, "Elapsed Time : ", elapsed_time);
  146. displayTimeStat(1, "Estimated Time : ", estimated_time);
  147. displayTimeStat(2, "Avg. Time per step : ", avg_time_step);
  148. char eltime[200];
  149. char estime[200];
  150. char avgtime[200];
  151. timediff2str(eltime, elapsed_time);
  152. timediff2str(estime, estimated_time);
  153. timediff2str(avgtime, avg_time_step);
  154. fprintf(
  155. stderr,
  156. "[PROGRESS] %s %s (elapsed time %s, estimated time %s, avg %s), \n",
  157. name.c_str(), percent_text, eltime, estime, avgtime);
  158. //set progress value.
  159. if (graphicIsAvailable())
  160. {
  161. progressdialog->setValue(progress * 100);
  162. }
  163. }
  164. }
  165. bool ProgressBarQt::graphicIsAvailable()
  166. {
  167. return ( useGraphics && ( getenv ( "DISPLAY" ) != NULL ) );
  168. }