Flu_Progress_Meter.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // $Id: Flu_Progress_Meter.cpp,v 1.17 2005/02/04 21:21:09 jbryan Exp $
  2. /***************************************************************
  3. * FLU - FLTK Utility Widgets
  4. * Copyright (C) 2002 Ohio Supercomputer Center, Ohio State University
  5. *
  6. * This file and its content is protected by a software license.
  7. * You should have received a copy of this license with this file.
  8. * If not, please contact the Ohio Supercomputer Center immediately:
  9. * Attn: Jason Bryan Re: FLU 1224 Kinnear Rd, Columbus, Ohio 43212
  10. *
  11. ***************************************************************/
  12. #include "FLU/Flu_Progress_Meter.H"
  13. #if !defined WIN32
  14. #define TSCALE (1.0 / 1.0e6)
  15. #else
  16. #define TSCALE (1.0 / CLOCKS_PER_SEC)
  17. #endif
  18. Flu_Progress_Meter :: Flu_Progress_Meter( const char* t )
  19. : Fl_Double_Window( Fl::w()/2-350/2, Fl::h()/2-180/2, 350, 180, t )
  20. {
  21. _cancelled = false;
  22. _cancelCB = NULL;
  23. _cancelCBD = NULL;
  24. _showETC = true;
  25. _label = new Flu_Label( 10, 10, 330, 50 );
  26. _label->align( _label->align() | FL_ALIGN_WRAP );
  27. etc = new Flu_Label( 10, 60, 330, 30 );
  28. etc->hide();
  29. progress = new Flu_Progress( 10, 100, 330, 30 );
  30. cancel = new Fl_Button( w()/2-30, h()-40, 60, 30, "Cancel" );
  31. end();
  32. Fl_Double_Window::hide();
  33. progress->align( FL_ALIGN_TOP | FL_ALIGN_LEFT );
  34. cancel->callback( _onCancelCB, this );
  35. }
  36. Flu_Progress_Meter :: ~Flu_Progress_Meter()
  37. {
  38. Fl::remove_timeout( _secondTimerCB, this );
  39. hide();
  40. }
  41. inline void secs2HMS( double secs, int &H, int &M, int &S )
  42. {
  43. S = (int)secs;
  44. M = S / 60; S -= M*60;
  45. H = M / 60; M -= H*60;
  46. }
  47. void Flu_Progress_Meter::reset()
  48. {
  49. Fl::remove_timeout( _secondTimerCB, this );
  50. gettimeofday( &startT, 0 );
  51. deltaT = startT;
  52. Fl::add_timeout( 0.0f, _secondTimerCB, this );
  53. value(0);
  54. }
  55. void Flu_Progress_Meter :: secondTimerCB( bool repeatTimer )
  56. {
  57. // get the current time and use it to compute the elapsed time, if necessary
  58. timeval now;
  59. gettimeofday( &now, 0 );
  60. if( _showETC && shown() )
  61. {
  62. // compute the elapsed time
  63. double elapsed = (now.tv_sec - startT.tv_sec) + ( (now.tv_usec - startT.tv_usec) * TSCALE );
  64. // estimate the remaining time based on the elapsed time and the current progress
  65. double remaining = elapsed / this->value() - elapsed + 1.0f;
  66. int es, em, eh, rs, rm, rh;
  67. secs2HMS( elapsed, eh, em, es );
  68. //if( elapsed < 1.0 )
  69. //rh = rm = rs = 0;
  70. //else
  71. secs2HMS( remaining, rh, rm, rs );
  72. char buf[128];
  73. sprintf( buf, "Elapsed Time: %03d:%02d:%02d\n"
  74. "Remaining Time: %03d:%02d:%02d",
  75. eh, em, es, rh, rm, rs );
  76. etc->label( buf );
  77. etc->show();
  78. }
  79. else
  80. etc->hide();
  81. if( repeatTimer )
  82. {
  83. Fl::repeat_timeout( 1.0f, _secondTimerCB, this );
  84. Fl::check();
  85. }
  86. }
  87. bool Flu_Progress_Meter :: value( float v )
  88. {
  89. timeval now;
  90. gettimeofday( &now, 0 );
  91. if( v-value() >= 0.01f || ((now.tv_sec - deltaT.tv_sec) + ( (now.tv_usec - deltaT.tv_usec) * TSCALE ) ) >= 1.0 )
  92. {
  93. deltaT = now;
  94. secondTimerCB( false );
  95. Fl::check();
  96. if( progress )
  97. progress->value( v );
  98. }
  99. return _cancelled;
  100. }
  101. void Flu_Progress_Meter :: show( bool cancelBtnVisible )
  102. {
  103. gettimeofday( &startT, 0 );
  104. deltaT = startT;
  105. _cancelled = false;
  106. if( _cancelCB || cancelBtnVisible )
  107. cancel->show();
  108. else
  109. cancel->hide();
  110. //set_modal();
  111. Fl_Double_Window::show();
  112. Fl::add_timeout( 0.0f, _secondTimerCB, this );
  113. Fl::flush();
  114. }
  115. void Flu_Progress_Meter :: hide()
  116. {
  117. Fl::remove_timeout( _secondTimerCB, this );
  118. Fl_Double_Window::hide();
  119. Fl::flush();
  120. }