source: src/UIElements/Views/Qt4/QtStatusBar.cpp

Candidate_v1.6.1
Last change on this file was 7516f6, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

FIX: several Qt...Lists required mutex to control refill triggered by observable update.

  • Property mode set to 100644
File size: 6.8 KB
RevLine 
[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
[9eb71b3]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]55using namespace MoleCuilder;
[b47bfc]56
[4cf323d]57QtStatusBar::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]89QtStatusBar::~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
[7516f6]101void QtStatusBar::paintEvent(QPaintEvent * event)
102{
103 boost::recursive_mutex::scoped_lock lock(refill_mutex);
104
105 QStatusBar::paintEvent(event);
106}
107
108void QtStatusBar::update(Observable *subject)
109{
[378f2d5]110 if (subject == World::getPointer()){
[b47bfc]111 atomCount = World::getInstance().numAtoms();
112 moleculeCount = World::getInstance().numMolecules();
[378f2d5]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 {
[b47bfc]119 // we probably have some process
[426c84]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.
[b47bfc]123 Process *proc;
124 if((proc=dynamic_cast<Process*>(subject))){
[426c84]125 const bool StopStatus = proc->doesStop();
126 emit redrawProgressBar(proc->getName(), proc->getMaxSteps(), proc->getCurrStep(), StopStatus);
[b47bfc]127 }
128 }
129}
130
[378f2d5]131void QtStatusBar::startTimer()
132{
133 timer->start(timer_interval);
134}
135
136void QtStatusBar::stopTimer()
137{
138 timer->stop();
139}
140
141void QtStatusBar::updateStatusMessage()
142{
[7516f6]143 boost::recursive_mutex::scoped_lock lock(refill_mutex);
144
[378f2d5]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
157void 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
172void QtStatusBar::subjectKilled(Observable *subject)
173{
[b47bfc]174 // Processes don't notify when they are killed
[378f2d5]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 }
[b47bfc]187}
188
[7516f6]189void QtStatusBar::redrawStatus()
190{
191 boost::recursive_mutex::scoped_lock lock(refill_mutex);
192
[b47bfc]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
[426c84]199void QtStatusBar::updateProgressBar(
200 const std::string name,
201 const unsigned int maxsteps,
202 const unsigned int currentstep,
203 const bool StopStatus)
204{
[7516f6]205 boost::recursive_mutex::scoped_lock lock(refill_mutex);
206
[b47bfc]207 progressIndicator *ind=0;
[426c84]208 progressBars_t::iterator iter = progressBars.find(name);
[b47bfc]209 // see what we have to do with the process
[426c84]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;
[b47bfc]216 }
[426c84]217 if (activeProcess != name) {
[b47bfc]218 addWidget(ind->container);
[426c84]219 activeProcess = name;
[b47bfc]220 }
[426c84]221 ind->bar->setValue(currentstep);
[b47bfc]222 parent->repaint();
[426c84]223 if ((iter != progressBars.end()) && StopStatus) {
[b47bfc]224 removeWidget(ind->container);
[426c84]225 activeProcess = std::string("");
226 progressBars.erase(name);
[b47bfc]227 delete ind;
228 }
229}
230
231
232
[7516f6]233QtStatusBar::progressIndicator::progressIndicator(const std::string &name)
234{
[b47bfc]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
[4cf323d]247QtStatusBar::progressIndicator::~progressIndicator(){
[b47bfc]248 delete container;
249}
Note: See TracBrowser for help on using the repository browser.