COPASI API  4.16.103
CQFunctionsWidget.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 "CQFunctionsWidget.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 #include "function/CFunctionDB.h"
26 
27 /*
28  * Constructs a CQFunctionsWidget which is a child of 'parent', with the
29  * name 'name'.'
30  */
31 CQFunctionsWidget::CQFunctionsWidget(QWidget* parent, const char* name)
32  : CopasiWidget(parent, name)
33 {
34  setupUi(this);
35 
36  //Create Source Data Model.
37  mpFunctionDM = new CQFunctionDM(this);
38 
39  //Create the Proxy Model for sorting/filtering and set its properties.
41  mpProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
42  mpProxyModel->setFilterKeyColumn(-1);
43 
44  mpTblFunctions->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
45  mpTblFunctions->verticalHeader()->hide();
46  mpTblFunctions->sortByColumn(COL_ROW_NUMBER, Qt::AscendingOrder);
47 
48  // Connect the table widget
49  connect(mpFunctionDM, SIGNAL(notifyGUI(ListViews::ObjectType, ListViews::Action, const std::string)),
50  this, SLOT(protectedNotify(ListViews::ObjectType, ListViews::Action, const std::string)));
51  connect(mpFunctionDM, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
52  this, SLOT(dataChanged(const QModelIndex&, const QModelIndex&)));
53  connect(mpLEFilter, SIGNAL(textChanged(const QString &)),
54  this, SLOT(slotFilterChanged()));
55 }
56 
57 /*
58  * Destroys the object and frees any allocated resources
59  */
61 {
64  // no need to delete child widgets, Qt does it all for us
65 }
66 
68 {
71 }
72 
74 {
75  if (mpTblFunctions->hasFocus())
77 
79 }
80 
82 {
83  const QItemSelectionModel * pSelectionModel = mpTblFunctions->selectionModel();
84 
85  QModelIndexList mappedSelRows;
86  size_t i, imax = mpFunctionDM->rowCount();
87 
88  for (i = 0; i < imax; i++)
89  {
90  if (pSelectionModel->isRowSelected((int) i, QModelIndex()))
91  {
92  mappedSelRows.append(mpProxyModel->mapToSource(mpProxyModel->index((int) i, 0)));
93  }
94  }
95 
96  if (mappedSelRows.empty())
97  {return;}
98 
99  mpFunctionDM->removeRows(mappedSelRows);
100 }
101 
103 {
104 
105  int ret = CQMessageBox::question(this, tr("Confirm Delete"), "Delete all Functions?",
106  QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
107 
108  if (ret == QMessageBox::Yes)
109  {
110  mpFunctionDM->clear();
111  }
112 
114 }
115 
117 {
118  if (!mIgnoreUpdates)
119  {
120  enterProtected();
121  }
122 
123  return true;
124 }
125 
127 {
128  return true;
129 }
130 
132 {
133  if (mpTblFunctions->selectionModel() != NULL)
134  {
135  disconnect(mpTblFunctions->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
136  this, SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
137  }
138 
139  mpProxyModel->setSourceModel(mpFunctionDM);
140  //Set Model for the TableView
141  mpTblFunctions->setModel(NULL);
142  mpTblFunctions->setModel(mpProxyModel);
143  connect(mpTblFunctions->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
144  this, SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
146  mpTblFunctions->resizeColumnsToContents();
147 
148  return true;
149 }
150 
152 {
153  bool selected = false;
154 
155  QModelIndexList selRows = mpTblFunctions->selectionModel()->selectedRows();
156 
157  if (selRows.size() == 0)
158  selected = false;
159  else
160  {
161  if (selRows.size() == 1)
162  {
163  if (mpFunctionDM->isDefaultRow(mpProxyModel->mapToSource(selRows[0])))
164  selected = false;
165  else
166  selected = true;
167  }
168  else
169  selected = true;
170  }
171 
172  mpBtnDelete->setEnabled(selected);
173 
174  if (mpProxyModel->rowCount() - 1)
175  mpBtnClear->setEnabled(true);
176  else
177  mpBtnClear->setEnabled(false);
178 }
179 
180 void CQFunctionsWidget::slotSelectionChanged(const QItemSelection& C_UNUSED(selected),
181  const QItemSelection& C_UNUSED(deselected))
182 {
184 }
185 
186 void CQFunctionsWidget::dataChanged(const QModelIndex& C_UNUSED(topLeft),
187  const QModelIndex& C_UNUSED(bottomRight))
188 {
189  mpTblFunctions->resizeColumnsToContents();
191 }
192 
193 void CQFunctionsWidget::slotDoubleClicked(const QModelIndex proxyIndex)
194 {
195  QModelIndex index = mpProxyModel->mapToSource(proxyIndex);
196 
197  if (index.row() < 0)
198  return;
199 
200  if (mpFunctionDM->isDefaultRow(index))
201  {
203  }
204 
205  std::string key = CCopasiRootContainer::getFunctionList()->loadedFunctions()[index.row()]->getKey();
206 
207  if (CCopasiRootContainer::getKeyFactory()->get(key))
209 }
210 
212 {
213  if (ev->key() == Qt::Key_Delete)
215  else if (ev->key() == Qt::Key_C && (ev->modifiers() & Qt::ControlModifier))
216  {
217  QModelIndexList selRows = mpTblFunctions->selectionModel()->selectedRows(0);
218 
219  if (selRows.empty())
220  {return;}
221 
222  QString str;
223  QModelIndexList::const_iterator i;
224 
225  for (i = selRows.begin(); i != selRows.end(); ++i)
226  {
227  for (int x = 0; x < mpFunctionDM->columnCount(); ++x)
228  {
229  if (!mpTblFunctions->isColumnHidden(x))
230  {
231  if (!str.isEmpty())
232  str += "\t";
233 
234  str += mpFunctionDM->index(mpProxyModel->mapToSource(*i).row(), x).data().toString();
235  }
236  }
237 
238  str += "\n";
239  }
240 
241  QApplication::clipboard()->setText(str);
242  }
243 }
244 
246 {
247  QRegExp regExp(mpLEFilter->text() + "|New Function", Qt::CaseInsensitive, QRegExp::RegExp);
248  mpProxyModel->setFilterRegExp(regExp);
249 }
CCopasiVectorN< CFunction > & loadedFunctions()
#define pdelete(p)
Definition: copasi.h:215
virtual void slotDoubleClicked(const QModelIndex proxyIndex)
objectType
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
CQFunctionDM * mpFunctionDM
void switchToOtherWidget(const size_t &id, const std::string &key)
Definition: listviews.cpp:926
virtual void keyPressEvent(QKeyEvent *ev)
virtual bool clear()
virtual void slotBtnDeleteClicked()
#define C_INVALID_INDEX
Definition: copasi.h:222
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
#define C_UNUSED(p)
Definition: copasi.h:220
bool mIgnoreUpdates
Definition: copasiWidget.h:67
virtual const std::string & getKey() const
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
CQSortFilterProxyModel * mpProxyModel
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
ListViews * mpListView
Definition: copasiWidget.h:62
virtual bool enterProtected()
static CFunctionDB * getFunctionList()
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
static CKeyFactory * getKeyFactory()
virtual void slotBtnClearClicked()
virtual void slotFilterChanged()
virtual void slotSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
virtual void slotBtnNewClicked()
virtual bool isDefaultRow(const QModelIndex &i) const
CQFunctionsWidget(QWidget *parent=0, const char *name=0)
static StandardButton question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
#define COL_ROW_NUMBER