progress_bar.py
- Code: Select all
import rocket
class ProgressBar(rocket.Element):
"""Progress bar class to represent the progress of a certain task
which typical will take more than a few seconds to complete. This
provides feedback to the user on the estimated time to completion
and let's him know that the program has not crashed.
"""
def __init__(self, tag):
rocket.Element.__init__(self, tag)
self.inner_rml = '<progressfill/>'
self.progress = 0
self.AddEventListener('setprogress', self._onSetProgress)
@staticmethod
def _onSetProgress():
self.progress = event.parameters['progress']
def _setProgress(self, progress):
self.first_child.style.width = str(progress) + r'%'
self._progress = progress
def _getProgress(self):
return self._progress
progress = property(_getProgress, _setProgress)
rocket.RegisterTag('progressbar', ProgressBar)
To use it simply import progress_bar in your rml file, and place the <progressbar /> tag wherever you want in the document. This element creates a progressfill sub-element to represent the progress. You will need to style progressbar and progressfill as you see fit in the rcss file. Here is a very simple example of how it can be styled but you will probably want to use a tiled-horizontal decorator with some nice graphics:
- Code: Select all
progressbar
{
display: block;
width: auto;
height: 30px;
background: #000000;
padding: 5px;
}
progressfill
{
display: block;
height: 100%;
background: #efbf00;
}
To set the progress in python you can type something like:
- Code: Select all
progressBar = document.GetElementById('progressbar')
progressBar.progress = 20
The progress parameter is given as a percentage, so this is now set to 20%.
From C++ you can't access the class directly (I think, maybe I'm wrong?) so instead you can send it an event to set the progress like so:
- Code: Select all
Rocket::Core::Element* pProgressBar = pDoc->GetElementById("progressbar");
EMP::Core::Dictionary parameters;
parameters.Set("progress", 20);
pProgressBar->DispatchEvent("setprogress", parameters);

