COPASI API  4.16.103
CProgressBar.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2004 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <QtGui/QApplication>
16 #include <QtGui/QLayout>
17 #include <QtCore/QWaitCondition>
18 
19 #include <QtGui/QCloseEvent>
20 #include <QtCore/QMutex>
21 #include <QtCore/QMutexLocker>
22 
23 #include "copasi.h"
24 #include "qtUtilities.h"
25 #include "utilities/CVector.h"
26 #include "CProgressBar.h"
27 #include "DataModelGUI.h"
28 #include "CQProgressItemText.h"
29 #include "CQProgressItemBar.h"
30 #include "CQMessageBox.h"
31 #include "copasiui3window.h"
32 
33 #undef min
34 #undef max
35 
36 // static
37 CProgressBar * CProgressBar::create(QWidget* parent, const char* name, Qt::WindowModality windowModality)
38 {
39  QWidget * pParent = parent;
40 
41  if (pParent == NULL)
42  {
44  }
45 
47  {
48  return new CProgressBar(pParent, name, windowModality);
49  }
50  else
51  {
52  return NULL;
53  }
54 }
55 
56 CProgressBar::CProgressBar(QWidget* parent, const char* name, Qt::WindowModality windowModality):
57  CQProgressDialog(parent, name, windowModality, Qt::WindowMinimizeButtonHint),
59  mProgressItemList(1),
60  mNextEventProcessing(QDateTime::currentDateTime()),
61  mpMainWidget(NULL)
62 {
64 
65  mProgressItemList[0] = NULL;
66 
67  connect(this, SIGNAL(signalAddItem(const int)),
68  this, SLOT(slotAddItem(const int)));
69 
70  connect(this, SIGNAL(signalSetName(QString)),
71  this, SLOT(slotSetName(QString)));
72 
73  connect(this, SIGNAL(signalProgressAll()),
74  this, SLOT(slotProgressAll()));
75 
76  connect(this, SIGNAL(signalFinishItem(const int)),
77  this, SLOT(slotFinishItem(const int)));
78 }
79 
81 {
82  finish();
83 
84  size_t i, imax = mProgressItemList.size();
85 
86  for (i = 0; i < imax; i++)
88 }
89 
90 size_t CProgressBar::addItem(const std::string & name,
91  const CCopasiParameter::Type & type,
92  const void * pValue,
93  const void * pEndValue)
94 
95 {
96  size_t hItem = CProcessReport::addItem(name, type, pValue, pEndValue);
97 
99  {
100  QMutexLocker Locker(&mMutex);
101  mSlotFinished = false;
102 
103  emit signalAddItem((int) hItem);
104 
105  if (!mSlotFinished)
106  {
107  mWaitSlot.wait(&mMutex);
108  }
109  }
110  else
111  {
112  slotAddItem((int) hItem);
113  QCoreApplication::processEvents();
114  }
115 
116  return hItem;
117 }
118 
119 void CProgressBar::slotAddItem(const int handle)
120 {
121  size_t Handle = (size_t) handle;
122 
123  if (Handle == C_INVALID_INDEX) return;
124 
125  QMutexLocker Locker(&mMutex);
126 
127  if (Handle >= mProgressItemList.size()) // we need to resize
128  {
129  size_t i, imax = mProgressItemList.size();
130 
131  mProgressItemList.resize(2 * imax, true); // Note, imax is never zero
132 
133  i = imax;
134  imax *= 2;
135 
136  while (i < imax) mProgressItemList[i++] = NULL;
137  }
138 
139  if (mProcessReportItemList[Handle]->hasEndValue())
140  mProgressItemList[Handle] = new CQProgressItemBar(static_cast<CQProgressDialog *>(this));
141  else
142  mProgressItemList[Handle] = new CQProgressItemText(static_cast<CQProgressDialog *>(this));
143 
144  mProgressItemList[Handle]->initFromProcessReportItem(mProcessReportItemList[Handle]);
146 
147  mSlotFinished = true;
148  mWaitSlot.wakeAll();
149 }
150 
151 bool CProgressBar::resetItem(const size_t & handle)
152 {
153  if (!isValidHandle(handle) || mProgressItemList[handle] == NULL) return false;
154 
155  return (mProgressItemList[handle]->reset() && mProceed);
156 }
157 
158 bool CProgressBar::progressItem(const size_t & handle)
159 {
160  if (!isValidHandle(handle) || mProgressItemList[handle] == NULL) return false;
161 
162  QDateTime currDateTime = QDateTime::currentDateTime();
163 
164  if (mNextEventProcessing >= currDateTime)
165  return mProceed;
166 
167  mNextEventProcessing = currDateTime.addSecs(1);
168 
169  if (mPause)
170  {
171  QMutexLocker Locker(&mMutex);
172  mWaitPause.wait(&mMutex);
173  }
174 
176  {
177  if (mSlotFinished)
178  {
179  mSlotFinished = false;
180 
181  emit signalProgressAll();
182  }
183  }
184  else
185  {
186  slotProgressAll();
187  QCoreApplication::processEvents();
188  }
189 
190  return mProceed;
191 }
192 
194 {
195  size_t hItem, hmax = mProgressItemList.size();
196 
197  QMutexLocker Locker(&mMutex);
198 
199  for (hItem = 0; hItem < hmax; hItem++)
200  {
201  if (mProgressItemList[hItem])
202  mProceed &= mProgressItemList[hItem]->process();
203  }
204 
205  mSlotFinished = true;
206 }
207 
209 {
210  // Assure that all signals have been properly handled before we delete
211  // the progress items
213  QCoreApplication::processEvents();
214 
215  // The method must only be called from the main thread!
217  CQProgressItem ** ppEnd = ppIt + mProgressItemList.size();
219 
220  for (; ppIt != ppEnd; ++ppIt, ++ppItem)
221  {
222  if (*ppIt != NULL)
223  {
224  removeProgressItem(*ppIt);
225 
226  // We must make sure the CProcessReportItem is not deleted prior to the actual delete.
227  // The destructor of CQProgressItem will take care of the deletion.
228  *ppItem = NULL;
229 
230  (*ppIt)->deleteLater();
231  (*ppIt) = NULL;
232  }
233  }
234 
236 
238  done(1);
239 
240  return mProceed;
241 }
242 
243 bool CProgressBar::finishItem(const size_t & handle)
244 {
245  if (!isValidHandle(handle) || mProgressItemList[handle] == NULL) return false;
246 
248  {
249  QMutexLocker Locker(&mMutex);
250  mSlotFinished = false;
251 
252  emit signalFinishItem((int) handle);
253 
254  if (!mSlotFinished)
255  {
256  mWaitSlot.wait(&mMutex);
257  }
258  }
259  else
260  {
261  slotFinishItem((int) handle);
262  QCoreApplication::processEvents();
263  }
264 
265  return mProceed;
266 }
267 
268 void CProgressBar::slotFinishItem(const int handle)
269 {
270  size_t Handle = (size_t) handle;
271 
272  if (isValidHandle(Handle) &&
273  mProgressItemList[Handle] != NULL)
274  {
275  QMutexLocker Locker(&mMutex);
276 
278 
279  // We must make sure the CProcessReportItem is not deleted prior to the actual delete.
280  // The destructor of CQProgressItem will take care of the deletion.
281  mProcessReportItemList[Handle] = NULL;
282 
283  mProgressItemList[Handle]->deleteLater();
284  mProgressItemList[Handle] = NULL;
285  }
286 
287  mSlotFinished = true;
288  mWaitSlot.wakeAll();
289 }
290 
292 {
293  return mProceed;
294 }
295 
296 // virtual
297 bool CProgressBar::setName(const std::string & name)
298 {
300  {
301  QMutexLocker Locker(&mMutex);
302  mSlotFinished = false;
303 
304  emit signalSetName(FROM_UTF8(name));
305 
306  if (!mSlotFinished)
307  {
308  mWaitSlot.wait(&mMutex);
309  }
310  }
311  else
312  {
313  slotSetName(FROM_UTF8(name));
314  QCoreApplication::processEvents();
315  }
316 
317  return true;
318 }
319 void CProgressBar::slotSetName(QString name)
320 {
321  QMutexLocker Locker(&mMutex);
322 
323  setWindowTitle(name);
325 
326  mSlotFinished = true;
327  mWaitSlot.wakeAll();
328 }
329 
330 void CProgressBar::closeEvent(QCloseEvent *e)
331 {
332  CQMessageBox::information(this, "COPASI", "COPASI is currently executing tasks.\n"
333  "Please stop them first before closing COPASI.");
334 
335  e->ignore();
336 }
337 
338 // virtual
340 {
342  mWaitPause.wakeAll();
343 }
344 
345 // virtual
347 {
349  mWaitPause.wakeAll();
350 }
virtual void slotAddItem(const int handle)
#define pdelete(p)
Definition: copasi.h:215
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
virtual bool finish()
virtual void closeEvent(QCloseEvent *e)
virtual void slotSetName(QString name)
virtual void done(int)
static bool isMainThread()
static CProgressBar * create(QWidget *parent=0, const char *name=0, Qt::WindowModality windowModality=Qt::WindowModal)
virtual bool isValidHandle(const size_t handle) const
virtual bool setName(const std::string &name)
CVector< CQProgressItem * > mProgressItemList
Definition: CProgressBar.h:135
virtual bool progressItem(const size_t &handle)
void disableSliders(bool disable)
virtual void btnContinuePressed()
bool removeProgressItem(CQProgressItem *pItem)
void resize(size_t size, const bool &copy=false)
Definition: CVector.h:301
void signalFinishItem(const int handle)
static StandardButton information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
#define C_INVALID_INDEX
Definition: copasi.h:222
virtual size_t addItem(const std::string &name, const CCopasiParameter::Type &type, const void *pValue, const void *pEndValue=NULL)
QDateTime mNextEventProcessing
Definition: CProgressBar.h:137
virtual bool proceed()
static CopasiUI3Window * getMainWindow()
CVector< CProcessReportItem * > mProcessReportItemList
CProgressBar(QWidget *parent, const char *name, Qt::WindowModality windowModality)
void signalSetName(QString name)
virtual void slotFinishItem(const int handle)
virtual bool resetItem(const size_t &handle)
virtual ~CProgressBar()
virtual bool reset()
bool insertProgressItem(CQProgressItem *pItem)
size_t addItem(const std::string &name, const std::string &value, const std::string *pEndValue=NULL)
QWaitCondition mWaitPause
Definition: CProgressBar.h:130
virtual bool finishItem(const size_t &handle)
virtual bool setName(const std::string &name)
void signalAddItem(const int handle)
virtual void btnContinuePressed()
void signalProgressAll()
size_t size() const
Definition: CVector.h:100
virtual void btnStopPressed()
virtual void slotProgressAll()
virtual bool finish()
CType * array()
Definition: CVector.h:139
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
QWaitCondition mWaitSlot
Definition: CProgressBar.h:129
virtual void btnStopPressed()
bool mSlotFinished
Definition: CProgressBar.h:126