COPASI API  4.16.103
worker.cpp
Go to the documentation of this file.
1 // Copyright (C) 2012 - 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 #include "worker.h"
7 
8 #include "UI/copasiui3window.h"
9 #include "UI/DataModelGUI.h"
10 #include "UI/CQFittingWidget.h"
11 #include "UI/listviews.h"
14 #include <QtCore/QThread>
17 #include "UI/CWindowInterface.h"
18 
19 #include <arguments.h>
20 #include <QtCore/QString>
21 #include <QtCore/QList>
22 #include <QtCore/QMutex>
23 #include <QtCore/QWaitCondition>
24 
25 Worker::Worker(CopasiUI3Window *window, Arguments *args) : mTaskStarted(false)
26 {
27  mpArgs = args;
28  mpWindow = window;
30 
31  // open the newly generated model
32  mpWindow->openInitialDocument(QString((args->getFilename() + ".view.cps").c_str()));
33 
34  // connect the finish slot, so it will switch to the correct tab
35  connect(mpDataModel, SIGNAL(finished(bool)), this, SLOT(finishedLoading(bool)));
36  connect(mpDataModel, SIGNAL(notifyView(ListViews::ObjectType, ListViews::Action, std::string)),
37  this, SLOT(slotNotify(ListViews::ObjectType, ListViews::Action, std::string)));
38 }
39 
41 {
42 
43  if (objectType == ListViews::STATE && action == ListViews::CHANGE && mTaskStarted)
44  {
45  mTaskStarted = false;
46 
47  if (mpArgs->haveOutputDir())
48  {
49  const QList< QPointer<QMainWindow> >& windows = mpWindow->getWindows();
50 
51  for (int index = 0; index < windows.count(); ++index)
52  {
53  const QMainWindow* mainWindow = windows[index];
54 
55  if (mainWindow == NULL) continue;
56 
57  const CWindowInterface* window = dynamic_cast<const CWindowInterface*>(mainWindow);
58 
59  if (window == NULL) continue;
60 
61  QString fileName = QString("%1/plot%2.%3")
62  .arg(mpArgs->getOutputDir().c_str())
63  .arg(index + 1)
64  .arg(mpArgs->getFileType().c_str());
65  window->saveToFile(fileName);
66  }
67  }
68 
70  {
71  // quit
72  qApp->quit();
73  }
74  }
75 
76  return true;
77 }
78 
80 {
81  if (task == NULL) return NULL;
82 
83  ListViews* views = mpWindow->getMainWidget();
84 
85  switch (task->getType())
86  {
88  return reinterpret_cast<TaskWidget*>(views->getSteadyStateWidget());
89 
91  return reinterpret_cast<TaskWidget*>(views->getTrajectoryWidget());
92 
93  case CCopasiTask::scan:
94  return reinterpret_cast<TaskWidget*>(views->getScanWidget());
95 
97  return reinterpret_cast<TaskWidget*>(views->getOptimizationWidget());
98 
100  return reinterpret_cast<TaskWidget*>(views->getFittingWidget());
101 
102  case CCopasiTask::mca:
103  return reinterpret_cast<TaskWidget*>(views->getMCAWidget());
104 
105  case CCopasiTask::lna:
106  return reinterpret_cast<TaskWidget*>(views->getLNAWidget());
107 
108  case CCopasiTask::lyap:
111  case CCopasiTask::sens:
114  case CCopasiTask::unset:
115  default:
116  // have no widget for these
117  return NULL;
118  }
119 }
120 
122 {
123  if (task == NULL) return -1;
124 
125  switch (task->getType())
126  {
128  return 21;
129 
131  return 23;
132 
133  case CCopasiTask::scan:
134  return 31;
135 
137  return 32;
138 
140  return 33;
141 
142  case CCopasiTask::mca:
143  return 24;
144 
145  case CCopasiTask::lna:
146  return 35;
147 
148  case CCopasiTask::lyap:
151  case CCopasiTask::sens:
154  case CCopasiTask::unset:
155  default:
156  // have no id
157  return -1;
158  }
159 }
160 
162 {
163  if (widget == NULL) return -1;
164 
165  CCopasiTask *task = widget->getTask();
166  return getIndexForTask(task);
167 }
168 
169 void Worker::finishedLoading(bool success)
170 {
171 
172  if (!success)
173  return;
174 
175  if (mpArgs->isSwitchToTask())
176  {
178  }
179 
180  if (mpArgs->isRunScheduled())
181  {
183 
184  if (task == NULL)
185  return;
186 
187  if (!mpArgs->isSwitchToTask())
188  mpWindow ->getMainWidget()->switchToOtherWidget(getIndexForTask(task), "");
189 
190  TaskWidget *widget = getWidgetForTask(task);
191  mTaskStarted = true;
192  widget->runTask();
193  }
194 
195  if (mpArgs->isHideWindow())
196  mpWindow->hide();
197 }
bool haveOutputDir() const
Definition: arguments.cpp:101
bool slotNotify(ListViews::ObjectType objectType, ListViews::Action action, std::string key="")
Definition: worker.cpp:40
SteadyStateWidget * getSteadyStateWidget()
Definition: listviews.cpp:1054
const QList< QPointer< QMainWindow > > & getWindows() const
CopasiUI3Window * mpWindow
Definition: worker.h:40
CQLNAWidget * getLNAWidget()
Definition: listviews.cpp:1069
bool mTaskStarted
Definition: worker.h:46
virtual void saveToFile(const QString &fileName) const
objectType
void switchToOtherWidget(const size_t &id, const std::string &key)
Definition: listviews.cpp:926
CQFittingWidget * getFittingWidget()
Definition: listviews.cpp:1074
bool isHideWindow() const
Definition: arguments.cpp:305
CQMCAWidget * getMCAWidget()
Definition: listviews.cpp:1064
static int getIndexForWidget(TaskWidget *widget)
Definition: worker.cpp:161
void finishedLoading(bool success)
Definition: worker.cpp:169
Arguments * mpArgs
Definition: worker.h:44
void openInitialDocument(const QString &file)
CCopasiTask * getTask()
Definition: TaskWidget.cpp:513
static CCopasiTask * getFirstScheduledTask()
Definition: arguments.cpp:53
TaskWidget * getWidgetForTask(CCopasiTask *task) const
Definition: worker.cpp:79
const std::string & getFilename() const
Definition: arguments.cpp:255
bool isQuitAfterTaskExecution() const
Definition: arguments.cpp:310
bool isSwitchToTask() const
Definition: arguments.cpp:237
const std::string & getOutputDir() const
Definition: arguments.cpp:106
int getTaskToSwitchTo() const
Definition: arguments.cpp:242
Worker(CopasiUI3Window *window, Arguments *args)
Definition: worker.cpp:25
CQTrajectoryWidget * getTrajectoryWidget()
Definition: listviews.cpp:1049
ListViews * getMainWidget()
Type getType() const
DataModelGUI * getDataModel()
CQOptimizationWidget * getOptimizationWidget()
Definition: listviews.cpp:1079
const std::string & getFileType() const
Definition: arguments.cpp:260
static int getIndexForTask(CCopasiTask *task)
Definition: worker.cpp:121
DataModelGUI * mpDataModel
Definition: worker.h:42
bool isRunScheduled() const
Definition: arguments.cpp:270
virtual bool runTask()=0
ScanWidget * getScanWidget()
Definition: listviews.cpp:1059