COPASI API  4.16.103
CQLayoutView.cpp
Go to the documentation of this file.
1 // Copyright (C) 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 #include <QtGui/QApplication>
7 #include <QtGui/QWheelEvent>
8 #include <QtGui/QAction>
9 #include <QtGui/QMenu>
10 #include <QtGui/QToolBar>
11 #include <QtCore/QFileInfo>
12 #include <QtGui/QHBoxLayout>
13 #include <QtGui/QLabel>
14 #include <QtGui/QComboBox>
15 
16 #include <qlayout/CQLayoutScene.h>
17 #include <qlayout/CQLayoutView.h>
18 
19 #include <copasi.h>
20 #include <layout/CLayout.h>
21 #include <layout/CListOfLayouts.h>
22 #include <layout/CLDefaultStyles.h>
25 
27 {
28 }
29 
30 void updateLayoutList(QComboBox* list, CCopasiDataModel* dataModel)
31 {
32  if (list == NULL || dataModel == NULL) return;
33 
34  list->clear();
35  CCopasiVector<CLayout> & layouts = *dataModel->getListOfLayouts();
37 
38  while (it != layouts.end())
39  {
40  const CLayout* layout = *it;
41  list->addItem(layout->getObjectName().c_str());
42  ++it;
43  }
44 }
45 
46 #define RENDERINFORMATION_TYPE_GLOBAL 0
47 #define RENDERINFORMATION_TYPE_LOCAL 1
48 #define RENDERINFORMATION_TYPE_DEFAULT 2
49 
51 {
52 
53  if (list == NULL || dataModel == NULL) return NULL;
54 
55  bool skipGlobal = list->count() > 0;
56  CLRenderInformationBase* result = NULL;
57 
58  // remove oldItems
59  if (skipGlobal)
60  {
61  for (size_t i = list->count(); i > 0; --i)
62  {
63  QString current = list->itemText(i - 1);
64  int type = list->itemData(i - 1).toInt();
65 
66  if (type == RENDERINFORMATION_TYPE_LOCAL)
67  list->removeItem(i - 1);
68  }
69  }
70 
71  // add local information
72  if (layout != NULL)
73  {
76 
77  while (it != render.end())
78  {
79  CLLocalRenderInformation* current = *it;
80 
81  if (result == NULL) result = current;
82 
83  if (current->getName().empty())
84  list->insertItem(0, current->getKey().c_str(), QVariant::fromValue(RENDERINFORMATION_TYPE_LOCAL));
85  else
86  list->insertItem(0, current->getName().c_str(), QVariant::fromValue(RENDERINFORMATION_TYPE_LOCAL));
87 
88  ++it;
89  }
90  }
91 
92  // bail
93  if (skipGlobal)
94  {
95  if (result == NULL && dataModel->getListOfLayouts()->getListOfGlobalRenderInformationObjects().size() > 0)
97 
98  if (result == NULL && getNumDefaultStyles() > 0)
99  result = getDefaultStyle(0);
100 
101  return result;
102  }
103 
104  // add global ones
105  {
108 
109  while (it != render.end())
110  {
111  CLGlobalRenderInformation* current = *it;
112 
113  if (result == NULL) result = current;
114 
115  if (current->getName().empty())
116  list->addItem(current->getKey().c_str(), QVariant::fromValue(RENDERINFORMATION_TYPE_GLOBAL));
117  else
118  list->addItem(current->getName().c_str(), QVariant::fromValue(RENDERINFORMATION_TYPE_GLOBAL));
119 
120  ++it;
121  }
122 
123  // add default ones
124  {
125  size_t i, iMax = getNumDefaultStyles();
126  CLRenderInformationBase* current = NULL;
127 
128  for (i = 0; i < iMax; ++i)
129  {
130  current = getDefaultStyle(i);
131 
132  if (result == NULL) result = current;
133 
134  std::string text = current->getKey();
135 
136  if (!current->getName().empty())
137  {
138  text = current->getName();
139  }
140 
141  list->addItem(text.c_str(), QVariant::fromValue(RENDERINFORMATION_TYPE_DEFAULT));
142  }
143  }
144  }
145 
146  return result;
147 }
148 
150 {
151  if (mIgnoreEvents || index < 0) return;
152 
153  if (mpDataModel == NULL) return;
154 
155  CQLayoutScene* current = dynamic_cast<CQLayoutScene*>(scene());
156 
157  if (current == NULL || current->getCurrentLayout() == NULL) return;
158 
159  CLRenderInformationBase* render = NULL;
160  int numLocal = current->getCurrentLayout()->getListOfLocalRenderInformationObjects().size();
161 
162  if (numLocal > index)
163  {
164  render = current->getCurrentLayout()->getListOfLocalRenderInformationObjects()[index];
165  }
166 
168 
169  if (render == NULL && numLocal + numGlobal > index)
170  {
172  }
173 
174  size_t numDefault = getNumDefaultStyles();
175 
176  if (render == NULL && numLocal + numGlobal + numDefault > (size_t)index)
177  {
178  render = getDefaultStyle(index - numGlobal - numLocal);
179  }
180 
181  if (render == NULL)
182  return;
183 
184  current->setRenderInformation(mpDataModel, render);
185  current->recreate();
186 }
187 
189 {
190  if (mIgnoreEvents || index < 0) return;
191 
192  if (mpDataModel == NULL) return;
193 
194  CQLayoutScene* current = dynamic_cast<CQLayoutScene*>(scene());
195 
196  if (current == NULL) return;
197 
198  CLayout* layout = (*mpDataModel->getListOfLayouts())[index];
199 
200  if (layout == NULL || layout == current->getCurrentLayout()) return;
201 
202  mIgnoreEvents = true;
203  // update the corresponding render information list
205 
206  if (render != NULL)
207  {
208  if (render->getName().empty())
209  mpRenderDropdown->setCurrentIndex(mpRenderDropdown->findText(render->getKey().c_str()));
210  else
211  mpRenderDropdown->setCurrentIndex(mpRenderDropdown->findText(render->getName().c_str()));
212  }
213 
214  mIgnoreEvents = false;
215  current->setLayout(layout, mpDataModel, render);
216  current->recreate();
217 }
218 
220  : QGraphicsView(parent)
221  , mScaleFactor(1.15)
222  , mIgnoreEvents(false)
223 {
224  setDragMode(QGraphicsView::ScrollHandDrag);
225  createActions();
226 }
227 
228 void CQLayoutView::slotSaveToFile(const QString& fileName)
229 {
230  CQLayoutScene* layoutScene = dynamic_cast<CQLayoutScene*>(scene());
231 
232  if (layoutScene == NULL) return;
233 
234  layoutScene->saveToFile(
235  fileName.toStdString(),
236  QFileInfo(fileName).suffix().toStdString()
237  );
238 }
239 
241  : QGraphicsView(scene)
242  , mScaleFactor(1.15)
243  , mIgnoreEvents(false)
244 
245 {
246  setDragMode(QGraphicsView::ScrollHandDrag);
247  createActions();
248 }
249 
250 void CQLayoutView::fillZoomMenu(QMenu* menu)
251 {
252  if (menu == NULL) return;
253 
254  menu->addAction(mpActFitOnScreen);
255  menu->addAction(mpActZoomIn);
256  menu->addAction(mpActZoomOut);
257  menu->addAction(mpActResetZoom);
258 }
259 
260 void CQLayoutView::fillZoomTooBar(QToolBar* toolBar)
261 {
262  if (toolBar == NULL) return;
263 
264  toolBar->addAction(mpActFitOnScreen);
265  toolBar->addAction(mpActZoomIn);
266  toolBar->addAction(mpActZoomOut);
267  toolBar->addAction(mpActResetZoom);
268 }
269 
271 {
272  mpActFitOnScreen = new QAction(tr("Fit On Screen"), this);
273  mpActFitOnScreen->setShortcut(QKeySequence::fromString("F5"));
275  connect(mpActFitOnScreen, SIGNAL(triggered()), this, SLOT(slotFitOnScreen()));
276  addAction(mpActFitOnScreen);
277 
278  mpActResetZoom = new QAction(tr("Reset Zoom"), this);
280  connect(mpActResetZoom, SIGNAL(triggered()), this, SLOT(slotResetZoom()));
281  addAction(mpActResetZoom);
282 
283  mpActZoomIn = new QAction(tr("Zoom In"), this);
285  mpActZoomIn->setShortcut(QKeySequence::ZoomIn);
286  connect(mpActZoomIn, SIGNAL(triggered()), this, SLOT(slotZoomIn()));
287  addAction(mpActZoomIn);
288 
289  mpActZoomOut = new QAction(tr("Zoom Out"), this);
291  mpActZoomOut->setShortcut(QKeySequence::ZoomOut);
292  connect(mpActZoomOut, SIGNAL(triggered()), this, SLOT(slotZoomOut()));
293  addAction(mpActZoomOut);
294 
295  mpLayoutDropdown = new QComboBox;
296  mpLayoutDropdown->setSizeAdjustPolicy(QComboBox::AdjustToContentsOnFirstShow);
297 
298  mpRenderDropdown = new QComboBox;
299  mpRenderDropdown->setSizeAdjustPolicy(QComboBox::AdjustToContentsOnFirstShow);
300 
301  connect(mpLayoutDropdown, SIGNAL(currentIndexChanged(int)), this, SLOT(slotLayoutChanged(int)));
302  connect(mpRenderDropdown, SIGNAL(currentIndexChanged(int)), this, SLOT(slotRenderInformationChanged(int)));
303 
304  setInteractive(true);
305  setRenderHints(QPainter::Antialiasing);
306 }
307 
308 void CQLayoutView::fillSelectionToolBar(QToolBar* toolBar)
309 {
310  QFrame* pFrame1 = new QFrame;
311  QHBoxLayout* pLayout = new QHBoxLayout;
312  pLayout->setSpacing(3);
313  pFrame1->setLayout(pLayout);
314  QLabel* pLabel = new QLabel(tr("Layout:"));
315  pLayout->addWidget(pLabel);
316  pLayout->addWidget(mpLayoutDropdown);
317 
318  QFrame* pFrame2 = new QFrame;
319  pLayout = new QHBoxLayout;
320  pLayout->setSpacing(3);
321  pFrame2->setLayout(pLayout);
322  QLabel* mpRenderLabel = new QLabel(tr("Render Information:"));
323  pLayout->addWidget(mpRenderLabel);
324  pLayout->addWidget(mpRenderDropdown);
325 
326  toolBar->addWidget(pFrame1);
327  toolBar->addWidget(pFrame2);
328 }
329 
331 {
332  setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
333  fitInView(scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
334  update();
335 }
337 {
338  setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
339  resetMatrix();
340  ensureVisible(scene()->itemsBoundingRect());
341  update();
342 }
344 {
345  setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
346  scale(mScaleFactor, mScaleFactor);
347 }
349 {
350  setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
351  scale(1.0 / mScaleFactor, 1.0 / mScaleFactor);
352 }
353 
355 {
356  return mpDataModel;
357 }
358 
360 {
361  mpDataModel = dataModel;
362 
364 
365  if (layout != NULL)
366  mpLayoutDropdown->setCurrentIndex(mpLayoutDropdown->findText(layout->getObjectName().c_str()));
367 
368  updateRenderInformationList(mpRenderDropdown, dataModel, layout);
369 }
370 
371 void CQLayoutView::wheelEvent(QWheelEvent* event)
372 {
373  if (QApplication::keyboardModifiers() == Qt::ControlModifier ||
374  QApplication::keyboardModifiers() == Qt::MetaModifier ||
375  QApplication::keyboardModifiers() == Qt::ShiftModifier)
376  {
377  if (event->delta() > 0)
378  {
379  // Zoom in
380  slotZoomIn();
381  }
382  else
383  {
384  // Zooming out
385  slotZoomOut();
386  }
387  }
388  else
389  {
390  QGraphicsView::wheelEvent(event);
391  }
392 }
const CLayout * getCurrentLayout() const
bool mIgnoreEvents
Definition: CQLayoutView.h:51
void saveToFile(const std::string &fileName, const std::string &fileType="pdf")
void setDataModel(CCopasiDataModel *dataModel, CLayout *layout=NULL)
#define RENDERINFORMATION_TYPE_DEFAULT
CLRenderInformationBase * updateRenderInformationList(QComboBox *list, CCopasiDataModel *dataModel, CLayout *layout)
void setLayout(CLayout *layout, CCopasiDataModel *model=NULL, CLRenderInformationBase *renderInformation=NULL)
QComboBox * mpLayoutDropdown
Definition: CQLayoutView.h:49
const std::string & getObjectName() const
const CCopasiVector< CLLocalRenderInformation > & getListOfLocalRenderInformationObjects() const
Definition: CLayout.h:149
virtual size_t size() const
QAction * mpActZoomOut
Definition: CQLayoutView.h:45
iterator begin()
void fillZoomTooBar(QToolBar *toolBar)
QAction * mpActFitOnScreen
Definition: CQLayoutView.h:46
double mScaleFactor
Definition: CQLayoutView.h:48
void slotResetZoom()
CLGlobalRenderInformation * getDefaultStyle(size_t index)
void fillZoomMenu(QMenu *menu)
iterator end()
CCopasiDataModel * getDataModel()
void fillSelectionToolBar(QToolBar *toolBar)
QAction * mpActZoomIn
Definition: CQLayoutView.h:44
void setRenderInformation(CCopasiDataModel *model, CLRenderInformationBase *renderInformation)
CCopasiDataModel * mpDataModel
Definition: CQLayoutView.h:52
size_t getNumDefaultStyles()
void slotFitOnScreen()
static const QIcon & icon(const IconID &id)
QAction * mpActResetZoom
Definition: CQLayoutView.h:47
void slotZoomOut()
void slotLayoutChanged(int)
CQLayoutView(QWidget *parent=NULL)
QComboBox * mpRenderDropdown
Definition: CQLayoutView.h:50
CListOfLayouts * getListOfLayouts()
#define RENDERINFORMATION_TYPE_GLOBAL
void createActions()
virtual ~CQLayoutView()
const std::string & getKey() const
const std::string & getName() const
void slotRenderInformationChanged(int)
void slotSaveToFile(const QString &fileName)
void wheelEvent(QWheelEvent *event)
void updateLayoutList(QComboBox *list, CCopasiDataModel *dataModel)
#define RENDERINFORMATION_TYPE_LOCAL
const CCopasiVector< CLGlobalRenderInformation > & getListOfGlobalRenderInformationObjects() const