Flu_Progress_Meter.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // $Id: Flu_Progress_Meter.cpp,v 1.16 2004/06/09 19:51:15 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. Fl::add_timeout( 0.0f, _secondTimerCB, this );
  52. value(0);
  53. }
  54. void Flu_Progress_Meter :: secondTimerCB( bool repeatTimer )
  55. {
  56. // get the current time and use it to compute the elapsed time, if necessary
  57. timeval now;
  58. gettimeofday( &now, 0 );
  59. if( _showETC && shown() )
  60. {
  61. // compute the elapsed time
  62. double elapsed = (now.tv_sec - startT.tv_sec) + ( (now.tv_usec - startT.tv_usec) * TSCALE );
  63. // estimate the remaining time based on the elapsed time and the current progress
  64. double remaining = elapsed / this->value() - elapsed + 1.0f;
  65. int es, em, eh, rs, rm, rh;
  66. secs2HMS( elapsed, eh, em, es );
  67. //if( elapsed < 1.0 )
  68. //rh = rm = rs = 0;
  69. //else
  70. secs2HMS( remaining, rh, rm, rs );
  71. char buf[128];
  72. sprintf( buf, "Elapsed Time: %03d:%02d:%02d\n"
  73. "Remaining Time: %03d:%02d:%02d",
  74. eh, em, es, rh, rm, rs );
  75. etc->label( buf );
  76. etc->show();
  77. }
  78. else
  79. etc->hide();
  80. if( repeatTimer )
  81. {
  82. Fl::repeat_timeout( 1.0f, _secondTimerCB, this );
  83. Fl::check();
  84. }
  85. }
  86. bool Flu_Progress_Meter :: value( float v )
  87. {
  88. secondTimerCB( false );
  89. if( progress )
  90. progress->value( v );
  91. return _cancelled;
  92. }
  93. void Flu_Progress_Meter :: show( bool cancelBtnVisible )
  94. {
  95. gettimeofday( &startT, 0 );
  96. _cancelled = false;
  97. if( _cancelCB || cancelBtnVisible )
  98. cancel->show();
  99. else
  100. cancel->hide();
  101. //set_modal();
  102. Fl_Double_Window::show();
  103. Fl::add_timeout( 0.0f, _secondTimerCB, this );
  104. Fl::flush();
  105. }
  106. void Flu_Progress_Meter :: hide()
  107. {
  108. Fl::remove_timeout( _secondTimerCB, this );
  109. Fl_Double_Window::hide();
  110. Fl::flush();
  111. }