[b47bfc] | 1 | /*
|
---|
| 2 | * QTStatusBar.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Feb 17, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include <sstream>
|
---|
| 9 |
|
---|
| 10 | #include <QtGui/QLabel>
|
---|
| 11 | #include <QtGui/QBoxLayout>
|
---|
| 12 | #include <QtGui/QProgressBar>
|
---|
| 13 |
|
---|
| 14 | #include "QTStatusBar.hpp"
|
---|
| 15 | #include "World.hpp"
|
---|
| 16 | #include "helpers.hpp"
|
---|
| 17 | #include "Actions/Process.hpp"
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | QTStatusBar::QTStatusBar(QWidget *_parent) :
|
---|
| 21 | QStatusBar(_parent),
|
---|
| 22 | Observer("QTStatusBar"),
|
---|
| 23 | parent(_parent),
|
---|
| 24 | atomCount(World::getInstance().numAtoms()),
|
---|
| 25 | moleculeCount(World::getInstance().numMolecules())
|
---|
| 26 | {
|
---|
| 27 | World::getInstance().signOn(this);
|
---|
| 28 | Process::AddObserver(this);
|
---|
| 29 | statusLabel = new QLabel(this);
|
---|
| 30 | statusLabel->setFrameStyle(QFrame::NoFrame | QFrame::Plain);
|
---|
| 31 | addPermanentWidget(statusLabel);
|
---|
| 32 | redrawStatus();
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | QTStatusBar::~QTStatusBar()
|
---|
| 36 | {
|
---|
| 37 | Process::RemoveObserver(this);
|
---|
| 38 | World::getInstance().signOff(this);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | void QTStatusBar::update(Observable *subject){
|
---|
| 42 | if (subject==World::getPointer()){
|
---|
| 43 | atomCount = World::getInstance().numAtoms();
|
---|
| 44 | moleculeCount = World::getInstance().numMolecules();
|
---|
| 45 | redrawStatus();
|
---|
| 46 | }
|
---|
| 47 | else {
|
---|
| 48 | // we probably have some process
|
---|
| 49 | Process *proc;
|
---|
| 50 | if((proc=dynamic_cast<Process*>(subject))){
|
---|
| 51 | redrawProcess(proc);
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void QTStatusBar::subjectKilled(Observable *subject){
|
---|
| 57 | // Processes don't notify when they are killed
|
---|
| 58 | atomCount = World::getInstance().numAtoms();
|
---|
| 59 | moleculeCount = World::getInstance().numMolecules();
|
---|
| 60 | World::getInstance().signOn(this);
|
---|
| 61 | redrawStatus();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | void QTStatusBar::redrawStatus(){
|
---|
| 65 | stringstream sstr;
|
---|
| 66 | sstr << "You have " << atomCount << " atom" << PLURAL_S(atomCount)
|
---|
| 67 | <<" in " << moleculeCount << " molecule" << PLURAL_S(moleculeCount);
|
---|
| 68 | statusLabel->setText(QString(sstr.str().c_str()));
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void QTStatusBar::redrawProcess(Process *proc){
|
---|
| 72 | progressIndicator *ind=0;
|
---|
| 73 | // see what we have to do with the process
|
---|
| 74 | if(proc->doesStart()){
|
---|
| 75 | ind = new progressIndicator(proc->getName());
|
---|
| 76 | ind->bar->setMaximum(proc->getMaxSteps());
|
---|
| 77 | progressBars.insert(pair<Process*,progressIndicator*>(proc,ind));
|
---|
| 78 | }
|
---|
| 79 | else {
|
---|
| 80 | ind = progressBars[proc];
|
---|
| 81 | }
|
---|
| 82 | if(activeProcess!=proc){
|
---|
| 83 | addWidget(ind->container);
|
---|
| 84 | activeProcess = proc;
|
---|
| 85 | }
|
---|
| 86 | ind->bar->setValue(proc->getCurrStep());
|
---|
| 87 | parent->repaint();
|
---|
| 88 | if(proc->doesStop()){
|
---|
| 89 | removeWidget(ind->container);
|
---|
| 90 | activeProcess = 0;
|
---|
| 91 | progressBars.erase(proc);
|
---|
| 92 | delete ind;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | QTStatusBar::progressIndicator::progressIndicator(string name){
|
---|
| 99 | stringstream sstr;
|
---|
| 100 | sstr << "Busy (" << name << ")";
|
---|
| 101 | container = new QWidget();
|
---|
| 102 | layout = new QHBoxLayout(container);
|
---|
| 103 | label = new QLabel(QString(sstr.str().c_str()));
|
---|
| 104 | bar = new QProgressBar();
|
---|
| 105 |
|
---|
| 106 | layout->addWidget(label);
|
---|
| 107 | layout->addWidget(bar);
|
---|
| 108 | container->setLayout(layout);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | QTStatusBar::progressIndicator::~progressIndicator(){
|
---|
| 112 | delete container;
|
---|
| 113 | }
|
---|