Hi,

Whenever we do a calculation, that takes a little time, for example rendering a video project, we have to show a little dialog that displays a progressbar, or something similar.

progress bar

But how to implement such a thing in a sensible way? The approach that I’ve choosen is the following:

First of all you define a simple abstract class, an interface in java jargon.

class IProgressListener
{
public:
virtual ~IProgressListener() {}
virtual bool progress( int percent ) = 0;
virtual void start() = 0;
virtual void end() = 0;
};

The progress method is called periodically in the calculation loop, and if the return value equals true, the loop is canceled. The parameter percent is a number between 0 and 100, which indicates the progress being made. Start and end are called in the beginning and respectivly end of the calculation function.

This is a simple but useful convention, however we have not done anything visible to the outside world yet. This is what we’ll do now. Derive a class for a dialog in your favorite UI-Environment, and implement this Interface, i.e. inherit this abstract class.

The next steps should be obvious, the start method should show the dialog in a modal state, and the end method should hide die Dialog. The progress call would update the progress bar, and make a call to the toolkit to yield any outstandig events, for the redraw to happen, and to make sure the events for the cancel button are processed.

Of course you have to make sure that any click on the cancel button results in the progress method returning true to stop the calculation.

So now the only thing left is to provide the calculation function with an instance of this dialog, and the rest should work automagically.

BTW. This approch has the advantage that the dialog can be easily substituted with a statusbar or whatever without touching the calculation function. For an implementation in FLTK see the openme repository. Another useful property of this idea is that ProgressListeners can be easily nested, as I’ve done in the Gungirl Sequencer.

A different approuch would have been to involve threads or multiple processes for the calculation, but this is evil and more prone to bugs and errors. ;)

Have fun.
-Richard

Tags Development, Open Movie Editor

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image