COPASI API  4.16.103
CQEventsWidget.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) 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include "CQEventsWidget.h"
16 
17 #include <QtGui/QHeaderView>
18 #include <QtGui/QClipboard>
19 #include <QtGui/QKeyEvent>
20 
21 #include "qtUtilities.h"
22 #include "copasi.h"
23 #include "CQMessageBox.h"
24 
25 #include "model/CModel.h"
28 
29 /*
30  * Constructs a CQEventsWidget which is a child of 'parent', with the
31  * name 'name'.'
32  */
33 CQEventsWidget::CQEventsWidget(QWidget* parent, const char* name)
34  : CopasiWidget(parent, name)
35 {
36  setupUi(this);
37 
38  //Create Source Data Model.
39  mpEventDM = new CQEventDM(this);
40 
41  //Create the Proxy Model for sorting/filtering and set its properties.
43  mpProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
44  mpProxyModel->setFilterKeyColumn(-1);
45 
46  mpTblEvents->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
47  mpTblEvents->verticalHeader()->hide();
48  mpTblEvents->sortByColumn(COL_ROW_NUMBER, Qt::AscendingOrder);
49 
50  // Connect the table widget
51  connect(mpEventDM, SIGNAL(notifyGUI(ListViews::ObjectType, ListViews::Action, const std::string)),
52  this, SLOT(protectedNotify(ListViews::ObjectType, ListViews::Action, const std::string)));
53  connect(mpEventDM, 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 {
66  // no need to delete child widgets, Qt does it all for us
67 }
68 
70 {
73 }
74 
76 {
77  if (mpTblEvents->hasFocus())
79 
81 }
82 
84 {
85  const QItemSelectionModel * pSelectionModel = mpTblEvents->selectionModel();
86 
87  QModelIndexList mappedSelRows;
88  size_t i, imax = mpEventDM->rowCount();
89 
90  for (i = 0; i < imax; i++)
91  {
92  if (pSelectionModel->isRowSelected((int) i, QModelIndex()))
93  {
94  mappedSelRows.append(mpProxyModel->mapToSource(mpProxyModel->index((int) i, 0)));
95  }
96  }
97 
98  if (mappedSelRows.empty())
99  {return;}
100 
101  mpEventDM->removeRows(mappedSelRows);
102 }
103 
105 {
106 
107  int ret = CQMessageBox::question(this, tr("Confirm Delete"), "Delete all Events?",
108  QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
109 
110  if (ret == QMessageBox::Yes)
111  {
112  mpEventDM->clear();
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 (mpTblEvents->selectionModel() != NULL)
136  {
137  disconnect(mpTblEvents->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
138  this, SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
139  }
140 
141  mpProxyModel->setSourceModel(mpEventDM);
142  //Set Model for the TableView
143  mpTblEvents->setModel(NULL);
144  mpTblEvents->setModel(mpProxyModel);
145  connect(mpTblEvents->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
146  this, SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
148  mpTblEvents->resizeColumnsToContents();
149 
150  return true;
151 }
152 
154 {
155  bool selected = false;
156 
157  QModelIndexList selRows = mpTblEvents->selectionModel()->selectedRows();
158 
159  if (selRows.size() == 0)
160  selected = false;
161  else
162  {
163  if (selRows.size() == 1)
164  {
165  if (mpEventDM->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 CQEventsWidget::slotSelectionChanged(const QItemSelection& C_UNUSED(selected),
183  const QItemSelection& C_UNUSED(deselected))
184 {
186 }
187 
188 void CQEventsWidget::dataChanged(const QModelIndex& C_UNUSED(topLeft),
189  const QModelIndex& C_UNUSED(bottomRight))
190 {
191  mpTblEvents->resizeColumnsToContents();
193 }
194 
195 void CQEventsWidget::slotDoubleClicked(const QModelIndex proxyIndex)
196 {
197  QModelIndex index = mpProxyModel->mapToSource(proxyIndex);
198 
199  if (index.row() < 0)
200  return;
201 
202  if (mpEventDM->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->getEvents()[index.row()]->getKey();
216 
217  if (CCopasiRootContainer::getKeyFactory()->get(key))
219 }
220 
221 void CQEventsWidget::keyPressEvent(QKeyEvent* ev)
222 {
223  if (ev->key() == Qt::Key_Delete)
225  else if (ev->key() == Qt::Key_C && (ev->modifiers() & Qt::ControlModifier))
226  {
227  QModelIndexList selRows = mpTblEvents->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 < mpEventDM->columnCount(); ++x)
238  {
239  if (!mpTblEvents->isColumnHidden(x))
240  {
241  if (!str.isEmpty())
242  str += "\t";
243 
244  str += mpEventDM->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 Event", Qt::CaseInsensitive, QRegExp::RegExp);
258  mpProxyModel->setFilterRegExp(regExp);
259 }
virtual void slotFilterChanged()
CQEventDM * mpEventDM
#define pdelete(p)
Definition: copasi.h:215
objectType
virtual void slotDoubleClicked(const QModelIndex proxyIndex)
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: CQEventDM.cpp:27
CCopasiVectorN< CEvent > & getEvents()
Definition: CModel.cpp:1110
void switchToOtherWidget(const size_t &id, const std::string &key)
Definition: listviews.cpp:926
virtual void slotBtnClearClicked()
virtual bool clear()
#define C_INVALID_INDEX
Definition: copasi.h:222
void deleteSelectedEvents()
virtual void slotBtnNewClicked()
#define C_UNUSED(p)
Definition: copasi.h:220
virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
virtual bool enterProtected()
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: CQEventDM.cpp:31
CQSortFilterProxyModel * mpProxyModel
bool mIgnoreUpdates
Definition: copasiWidget.h:67
virtual void slotSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
virtual const std::string & getKey() const
ListViews * mpListView
Definition: copasiWidget.h:62
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
virtual bool protectedNotify(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key="")
virtual bool update(ListViews::ObjectType objectType, ListViews::Action action, const std::string &key)
bool removeRows(QModelIndexList rows, const QModelIndex &index=QModelIndex())
Definition: CQEventDM.cpp:277
static CKeyFactory * getKeyFactory()
virtual bool leave()
virtual void keyPressEvent(QKeyEvent *ev)
Definition: CModel.h:50
virtual void slotBtnDeleteClicked()
virtual bool isDefaultRow(const QModelIndex &i) const
CQEventsWidget(QWidget *parent=0, const char *name=0)
static StandardButton question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
#define COL_ROW_NUMBER