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