ProgressBarQt.cpp 3.7 KB

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