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