COPASI API  4.16.103
CQCompartmentsWidget.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 "CQCompartmentsWidget.h"
12 
13 #include <QtGui/QHeaderView>
14 #include <QtGui/QClipboard>
15 #include <QtGui/QKeyEvent>
16 
17 #include "qtUtilities.h"
18 #include "copasi.h"
19 #include "CQMessageBox.h"
20 
21 #include "model/CModel.h"
24 
25 /*
26  * Constructs a CQCompartmentsWidget which is a child of 'parent', with the
27  * name 'name'.'
28  */
29 CQCompartmentsWidget::CQCompartmentsWidget(QWidget* parent, const char* name)
30  : CopasiWidget(parent, name)
31 {
32  setupUi(this);
33 
34  //Create Source Data Model.
35  mpCompartmentDM = new CQCompartmentDM(this);
36 
37  //Create the Proxy Model for sorting/filtering and set its properties.
39  mpProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
40  mpProxyModel->setFilterKeyColumn(-1);
41 
42  //Setting values for Types comboBox
44  mpTblCompartments->setItemDelegateForColumn(COL_TYPE_COMPARTMENTS, mpTypeDelegate);
45 
46  mpTblCompartments->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
47  mpTblCompartments->verticalHeader()->hide();
48  mpTblCompartments->sortByColumn(COL_ROW_NUMBER, Qt::AscendingOrder);
49 
50  // Connect the table widget
51  connect(mpCompartmentDM, SIGNAL(notifyGUI(ListViews::ObjectType, ListViews::Action, const std::string)),
52  this, SLOT(protectedNotify(ListViews::ObjectType, ListViews::Action, const std::string)));
53  connect(mpCompartmentDM, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
54  this, SLOT(dataChanged(const QModelIndex&, const QModelIndex&)));
55  connect(mpLEFilter, SIGNAL(textChanged(const QString &)),
56  this, SLOT(slotFilterChanged()));
57 }
58 
59 /*
60  * Destroys the object and frees any allocated resources
61  */
63 {
67  // no need to delete child widgets, Qt does it all for us
68 }
69 
71 {
74 }
75 
77 {
78  if (mpTblCompartments->hasFocus())
80 
82 }
83 
85 {
86  const QItemSelectionModel * pSelectionModel = mpTblCompartments->selectionModel();
87 
88  QModelIndexList mappedSelRows;
89  size_t i, imax = mpCompartmentDM->rowCount();
90 
91  for (i = 0; i < imax; i++)
92  {
93  if (pSelectionModel->isRowSelected((int) i, QModelIndex()))
94  {
95  mappedSelRows.append(mpProxyModel->mapToSource(mpProxyModel->index((int) i, 0)));
96  }
97  }
98 
99  if (mappedSelRows.empty())
100  {return;}
101 
102  mpCompartmentDM->removeRows(mappedSelRows);
103 }
104 
106 {
107  int ret = CQMessageBox::question(this, tr("Confirm Delete"), "Delete all Compartments?",
108  QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
109 
110  if (ret == QMessageBox::Yes)
111  {
113  }
114 
116 }
117 
119 {
120  if (!mIgnoreUpdates)
121  {
122  enterProtected();
123  }
124 
125  return true;
126 }
127 
129 {
130  return true;
131 }
132 
134 {
135  if (mpTblCompartments->selectionModel() != NULL)
136  {
137  disconnect(mpTblCompartments->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
138  this, SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
139  }
140 
141  mpProxyModel->setSourceModel(mpCompartmentDM);
142  //Set Model for the TableView
143  mpTblCompartments->setModel(NULL);
144  mpTblCompartments->setModel(mpProxyModel);
145  connect(mpTblCompartments->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
146  this, SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
148  mpTblCompartments->resizeColumnsToContents();
149 
150  return true;
151 }
152 
154 {
155  bool selected = false;
156 
157  QModelIndexList selRows = mpTblCompartments->selectionModel()->selectedRows();
158 
159  if (selRows.size() == 0)
160  selected = false;
161  else
162  {
163  if (selRows.size() == 1)
164  {
165  if (mpCompartmentDM->isDefaultRow(mpProxyModel->mapToSource(selRows[0])))
166  selected = false;
167  else
168  selected = true;
169  }
170  else
171  selected = true;
172  }
173 
174  mpBtnDelete->setEnabled(selected);
175 
176  if (mpProxyModel->rowCount() - 1)
177  mpBtnClear->setEnabled(true);
178  else
179  mpBtnClear->setEnabled(false);
180 }
181 
182 void CQCompartmentsWidget::slotSelectionChanged(const QItemSelection& C_UNUSED(selected),
183  const QItemSelection& C_UNUSED(deselected))
184 {
186 }
187 
188 void CQCompartmentsWidget::dataChanged(const QModelIndex& C_UNUSED(topLeft),
189  const QModelIndex& C_UNUSED(bottomRight))
190 {
191  mpTblCompartments->resizeColumnsToContents();
193 }
194 
195 void CQCompartmentsWidget::slotDoubleClicked(const QModelIndex proxyIndex)
196 {
197  QModelIndex index = mpProxyModel->mapToSource(proxyIndex);
198 
199  if (index.row() < 0)
200  return;
201 
202  if (mpCompartmentDM->isDefaultRow(index))
203  {
205  }
206 
207  assert(CCopasiRootContainer::getDatamodelList()->size() > 0);
209  assert(pDataModel != NULL);
210  CModel * pModel = pDataModel->getModel();
211 
212  if (pModel == NULL)
213  return;
214 
215  std::string key = pModel->getCompartments()[index.row()]->getKey();
216 
217  if (CCopasiRootContainer::getKeyFactory()->get(key))
219 }
220 
222 {
223  if (ev->key() == Qt::Key_Delete)
225  else if (ev->key() == Qt::Key_C && (ev->modifiers() & Qt::ControlModifier))
226  {
227  QModelIndexList selRows = mpTblCompartments->selectionModel()->selectedRows(0);
228 
229  if (selRows.empty())
230  {return;}
231 
232  QString str;
233  QModelIndexList::const_iterator i;
234 
235  for (i = selRows.begin(); i != selRows.end(); ++i)
236  {
237  for (int x = 0; x < mpCompartmentDM->columnCount(); ++x)
238  {
239  if (!mpTblCompartments->isColumnHidden(x))
240  {
241  if (!str.isEmpty())
242  str += "\t";
243 
244  str += mpCompartmentDM->index(mpProxyModel->mapToSource(*i).row(), x).data().toString();
245  }
246  }
247 
248  str += "\n";
249  }
250 
251  QApplication::clipboard()->setText(str);
252  }
253 }
254 
256 {
257  QRegExp regExp(mpLEFilter->text() + "|New Compartment", Qt::CaseInsensitive, QRegExp::RegExp);
258  mpProxyModel->setFilterRegExp(regExp);
259 }
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
virtual void keyPressEvent(QKeyEvent *ev)
#define pdelete(p)
Definition: copasi.h:215
#define COL_TYPE_COMPARTMENTS
objectType
CQIndexComboDelegate * mpTypeDelegate
void switchToOtherWidget(const size_t &id, const std::string &key)
Definition: listviews.cpp:926
virtual void slotSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
CQCompartmentDM * mpCompartmentDM
CQCompartmentsWidget(QWidget *parent=0, const char *name=0)
virtual bool clear()
virtual void slotBtnDeleteClicked()
#define C_INVALID_INDEX
Definition: copasi.h:222
#define C_UNUSED(p)
Definition: copasi.h:220
virtual void slotDoubleClicked(const QModelIndex proxyIndex)
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
bool mIgnoreUpdates
Definition: copasiWidget.h:67
virtual void slotBtnClearClicked()
virtual const std::string & getKey() const
ListViews * mpListView
Definition: copasiWidget.h:62
const QStringList & getTypes()
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
CQSortFilterProxyModel * mpProxyModel
CCopasiVectorNS< CCompartment > & getCompartments()
Definition: CModel.cpp:1145
static CKeyFactory * getKeyFactory()
virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: CModel.h:50
virtual void slotBtnNewClicked()
virtual bool isDefaultRow(const QModelIndex &i) const
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
static StandardButton question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
#define COL_ROW_NUMBER