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