COPASI API  4.16.103
curve2dwidget.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 #include <QtGui/QPainter>
7 #include "curve2dwidget.h"
8 #include "CQPlotEditWidget.h"
9 
11 #include "UI/qtUtilities.h"
12 
13 #include "copasi.h"
14 
16 #include "plot/CPlotItem.h"
17 #include "CQPlotColors.h"
19 
20 /*
21  * Constructs a Curve2DWidget as a child of 'parent', with the
22  * name 'name' and widget flags set to 'f'.
23  */
24 Curve2DWidget::Curve2DWidget(QWidget* parent, const char* /* name */, Qt::WindowFlags fl):
25  CQPlotEditWidget(parent, fl),
26  mpObjectY(NULL),
27  mpObjectX(NULL)
28 {
29  setupUi(this);
30 
33 }
34 
35 /*
36  * Destroys the object and frees any allocated resources
37  */
39 {
40  // no need to delete child widgets, Qt does it all for us
41 }
42 
44 {
45  if (!curve)
46  {
47  // We need to reset the widget to defaults
48  mpEditTitle->setText("");
49 
50  mpObjectX = NULL;
51  mpEditX->setText("");
52 
53  mpObjectY = NULL;
54  mpEditY->setText("");
55 
56  mpBoxType->setCurrentIndex(0);
57  mpBoxLineSubType->setCurrentIndex(0);
58  mpBoxColor->clear();
59 
60  mpCheckBefore->setChecked(false);
61  mpCheckDuring->setChecked(true);
62  mpCheckAfter->setChecked(false);
63 
64  return true;
65  }
66 
67  if (curve->getType() != CPlotItem::curve2d) return false;
68 
69  //if (curve->getChannels().getSize != 2) return false;
70 
71  mpEditTitle->setText(FROM_UTF8(curve->getTitle()));
72 
73  //TODO: check if objects exist....
74  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
76  assert(pDataModel != NULL);
77  mpObjectX = mpObjectY = NULL;
78 
79  if (curve->getChannels().size() >= 1)
80  mpObjectX = pDataModel->getDataObject(curve->getChannels()[0]);
81 
82  if (curve->getChannels().size() >= 2)
83  mpObjectY = pDataModel->getDataObject(curve->getChannels()[1]);
84 
85  if (mpObjectX)
86  mpEditX->setText(FROM_UTF8(mpObjectX->getObjectDisplayName()));
87 
88  if (mpObjectY)
89  mpEditY->setText(FROM_UTF8(mpObjectY->getObjectDisplayName()));
90 
91  const void* tmp;
92 
93  //Type
94  if (!(tmp = curve->getValue("Line type").pVOID)) return false;
95 
96  unsigned C_INT32 linetype = *(unsigned C_INT32*)tmp;
97  mpBoxType->setCurrentIndex(linetype);
98 
99  typeChanged(linetype);
100 
101  //line subtype & width
102  if (linetype == 0 || linetype == 3)
103  {
104  if (!(tmp = curve->getValue("Line subtype").pVOID)) return false;
105 
106  mpBoxLineSubType->setCurrentIndex(*(unsigned C_INT32*)tmp);
107 
108  //mpBoxWidth
109  if (!(tmp = curve->getValue("Line width").pVOID)) return false;
110 
111  mpSpinBoxWidth->setValue(*(C_FLOAT64*)tmp);
112  }
113 
114  // points
115  if (linetype == 1)
116  {
117  if (!(tmp = curve->getValue("Line width").pVOID)) return false;
118 
119  mpSpinBoxWidth->setValue(*(C_FLOAT64*)tmp);
120  }
121 
122  //symbol type
123  if (linetype == 2 || linetype == 3)
124  {
125  if (!(tmp = curve->getValue("Symbol subtype").pVOID)) return false;
126 
127  mpBoxSymbolSubType->setCurrentIndex(*(unsigned C_INT32*)tmp);
128  }
129 
130  //color TODO
131  mpBoxColor->clear();
132  mpBoxColor->addItem("auto");
133  size_t i;
134 
135  for (i = 0; i < CQPlotColors::getNumCopasiColors(); ++i)
136  {
137  QColor color = CQPlotColors::getColor("auto", i);
138  QPixmap pix(12, 12);
139  QPainter painter(&pix);
140 
141  if (color.isValid())
142  {
143  painter.setPen(Qt::gray);
144  painter.setBrush(QBrush(color));
145  painter.drawRect(0, 0, 12, 12);
146  }
147 
148  QIcon icon;
149  icon.addPixmap(pix);
150 
151  mpBoxColor->addItem(icon, CQPlotColors::getCopasiColorStr(i).c_str());
152  }
153 
154  if (!(tmp = curve->getValue("Color").pVOID)) return false;
155 
156  std::string colorstr = *(std::string*)tmp;
157  int tmpindex;
158 
159  if ((tmpindex = mpBoxColor->findText(colorstr.c_str())) != -1)
160  mpBoxColor->setCurrentIndex(tmpindex);
161  else
162  {
163  QColor color = QColor(colorstr.c_str());
164  QPixmap pix(12, 12);
165  QPainter painter(&pix);
166 
167  if (color.isValid())
168  {
169  painter.setPen(Qt::gray);
170  painter.setBrush(QBrush(color));
171  painter.drawRect(0, 0, 12, 12);
172  }
173 
174  QIcon icon;
175  icon.addPixmap(pix);
176 
177  mpBoxColor->addItem(icon, colorstr.c_str());
178  mpBoxColor->setCurrentIndex(mpBoxColor->count() - 1);
179  }
180 
181  //channel
182  mpCheckBefore->setChecked(curve->getActivity() & COutputInterface::BEFORE);
183  mpCheckDuring->setChecked(curve->getActivity() & COutputInterface::DURING);
184  mpCheckAfter->setChecked(curve->getActivity() & COutputInterface::AFTER);
185 
186  return true; //TODO
187 }
188 
189 bool Curve2DWidget::SaveToCurveSpec(CPlotItem * curve, const CPlotItem *original /*= NULL*/) const
190 {
191  //if (!(mpObjectX && mpObjectY)) return false;
192 
193  //curve->setType(CPlotItem::curve2d);
194 
195  std::string title = TO_UTF8(mpEditTitle->text());
196  unsigned C_INT32 lineType = (unsigned C_INT32)mpBoxType->currentIndex();
197  unsigned C_INT32 lineSubType = (unsigned C_INT32)mpBoxLineSubType->currentIndex();
198  unsigned C_INT32 symbolSubType = (unsigned C_INT32)mpBoxSymbolSubType->currentIndex();
199  C_FLOAT64 lineWidth = (C_FLOAT64)mpSpinBoxWidth->value();
200  std::string color = TO_UTF8(mpBoxColor->currentText());
201 
202  C_INT32 Activity = 0;
203 
204  if (mpCheckBefore->isChecked()) Activity += COutputInterface::BEFORE;
205 
206  if (mpCheckDuring->isChecked()) Activity += COutputInterface::DURING;
207 
208  if (mpCheckAfter->isChecked()) Activity += COutputInterface::AFTER;
209 
212 
213  bool thingsChanged = false;
214 
215  if (original != NULL)
216  {
217  // compare whether things changed
218  if (original->getTitle() != title)
219  thingsChanged = true;
220 
221  if (thingsChanged || original->getType() != CPlotItem::curve2d)
222  thingsChanged = true;
223 
224  if (thingsChanged || *original->getValue("Line type").pUINT != lineType)
225  thingsChanged = true;
226 
227  if (thingsChanged || *original->getValue("Line subtype").pUINT != lineSubType)
228  thingsChanged = true;
229 
230  if (thingsChanged || *original->getValue("Symbol subtype").pUINT != symbolSubType)
231  thingsChanged = true;
232 
233  if (thingsChanged || *original->getValue("Line width").pDOUBLE != lineWidth)
234  thingsChanged = true;
235 
236  if (thingsChanged || *original->getValue("Color").pSTRING != color)
237  thingsChanged = true;
238 
239  if (thingsChanged || original->getActivity() != Activity)
240  thingsChanged = true;
241 
242  if (thingsChanged || original->getChannels().size() != 2)
243  thingsChanged = true;
244 
245  if (thingsChanged || original->getChannels()[0] != xName)
246  thingsChanged = true;
247 
248  if (thingsChanged || original->getChannels()[1] != yName)
249  thingsChanged = true;
250  }
251  else thingsChanged = true;
252 
253  if (!thingsChanged)
254  return false;
255 
256  //title
257  curve->setTitle(title);
258 
259  //channels
260  curve->getChannels().clear();
261  curve->getChannels().push_back(CPlotDataChannelSpec(xName));
262  curve->getChannels().push_back(CPlotDataChannelSpec(yName));
263 
264  curve->setValue("Line type", lineType);
265  curve->setValue("Line subtype", lineSubType);
266  curve->setValue("Symbol subtype", symbolSubType);
267  curve->setValue("Line width", lineWidth);
268 
269  //color
270  curve->setValue("Color", color);
271 
272  curve->setActivity((COutputInterface::Activity) Activity);
273 
274  return true;
275 }
276 
278 {
279  if (!mpModel) return;
280 
283  mpObjectX);
284 
285  if (mpObjectX)
286  {
287  mpEditX->setText(FROM_UTF8(mpObjectX->getObjectDisplayName()));
288 
289  if (mpObjectY)
290  mpEditTitle->setText(FROM_UTF8(mpObjectY->getObjectDisplayName()
291  + "|"
293 
294  //TODO update tab title
295  }
296  else
297  mpEditX->setText("");
298 }
299 
301 {
302  if (!mpModel) return;
303 
306  mpObjectY);
307 
308  if (mpObjectY)
309  {
310  mpEditY->setText(FROM_UTF8(mpObjectY->getObjectDisplayName()));
311 
312  if (mpObjectX)
313  mpEditTitle->setText(FROM_UTF8(mpObjectY->getObjectDisplayName()
314  + "|"
316 
317  //TODO update tab title
318  }
319  else
320  mpEditY->setText("");
321 }
322 
323 void Curve2DWidget::typeChanged(int linetype)
324 {
325  if (linetype == 0 || linetype == 3 || linetype == 1)
326  {
327  mpBoxLineSubType->setEnabled(linetype != 1);
328  mpSpinBoxWidth->setEnabled(true);
329  }
330  else
331  {
332  mpBoxLineSubType->setEnabled(linetype == 1);
333  mpSpinBoxWidth->setEnabled(false);
334  }
335 
336  //symbol type
337  if (linetype == 2 || linetype == 3)
338  {
339  mpBoxSymbolSubType->setEnabled(true);
340  }
341  else
342  {
343  mpBoxSymbolSubType->setEnabled(false);
344  }
345 }
346 
347 /**
348  * In multiple edit mode, we don't want to edit name & channels
349  */
351 {
352  mpEditTitle->setEnabled(!mode);
353  mpEditX->setEnabled(!mode);
354  mpEditY->setEnabled(!mode);
355  mpBtnX->setEnabled(!mode);
356  mpBtnY->setEnabled(!mode);
357 }
CCopasiObject * getDataObject(const CCopasiObjectName &CN) const
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
const CModel * mpModel
static size_t getNumCopasiColors()
Definition: CPlotColors.cpp:36
static QColor getColor(std::string colorstr, size_t colorindex)
virtual bool LoadFromCurveSpec(const CPlotItem *curve)
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
virtual CCopasiObjectName getCN() const
static std::string getCopasiColorStr(size_t index)
Definition: CPlotColors.cpp:17
virtual void typeChanged(int linetype)
virtual void setMultipleEditMode(bool mode)
std::vector< CPlotDataChannelSpec > & getChannels()
Definition: CPlotItem.cpp:214
void setActivity(const COutputInterface::Activity &activity)
Definition: CPlotItem.cpp:161
#define C_INT32
Definition: copasi.h:90
const CCopasiObject * mpObjectY
Definition: curve2dwidget.h:33
static const QIcon & icon(const IconID &id)
const CPlotItem::Type & getType() const
Definition: CPlotItem.cpp:158
bool setValue(const std::string &name, const CType &value)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
unsigned C_INT32 * pUINT
Curve2DWidget(QWidget *parent=0, const char *name=0, Qt::WindowFlags fl=0)
const COutputInterface::Activity & getActivity() const
Definition: CPlotItem.cpp:179
virtual void buttonPressedY()
#define C_FLOAT64
Definition: copasi.h:92
bool fl(const C_FLOAT64 &d1, const C_FLOAT64 &d2)
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
const CCopasiParameter::Value & getValue(const std::string &name) const
const std::string & getTitle() const
Definition: CPlotItem.cpp:228
void setTitle(const std::string &title)
Definition: CPlotItem.cpp:233
static const CCopasiObject * getObjectSingle(QWidget *pParent, const CQSimpleSelectionTree::ObjectClasses &classes, const CCopasiObject *pCurrentObject=NULL)
virtual bool SaveToCurveSpec(CPlotItem *curve, const CPlotItem *original=NULL) const
virtual void buttonPressedX()
const CCopasiObject * mpObjectX
Definition: curve2dwidget.h:34