COPASI API  4.16.103
CQLayoutThread.cpp
Go to the documentation of this file.
1 // Copyright (C) 2013 - 2015 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 <QtGui/QDockWidget>
7 #include <QtCore/QDateTime>
8 #include <QtCore/QSharedPointer>
9 
12 
14 #include <layout/CLayoutEngine.h>
15 #include <layout/CLayout.h>
16 #include <layout/CLayoutState.h>
17 
18 #include <UI/copasiui3window.h>
19 
21  : QThread(parent)
22  , mpParameterWindow(NULL)
23  , mpCurrentLayout(NULL)
24  , mNumIterations(1000)
25  , mUpdateWait(100)
26  , mStopLayout(false)
27  , mPause(false)
28  , mpCurrent(NULL)
29 {
30  qRegisterMetaType<QSharedPointer<CLayoutState> >();
31  mpParameterWindow = new CQSpringLayoutParameterWindow("Layout Parameters", parent);
32  connect(CopasiUI3Window::getMainWindow(), SIGNAL(signalQuit()), this, SLOT(terminateLayout()), Qt::DirectConnection);
33 }
34 
36 {
37  mSync.lock();
38  mStopLayout = true;
39  mPauseCond.wakeAll();
40  mSync.unlock();
41 
42  wait();
43 }
44 
46 {
47  mSync.lock();
48  mPause = false;
49  mSync.unlock();
50  mPauseCond.wakeAll();
51 }
52 
54 {
55  if (mSync.tryLock(30))
56  {
57  mPause = true;
58  mSync.unlock();
59  return true;
60  }
61 
62  return false;
63 }
64 
66 {
67  return mpParameterWindow;
68 }
69 
71 {
74 }
75 
77 {
78  if (layout == NULL || mpParameterWindow == NULL)
79  return;
80 
82  l.randomize();
83 
84  emit layoutStateChanged(QSharedPointer<CLayoutState>(new CLayoutState(layout)));
85  emit layoutUpdated();
86 }
87 
88 void CQLayoutThread::createSpringLayout(CLayout* layout, int numIntervals, int updateWait)
89 {
90  mpCurrentLayout = layout;
91  mNumIterations = numIntervals;
92  mUpdateWait = updateWait;
93  start();
94 }
95 
97 {
98  if (mpCurrent == NULL) return;
99 
101 }
102 
104 {
105  mStopLayout = true;
106  mPauseCond.wakeAll();
107 }
108 
110 {
111  if (mpCurrentLayout == NULL)
112  return;
113 
114  mStopLayout = false;
115 
116  if (mpCurrent != NULL)
117  {
118  delete mpCurrent;
119  mpCurrent = NULL;
120  }
121 
122  // create the spring layout
125 
126  CLayoutEngine le(mpCurrent, false);
127  int i = 0;
128  qint64 tick, last = 0;
129  double pot, oldPot = -1.0;
130 
131  for (; (i < mNumIterations) && (mStopLayout) == false; ++i)
132  {
133  mSync.lock();
134 
135  if (mPause)
136  mPauseCond.wait(&mSync);
137 
138  //test again, in case we should stop after the pause
139  if (mStopLayout)
140  {
141  mSync.unlock();
142  break;
143  }
144 
145  pot = le.step();
146 
147  // no more working on the layout, release lock
148  mSync.unlock();
149 
150  if (pot == 0.0 || fabs((pot - oldPot) / pot) < 1e-9)
151  {
152  break;
153  }
154  else
155  {
156  oldPot = pot;
157  }
158 
159  tick = QDateTime::currentMSecsSinceEpoch();
160 
161  if (mUpdateWait != 0 && (tick - last > mUpdateWait))
162  {
163  last = tick;
164 
165  // calculate new curves and emit state
166  finalize();
167 
168  emit layoutStateChanged(QSharedPointer<CLayoutState>(new CLayoutState(mpCurrentLayout)));
169  emit layoutUpdated();
170  }
171  }
172 
173  // calculate new curves and emit state
174  finalize();
175 
176  emit layoutStateChanged(QSharedPointer<CLayoutState>(new CLayoutState(mpCurrentLayout)));
177  emit layoutUpdated();
178 
179  emit layoutFinished();
180 }
void layoutStateChanged(QSharedPointer< CLayoutState > newState)
void createSpringLayout(CLayout *layout, int numIntervals=1000, int updateWait=100)
#define pdelete(p)
Definition: copasi.h:215
CQLayoutThread(QWidget *parent)
CCopasiSpringLayout::Parameters & getLayoutParameters()
CLayout * mpCurrentLayout
static CopasiUI3Window * getMainWindow()
void layoutFinished()
CCopasiSpringLayout * mpCurrent
virtual ~CQLayoutThread()
CQSpringLayoutParameterWindow * mpParameterWindow
void randomizeLayout(CLayout *layout)
QDockWidget * getParameterWindow()
void layoutUpdated()
virtual void run()
QWaitCondition mPauseCond