COPASI API  4.16.103
CSlider.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 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 "copasi.h"
16 
17 #include "CSlider.h"
18 #include "report/CKeyFactory.h"
22 #include "model/CModel.h"
23 
24 const char * CSlider::TypeName[] =
25 {"float", "unsignedFloat", "integer", "unsignedInteger", "Undefined", NULL};
26 
27 const char * CSlider::ScaleName[] =
28 {"linear", "logarithmic", "undefined", NULL};
29 
30 CSlider::CSlider(const std::string & name,
31  const CCopasiContainer * pParent):
32  CCopasiContainer(name, pParent, "Slider"),
33  mKey(CCopasiRootContainer::getKeyFactory()->add("Slider", this)),
34  mAssociatedEntityKey(),
35  mpSliderObject(NULL),
36  mSliderType(Float),
37  mValue(0.0),
38  mMinValue(0),
39  mMaxValue(0),
40  mTickNumber(1000),
41  mTickFactor(100),
42  mSync(true),
43  mScaling(CSlider::linear),
44  mCN(),
45  mInitialRefreshes()
46 {}
47 
49  const CCopasiContainer * pParent):
50  CCopasiContainer(src, pParent),
51  mKey(CCopasiRootContainer::getKeyFactory()->add("Slider", this)),
52  mAssociatedEntityKey(src.mAssociatedEntityKey),
53  mpSliderObject(src.mpSliderObject),
54  mSliderType(src.mSliderType),
55  mValue(src.mValue),
56  mMinValue(src.mMinValue),
57  mMaxValue(src.mMaxValue),
58  mTickNumber(src.mTickNumber),
59  mTickFactor(src.mTickFactor),
60  mSync(src.mSync),
61  mScaling(src.mScaling),
62  mCN(src.mCN),
63  mInitialRefreshes(src.mInitialRefreshes)
64 {}
65 
68 
69 bool CSlider::compile(const std::vector< CCopasiContainer * > & listOfContainer)
70 {
71  if (getObjectDataModel() == NULL) return false;
72 
73  //setSliderObject(CCopasiContainer::ObjectFromName(listOfContainer, getObjectName()));
74  setSliderObject(getObjectDataModel()->ObjectFromName(listOfContainer, mCN));
75 
76  if (this->mSync) this->sync();
77 
78  return (mpSliderObject != NULL);
79 }
80 
81 void CSlider::setSynced(bool synced)
82 {
83  this->mSync = synced;
84 }
85 
86 bool CSlider::getSynced() const
87 {
88  return this->mSync;
89 }
90 
91 const std::string & CSlider::getKey() const
92 {return mKey;}
93 
94 bool CSlider::setAssociatedEntityKey(const std::string & associatedEntityKey)
95 {
96  mAssociatedEntityKey = associatedEntityKey;
97 
98  return (CCopasiRootContainer::getKeyFactory()->get(associatedEntityKey) != NULL);
99 }
100 
101 const std::string & CSlider::getAssociatedEntityKey() const
102 {return mAssociatedEntityKey;}
103 
105 {
106  mpSliderObject = pObject;
107 
108  if (!pObject)
109  {
110  mInitialRefreshes.clear();
111  return false;
112  }
113 
114  mCN = pObject->getCN();
115 
116  std::set< const CCopasiObject * > ChangedObjects;
117  ChangedObjects.insert(pObject);
118 
119  CCopasiDataModel* pDataModel = getObjectDataModel();
120  assert(pDataModel != NULL);
121  mInitialRefreshes = pDataModel->getModel()->buildInitialRefreshSequence(ChangedObjects);
122 
123  if (mpSliderObject->isValueInt())
124  {
125  this->setSliderType(Integer);
126  }
127  else if (mpSliderObject->isValueDbl())
128  {
129  this->setSliderType(Float);
130  }
131  else
132  {
133  this->setSliderType(Undefined);
134  }
135 
136  if (this->mSync) this->sync();
137 
138  C_FLOAT64 value = this->getSliderValue();
139  this->mOriginalValue = value;
140 
141  if (this->mMinValue > value)
142  {
143  this->mMinValue = value;
144  }
145 
146  if (this->mMaxValue < value)
147  {
148  this->mMaxValue = value;
149  }
150 
151  return true;
152 }
153 
155 {
156  this->setSliderValue(this->mOriginalValue);
157 }
158 
160 {
161  if (this->mSliderType != Undefined)
162  {
163  C_FLOAT64 value = this->getSliderValue();
164  this->mMinValue = value / 2.0;
165  this->mMaxValue = value == 0 ? 1.0 : value * 2.0;
166  }
167 }
168 
170 {
171  //return setObjectName(objectCN);
172  mCN = objectCN;
173  return true;
174 }
175 
177 {return mpSliderObject;}
178 
179 const std::string & CSlider::getSliderObjectCN() const
180 {return mCN; /*getObjectName();*/}
181 
183 {
184  mSliderType = type;
185  return true;
186 }
187 
189 {return mSliderType;}
190 
192 {
193  if (mSliderType != Undefined)
194  {
195  mOriginalValue = value;
196 
197  if (mOriginalValue < this->mMinValue)
198  {
199  mOriginalValue = this->mMinValue;
200  }
201 
202  if (mOriginalValue > this->mMaxValue)
203  {
204  mOriginalValue = this->mMaxValue;
205  }
206 
207  return true;
208  }
209  else
210  {
211  return false;
212  }
213 }
214 
216  const bool & writeToObject)
217 {
218  if (mSliderType != Undefined)
219  {
220  mValue = value;
221 
222  if (mValue < this->mMinValue)
223  {
224  mValue = this->mMinValue;
225  }
226 
227  if (mValue > this->mMaxValue)
228  {
229  mValue = this->mMaxValue;
230  }
231 
232  if (this->mSync && writeToObject)
233  {
234  this->writeToObject();
235  }
236 
237  return true;
238  }
239  else
240  {
241  return false;
242  }
243 }
244 
246 {
247  if (!this->mpSliderObject) return;
248 
250  {
251  C_INT32* reference =
252  (C_INT32*)(((CCopasiObjectReference<C_INT32>*)mpSliderObject)->getValuePointer());
253 
254  mValue = *reference;
255  }
257  {
258  C_FLOAT64* reference =
260 
261  mValue = *reference;
262  }
263 }
264 
266 {
267  if (!mpSliderObject) return;
268 
269  if (mpSliderObject->isValueDbl())
271  else if (mpSliderObject->isValueInt())
272  mpSliderObject->setObjectValue((C_INT32) floor(mValue + 0.5));
273  else if (mpSliderObject->isValueBool())
275 
276  std::vector< Refresh * >::iterator it = mInitialRefreshes.begin();
277  std::vector< Refresh * >::iterator end = mInitialRefreshes.end();
278 
279  for (; it != end; ++it)
280  (**it)();
281 
282  return;
283 }
284 
286 {
287  return this->mOriginalValue;
288 }
289 
291 {
292  return this->mValue;
293 }
294 
295 bool CSlider::setMinValue(const C_FLOAT64 minValue)
296 {
297  if (mSliderType != Undefined)
298  {
299  mMinValue = minValue;
300 
301  if (mMaxValue < mMinValue)
302  {
304  }
305 
307  {
308  this->mValue = this->mMinValue;
309  this->writeToObject();
310  }
311 
312  return true;
313  }
314  else
315  {
316  return false;
317  }
318 }
319 
321 {return mMinValue;}
322 
323 bool CSlider::setMaxValue(const C_FLOAT64 maxValue)
324 {
325  if (mSliderType != Undefined)
326  {
327  mMaxValue = maxValue;
328 
329  if (mMaxValue < mMinValue)
330  {
332  }
333 
335  {
336  this->mValue = this->mMaxValue;
337  this->writeToObject();
338  }
339 
340  return true;
341  }
342  else
343  {
344  return false;
345  }
346 }
347 
349 {return mMaxValue;}
350 
351 bool CSlider::setTickNumber(const unsigned C_INT32 tickNumber)
352 {
353  mTickNumber = tickNumber;
354  return true;
355 }
356 
358 {return mTickNumber;}
359 
360 bool CSlider::setTickFactor(const unsigned C_INT32 tickFactor)
361 {
362  mTickFactor = tickFactor;
363  return true;
364 }
365 
367 {return mTickFactor;}
368 
370 {
371  return this->mScaling;
372 }
373 
375 {
376  this->mScaling = scaling;
377  return true;
378 }
379 
381 {
382  if (!strncmp("linear", scaleName, 7))
383  {
384  return CSlider::linear;
385  }
386  else if (!strncmp("logarithmic", scaleName, 12))
387  {
388  return CSlider::logarithmic;
389  }
390  else
391  {
393  }
394 }
395 
397 {
398  if (scale < 0 || scale > CSlider::undefinedScale) return ScaleName[undefinedScale];
399 
400  return ScaleName[scale];
401 }
402 
403 /**
404  * Checks whether the object the slider points to actually
405  * exists.
406  * Returns true if it does.
407  */
408 bool CSlider::isValid() const
409 {
410  bool result = false;
411  // check which object if any belongs to the CN and if it is the same as
412  // the pointer currently stored in the slider
413  assert(getObjectDataModel() != NULL);
414  const CModel* pModel = getObjectDataModel()->getModel();
415  assert(pModel != NULL);
416  std::vector<CCopasiContainer*> listOfContainers;
417  listOfContainers.push_back(const_cast<CModel*>(pModel));
418  const CCopasiObject* pObject = getObjectDataModel()->ObjectFromName(listOfContainers, this->mCN);
419  result = (pObject != NULL && pObject == this->mpSliderObject);
420  return result;
421 }
CCopasiDataModel * getObjectDataModel()
bool compile(const std::vector< CCopasiContainer * > &listOfContainer=CCopasiContainer::EmptyList)
Definition: CSlider.cpp:69
bool remove(const std::string &key)
bool isValueInt() const
C_FLOAT64 mValue
Definition: CSlider.h:311
const char * convertScaleToScaleName(Scale scale)
Definition: CSlider.cpp:396
bool setScaling(Scale scaling)
Definition: CSlider.cpp:374
void setObjectValue(const C_FLOAT64 &value)
bool getSynced() const
Definition: CSlider.cpp:86
virtual CCopasiObjectName getCN() const
unsigned C_INT32 mTickNumber
Definition: CSlider.h:331
CSlider(const std::string &name="Slider", const CCopasiContainer *pParent=NULL)
Definition: CSlider.cpp:30
bool setSliderObject(CCopasiObject *pObject)
Definition: CSlider.cpp:104
bool setOriginalValue(const C_FLOAT64 value)
Definition: CSlider.cpp:191
bool setMaxValue(const C_FLOAT64 maxValue)
Definition: CSlider.cpp:323
qreal linear(qreal a, qreal b, qreal t)
const std::string & getSliderObjectCN() const
Definition: CSlider.cpp:179
const std::string & getAssociatedEntityKey() const
Definition: CSlider.cpp:101
const std::string & getKey() const
Definition: CSlider.cpp:91
C_FLOAT64 mOriginalValue
Definition: CSlider.h:316
void setSynced(bool synced)
Definition: CSlider.cpp:81
CSlider::Type getSliderType() const
Definition: CSlider.cpp:188
#define C_INT32
Definition: copasi.h:90
bool setTickFactor(const unsigned C_INT32 tickFactor)
Definition: CSlider.cpp:360
static const char * TypeName[]
Definition: CSlider.h:45
Type mSliderType
Definition: CSlider.h:306
bool mSync
Definition: CSlider.h:341
C_FLOAT64 mMaxValue
Definition: CSlider.h:326
std::string mAssociatedEntityKey
Definition: CSlider.h:296
bool isValid() const
Definition: CSlider.cpp:408
bool setAssociatedEntityKey(const std::string &associatedEntityKey)
Definition: CSlider.cpp:94
bool setSliderType(const CSlider::Type type)
Definition: CSlider.cpp:182
static const char * ScaleName[]
Definition: CSlider.h:50
void resetValue()
Definition: CSlider.cpp:154
const C_FLOAT64 & getSliderValue() const
Definition: CSlider.cpp:290
void writeToObject()
Definition: CSlider.cpp:265
std::vector< Refresh * > mInitialRefreshes
Definition: CSlider.h:357
const C_FLOAT64 & getOriginalValue() const
Definition: CSlider.cpp:285
const C_FLOAT64 & getMaxValue() const
Definition: CSlider.cpp:348
bool setTickNumber(const unsigned C_INT32 tickNumber)
Definition: CSlider.cpp:351
~CSlider()
Definition: CSlider.cpp:66
static CKeyFactory * getKeyFactory()
#define C_FLOAT64
Definition: copasi.h:92
C_FLOAT64 mMinValue
Definition: CSlider.h:321
bool isValueBool() const
void sync()
Definition: CSlider.cpp:245
std::string mKey
Definition: CSlider.h:291
bool setMinValue(const C_FLOAT64 minValue)
Definition: CSlider.cpp:295
bool isValueDbl() const
unsigned C_INT32 mTickFactor
Definition: CSlider.h:336
virtual void * getValuePointer() const
Definition: CModel.h:50
unsigned C_INT32 getTickNumber() const
Definition: CSlider.cpp:357
std::vector< Refresh * > buildInitialRefreshSequence(std::set< const CCopasiObject * > &changedObjects)
Definition: CModel.cpp:4164
CCopasiObject * mpSliderObject
Definition: CSlider.h:301
CCopasiObject * ObjectFromName(const std::vector< CCopasiContainer * > &listOfContainer, const CCopasiObjectName &CN) const
const C_FLOAT64 & getMinValue() const
Definition: CSlider.cpp:320
bool setSliderValue(const C_FLOAT64 value, const bool &writeToObject=true)
Definition: CSlider.cpp:215
void resetRange()
Definition: CSlider.cpp:159
unsigned C_INT32 getTickFactor() const
Definition: CSlider.cpp:366
Scale getScaling() const
Definition: CSlider.cpp:369
CRegisteredObjectName mCN
Definition: CSlider.h:351
Scale convertScaleNameToScale(const char *scaleName)
Definition: CSlider.cpp:380
CCopasiObject * getSliderObject()
Definition: CSlider.cpp:176
Scale mScaling
Definition: CSlider.h:346