| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [bcf653] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
| [b47bfc] | 23 | /*
 | 
|---|
| [4cf323d] | 24 |  * QtStatusBar.cpp
 | 
|---|
| [b47bfc] | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Feb 17, 2010
 | 
|---|
 | 27 |  *      Author: crueger
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
| [bf3817] | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
| [bbbad5] | 34 | 
 | 
|---|
| [b47bfc] | 35 | #include <sstream>
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | #include <QtGui/QLabel>
 | 
|---|
 | 38 | #include <QtGui/QBoxLayout>
 | 
|---|
| [426c84] | 39 | #include <QtCore/QMetaType>
 | 
|---|
| [b47bfc] | 40 | #include <QtGui/QProgressBar>
 | 
|---|
| [378f2d5] | 41 | #include <QtCore/QTimer>
 | 
|---|
| [b47bfc] | 42 | 
 | 
|---|
| [4cf323d] | 43 | #include "QtStatusBar.hpp"
 | 
|---|
| [bbbad5] | 44 | 
 | 
|---|
| [ad011c] | 45 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [378f2d5] | 46 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| [bbbad5] | 47 | 
 | 
|---|
| [b47bfc] | 48 | #include "World.hpp"
 | 
|---|
| [378f2d5] | 49 | #include "Actions/ActionQueue.hpp"
 | 
|---|
 | 50 | #include "Actions/ActionStatusList.hpp"
 | 
|---|
| [b47bfc] | 51 | #include "Actions/Process.hpp"
 | 
|---|
 | 52 | 
 | 
|---|
| [5605793] | 53 | #define PLURAL_S(v) (((v)==1)?"":"s")
 | 
|---|
 | 54 | 
 | 
|---|
| [ce7fdc] | 55 | using namespace MoleCuilder;
 | 
|---|
| [b47bfc] | 56 | 
 | 
|---|
| [4cf323d] | 57 | QtStatusBar::QtStatusBar(QWidget *_parent) :
 | 
|---|
| [b47bfc] | 58 |   QStatusBar(_parent),
 | 
|---|
| [4cf323d] | 59 |   Observer("QtStatusBar"),
 | 
|---|
| [b47bfc] | 60 |   atomCount(World::getInstance().numAtoms()),
 | 
|---|
| [fc7504] | 61 |   moleculeCount(World::getInstance().numMolecules()),
 | 
|---|
| [426c84] | 62 |   parent(_parent),
 | 
|---|
| [378f2d5] | 63 |   activeProcess(""),
 | 
|---|
 | 64 |   StatusList(ActionQueue::getInstance().getStatusList()),
 | 
|---|
 | 65 |   StatusList_signedOn(false),
 | 
|---|
 | 66 |   timer(NULL),
 | 
|---|
| [19dfdf] | 67 |   timer_interval(4000)
 | 
|---|
| [b47bfc] | 68 | {
 | 
|---|
 | 69 |   World::getInstance().signOn(this);
 | 
|---|
 | 70 |   Process::AddObserver(this);
 | 
|---|
| [378f2d5] | 71 |   StatusList.signOn(this, ActionStatusList::StatusAdded);
 | 
|---|
 | 72 |   StatusList_signedOn = true;
 | 
|---|
| [b47bfc] | 73 |   statusLabel = new QLabel(this);
 | 
|---|
 | 74 |   statusLabel->setFrameStyle(QFrame::NoFrame | QFrame::Plain);
 | 
|---|
 | 75 |   addPermanentWidget(statusLabel);
 | 
|---|
 | 76 |   redrawStatus();
 | 
|---|
| [426c84] | 77 | 
 | 
|---|
| [378f2d5] | 78 |   // connect the timer
 | 
|---|
 | 79 |   timer = new QTimer(this);
 | 
|---|
 | 80 |   timer->stop();
 | 
|---|
 | 81 |   connect(timer, SIGNAL(timeout()), this, SLOT(updateStatusMessage()));
 | 
|---|
 | 82 | 
 | 
|---|
| [426c84] | 83 |   qRegisterMetaType<std::string>("std::string");
 | 
|---|
 | 84 |   connect(
 | 
|---|
 | 85 |       this, SIGNAL(redrawProgressBar(const std::string, const unsigned int, const unsigned int, const bool)),
 | 
|---|
 | 86 |       this, SLOT(updateProgressBar(const std::string, const unsigned int, const unsigned int, const bool)));
 | 
|---|
| [b47bfc] | 87 | }
 | 
|---|
 | 88 | 
 | 
|---|
| [4cf323d] | 89 | QtStatusBar::~QtStatusBar()
 | 
|---|
| [b47bfc] | 90 | {
 | 
|---|
| [378f2d5] | 91 |   // stop the timer if it is running
 | 
|---|
 | 92 |   if (timer->isActive())
 | 
|---|
 | 93 |     timer->stop();
 | 
|---|
 | 94 | 
 | 
|---|
| [b47bfc] | 95 |   Process::RemoveObserver(this);
 | 
|---|
 | 96 |   World::getInstance().signOff(this);
 | 
|---|
| [378f2d5] | 97 |   if (StatusList_signedOn)
 | 
|---|
 | 98 |     StatusList.signOff(this, ActionStatusList::StatusAdded);
 | 
|---|
| [b47bfc] | 99 | }
 | 
|---|
 | 100 | 
 | 
|---|
| [4cf323d] | 101 | void QtStatusBar::update(Observable *subject){
 | 
|---|
| [378f2d5] | 102 |   if (subject == World::getPointer()){
 | 
|---|
| [b47bfc] | 103 |     atomCount = World::getInstance().numAtoms();
 | 
|---|
 | 104 |     moleculeCount = World::getInstance().numMolecules();
 | 
|---|
| [378f2d5] | 105 |     // redraw only if no timer updates messages
 | 
|---|
 | 106 |     if (!timer->isActive())
 | 
|---|
 | 107 |       redrawStatus();
 | 
|---|
 | 108 |   } else if (subject == &StatusList) {
 | 
|---|
 | 109 |     // we do not react to general updates from StatusList
 | 
|---|
 | 110 |   } else {
 | 
|---|
| [b47bfc] | 111 |     // we probably have some process
 | 
|---|
| [426c84] | 112 |     // as notify comes from ActionQueue's thread, we have to use signal/slots
 | 
|---|
 | 113 |     // to inform ourselves but within the main() thread to be able to add
 | 
|---|
 | 114 |     // the progressbar widget.
 | 
|---|
| [b47bfc] | 115 |     Process *proc;
 | 
|---|
 | 116 |     if((proc=dynamic_cast<Process*>(subject))){
 | 
|---|
| [426c84] | 117 |       const bool StopStatus = proc->doesStop();
 | 
|---|
 | 118 |       emit redrawProgressBar(proc->getName(), proc->getMaxSteps(), proc->getCurrStep(), StopStatus);
 | 
|---|
| [b47bfc] | 119 |     }
 | 
|---|
 | 120 |   }
 | 
|---|
 | 121 | }
 | 
|---|
 | 122 | 
 | 
|---|
| [378f2d5] | 123 | void QtStatusBar::startTimer()
 | 
|---|
 | 124 | {
 | 
|---|
 | 125 |   timer->start(timer_interval);
 | 
|---|
 | 126 | }
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 | void QtStatusBar::stopTimer()
 | 
|---|
 | 129 | {
 | 
|---|
 | 130 |   timer->stop();
 | 
|---|
 | 131 | }
 | 
|---|
 | 132 | 
 | 
|---|
 | 133 | void QtStatusBar::updateStatusMessage()
 | 
|---|
 | 134 | {
 | 
|---|
 | 135 |   if (StatusList.size() != 0) {
 | 
|---|
 | 136 |     // get oldest message from the StatusList
 | 
|---|
 | 137 |     const std::string message = StatusList.popFirstMessage();
 | 
|---|
 | 138 |     statusLabel->setText(QString(message.c_str()));
 | 
|---|
 | 139 |   } else {
 | 
|---|
 | 140 |     // just send the standard message
 | 
|---|
 | 141 |     redrawStatus();
 | 
|---|
 | 142 |     // and stop the timer
 | 
|---|
 | 143 |     stopTimer();
 | 
|---|
 | 144 |   }
 | 
|---|
 | 145 | }
 | 
|---|
 | 146 | 
 | 
|---|
 | 147 | void QtStatusBar::recieveNotification(Observable *_publisher, Notification *_notification)
 | 
|---|
 | 148 | {
 | 
|---|
 | 149 |   if (_publisher == &StatusList) {
 | 
|---|
 | 150 |     switch(_notification->getChannelNo()) {
 | 
|---|
 | 151 |       case MoleCuilder::ActionStatusList::StatusAdded:
 | 
|---|
 | 152 |         if (!timer->isActive()) {
 | 
|---|
 | 153 |           // if timer is not already running
 | 
|---|
 | 154 |           updateStatusMessage();
 | 
|---|
 | 155 |           startTimer();
 | 
|---|
 | 156 |         }
 | 
|---|
 | 157 |         break;
 | 
|---|
 | 158 |     }
 | 
|---|
 | 159 |   }
 | 
|---|
 | 160 | }
 | 
|---|
 | 161 | 
 | 
|---|
 | 162 | void QtStatusBar::subjectKilled(Observable *subject)
 | 
|---|
 | 163 | {
 | 
|---|
| [b47bfc] | 164 |   // Processes don't notify when they are killed
 | 
|---|
| [378f2d5] | 165 |   if (subject == &StatusList) {
 | 
|---|
 | 166 |     // print all remaining messages
 | 
|---|
 | 167 |     while (StatusList.size() != 0)
 | 
|---|
 | 168 |       updateStatusMessage();
 | 
|---|
 | 169 |     // don't need to sign off, just note down that we are
 | 
|---|
 | 170 |     StatusList_signedOn = false;
 | 
|---|
 | 171 |   } else {
 | 
|---|
 | 172 |     atomCount = World::getInstance().numAtoms();
 | 
|---|
 | 173 |     moleculeCount = World::getInstance().numMolecules();
 | 
|---|
 | 174 |     World::getInstance().signOn(this);
 | 
|---|
 | 175 |     redrawStatus();
 | 
|---|
 | 176 |   }
 | 
|---|
| [b47bfc] | 177 | }
 | 
|---|
 | 178 | 
 | 
|---|
| [4cf323d] | 179 | void QtStatusBar::redrawStatus(){
 | 
|---|
| [b47bfc] | 180 |   stringstream sstr;
 | 
|---|
 | 181 |   sstr << "You have " << atomCount << " atom" << PLURAL_S(atomCount)
 | 
|---|
 | 182 |        <<" in " << moleculeCount << " molecule" << PLURAL_S(moleculeCount);
 | 
|---|
 | 183 |   statusLabel->setText(QString(sstr.str().c_str()));
 | 
|---|
 | 184 | }
 | 
|---|
 | 185 | 
 | 
|---|
| [426c84] | 186 | void QtStatusBar::updateProgressBar(
 | 
|---|
 | 187 |     const std::string name,
 | 
|---|
 | 188 |     const unsigned int maxsteps,
 | 
|---|
 | 189 |     const unsigned int currentstep,
 | 
|---|
 | 190 |     const bool StopStatus)
 | 
|---|
 | 191 | {
 | 
|---|
| [b47bfc] | 192 |   progressIndicator *ind=0;
 | 
|---|
| [426c84] | 193 |   progressBars_t::iterator iter = progressBars.find(name);
 | 
|---|
| [b47bfc] | 194 |   // see what we have to do with the process
 | 
|---|
| [426c84] | 195 |   if (iter == progressBars.end()) {
 | 
|---|
 | 196 |     ind = new progressIndicator(name);
 | 
|---|
 | 197 |     ind->bar->setMaximum(maxsteps);
 | 
|---|
 | 198 |     progressBars.insert( std::make_pair(name,ind) );
 | 
|---|
 | 199 |   } else {
 | 
|---|
 | 200 |     ind = iter->second;
 | 
|---|
| [b47bfc] | 201 |   }
 | 
|---|
| [426c84] | 202 |   if (activeProcess != name) {
 | 
|---|
| [b47bfc] | 203 |     addWidget(ind->container);
 | 
|---|
| [426c84] | 204 |     activeProcess = name;
 | 
|---|
| [b47bfc] | 205 |   }
 | 
|---|
| [426c84] | 206 |   ind->bar->setValue(currentstep);
 | 
|---|
| [b47bfc] | 207 |   parent->repaint();
 | 
|---|
| [426c84] | 208 |   if ((iter != progressBars.end()) && StopStatus) {
 | 
|---|
| [b47bfc] | 209 |     removeWidget(ind->container);
 | 
|---|
| [426c84] | 210 |     activeProcess = std::string("");
 | 
|---|
 | 211 |     progressBars.erase(name);
 | 
|---|
| [b47bfc] | 212 |     delete ind;
 | 
|---|
 | 213 |   }
 | 
|---|
 | 214 | }
 | 
|---|
 | 215 | 
 | 
|---|
 | 216 | 
 | 
|---|
 | 217 | 
 | 
|---|
| [426c84] | 218 | QtStatusBar::progressIndicator::progressIndicator(const std::string &name){
 | 
|---|
| [b47bfc] | 219 |   stringstream sstr;
 | 
|---|
 | 220 |   sstr << "Busy (" << name << ")";
 | 
|---|
 | 221 |   container = new QWidget();
 | 
|---|
 | 222 |   layout = new QHBoxLayout(container);
 | 
|---|
 | 223 |   label = new QLabel(QString(sstr.str().c_str()));
 | 
|---|
 | 224 |   bar = new QProgressBar();
 | 
|---|
 | 225 | 
 | 
|---|
 | 226 |   layout->addWidget(label);
 | 
|---|
 | 227 |   layout->addWidget(bar);
 | 
|---|
 | 228 |   container->setLayout(layout);
 | 
|---|
 | 229 | }
 | 
|---|
 | 230 | 
 | 
|---|
| [4cf323d] | 231 | QtStatusBar::progressIndicator::~progressIndicator(){
 | 
|---|
| [b47bfc] | 232 |   delete container;
 | 
|---|
 | 233 | }
 | 
|---|