COPASI API  4.16.103
TaskWidget.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) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <QtCore/QtDebug>
16 #include <QtGui/QFrame>
17 #include <QtGui/QVBoxLayout>
18 #include <QtGui/QHeaderView>
19 #include <QtGui/QComboBox>
20 #include <QtGui/QApplication>
21 
22 #include "TaskWidget.h"
23 #include "qtUtilities.h"
24 
25 #include "listviews.h"
26 #include "DataModelGUI.h"
27 #include "CQMessageBox.h"
28 #include "MyLineEdit.h"
29 #include "CProgressBar.h"
30 #include "copasiui3window.h"
32 #include "DefaultplotDialog.h"
33 #include "CQTaskHeaderWidget.h"
34 #include "CQTaskMethodWidget.h"
35 #include "CQTaskBtnWidget.h"
36 #include "utilities/CCopasiTask.h"
42 #include "model/CModel.h"
43 #include "report/CKeyFactory.h"
44 #include "UI/CQTaskThread.h"
45 #include "plotUI/CopasiPlot.h"
46 #include "plotUI/plotwindow.h"
47 
48 /*
49  * Constructs a TaskWidget which is a child of 'parent', with the
50  * name 'name' and widget flags set to 'f'.
51  */
52 TaskWidget::TaskWidget(QWidget* parent, const char* name, Qt::WFlags fl):
53  CopasiWidget(parent, name, fl),
54  mProgressBar(NULL),
55  mpHeaderWidget(NULL),
56  mpMethodWidget(NULL),
57  mpBtnWidget(NULL),
58  mpMethodLayout(NULL),
59  mpSpacer1(NULL),
60  mpSpacer2(NULL),
61  mpTask(NULL),
62  mpMethod(NULL),
63  mChanged(false)
64 {
65  if (!name)
66  setObjectName("TaskWidget");
67 
68  setWindowTitle(trUtf8("TaskWidget"));
69 
70  mpTaskThread = new CQTaskThread(this);
71 
74  mpBtnWidget = new CQTaskBtnWidget(this);
75 
76  connect(mpBtnWidget->mpBtnRun, SIGNAL(clicked()), this, SLOT(runBtnClicked()));
77  connect(mpBtnWidget->mpBtnRevert, SIGNAL(clicked()), this, SLOT(revertBtnClicked()));
78  connect(mpBtnWidget->mpBtnReport, SIGNAL(clicked()), this, SLOT(reportBtnClicked()));
79  connect(mpBtnWidget->mpBtnAssistant, SIGNAL(clicked()), this, SLOT(assistantBtnClicked()));
80 
81  connect(mpTaskThread, SIGNAL(finished()), this, SLOT(slotFinishThread()));
82 }
83 
85 {
87 }
88 
89 //************************************************************
90 #ifdef XXXX
91 void TaskWidget::addHeaderToGrid(unsigned int row)
92 {
93  if (!mpMethodLayout)
94  {
95  static_cast<QVBoxLayout *>(mpBtnWidget->layout())->insertStretch(0, 0);
96 
97  mpMethodLayout = new QGridLayout();
98  mpMethodLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
99  static_cast<QVBoxLayout *>(mpBtnWidget->layout())->insertLayout(0, mpMethodLayout);
100  }
101 
102  mpMethodLayout->addMultiCellWidget(mpHeaderWidget, row, row, 1, 2);
103 }
104 
105 bool TaskWidget::addHLineToGrid(QGridLayout* grid, unsigned int row, unsigned int maxcol)
106 {
107  QFrame * line = new QFrame(this, "line");
108  line->setFrameShape(QFrame::HLine);
109  line->setFrameShadow(QFrame::Sunken);
110  grid->addMultiCellWidget(line, row, row, 0, maxcol);
111 
112  return true;
113 }
114 
115 void TaskWidget::addMethodParameterTable(unsigned int row)
116 {
117  if (mpTblParameter) return;
118 
119  if (!mpMethodLayout)
120  {
121  static_cast<QVBoxLayout *>(mpBtnWidget->layout())->insertStretch(0, 0);
122 
123  mpMethodLayout = new QGridLayout();
124  mpMethodLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
125  static_cast<QVBoxLayout *>(mpBtnWidget->layout())->insertLayout(0, mpMethodLayout);
126  static_cast<QVBoxLayout *>(mpBtnWidget->layout())->setStretchFactor(mpMethodLayout, 10);
127  }
128 
129  mpLblParameter = new QLabel(this, "mpLblParameter");
130  mpLblParameter->setText(tr("Parameter"));
131  mpLblParameter->setAlignment(int(Qt::AlignTop | Qt::AlignRight));
132 
133  mpTblParameter = new QTableWidget(this);
134  mpTblParameter->setSelectionMode(QAbstractItemView::SingleSelection);
135  mpTblParameter->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
136 
137  mpTblParameter->setColumnCount(1);
138 
139  mpTblParameter->setHorizontalHeaderItem(0, new QTableWidgetItem());
140  mpTblParameter->horizontalHeaderItem(0)->setText("Value");
141  mpTblParameter->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
142 
143  mpTblParameter->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
144  mpTblParameter->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
145  mpTblParameter->horizontalHeader()->hide();
146 
147  mpMethodLayout->addWidget(mpLblParameter, row, 0);
148  mpMethodLayout->addWidget(mpTblParameter, row, 1, row, 2);
149 
150 #ifdef DEBUG_UI
151  qDebug() << "row = " << row;
152 #endif
153 
154  connect(mpTblParameter, SIGNAL(cellChanged(int, int)), this, SLOT(adjustTable()));
155 
156  return;
157 }
158 
159 void TaskWidget::addMethodSelectionBox(const unsigned C_INT32 * validMethods, unsigned int row)
160 {
161  if (!mpMethodLayout)
162  {
163  static_cast<QVBoxLayout *>(mpBtnWidget->layout())->insertStretch(0, 0);
164 
165  mpMethodLayout = new QGridLayout();
166  mpMethodLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
167  static_cast<QVBoxLayout *>(mpBtnWidget->layout())->insertLayout(0, mpMethodLayout);
168  }
169 
170  mpLblMethod = new QLabel(0, "mpLblMethod");
171  mpLblMethod->setText(tr("Method"));
172  mpLblMethod->setAlignment(int(Qt::AlignTop | Qt::AlignRight));
173 
174  mpBoxMethod = new QComboBox(this);
175 
176  unsigned C_INT32 i;
177 
178  for (i = 0; validMethods[i] != CCopasiMethod::unset; i++)
179  mpBoxMethod->insertItem(QString::fromUtf8(CCopasiMethod::SubTypeName[validMethods[i]]));
180 
181  mpSpacer2 = new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
182 
183  mpMethodLayout->addWidget(mpLblMethod, row, 0);
184  mpMethodLayout->addWidget(mpBoxMethod, row, 1);
185  mpMethodLayout->addItem(mpSpacer2, row, 2);
186 
187  connect(mpBoxMethod, SIGNAL(activated(int)), this, SLOT(changeMethod(int)));
188 
189  return;
190 }
191 #endif // XXXX
192 
194 {
195  if (!mpTask) return;
196 
197  CCopasiMethod* pMethod = mpTask->getMethod();
198 
199  if (pMethod != mpMethod)
200  {
201  pdelete(mpMethod);
202  mpMethod = pMethod;
203  }
204 
205  loadTask();
206 }
207 
209 {
210  // Assure that all edits to the current widget are committed.
211  mpBtnWidget->mpBtnRun->setFocus();
212 
213  runTask();
214 }
215 
217 {
218  if (!mpTask) return;
219 
221  pSelectDlg->setReport(&mpTask->getReport());
222  pSelectDlg->loadReportDefinitionVector();
223  pSelectDlg->exec();
224 
225  delete pSelectDlg;
226 }
227 
229 {
230  if (!mpTask) return;
231 
232  saveTask(); //this is necessary since the output may use information from the problem
233 
234  DefaultPlotDialog * pDlg = new DefaultPlotDialog(this);
235  pDlg->setTask(mpTask);
236 
237  if (pDlg->exec() == QDialog::Accepted)
238  {
240  }
241 
242  if (pDlg)delete pDlg;
243 }
244 
245 //************ executable button *******************
246 
248 {
249  if (!mpTask) return false;
250 
251  mpHeaderWidget->mpBoxExecutable->setChecked(mpTask->isScheduled());
252  mpHeaderWidget->mpUpdateModel->setChecked(mpTask->isUpdateModel());
253 
255  return true;
256 }
257 
259 {
260  if (!mpTask) return false;
261 
262  bool Value = mpHeaderWidget->mpBoxExecutable->isChecked();
263 
264  if (mpTask->isScheduled() != Value)
265  {
266  mpTask->setScheduled(Value);
267  mChanged = true;
268  }
269 
270  Value = mpHeaderWidget->mpUpdateModel->isChecked();
271 
272  if (mpTask->isUpdateModel() != Value)
273  {
274  mpTask->setUpdateModel(Value);
275  mChanged = true;
276  }
277 
279  return true;
280 }
281 
282 //************* parameter table ***********************
283 
285 {
286  if (!mpTask) return false;
287 
289 
290  return mpMethodWidget->loadMethod();
291 }
292 
294 {
295 #ifdef DEBUG_UI
296  qDebug() << "--> TaskWidget::adjustTable <--";
297 #endif
298 
299  // mpTblParameter->resizeColumnsToContents();
300  // mpTblParameter->resizeRowsToContents();
301 
302  /*
303  mpTblParameter->setFixedSize(mpTblParameter->columnWidth(0) + mpTblParameter->verticalHeader()->sizeHint().width() + 5,
304  mpTblParameter->verticalHeader()->sizeHint().height() * mpTblParameter->rowCount() + 5);
305  */
306 }
307 
309 {
310  if (!mpTask) return false;
311 
313 
314  return true;
315 }
316 
318 {
319  // save the state of the widget
320  if (!saveTask())
321  {
322  CQMessageBox::critical(this, "Simulation Error",
324  QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton);
325  return false;
326  }
327 
328  if (!mpTask) return false;
329 
330  if (mProgressBar != NULL)
331  {
332  //CQMessageBox::critical(this, "Task in Progress",
333  // "A task is currently running, another cannot be started before the current task ended.",
334  // QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton);
335  return false;
336  }
337 
338  // if overwrite is enabled and the file exists, then ask
339  if (!mpTask->getReport().getTarget().empty() &&
341  QFile(mpTask->getReport().getTarget().c_str()).exists())
342  {
343  if (QMessageBox::question(this,
344  QString("Confirm Overwrite"),
345  QString("The report file already exists. Would you like to overwrite it? \n\n(You can disable this dialog by clicking the 'Report' button.)"),
346  QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
347  return false;
348  }
349 
350  //set mouse cursor
351  setCursor(Qt::WaitCursor);
352 
353  //handle autosave feature
356 
357  //create progress bar
360 
362  return true;
363 }
364 
366 {
367  if (!mpTask) return false;
368 
369  if (mProgressBar != NULL)
370  {
371  mProgressBar->finish();
372  mProgressBar->deleteLater();
373  mProgressBar = NULL;
374  }
375 
376  mpTask->setCallBack(NULL);
377 
379 
380  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
381  (*CCopasiRootContainer::getDatamodelList())[0]->finish();
382 
383  // Update all values shown in the GUI
384  CModel * pModel = (*CCopasiRootContainer::getDatamodelList())[0]->getModel();
385  pModel->updateSimulatedValues(true);
386  pModel->updateNonSimulatedValues();
387 
389  unsetCursor();
391 
392  return loadTask();
393 }
394 
396 {
397  // Initialize the task
398  try
399  {
400  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
401 
404  }
405 
406  catch (CCopasiException & /*Exception*/)
407  {
409  {
410  if (mProgressBar != NULL) mProgressBar->finish();
411 
412  CQMessageBox::critical(this, "Initialization Error",
414  QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton);
415 
416  finishTask();
417  return false;
418  }
419  }
420 
422  {
423  if (mProgressBar != NULL) mProgressBar->finish();
424 
425  CQMessageBox::critical(this, "Initialization Error",
427  QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton);
428 
429  finishTask();
430  return false;
431  }
432 
434  {
435  C_INT Result =
436  CQMessageBox::question(this, "Initialization Warning",
438  QMessageBox::Ignore | QMessageBox::Abort, QMessageBox::Ignore);
439 
440  if (Result == QMessageBox::Abort)
441  {
442  finishTask();
443  return false;
444  }
445  }
446 
448 
449  // Execute the task
450  mpTaskThread->start();
451 
452  return true;
453 }
454 
456 {
457  if (!mpTaskThread->success() &&
458  CCopasiMessage::size() != 0)
459  {
460  CQMessageBox::critical(this, "Calculation Error",
462  QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton);
463  }
465  {
466  CQMessageBox::information(this, "Calculation Warning",
468  QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton);
469  }
470 
471  finishTask();
472 }
473 
475 {
477 
478  try {mpTask->restore();}
479 
480  catch (CCopasiException & /*Exception*/)
481  {
482  if (CCopasiMessage::peekLastMessage().getNumber() != MCCopasiMessage + 1)
483  {
484  if (mProgressBar != NULL) mProgressBar->finish();
485 
486  CQMessageBox::critical(this, "Calculation Error", CCopasiMessage::getAllMessageText().c_str(),
487  QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton);
489  }
490  }
491 
492  catch (...) {}
493 
495  {
496  CQMessageBox::information(this, "Calculation Warning",
498  QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton);
499  }
500 
502 
504 
506 }
507 
509 {
510  return true;
511 }
512 
514 {
515  return mpTask;
516 }
517 //*********************************************************************
518 
519 bool TaskWidget::update(ListViews::ObjectType objectType, ListViews::Action action, const std::string & C_UNUSED(key))
520 {
521  if (mIgnoreUpdates) return true;
522 
523  switch (objectType)
524  {
525  case ListViews::MODEL:
526 
527  if (action == ListViews::ADD &&
528  mpMethodWidget != NULL)
529  {
531  }
532 
533  break;
534 
535  default:
536  break;
537  }
538 
539  return true;
540 }
541 
543 {
544  return saveTask();
545 }
546 
548 {
549  mpTask = dynamic_cast< CCopasiTask * >(mpObject);
550 
551  // :TODO: We need a message here.
552  if (!mpTask) return false;
553 
555 
556  return loadTask();
557 }
void setReport(CReport *newReport)
#define C_INT
Definition: copasi.h:115
static const CCopasiMessage & peekLastMessage()
virtual bool setCallBack(CProcessReport *pCallBack)
bool saveMethod()
Definition: TaskWidget.cpp:308
QGridLayout * mpMethodLayout
Definition: TaskWidget.h:125
#define pdelete(p)
Definition: copasi.h:215
void setScheduled(const bool &scheduled)
virtual void setTask(CCopasiTask *t)
const bool & confirmOverwrite() const
Definition: CReport.cpp:101
bool loadCommon()
Definition: TaskWidget.cpp:247
virtual bool leave()
Definition: TaskWidget.cpp:542
void suspendAutoSave(const bool &suspend)
void finishTask()
Definition: TaskWidget.cpp:474
virtual bool loadTask()=0
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
void runBtnClicked()
Definition: TaskWidget.cpp:208
const bool & isUpdateModel() const
static CProgressBar * create(QWidget *parent=0, const char *name=0, Qt::WindowModality windowModality=Qt::WindowModal)
void updateNonSimulatedValues(void)
Definition: CModel.cpp:1892
static std::string getAllMessageText(const bool &chronological=true)
void updateSimulatedValues(const bool &updateMoieties)
Definition: CModel.cpp:1851
QSpacerItem * mpSpacer2
Definition: TaskWidget.h:128
const size_t & getNumber() const
bool saveCommon()
Definition: TaskWidget.cpp:258
CCopasiTask * getTask()
Definition: TaskWidget.cpp:513
bool loadMethod()
Definition: TaskWidget.cpp:284
CCopasiObject * mpObject
Definition: copasiWidget.h:64
const bool & isScheduled() const
const std::string & getTarget() const
Definition: CReport.cpp:89
static StandardButton information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
CProgressBar * mProgressBar
Definition: TaskWidget.h:121
#define C_UNUSED(p)
Definition: copasi.h:220
#define C_INT32
Definition: copasi.h:90
static CopasiUI3Window * getMainWindow()
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
Definition: TaskWidget.cpp:519
bool commonAfterRunTask()
Definition: TaskWidget.cpp:365
static void clearDeque()
#define MCCopasiMessage
bool mChanged
Definition: TaskWidget.h:133
bool mIgnoreUpdates
Definition: copasiWidget.h:67
virtual bool taskFinishedEvent()
Definition: TaskWidget.cpp:508
ListViews * mpListView
Definition: copasiWidget.h:62
static size_t size()
void setTask(CCopasiTask *pTask)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
CCopasiTask * mpTask
Definition: TaskWidget.h:130
void assistantBtnClicked()
Definition: TaskWidget.cpp:228
CQTaskMethodWidget * mpMethodWidget
Definition: TaskWidget.h:123
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
void slotFinishThread()
Definition: TaskWidget.cpp:455
void revertBtnClicked()
Definition: TaskWidget.cpp:193
CCopasiMethod * getMethod()
void adjustTable()
Definition: TaskWidget.cpp:293
static const std::string SubTypeName[]
Definition: CCopasiMethod.h:95
bool commonRunTask()
Definition: TaskWidget.cpp:395
CQTaskBtnWidget * mpBtnWidget
Definition: TaskWidget.h:124
CReport & getReport()
static StandardButton critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
virtual bool finish()
CQTaskHeaderWidget * mpHeaderWidget
Definition: TaskWidget.h:122
bool fl(const C_FLOAT64 &d1, const C_FLOAT64 &d2)
CQTaskThread * mpTaskThread
Definition: TaskWidget.h:134
const bool & success() const
void reportBtnClicked()
Definition: TaskWidget.cpp:216
Definition: CModel.h:50
virtual bool enterProtected()
Definition: TaskWidget.cpp:547
CCopasiMethod * mpMethod
Definition: TaskWidget.h:131
virtual bool restore()
bool commonBeforeRunTask()
Definition: TaskWidget.cpp:317
void setUpdateModel(const bool &updateModel)
virtual bool saveTask()=0
virtual bool runTask()=0
TaskWidget(QWidget *parent=0, const char *name=0, Qt::WFlags fl=0)
Definition: TaskWidget.cpp:52
static StandardButton question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
static CCopasiMessage::Type getHighestSeverity()