Re: ProgressBar and error handling



On 2/26/07, Sirisha Muppavarapu <sirisha muppavarapu veralight com> wrote:



Thanks for ur response Paul. Here is more explanation on what I am doing.

 onTimeOut() method is called every 0.5s. Mean while, I do not know when the
error will happen. If the error happens, I open a new CErrorWindow window
(Plz note CErrorWindow is not a Dialog, but a subclass derived from window
and is displayed in place of CProgressBar window. My application design is
such that I can't use regular dialog window).

 "Is there a reason you're passing the progress bar to the error dialog?"

 Yes, In CErrorWindow, on clicking "Continue" button, I am showing the
CProgressBar window using the reference passed to CErrorWindow, by calling

 void CErrorWindow :: continueAction() {
  this->hide();
  Gtk::Main::run(m_handleToProgressBar);
  present();
 }

 What I am trying to achieve here is to get back to the same CProgresBar
object where I got interrupted and resume from there.

 This is my first ever Gtk app. I am sure, there must be better design out
there to do the same. Also, I am not using glade to manage my windows.
Everytime I need to navigate from one window to the other (at the click of a
button), I do something similar to above or something like

         this->hide();
         CSomeWindow *some = new CSomeWindow(parameters);
         Gtk::Main::run(*some);
         present();

 I am really worried about "N" objects being created and lying there without
destructor being invoked. Is there a better way of doing all the above
without using glade?

 Thanks
 Sirisha



 -----Original Message-----
 From: paul joseph davis gmail com on behalf of Paul Davis
 Sent: Mon 2/26/2007 6:23 PM
 To: Sirisha Muppavarapu
 Cc: gtkmm-list gnome org
 Subject: Re: ProgressBar and error handling

 On 2/26/07, Sirisha Muppavarapu
<sirisha muppavarapu veralight com> wrote:
 >
 >
 >
 >
 > Hi All
 >
 >  I have this ProgressBar Window which is running a task and updates
 > itself(using set_fraction) and it uses signal_timeout()(and onTimeOut()
 > callback) method. During this backend processing before updating
itself,if
 > it encounters an error, it should show another window with the error msg.
On
 > clicking continue on the error message, the task should continue its
 > progress and the progress bar should show progress from where it left.
 >
 >  I am able to continue running the task at the back end but how to
continue
 > the progress bar's progress from where I left? say it encountered an
error
 > at 24% and on clicking continue, the progress should proceed from 24%
 >
 >  Any ideas on how to achieve this?
 >
 >  Thanks a ton in advance
 >  Sirisha
 >
 >  Code Snippet
 >  -------------
 >
 >  class CProgressBar : public Gtk::Window {
 >
 >  public:
 >  bool onTimeOut();
 >
 >  }
 >
 >  CProgressBar {
 >  //someother initializations and logic
 >  Glib::signal_timeout().connect( sigc::mem_fun(*this,
 > &CGProgressBar::onTimeout), 500 );
 >  }
 >
 >  bool CProgressBar::onTimeOut() {
 >
 >  //update the progress bar using set_fraction

 When you come back into onTimeOut, this logic should figure out where
 to start from. You can get the current progress bar value with
 get_fraction() IIRC.

 >  //and wait for error to happen in the backend
 >
 >     if(error) {
 >      this->hide();
 >      //pass the CProgressBar reference to CErrorWindow object

 Is there a reason you're passing the progress bar to the error dialog?


 >      CErrorWindow *error = new CErrorWindow(*this);
 >      Gtk::Main::run(*error);
 >      present();
 >      return false;
 >     }
 >  return true;
 >  }//end of method
 >
 >
 >
 >
 >  int main() {
 >
 >  CProgressBar *pg = new CProgressBar();
 >  Gtk::Main::run(*pg);
 >  present();
 >  }
 >
 >
 >  In CErrorWindow, there are 2 button "CONTINUE" and "CANCEL"
 >
 >  clicking on continue should take you back to CProgressBar and resume
from
 > where it left.
 >
 >
 > _______________________________________________
 > gtkmm-list mailing list
 > gtkmm-list gnome org
 > http://mail.gnome.org/mailman/listinfo/gtkmm-list
 >
 >

 You should just be able to call back to onTimeOut unless I'm missing
 something important.  The progress bar will know what fraction its at,
 but its your job to tell it how to update, which includes continuing
 from 25% or what not.

 HTH,
 Paul Davis





Here's a working example that does what you ask.  I don't understand
why you can't use a dialog.  I did cause I don't really see anyother
way to do such a think.

And recursively calling the main loop seems ungood.  Maybe murray or
someone with more knowledge could shed light on if this will cause
problems or not.

Paul
#include <iostream>

#include <gtkmm.h>

class MainWindow : public Gtk::Window
{
	public:

		MainWindow()
		{
			set_size_request( 400, 30 ) ;
			set_title( "ProgressBar Test" ) ;

			Gtk::HBox*	hbox = Gtk::manage( new Gtk::HBox() ) ;
			hbox->set_border_width( 5 ) ;
			Gtk::Label*	lbl = Gtk::manage( new Gtk::Label( "Status: " ) ) ;

			hbox->pack_start( *lbl, Gtk::PACK_SHRINK ) ;

			_pb = Gtk::manage( new Gtk::ProgressBar() ) ;
			_pb->set_fraction( 0.0 ) ;
			_pb->show() ;
			hbox->pack_start( *_pb, Gtk::PACK_EXPAND_WIDGET ) ;

			add( *hbox ) ;

			Glib::signal_timeout().connect( sigc::mem_fun( *this, &MainWindow::updateProgressBar ), 250 ) ;
			_error_shown = false ;

			show_all_children() ;
		}

		bool
		updateProgressBar()
		{
			std::cerr << "Timeout" << std::endl ;

			if( !_error_shown && _pb->get_fraction() > 0.8 )
			{
				_error_shown = true ;
				Gtk::MessageDialog d( *this, "This is an error dialog.", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true ) ;
				d.run() ;
				d.hide() ;
				return true ;
			}
			else if( _pb->get_fraction() >= 1.0 )
			{
				Gtk::MessageDialog d( *this, "Operation finished.", false, Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, true ) ;
				d.run() ;
				d.hide() ;
				hide() ;

				return false ;
			}
			else
			{
				double frac = _pb->get_fraction() ;
				frac += 0.05 ;
				if( frac >= 1.0 ) frac = 1.0 ;
				_pb->set_fraction( frac ) ;
			}

			return true ;
		}

	private:

		bool				_error_shown ;
		Gtk::ProgressBar*	_pb ;
} ;

int
main( int argc, char* argv[] )
{
	Gtk::Main m( argc, argv ) ;
	MainWindow mw ;
	Gtk::Main::run( mw ) ;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]