COPASI API  4.16.103
CQGLLayoutViewer.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 #include "CQGLLayoutViewer.h"
7 
8 #include <QtGui/QHBoxLayout>
9 #include <QtGui/QLayout>
10 #include <QtGui/QScrollBar>
11 #include <QtGui/QFrame>
12 #include <QtOpenGL/QGLFormat>
13 #include "CQGLLayoutPainter.h"
14 
15 /**
16  * Constructor.
17  */
18 CQGLLayoutViewer::CQGLLayoutViewer(QWidget* pParent, Qt::WindowFlags f): QFrame(pParent, f)
19 {
20  QVBoxLayout* pVBoxLayout = new QVBoxLayout(this);
21  this->setLayout(pVBoxLayout);
22  pVBoxLayout->setContentsMargins(0, 0, 0, 0);
23  QFrame* pHBox = new QFrame(this);
24  QHBoxLayout* pHBoxLayout = new QHBoxLayout(pHBox);
25  pHBox->setLayout(pHBoxLayout);
26  pHBoxLayout->setContentsMargins(0, 0, 0, 0);
27  // the QGLformat needs to enable sample buffers, otherwise
28  // there is no anti aliasing
29  QGLFormat format(QGL::SampleBuffers);
30  format.setDoubleBuffer(TRUE);
31  this->mpLayoutPainter = new CQGLLayoutPainter(format, pHBox);
32  pHBoxLayout->addWidget(this->mpLayoutPainter);
33  this->mpVerticalScrollbar = new QScrollBar(Qt::Vertical, pHBox);
34  this->mpVerticalScrollbar->setSingleStep(1);
35  pHBoxLayout->addWidget(this->mpVerticalScrollbar);
36  pVBoxLayout->addWidget(pHBox);
37  this->mpHorizontalScrollbar = new QScrollBar(Qt::Horizontal, this);
38  this->mpHorizontalScrollbar->setSingleStep(1);
39  pVBoxLayout->addWidget(this->mpHorizontalScrollbar);
40  connect(this->mpVerticalScrollbar, SIGNAL(valueChanged(int)), this, SLOT(slotVValueChanged(int)));
41  connect(this->mpHorizontalScrollbar, SIGNAL(valueChanged(int)), this, SLOT(slotHValueChanged(int)));
42  connect(this->mpLayoutPainter, SIGNAL(documentChanged()), this, SLOT(slotDocumentChanged()));
43  connect(this->mpLayoutPainter, SIGNAL(singleCurveSelected(bool)), this, SLOT(slotSingleCurveSelected(bool)));
44 }
45 
46 /**
47  * Destructor.
48  */
50 {}
51 
52 void CQGLLayoutViewer::resizeEvent(QResizeEvent* e)
53 {
54  this->updateScrollbars();
55  QFrame::resizeEvent(e);
56 }
57 
59 {
60  this->mpLayoutPainter->setZoomFactor(zoom);
61  this->updateScrollbars();
62  this->mpLayoutPainter->update();
63 }
64 
66 {
67  // reset the scollbar range
68  // disconnect the scrollbar listeners and handle the update so that the GL
69  // window is only redrawn once
70  double zoom = this->mpLayoutPainter->getZoomFactor();
71  double maxX = this->mpLayoutPainter->maxX();
72  double maxY = this->mpLayoutPainter->maxY();
73  double minX = this->mpLayoutPainter->minX();
74  double minY = this->mpLayoutPainter->minY();
75  double graphWidth = (maxX - minX) * zoom;
76  double graphHeight = (maxY - minY) * zoom;
77  double rectangleHeight = this->mpLayoutPainter->contentsRect().height();
78  double rectangleWidth = this->mpLayoutPainter->contentsRect().width();
79  disconnect(this->mpVerticalScrollbar, SIGNAL(valueChanged(int)), this, SLOT(slotVValueChanged(int)));
80  disconnect(this->mpHorizontalScrollbar, SIGNAL(valueChanged(int)), this, SLOT(slotHValueChanged(int)));
81 
82  if (graphHeight < rectangleHeight)
83  {
84  this->mpVerticalScrollbar->hide();
85  this->mpVerticalScrollbar->setValue(0);
87  }
88  else
89  {
90  this->mpVerticalScrollbar->setPageStep(rectangleHeight);
91  int oldMaximum = this->mpVerticalScrollbar->maximum();
92  int newMaximum = (int)(graphHeight - rectangleHeight);
93  int oldValue = this->mpVerticalScrollbar->value();
94  int newValue = 0;
95 
96  if (oldValue != 0 && oldMaximum != 0)
97  {
98  newValue = (int)((double) oldValue * (double) newMaximum / (double) oldMaximum);
99  }
100 
101  this->mpVerticalScrollbar->setRange(0, newMaximum);
102  this->mpVerticalScrollbar->setValue(newValue);
103  this->mpLayoutPainter->setCurrentPositionY((double)(minY + newValue / zoom));
104  this->mpVerticalScrollbar->show();
105  }
106 
107  if (graphWidth < rectangleWidth)
108  {
109  this->mpHorizontalScrollbar->hide();
110  this->mpHorizontalScrollbar->setValue(0);
112  }
113  else
114  {
115  this->mpHorizontalScrollbar->setPageStep(rectangleWidth);
116  int oldMaximum = this->mpHorizontalScrollbar->maximum();
117  int newMaximum = (int)(graphWidth - rectangleWidth);
118  int oldValue = this->mpHorizontalScrollbar->value();
119  int newValue = 0;
120 
121  if (oldValue != 0 && oldMaximum != 0)
122  {
123  newValue = (int)((double) oldValue * (double) newMaximum / (double) oldMaximum);
124  }
125 
126  this->mpHorizontalScrollbar->setRange(0, newMaximum);
127  this->mpHorizontalScrollbar->setValue(newValue);
128  this->mpLayoutPainter->setCurrentPositionX((double)(minX + newValue / zoom));
129  this->mpHorizontalScrollbar->show();
130  }
131 
132  connect(this->mpVerticalScrollbar, SIGNAL(valueChanged(int)), this, SLOT(slotVValueChanged(int)));
133  connect(this->mpHorizontalScrollbar, SIGNAL(valueChanged(int)), this, SLOT(slotHValueChanged(int)));
134 }
135 
137 {
138  double minY = this->mpLayoutPainter->minY();
139  double zoom = this->mpLayoutPainter->getZoomFactor();
140  this->mpLayoutPainter->setCurrentPositionY((double)(minY + value / zoom));
141 }
142 
144 {
145  double minX = this->mpLayoutPainter->minX();
146  double zoom = this->mpLayoutPainter->getZoomFactor();
147  this->mpLayoutPainter->setCurrentPositionX((double)(minX + value / zoom));
148 }
149 
151 {
152  // TODO disconnect the scrollbar listeners
153  // so that the display is only redrawn once
154  this->mpLayoutPainter->resetView();
155  this->updateScrollbars();
156 }
157 
158 /**
159  * Calculates the ratio that is needed to fit the diagram on the current viewport
160  * and sets this as the zoom factor.
161  * The methods return the newly calculated zoom factor.
162  */
164 {
165  double zoom = this->mpLayoutPainter->fitToScreen();
166  this->updateScrollbars();
167  return zoom;
168 }
169 
171 {
172  this->mpLayoutPainter->update();
173 }
174 
175 void CQGLLayoutViewer::update(const CCopasiDataModel* pDatamodel, CLayout* pLayout, const CLRenderInformationBase* pRenderInfo, const QString& baseDir)
176 {
177  // pass the options on to the layout painter
178  if (pRenderInfo)
179  {
180  this->mpLayoutPainter->update(pDatamodel, pLayout, pRenderInfo, baseDir);
181  this->updateScrollbars();
182  }
183 }
184 
185 void CQGLLayoutViewer::change_style(const CLRenderInformationBase* pRenderInfo, bool defaultStyle)
186 {
187  this->mpLayoutPainter->change_style(pRenderInfo, defaultStyle);
188 }
189 
190 /**
191  * This slot is called when the layout painter changes
192  * the document (layout).
193  */
195 {
196  emit documentChanged();
197 }
198 
199 /**
200  * This slot is called when the selection changes.
201  * The value determines whether a single curve is selected or not.
202  */
204 {
205  emit singleCurveSelected(selected);
206 }
207 
208 /**
209  * This slot is called when the user initiates the action to revert the
210  * currently selected curve.
211  */
213 {
214  this->mpLayoutPainter->revertCurve();
215 }
216 
217 /**
218  * Returns a pointer to the OpenGL painter.
219  */
221 {
222  return this->mpLayoutPainter;
223 }
224 
225 /**
226  * Returns a const pointer to the OpenGL painter.
227  */
229 {
230  return this->mpLayoutPainter;
231 }
void singleCurveSelected(bool selected)
virtual ~CQGLLayoutViewer()
double minX() const
#define TRUE
Definition: CGA.h:25
CQGLLayoutPainter * getPainter()
void update(const CCopasiDataModel *pDataModel, CLayout *pLayout, const CLRenderInformationBase *pRenderInfo, const QString &baseDir)
double maxY() const
void setZoomFactor(double zoom)
CQGLLayoutViewer(QWidget *pParent=0, Qt::WindowFlags f=0)
void setCurrentPositionX(double x)
void documentChanged()
void setCurrentPositionY(double y)
void slotSingleCurveSelected(bool selected)
void setZoomFactor(double)
void change_style(const CLRenderInformationBase *pRenderInfo, bool defaultStyle=false)
double maxX() const
double getZoomFactor() const
CQGLLayoutPainter * mpLayoutPainter
void slotVValueChanged(int value)
QScrollBar * mpVerticalScrollbar
void change_style(const CLRenderInformationBase *pRenderInfo, bool defaultStyle=false)
virtual void resizeEvent(QResizeEvent *e)
double minY() const
void slotHValueChanged(int value)
QScrollBar * mpHorizontalScrollbar