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::paintEvent(QPaintEvent * event)
|
---|
102 | {
|
---|
103 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
104 |
|
---|
105 | QStatusBar::paintEvent(event);
|
---|
106 | }
|
---|
107 |
|
---|
108 | void QtStatusBar::update(Observable *subject)
|
---|
109 | {
|
---|
110 | if (subject == World::getPointer()){
|
---|
111 | atomCount = World::getInstance().numAtoms();
|
---|
112 | moleculeCount = World::getInstance().numMolecules();
|
---|
113 | // redraw only if no timer updates messages
|
---|
114 | if (!timer->isActive())
|
---|
115 | redrawStatus();
|
---|
116 | } else if (subject == &StatusList) {
|
---|
117 | // we do not react to general updates from StatusList
|
---|
118 | } else {
|
---|
119 | // we probably have some process
|
---|
120 | // as notify comes from ActionQueue's thread, we have to use signal/slots
|
---|
121 | // to inform ourselves but within the main() thread to be able to add
|
---|
122 | // the progressbar widget.
|
---|
123 | Process *proc;
|
---|
124 | if((proc=dynamic_cast<Process*>(subject))){
|
---|
125 | const bool StopStatus = proc->doesStop();
|
---|
126 | emit redrawProgressBar(proc->getName(), proc->getMaxSteps(), proc->getCurrStep(), StopStatus);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | void QtStatusBar::startTimer()
|
---|
132 | {
|
---|
133 | timer->start(timer_interval);
|
---|
134 | }
|
---|
135 |
|
---|
136 | void QtStatusBar::stopTimer()
|
---|
137 | {
|
---|
138 | timer->stop();
|
---|
139 | }
|
---|
140 |
|
---|
141 | void QtStatusBar::updateStatusMessage()
|
---|
142 | {
|
---|
143 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
144 |
|
---|
145 | if (StatusList.size() != 0) {
|
---|
146 | // get oldest message from the StatusList
|
---|
147 | const std::string message = StatusList.popFirstMessage();
|
---|
148 | statusLabel->setText(QString(message.c_str()));
|
---|
149 | } else {
|
---|
150 | // just send the standard message
|
---|
151 | redrawStatus();
|
---|
152 | // and stop the timer
|
---|
153 | stopTimer();
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | void QtStatusBar::recieveNotification(Observable *_publisher, Notification *_notification)
|
---|
158 | {
|
---|
159 | if (_publisher == &StatusList) {
|
---|
160 | switch(_notification->getChannelNo()) {
|
---|
161 | case MoleCuilder::ActionStatusList::StatusAdded:
|
---|
162 | if (!timer->isActive()) {
|
---|
163 | // if timer is not already running
|
---|
164 | updateStatusMessage();
|
---|
165 | startTimer();
|
---|
166 | }
|
---|
167 | break;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | void QtStatusBar::subjectKilled(Observable *subject)
|
---|
173 | {
|
---|
174 | // Processes don't notify when they are killed
|
---|
175 | if (subject == &StatusList) {
|
---|
176 | // print all remaining messages
|
---|
177 | while (StatusList.size() != 0)
|
---|
178 | updateStatusMessage();
|
---|
179 | // don't need to sign off, just note down that we are
|
---|
180 | StatusList_signedOn = false;
|
---|
181 | } else {
|
---|
182 | atomCount = World::getInstance().numAtoms();
|
---|
183 | moleculeCount = World::getInstance().numMolecules();
|
---|
184 | World::getInstance().signOn(this);
|
---|
185 | redrawStatus();
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | void QtStatusBar::redrawStatus()
|
---|
190 | {
|
---|
191 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
192 |
|
---|
193 | stringstream sstr;
|
---|
194 | sstr << "You have " << atomCount << " atom" << PLURAL_S(atomCount)
|
---|
195 | <<" in " << moleculeCount << " molecule" << PLURAL_S(moleculeCount);
|
---|
196 | statusLabel->setText(QString(sstr.str().c_str()));
|
---|
197 | }
|
---|
198 |
|
---|
199 | void QtStatusBar::updateProgressBar(
|
---|
200 | const std::string name,
|
---|
201 | const unsigned int maxsteps,
|
---|
202 | const unsigned int currentstep,
|
---|
203 | const bool StopStatus)
|
---|
204 | {
|
---|
205 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
206 |
|
---|
207 | progressIndicator *ind=0;
|
---|
208 | progressBars_t::iterator iter = progressBars.find(name);
|
---|
209 | // see what we have to do with the process
|
---|
210 | if (iter == progressBars.end()) {
|
---|
211 | ind = new progressIndicator(name);
|
---|
212 | ind->bar->setMaximum(maxsteps);
|
---|
213 | progressBars.insert( std::make_pair(name,ind) );
|
---|
214 | } else {
|
---|
215 | ind = iter->second;
|
---|
216 | }
|
---|
217 | if (activeProcess != name) {
|
---|
218 | addWidget(ind->container);
|
---|
219 | activeProcess = name;
|
---|
220 | }
|
---|
221 | ind->bar->setValue(currentstep);
|
---|
222 | parent->repaint();
|
---|
223 | if ((iter != progressBars.end()) && StopStatus) {
|
---|
224 | removeWidget(ind->container);
|
---|
225 | activeProcess = std::string("");
|
---|
226 | progressBars.erase(name);
|
---|
227 | delete ind;
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 |
|
---|
233 | QtStatusBar::progressIndicator::progressIndicator(const std::string &name)
|
---|
234 | {
|
---|
235 | stringstream sstr;
|
---|
236 | sstr << "Busy (" << name << ")";
|
---|
237 | container = new QWidget();
|
---|
238 | layout = new QHBoxLayout(container);
|
---|
239 | label = new QLabel(QString(sstr.str().c_str()));
|
---|
240 | bar = new QProgressBar();
|
---|
241 |
|
---|
242 | layout->addWidget(label);
|
---|
243 | layout->addWidget(bar);
|
---|
244 | container->setLayout(layout);
|
---|
245 | }
|
---|
246 |
|
---|
247 | QtStatusBar::progressIndicator::~progressIndicator(){
|
---|
248 | delete container;
|
---|
249 | }
|
---|