COPASI API  4.16.103
CCopasiSelectionDialog.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) 2004 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <QtGui/QPushButton>
16 #include <QtGui/QCheckBox>
17 #include <QtGui/QLayout>
18 #include <QtGui/QComboBox>
19 //Added by qt3to4:
20 #include <QtGui/QHBoxLayout>
21 #include <QtGui/QVBoxLayout>
22 #include <QtGui/QDialogButtonBox>
23 
24 #include "CCopasiSelectionDialog.h"
25 #include "CCopasiSelectionWidget.h"
26 
27 #include "copasi.h"
28 
31 
32 #include "qtUtilities.h"
34 #include "model/CModel.h"
35 #include "CQMatrixDialog.h"
36 #include "CQMessageBox.h"
37 
38 CCopasiSelectionDialog::CCopasiSelectionDialog(QWidget * parent , const char * name , bool modal):
39  QDialog(parent),
40  mpModeCheckBox(NULL),
41  mpButtonBox(NULL),
42  mpMainWidget(NULL),
43  mpMainLayout(NULL),
44  mpTmpVector(new std::vector< const CCopasiObject * >()),
45  mpOutputVector(NULL),
46  mExpertMode(false),
47  mExpertModeEnabled(true)
48 {
49  setObjectName(QString::fromUtf8(name));
50  setModal(modal);
51  setAttribute(Qt::WA_DeleteOnClose);
52  mpMainLayout = new QVBoxLayout(this);
53 
55  mpMainLayout->addWidget(mpSelectionWidget);
56 
57  mpButtonBox = new QHBoxLayout();
58  mpMainLayout->addLayout(mpButtonBox);
59 
60  QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
61  mpButtonBox->addWidget(box);
62 
63  mpModeCheckBox = new QCheckBox("expert mode", this);
64  mpModeCheckBox->setObjectName("expertMode");
65  mpModeCheckBox->setChecked(false);
66  mpButtonBox->addWidget(mpModeCheckBox);
67 
68  // this->mpButtonBox->addSpacing(20);
69  // this->mpButtonBox->addStretch();
70 
71  connect(box, SIGNAL(accepted()), this, SLOT(okButton_clicked()));
72  connect(box, SIGNAL(rejected()), this, SLOT(cancelButton_clicked()));
73  connect(this->mpModeCheckBox, SIGNAL(toggled(bool)), this, SLOT(modeButton_toggled(bool)));
74 
76 
77  this->setTabOrder(this->mpModeCheckBox, this->mpMainWidget);
78 }
79 
81 {
82  delete mpTmpVector;
83 }
84 
87 {
88  this->mpSelectionWidget->populateTree(pModel, classes);
89 }
90 
91 void CCopasiSelectionDialog::setOutputVector(std::vector< const CCopasiObject * > * outputVector)
92 {
93  this->mpSelectionWidget->setOutputVector(outputVector);
94 }
95 
97 {
98  // fill outputVector
99  this->mpSelectionWidget->commit();
100  done(QDialog::Accepted);
101 }
102 
104 {
105  done(QDialog::Rejected);
106 }
107 
109 {
110  this->mpSelectionWidget->setExpertMode(checked);
111 }
112 
113 void CCopasiSelectionDialog::setSingleSelection(bool singleSelectionMode)
114 {
115  this->mpSelectionWidget->setSingleSelection(singleSelectionMode);
116 }
117 
119 {
120  if (enable == this->mExpertModeEnabled) return;
121 
122  this->mExpertModeEnabled = enable;
123 
124  if (!this->mExpertModeEnabled)
125  {
126  this->mpModeCheckBox->setChecked(false);
127  this->mpModeCheckBox->hide();
128  }
129  else
130  {
131  this->mpModeCheckBox->show();
132  }
133 }
134 
135 const CCopasiObject *
137  const CQSimpleSelectionTree::ObjectClasses & classes,
138  const CCopasiObject * pCurrentObject)
139 {
140  std::vector< const CCopasiObject * > Selection;
141 
142  if (pCurrentObject != NULL)
143  Selection.push_back(pCurrentObject);
144 
145  CCopasiSelectionDialog * pDialog = new CCopasiSelectionDialog(parent);
146  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
147  pDialog->setWindowTitle("Select Item");
148  pDialog->setModel((*CCopasiRootContainer::getDatamodelList())[0]->getModel(), classes);
149  pDialog->setSingleSelection(true);
150  pDialog->setOutputVector(&Selection);
151 
152  int Result = pDialog->exec();
153 
154  if (Result == QDialog::Accepted && Selection.size() != 0)
155  {
156  const CCopasiObject *pObject = Selection[0];
157  const CArrayAnnotation * pArray;
158 
159  // if the selected object is an array then select firstly one cell of it
160  if ((pArray = dynamic_cast< const CArrayAnnotation * >(pObject)))
161  {
162  pObject = chooseCellMatrix(pArray, true, true)[0];
163 
164  if (!pObject) return NULL;
165  }
166 
167  return pObject;
168  }
169 
170  if (Result == QDialog::Rejected && pCurrentObject != NULL)
171  return pCurrentObject;
172 
173  return NULL;
174 }
175 
176 std::vector< const CCopasiObject * > CCopasiSelectionDialog::getObjectVector(QWidget * parent,
177  const CQSimpleSelectionTree::ObjectClasses & classes,
178  const std::vector< const CCopasiObject * > * pCurrentSelection)
179 {
180  std::vector< const CCopasiObject * > Selection;
181 
182  if (pCurrentSelection)
183  Selection = *pCurrentSelection;
184 
185  CCopasiSelectionDialog * pDialog = new CCopasiSelectionDialog(parent);
186  pDialog->setWindowTitle("Select Items");
187  pDialog->setToolTip("Select multiple items by holding down the Ctrl or Shift (or equivalent) key.");
188  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
189  pDialog->setModel((*CCopasiRootContainer::getDatamodelList())[0]->getModel(), classes);
190  pDialog->setSingleSelection(false);
191  pDialog->setOutputVector(&Selection);
192 
193  if (pDialog->exec() == QDialog::Rejected && pCurrentSelection)
194  return *pCurrentSelection;
195  else
196  // return Selection;
197  {
198  std::vector<const CCopasiObject *> newSelection;
199 
200  std::vector< const CCopasiObject * >::iterator itSelection = Selection.begin();
201 
202  for (; itSelection != Selection.end(); ++itSelection)
203  {
204  // if the current object is an array then select firstly one cell of it
205  const CArrayAnnotation * pArray;
206 
207  if ((pArray = dynamic_cast< const CArrayAnnotation * >(*itSelection)))
208  {
209  // second parameter is false in order 'ALL' options on the matrix dialog to appear
210  std::vector<const CCopasiObject *> tmp = chooseCellMatrix(pArray, false, true); //TODO value flag
211  std::vector<const CCopasiObject *>::const_iterator tmpit, tmpitEnd = tmp.end();
212 
213  for (tmpit = tmp.begin(); tmpit != tmpitEnd; ++tmpit)
214  newSelection.push_back(*tmpit);
215  }
216  // otherwise, just put it into newSelection
217  else
218  {
219  newSelection.push_back(*itSelection);
220  }
221  }
222 
223  return newSelection;
224  }
225 
226  // return Selection;
227 }
228 
229 std::vector<const CCopasiObject*>
230 CCopasiSelectionDialog::chooseCellMatrix(const CArrayAnnotation * pArrayAnnotation, bool single, bool value, std::string caption)
231 {
232  std::vector< const CCopasiObject* > returnVector;
233 
234  if (single)
235  {returnVector.resize(1); returnVector[0] = NULL;}
236  else
237  returnVector.resize(0);
238 
239  if (!pArrayAnnotation) return returnVector;
240 
241  //handle zero-dimensional array
242  if (pArrayAnnotation->size().size() == 0)
243  {
245  index.resize(0);
246  returnVector.resize(1);
247  returnVector[0] = static_cast< const CCopasiObject * >(pArrayAnnotation->addElementReference(index));
248  return returnVector;
249  }
250 
251  CQMatrixDialog * pDialog = new CQMatrixDialog();
252 
253  pDialog->setWindowTitle(tr(TO_UTF8(FROM_UTF8(caption) + "Cell Selection of " + FROM_UTF8(pArrayAnnotation->getObjectName()))));
254  pDialog->setArray(pArrayAnnotation, single);
255 
256  int Result = pDialog->exec();
257 
258  if (Result == QDialog::Accepted)
259  {
261  index.resize(pArrayAnnotation->dimensionality());
262 
263  if (index.size() > 2)
264  CQMessageBox::warning(0, "Dimensionality Problem", "Need more handle for high dimension of array",
265  QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton);
266 
267  if (single)
268  {
269  // single cell selection
270  if (value)
271  {
272  index[0] = pDialog->mpCBRow->currentIndex();
273 
274  if (index.size() > 1)
275  index[1] = pDialog->mpCBColumn->currentIndex();
276 
277  returnVector[0] = static_cast< const CCopasiObject * >(pArrayAnnotation->addElementReference(index));
278  }
279 
280  return returnVector;
281  }
282 
283  // multi cell selection
284 
285  //if "All" is selected for both rows and columns, and an object with numerical value
286  //is not requested, return the array annotation as such
287  if (pDialog->mpCBRow->currentIndex() == 0 && pDialog->mpCBColumn->currentIndex() == 0
288  && !value)
289  {
290  // whole matrix should be chosen -> the object itself will be returned
291  returnVector.resize(1);
292  returnVector[0] = (CCopasiObject *) pArrayAnnotation;
293  return returnVector;
294  }
295 
296  size_t minRows, maxRows, minCols, maxCols;
297  size_t i, j;
298 
299  if (pDialog->mpCBRow->currentIndex())
300  {
301  // not ALL option
302  minRows = pDialog->mpCBRow->currentIndex() - 1;
303  maxRows = minRows + 1;
304  }
305  else
306  {
307  // ALL option
308  minRows = 0;
309  maxRows = pArrayAnnotation->size()[0];
310  }
311 
312  if (index.size() == 2)
313  {
314  if (pDialog->mpCBColumn->currentIndex())
315  {
316  // not ALL option
317  minCols = pDialog->mpCBColumn->currentIndex() - 1;
318  maxCols = minCols + 1;
319  }
320  else
321  {
322  // ALL option
323  minCols = 0;
324  maxCols = pArrayAnnotation->size()[1];
325  }
326 
327  for (i = minRows; i < maxRows; ++i)
328  {
329  for (j = minCols; j < maxCols; ++j)
330  {
331  returnVector.push_back(static_cast< const CCopasiObject * >(pArrayAnnotation->addElementReference((int) i, (int) j)));
332  }
333  }
334  }
335 
336  if (index.size() == 1)
337  {
338  for (i = minRows; i < maxRows; ++i)
339  {
340  returnVector.push_back(static_cast< const CCopasiObject * >(pArrayAnnotation->addElementReference((int) i)));
341  }
342  }
343 
344  return returnVector;
345  }
346 
347  else
348  {
349  // Rejected case
350  if (single)
351  returnVector[0] = NULL;
352 
353  return returnVector;
354  }
355 }
void setExpertMode(bool expertMode)
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
const std::string & getObjectName() const
static std::vector< const CCopasiObject * > getObjectVector(QWidget *pParent, const CQSimpleSelectionTree::ObjectClasses &classes, const std::vector< const CCopasiObject * > *pCurrentSelection=NULL)
void setSingleSelection(bool singleSelectionMode)
void setSingleSelection(bool singleSelection)
std::vector< size_t > index_type
Definition: CCopasiArray.h:34
CCopasiSelectionWidget * mpSelectionWidget
CCopasiSelectionDialog(QWidget *parent=0, const char *name=0, bool modal=false)
static std::vector< const CCopasiObject * > chooseCellMatrix(const CArrayAnnotation *pArrayAnnotation, bool single, bool value, std::string caption="")
static StandardButton warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
void populateTree(const CModel *model, const CQSimpleSelectionTree::ObjectClasses &classes)
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
void setOutputVector(std::vector< const CCopasiObject * > *outputVector)
void setOutputVector(std::vector< const CCopasiObject * > *outputVector)
Header file of class CArrayAnnotation.
void modeButton_toggled(bool checked)
void setModel(const CModel *model, const CQSimpleSelectionTree::ObjectClasses &classes)
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
const CObjectInterface * addElementReference(CCopasiAbstractArray::index_type index) const
Definition: CModel.h:50
virtual void setArray(const CArrayAnnotation *tmp, bool single)
CCopasiAbstractArray::index_type size() const
size_t dimensionality() const
static const CCopasiObject * getObjectSingle(QWidget *pParent, const CQSimpleSelectionTree::ObjectClasses &classes, const CCopasiObject *pCurrentObject=NULL)
std::vector< const CCopasiObject * > * mpTmpVector