COPASI API  4.16.103
CQSpeciesWidget.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) 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 #include "CQSpeciesWidget.h"
12 
13 #include <QtGui/QHeaderView>
14 #include <QtGui/QClipboard>
15 #include <QtGui/QKeyEvent>
16 
17 #include "copasi.h"
18 
19 #include "qtUtilities.h"
20 #include "CQMessageBox.h"
21 
22 #include "model/CModel.h"
25 
26 /*
27  * Constructs a CQSpeciesWidget which is a child of 'parent', with the
28  * name 'name'.'
29  */
30 CQSpeciesWidget::CQSpeciesWidget(QWidget* parent, const char* name)
31  : CopasiWidget(parent, name)
32 {
33  setupUi(this);
34 
35  //Create Source Data Model.
36  mpSpecieDM = new CQSpecieDM(this);
37 
38  //Create the Proxy Model for sorting/filtering and set its properties.
40  mpProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
41  mpProxyModel->setFilterKeyColumn(-1);
42  mpProxyModel->setSourceModel(mpSpecieDM);
43 
44  mpTblSpecies->setModel(mpProxyModel);
45 
46  //Setting values for Compartment comboBox
48  mpTblSpecies->setItemDelegateForColumn(COL_COMPARTMENT, mpCompartmentDelegate);
49 
50  //Setting values for Types comboBox
52  mpTblSpecies->setItemDelegateForColumn(COL_TYPE_SPECIES, mpTypeDelegate);
53 
54  mpTblSpecies->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
55  mpTblSpecies->verticalHeader()->hide();
56  mpTblSpecies->sortByColumn(COL_ROW_NUMBER, Qt::AscendingOrder);
57 
58  // Connect the table widget
59  connect(mpSpecieDM, SIGNAL(notifyGUI(ListViews::ObjectType, ListViews::Action, const std::string)),
60  this, SLOT(protectedNotify(ListViews::ObjectType, ListViews::Action, const std::string)));
61  connect(mpSpecieDM, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
62  this, SLOT(dataChanged(const QModelIndex&, const QModelIndex&)));
63  connect(mpLEFilter, SIGNAL(textChanged(const QString &)),
64  this, SLOT(slotFilterChanged()));
65 }
66 
67 /*
68  * Destroys the object and frees any allocated resources
69  */
71 {
76  // no need to delete child widgets, Qt does it all for us
77 }
78 
80 {
83 }
84 
86 {
87  if (mpTblSpecies->hasFocus())
89 
91 }
92 
94 {
95  const QItemSelectionModel * pSelectionModel = mpTblSpecies->selectionModel();
96 
97  QModelIndexList mappedSelRows;
98  size_t i, imax = mpSpecieDM->rowCount();
99 
100  for (i = 0; i < imax; i++)
101  {
102  if (pSelectionModel->isRowSelected((int) i, QModelIndex()))
103  {
104  mappedSelRows.append(mpProxyModel->mapToSource(mpProxyModel->index((int) i, 0)));
105  }
106  }
107 
108  if (mappedSelRows.empty())
109  {return;}
110 
111  mpSpecieDM->removeRows(mappedSelRows);
112 }
113 
115 {
116 
117  int ret = CQMessageBox::question(this, tr("Confirm Delete"), "Delete all Species?",
118  QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
119 
120  if (ret == QMessageBox::Yes)
121  {
122  mpSpecieDM->clear();
123  }
124 
126 }
127 
129 {
130  if (!mIgnoreUpdates)
131  {
132  enterProtected();
133  }
134 
135  return true;
136 }
137 
139 {
140  return true;
141 }
142 
144 {
145  if (mpTblSpecies->selectionModel() != NULL)
146  {
147  disconnect(mpTblSpecies->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
148  this, SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
149  }
150 
151  mpProxyModel->setSourceModel(mpSpecieDM);
152  //Set Model for the TableView
153  mpTblSpecies->setModel(NULL);
154  mpTblSpecies->setModel(mpProxyModel);
155 
156  connect(mpTblSpecies->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
157  this, SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
159  mpTblSpecies->resizeColumnsToContents();
162 
163  return true;
164 }
165 
167 {
168  bool selected = false;
169 
170  QModelIndexList selRows = mpTblSpecies->selectionModel()->selectedRows();
171 
172  if (selRows.size() == 0)
173  selected = false;
174  else
175  {
176  if (selRows.size() == 1)
177  {
178  if (mpSpecieDM->isDefaultRow(mpProxyModel->mapToSource(selRows[0])))
179  selected = false;
180  else
181  selected = true;
182  }
183  else
184  selected = true;
185  }
186 
187  mpBtnDelete->setEnabled(selected);
188 
189  if (mpProxyModel->rowCount() - 1)
190  mpBtnClear->setEnabled(true);
191  else
192  mpBtnClear->setEnabled(false);
193 }
194 
195 void CQSpeciesWidget::slotSelectionChanged(const QItemSelection& C_UNUSED(selected),
196  const QItemSelection& C_UNUSED(deselected))
197 {
199 }
200 
201 void CQSpeciesWidget::dataChanged(const QModelIndex& C_UNUSED(topLeft),
202  const QModelIndex& C_UNUSED(bottomRight))
203 {
204  mpTblSpecies->resizeColumnsToContents();
208 }
209 
210 void CQSpeciesWidget::slotDoubleClicked(const QModelIndex proxyIndex)
211 {
212  QModelIndex index = mpProxyModel->mapToSource(proxyIndex);
213 
214  if (index.row() < 0)
215  return;
216 
217  if (mpSpecieDM->isDefaultRow(index))
218  {
220  }
221 
222  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
224  assert(pDataModel != NULL);
225  CModel * pModel = pDataModel->getModel();
226 
227  if (pModel == NULL)
228  return;
229 
230  std::string key = pModel->getMetabolites()[index.row()]->getKey();
231 
232  if (CCopasiRootContainer::getKeyFactory()->get(key))
234 }
235 
237 {
238  if (ev->key() == Qt::Key_Delete)
240  else if (ev->key() == Qt::Key_C && (ev->modifiers() & Qt::ControlModifier))
241  {
242  QModelIndexList selRows = mpTblSpecies->selectionModel()->selectedRows(0);
243 
244  if (selRows.empty())
245  {return;}
246 
247  QString str;
248  QModelIndexList::const_iterator i;
249 
250  for (i = selRows.begin(); i != selRows.end(); ++i)
251  {
252  for (int x = 0; x < mpSpecieDM->columnCount(); ++x)
253  {
254  if (!mpTblSpecies->isColumnHidden(x))
255  {
256  if (!str.isEmpty())
257  str += "\t";
258 
259  str += mpSpecieDM->index(mpProxyModel->mapToSource(*i).row(), x).data().toString();
260  }
261  }
262 
263  str += "\n";
264  }
265 
266  QApplication::clipboard()->setText(str);
267  }
268 }
269 
271 {
272  QRegExp regExp(mpLEFilter->text() + "|New Species", Qt::CaseInsensitive, QRegExp::RegExp);
273  mpProxyModel->setFilterRegExp(regExp);
274 }
275 
276 void CQSpeciesWidget::setFramework(int framework)
277 {
278  CopasiWidget::setFramework(framework);
279 
280  switch (mFramework)
281  {
282  case 0:
283  mpTblSpecies->showColumn(COL_ICONCENTRATION);
284  mpTblSpecies->showColumn(COL_CONCENTRATION);
285  mpTblSpecies->showColumn(COL_CRATE);
286 
287  mpTblSpecies->hideColumn(COL_INUMBER);
288  mpTblSpecies->hideColumn(COL_NUMBER);
289  mpTblSpecies->hideColumn(COL_NRATE);
290 
291  mpSpecieDM->setFlagConc(true);
292  break;
293 
294  case 1:
295  mpTblSpecies->hideColumn(COL_ICONCENTRATION);
296  mpTblSpecies->hideColumn(COL_CONCENTRATION);
297  mpTblSpecies->hideColumn(COL_CRATE);
298 
299  mpTblSpecies->showColumn(COL_INUMBER);
300  mpTblSpecies->showColumn(COL_NUMBER);
301  mpTblSpecies->showColumn(COL_NRATE);
302 
303  mpSpecieDM->setFlagConc(false);
304  break;
305  }
306 }
307 
309 {
310  const CCopasiVector < CCompartment > & compartments =
311  (*CCopasiRootContainer::getDatamodelList())[0]->getModel()->getCompartments();
312  mCompartments.clear();
313 
314  for (unsigned C_INT32 jj = 0; jj < compartments.size(); jj++)
315  mCompartments.push_back(FROM_UTF8(compartments[jj]->getObjectName()));
316 }
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: CQSpecieDM.cpp:59
void setFlagConc(bool flag)
Definition: CQSpecieDM.cpp:44
CQSortFilterProxyModel * mpProxyModel
virtual void slotDoubleClicked(const QModelIndex proxyIndex)
#define pdelete(p)
Definition: copasi.h:215
objectType
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
void switchToOtherWidget(const size_t &id, const std::string &key)
Definition: listviews.cpp:926
const CCopasiVector< CMetab > & getMetabolites() const
Definition: CModel.cpp:1051
virtual bool enterProtected()
virtual size_t size() const
virtual bool clear()
QStringList mCompartments
CQSpecieDM * mpSpecieDM
virtual void slotBtnNewClicked()
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: CQSpecieDM.cpp:54
#define COL_TYPE_SPECIES
Definition: CQSpecieDM.h:28
virtual void slotSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
#define C_INVALID_INDEX
Definition: copasi.h:222
#define COL_CONCENTRATION
Definition: CQSpecieDM.h:31
#define C_UNUSED(p)
Definition: copasi.h:220
#define C_INT32
Definition: copasi.h:90
const QStringList & getTypes()
Definition: CQSpecieDM.cpp:39
void deleteSelectedSpecies()
virtual void slotFilterChanged()
#define COL_NRATE
Definition: CQSpecieDM.h:34
bool mIgnoreUpdates
Definition: copasiWidget.h:67
virtual const std::string & getKey() const
virtual void keyPressEvent(QKeyEvent *ev)
virtual void slotBtnClearClicked()
CQIndexComboDelegate * mpTypeDelegate
ListViews * mpListView
Definition: copasiWidget.h:62
#define COL_INUMBER
Definition: CQSpecieDM.h:30
#define COL_COMPARTMENT
Definition: CQSpecieDM.h:27
#define COL_NUMBER
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
virtual void slotBtnDeleteClicked()
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
Definition: CQSpecieDM.cpp:541
virtual void setFramework(int framework)
static CKeyFactory * getKeyFactory()
virtual bool leave()
Definition: CModel.h:50
virtual bool isDefaultRow(const QModelIndex &i) const
CQComboDelegate * mpCompartmentDelegate
CQSpeciesWidget(QWidget *parent=0, const char *name=0)
virtual void setFramework(int framework)
#define COL_CRATE
Definition: CQSpecieDM.h:33
static StandardButton question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
#define COL_ICONCENTRATION
Definition: CQSpecieDM.h:29
#define COL_ROW_NUMBER