ProgressBarQt.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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, 1, 1 );
  37. layout->addWidget ( progressdialog, 0, 0 );
  38. dialogwindow->setLayout ( layout );
  39. }
  40. reset ( _name );
  41. }
  42. ProgressBarQt::~ProgressBarQt()
  43. {
  44. }
  45. void ProgressBarQt::timediff2str(char *text, double dtime)
  46. {
  47. int time = int (dtime / 60);
  48. double seconds = dtime - time*60;
  49. int minutes;
  50. int hours;
  51. minutes = time % 60;
  52. time /= 60;
  53. hours = time;
  54. if (hours != 0)
  55. {
  56. sprintf(text, "%dh %dm %5.2fs", hours, minutes, seconds);
  57. }
  58. else if (minutes != 0)
  59. {
  60. sprintf(text, "%dm %5.2fs", minutes, seconds);
  61. }
  62. else
  63. {
  64. sprintf(text, "%fs", seconds);
  65. }
  66. }
  67. void ProgressBarQt::displayTimeStat(int posy, char *text, double time)
  68. {
  69. // Text (char *str,int x0,int y0,int val,int exp,Image img);
  70. char disptext[200];
  71. char timetext[200];
  72. timediff2str ( timetext, time );
  73. sprintf ( disptext, "%s %s", text, timetext );
  74. }
  75. double ProgressBarQt::getCurrentTime()
  76. {
  77. #ifdef WIN32
  78. fthrow(Exception,"function not yet ported, use boost::time_date here");
  79. #else
  80. struct timeval curtime;
  81. gettimeofday ( &curtime, NULL );
  82. //return curtime.tv_sec * 100.0 + curtime.tv_usec / 10000.0;
  83. return curtime.tv_sec + curtime.tv_usec * 1e-6;
  84. #endif
  85. }
  86. void ProgressBarQt::show()
  87. {
  88. if ( !display_on )
  89. {
  90. display_on = true;
  91. }
  92. if ( graphicIsAvailable() )
  93. {
  94. if ( qApp == NULL )
  95. {
  96. QtFramework::instance();
  97. }
  98. dialogwindow->show();
  99. }
  100. }
  101. void ProgressBarQt::hide()
  102. {
  103. if (display_on)
  104. {
  105. display_on = false;
  106. }
  107. if (graphicIsAvailable())
  108. {
  109. dialogwindow->hide();
  110. }
  111. }
  112. void ProgressBarQt::stop()
  113. {
  114. working = false;
  115. }
  116. void ProgressBarQt::reset ( const std::string & _name )
  117. {
  118. name = _name;
  119. step = 0;
  120. elapsed_time = 0;
  121. avg_time_step = 0;
  122. start_time = getCurrentTime();
  123. }
  124. void ProgressBarQt::update ( int count )
  125. {
  126. step++;
  127. double progress = step / (double) count;
  128. elapsed_time = getCurrentTime() - start_time;
  129. avg_time_step = elapsed_time / step;
  130. estimated_time = (count - step) * avg_time_step;
  131. size_t mod;
  132. if (avg_time_step > 1.0)
  133. mod = 1;
  134. else
  135. mod = (size_t) (1.0 / avg_time_step) + 1;
  136. if ((mod <= 1) || (step % mod == 0))
  137. {
  138. char percent_text[10];
  139. sprintf(percent_text, "%4.2f %%", step * 100.0 / count);
  140. displayTimeStat(0, "Elapsed Time : ", elapsed_time);
  141. displayTimeStat(1, "Estimated Time : ", estimated_time);
  142. displayTimeStat(2, "Avg. Time per step : ", avg_time_step);
  143. char eltime[200];
  144. char estime[200];
  145. char avgtime[200];
  146. timediff2str(eltime, elapsed_time);
  147. timediff2str(estime, estimated_time);
  148. timediff2str(avgtime, avg_time_step);
  149. fprintf(
  150. stderr,
  151. "[PROGRESS] %s %s (elapsed time %s, estimated time %s, avg %s), \n",
  152. name.c_str(), percent_text, eltime, estime, avgtime);
  153. //set progress value.
  154. if (graphicIsAvailable())
  155. {
  156. progressdialog->setValue(progress * 100);
  157. }
  158. }
  159. }
  160. bool ProgressBarQt::graphicIsAvailable()
  161. {
  162. return ( useGraphics && ( getenv ( "DISPLAY" ) != NULL ) );
  163. }