| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 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/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * QtStatusBar.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Feb 17, 2010 | 
|---|
| 27 | *      Author: crueger | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | #include <sstream> | 
|---|
| 36 |  | 
|---|
| 37 | #include <QtGui/QLabel> | 
|---|
| 38 | #include <QtGui/QBoxLayout> | 
|---|
| 39 | #include <QtCore/QMetaType> | 
|---|
| 40 | #include <QtGui/QProgressBar> | 
|---|
| 41 | #include <QtCore/QTimer> | 
|---|
| 42 |  | 
|---|
| 43 | #include "QtStatusBar.hpp" | 
|---|
| 44 |  | 
|---|
| 45 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 46 | #include "CodePatterns/Observer/Notification.hpp" | 
|---|
| 47 |  | 
|---|
| 48 | #include "World.hpp" | 
|---|
| 49 | #include "Actions/ActionQueue.hpp" | 
|---|
| 50 | #include "Actions/ActionStatusList.hpp" | 
|---|
| 51 | #include "Actions/Process.hpp" | 
|---|
| 52 |  | 
|---|
| 53 | #define PLURAL_S(v) (((v)==1)?"":"s") | 
|---|
| 54 |  | 
|---|
| 55 | using namespace MoleCuilder; | 
|---|
| 56 |  | 
|---|
| 57 | QtStatusBar::QtStatusBar(QWidget *_parent) : | 
|---|
| 58 | QStatusBar(_parent), | 
|---|
| 59 | Observer("QtStatusBar"), | 
|---|
| 60 | atomCount(World::getInstance().numAtoms()), | 
|---|
| 61 | moleculeCount(World::getInstance().numMolecules()), | 
|---|
| 62 | parent(_parent), | 
|---|
| 63 | activeProcess(""), | 
|---|
| 64 | StatusList(ActionQueue::getInstance().getStatusList()), | 
|---|
| 65 | StatusList_signedOn(false), | 
|---|
| 66 | timer(NULL), | 
|---|
| 67 | timer_interval(4000) | 
|---|
| 68 | { | 
|---|
| 69 | World::getInstance().signOn(this); | 
|---|
| 70 | Process::AddObserver(this); | 
|---|
| 71 | StatusList.signOn(this, ActionStatusList::StatusAdded); | 
|---|
| 72 | StatusList_signedOn = true; | 
|---|
| 73 | statusLabel = new QLabel(this); | 
|---|
| 74 | statusLabel->setFrameStyle(QFrame::NoFrame | QFrame::Plain); | 
|---|
| 75 | addPermanentWidget(statusLabel); | 
|---|
| 76 | redrawStatus(); | 
|---|
| 77 |  | 
|---|
| 78 | // connect the timer | 
|---|
| 79 | timer = new QTimer(this); | 
|---|
| 80 | timer->stop(); | 
|---|
| 81 | connect(timer, SIGNAL(timeout()), this, SLOT(updateStatusMessage())); | 
|---|
| 82 |  | 
|---|
| 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))); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | QtStatusBar::~QtStatusBar() | 
|---|
| 90 | { | 
|---|
| 91 | // stop the timer if it is running | 
|---|
| 92 | if (timer->isActive()) | 
|---|
| 93 | timer->stop(); | 
|---|
| 94 |  | 
|---|
| 95 | Process::RemoveObserver(this); | 
|---|
| 96 | World::getInstance().signOff(this); | 
|---|
| 97 | if (StatusList_signedOn) | 
|---|
| 98 | StatusList.signOff(this, ActionStatusList::StatusAdded); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | void QtStatusBar::update(Observable *subject){ | 
|---|
| 102 | if (subject == World::getPointer()){ | 
|---|
| 103 | atomCount = World::getInstance().numAtoms(); | 
|---|
| 104 | moleculeCount = World::getInstance().numMolecules(); | 
|---|
| 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 { | 
|---|
| 111 | // we probably have some process | 
|---|
| 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. | 
|---|
| 115 | Process *proc; | 
|---|
| 116 | if((proc=dynamic_cast<Process*>(subject))){ | 
|---|
| 117 | const bool StopStatus = proc->doesStop(); | 
|---|
| 118 | emit redrawProgressBar(proc->getName(), proc->getMaxSteps(), proc->getCurrStep(), StopStatus); | 
|---|
| 119 | } | 
|---|
| 120 | } | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 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 | { | 
|---|
| 164 | // Processes don't notify when they are killed | 
|---|
| 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 | } | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | void QtStatusBar::redrawStatus(){ | 
|---|
| 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 |  | 
|---|
| 186 | void QtStatusBar::updateProgressBar( | 
|---|
| 187 | const std::string name, | 
|---|
| 188 | const unsigned int maxsteps, | 
|---|
| 189 | const unsigned int currentstep, | 
|---|
| 190 | const bool StopStatus) | 
|---|
| 191 | { | 
|---|
| 192 | progressIndicator *ind=0; | 
|---|
| 193 | progressBars_t::iterator iter = progressBars.find(name); | 
|---|
| 194 | // see what we have to do with the process | 
|---|
| 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; | 
|---|
| 201 | } | 
|---|
| 202 | if (activeProcess != name) { | 
|---|
| 203 | addWidget(ind->container); | 
|---|
| 204 | activeProcess = name; | 
|---|
| 205 | } | 
|---|
| 206 | ind->bar->setValue(currentstep); | 
|---|
| 207 | parent->repaint(); | 
|---|
| 208 | if ((iter != progressBars.end()) && StopStatus) { | 
|---|
| 209 | removeWidget(ind->container); | 
|---|
| 210 | activeProcess = std::string(""); | 
|---|
| 211 | progressBars.erase(name); | 
|---|
| 212 | delete ind; | 
|---|
| 213 | } | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 |  | 
|---|
| 217 |  | 
|---|
| 218 | QtStatusBar::progressIndicator::progressIndicator(const std::string &name){ | 
|---|
| 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 |  | 
|---|
| 231 | QtStatusBar::progressIndicator::~progressIndicator(){ | 
|---|
| 232 | delete container; | 
|---|
| 233 | } | 
|---|