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