COPASI API  4.16.103
scrollzoomer.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) 2006 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 // Copyright � 1997 Josef Wilgen
16 // Copyright � 2002 Uwe Rathmann
17 //
18 // This file is published under the Qwt License, Version 1.0.
19 // You should have received a copy of this licence in the file
20 // QwtLicense.
21 //
22 // Modifications made to the original are
23 #include <QtCore/QEvent>
24 #include <qwt_plot_canvas.h>
25 #include <qwt_plot_layout.h>
26 #include <qwt_scale_engine.h>
27 //Added by qt3to4:
28 #include <QtGui/QResizeEvent>
29 #include <QtCore/QChildEvent>
30 #include "scrollbar.h"
31 #include "scrollzoomer.h"
32 
33 LogPlotZoomer::LogPlotZoomer(QwtPlotCanvas *canvas):
34  QwtPlotZoomer(canvas)
35 {}
36 
37 QwtText LogPlotZoomer::trackerText(const QwtDoublePoint &pos) const
38 {
39  switch (rubberBand())
40  {
41  case HLineRubberBand:
42  return QString().sprintf("%.4g", pos.y());
43  case VLineRubberBand:
44  return QString().sprintf("%.4g", pos.x());
45  default:
46  return QString().sprintf("%.4g, %.4g", pos.x(), pos.y());
47  }
48 
49  return QwtText(); // make some dumb compilers happy
50 }
51 
52 /*void QwtPlotZoomer::move(double x, double y)
53 {
54  x = qwtMax(x, zoomBase().left());
55  x = qwtMin(x, zoomBase().right() - zoomRect().width());
56 
57  y = qwtMax(y, zoomBase().top());
58  y = qwtMin(y, zoomBase().bottom() - zoomRect().height());
59 
60  if (x != zoomRect().left() || y != zoomRect().top())
61  {
62  d_data->zoomStack[d_data->zoomRectIndex].moveTo(x, y);
63  rescale();
64  }
65 }*/
66 
67 void LogPlotZoomer::move(double x, double y)
68 {
69  //QwtPlotZoomer::move(x,y);
70 
71  x = qwtMax(x, zoomBase().left());
72  x = qwtMin(x, zoomBase().right() - zoomRect().width());
73 
74  y = qwtMax(y, zoomBase().top());
75  y = qwtMin(y, zoomBase().bottom() - zoomRect().height());
76 
77  if (x != zoomRect().left() || y != zoomRect().top())
78  {
79  //zoomStack()[zoomRectIndex()].moveTo(x, y);
80  QwtDoubleRect & rect = const_cast<QwtDoubleRect &>(zoomStack()[zoomRectIndex()]);
81 
82  //handle x axis
83  const int xAxis = QwtPlotZoomer::xAxis();
84  const QwtScaleEngine *sex = plot()->axisScaleEngine(xAxis);
85 
86  if (dynamic_cast<const QwtLog10ScaleEngine*>(sex))
87  {
88  //logarithmic
89  double factor = rect.right() / rect.left();
90  rect.setRight(x * factor);
91  rect.setLeft(x);
92  }
93  else
94  {
95  rect.moveLeft(x);
96  }
97 
98  const int yAxis = QwtPlotZoomer::yAxis();
99 
100  const QwtScaleEngine *sey = plot()->axisScaleEngine(yAxis);
101 
102  if (dynamic_cast<const QwtLog10ScaleEngine*>(sey))
103  {
104  //logarithmic
105  double factor = rect.bottom() / rect.top();
106  rect.setBottom(y * factor);
107  rect.setTop(y);
108  }
109  else
110  {
111  rect.moveTop(y);
112  }
113 
114  //zoomStack()[zoomRectIndex()].moveTo(x, y);
115  rescale();
116  }
117 }
118 
119 //******************************************
120 
122 {
123 public:
125  scrollBar(NULL),
126  position(ScrollZoomer::OppositeToScale),
127 #if QT_VERSION < 0x040000
128  mode(Q3ScrollView::Auto)
129 #else
130  mode(Qt::ScrollBarAsNeeded)
131 #endif
132  {
133  }
134 
136  {
137  delete scrollBar;
138  }
139 
142 #if QT_VERSION < 0x040000
143  Q3ScrollView::ScrollBarMode mode;
144 #else
145  Qt::ScrollBarPolicy mode;
146 #endif
147 };
148 
149 //******************************************
150 
151 ScrollZoomer::ScrollZoomer(QwtPlotCanvas *canvas):
152  LogPlotZoomer(canvas),
153  d_cornerWidget(NULL),
154  d_hScrollData(NULL),
155  d_vScrollData(NULL)
156 {
157  if (!canvas)
158  return;
159 
162 }
163 
165 {
166  delete d_cornerWidget;
167  delete d_vScrollData;
168  delete d_hScrollData;
169 }
170 
172 {
173  QwtPlotZoomer::rescale();
175 }
176 
178 {
179  ScrollBar *&sb = (o == Qt::Vertical)
181 
182  if (sb == NULL)
183  {
184  sb = new ScrollBar(o, canvas());
185  sb->hide();
186  connect(sb,
187  SIGNAL(valueChanged(Qt::Orientation, double, double)),
188  SLOT(scrollBarMoved(Qt::Orientation, double, double)));
189  }
190 
191  return sb;
192 }
193 
195 {
196  return d_hScrollData->scrollBar;
197 }
198 
200 {
201  return d_vScrollData->scrollBar;
202 }
203 
204 #if QT_VERSION < 0x040000
205 void ScrollZoomer::setHScrollBarMode(Q3ScrollView::ScrollBarMode mode)
206 #else
207 void ScrollZoomer::setHScrollBarMode(Qt::ScrollBarPolicy mode)
208 #endif
209 {
210  if (hScrollBarMode() != mode)
211  {
212  d_hScrollData->mode = mode;
213  updateScrollBars();
214  }
215 }
216 
217 #if QT_VERSION < 0x040000
218 void ScrollZoomer::setVScrollBarMode(Q3ScrollView::ScrollBarMode mode)
219 #else
220 void ScrollZoomer::setVScrollBarMode(Qt::ScrollBarPolicy mode)
221 #endif
222 {
223  if (vScrollBarMode() != mode)
224  {
225  d_vScrollData->mode = mode;
226  updateScrollBars();
227  }
228 }
229 
230 #if QT_VERSION < 0x040000
231 Q3ScrollView::ScrollBarMode ScrollZoomer::hScrollBarMode() const
232 #else
233 Qt::ScrollBarPolicy ScrollZoomer::hScrollBarMode() const
234 #endif
235 
236 {
237  return d_hScrollData->mode;
238 }
239 
240 #if QT_VERSION < 0x040000
241 Q3ScrollView::ScrollBarMode ScrollZoomer::vScrollBarMode() const
242 #else
243 Qt::ScrollBarPolicy ScrollZoomer::vScrollBarMode() const
244 #endif
245 
246 {
247  return d_vScrollData->mode;
248 }
249 
251 {
252  if (d_hScrollData->position != pos)
253  {
254  d_hScrollData->position = pos;
256  }
257 }
258 
260 {
261  if (d_vScrollData->position != pos)
262  {
263  d_vScrollData->position = pos;
265  }
266 }
267 
269 {
270  return d_hScrollData->position;
271 }
272 
274 {
275  return d_vScrollData->position;
276 }
277 
279 {
280  if (w != d_cornerWidget)
281  {
282  if (canvas())
283  {
284  delete d_cornerWidget;
285  d_cornerWidget = w;
286 
287  if (d_cornerWidget->parent() != canvas())
288  {
289 #if QT_VERSION < 0x040000
290  d_cornerWidget->reparent(canvas(), QPoint(0, 0));
291 #else
292  d_cornerWidget->setParent(canvas());
293 #endif
294  }
295 
297  }
298  }
299 }
300 
302 {
303  return d_cornerWidget;
304 }
305 
306 bool ScrollZoomer::eventFilter(QObject *o, QEvent *e)
307 {
308  if (o == canvas())
309  {
310  switch (e->type())
311  {
312  case QEvent::Resize:
313  {
314  const int fw = ((QwtPlotCanvas *)canvas())->frameWidth();
315 
316  QRect rect;
317  rect.setSize(((QResizeEvent *)e)->size());
318  rect.setRect(rect.x() + fw, rect.y() + fw,
319  rect.width() - 2 * fw, rect.height() - 2 * fw);
320 
321  layoutScrollBars(rect);
322  break;
323  }
324  case QEvent::ChildRemoved:
325  {
326  const QObject *child = ((QChildEvent *)e)->child();
327 
328  if (child == d_cornerWidget)
329  d_cornerWidget = NULL;
330  else if (child == d_hScrollData->scrollBar)
331  d_hScrollData->scrollBar = NULL;
332  else if (child == d_vScrollData->scrollBar)
333  d_vScrollData->scrollBar = NULL;
334 
335  break;
336  }
337  default:
338  break;
339  }
340  }
341 
342  return QwtPlotZoomer::eventFilter(o, e);
343 }
344 
345 bool ScrollZoomer::needScrollBar(Qt::Orientation o) const
346 {
347 #if QT_VERSION < 0x040000
348  Q3ScrollView::ScrollBarMode mode;
349 #else
350  Qt::ScrollBarPolicy mode;
351 #endif
352  double zoomMin, zoomMax, baseMin, baseMax;
353 
354  if (o == Qt::Horizontal)
355  {
356  mode = d_hScrollData->mode;
357  baseMin = zoomBase().left();
358  baseMax = zoomBase().right();
359  zoomMin = zoomRect().left();
360  zoomMax = zoomRect().right();
361  }
362  else
363  {
364  mode = d_vScrollData->mode;
365  baseMin = zoomBase().top();
366  baseMax = zoomBase().bottom();
367  zoomMin = zoomRect().top();
368  zoomMax = zoomRect().bottom();
369  }
370 
371  bool needed = false;
372 
373  switch (mode)
374  {
375 #if QT_VERSION < 0x040000
376  case Q3ScrollView::AlwaysOn:
377 #else
378  case Qt::ScrollBarAlwaysOn:
379 #endif
380  needed = true;
381  break;
382 #if QT_VERSION < 0x040000
383  case Q3ScrollView::AlwaysOff:
384 #else
385  case Qt::ScrollBarAlwaysOff:
386 #endif
387  needed = false;
388  break;
389  default:
390  {
391  if (baseMin < zoomMin || baseMax > zoomMax)
392  needed = true;
393 
394  break;
395  }
396  }
397 
398  return needed;
399 }
400 
402 {
403  if (!canvas())
404  return;
405 
406  const int xAxis = QwtPlotZoomer::xAxis();
407  const int yAxis = QwtPlotZoomer::yAxis();
408 
409  int xScrollBarAxis = xAxis;
410 
412  xScrollBarAxis = oppositeAxis(xScrollBarAxis);
413 
414  int yScrollBarAxis = yAxis;
415 
417  yScrollBarAxis = oppositeAxis(yScrollBarAxis);
418 
419  QwtPlotLayout *layout = plot()->plotLayout();
420 
421  bool showHScrollBar = needScrollBar(Qt::Horizontal);
422 
423  if (showHScrollBar)
424  {
425  ScrollBar *sb = scrollBar(Qt::Horizontal);
426 
427  sb->setPalette(plot()->palette());
428 
429  const QwtScaleEngine *se = plot()->axisScaleEngine(xAxis);
430  sb->setInverted(se->testAttribute(QwtScaleEngine::Inverted));
431  sb->setLogScale(dynamic_cast<const QwtLog10ScaleEngine*>(se));
432 
433  sb->setBase(zoomBase().left(), zoomBase().right());
434  sb->moveSlider(zoomRect().left(), zoomRect().right());
435 
436  if (!sb->isVisibleTo(canvas()))
437  {
438  sb->show();
439  layout->setCanvasMargin(layout->canvasMargin(xScrollBarAxis)
440  + sb->extent(), xScrollBarAxis);
441  }
442  }
443  else
444  {
445  if (horizontalScrollBar())
446  {
447  horizontalScrollBar()->hide();
448  layout->setCanvasMargin(layout->canvasMargin(xScrollBarAxis)
449  - horizontalScrollBar()->extent(), xScrollBarAxis);
450  }
451  }
452 
453  bool showVScrollBar = needScrollBar(Qt::Vertical);
454 
455  if (showVScrollBar)
456  {
457  ScrollBar *sb = scrollBar(Qt::Vertical);
458 
459  sb->setPalette(plot()->palette());
460 
461  const QwtScaleEngine *se = plot()->axisScaleEngine(yAxis);
462  sb->setInverted(!(se->testAttribute(QwtScaleEngine::Inverted)));
463  sb->setLogScale(dynamic_cast<const QwtLog10ScaleEngine*>(se));
464 
465  sb->setBase(zoomBase().top(), zoomBase().bottom());
466  sb->moveSlider(zoomRect().top(), zoomRect().bottom());
467 
468  if (!sb->isVisibleTo(canvas()))
469  {
470  sb->show();
471  layout->setCanvasMargin(layout->canvasMargin(yScrollBarAxis)
472  + sb->extent(), yScrollBarAxis);
473  }
474  }
475  else
476  {
477  if (verticalScrollBar())
478  {
479  verticalScrollBar()->hide();
480  layout->setCanvasMargin(layout->canvasMargin(yScrollBarAxis)
481  - verticalScrollBar()->extent(), yScrollBarAxis);
482  }
483  }
484 
485  if (showHScrollBar && showVScrollBar)
486  {
487  if (d_cornerWidget == NULL)
488  {
489  d_cornerWidget = new QWidget(canvas());
490  d_cornerWidget->setPalette(plot()->palette());
491  }
492 
493  d_cornerWidget->show();
494  }
495  else
496  {
497  if (d_cornerWidget)
498  d_cornerWidget->hide();
499  }
500 
501  layoutScrollBars(((QwtPlotCanvas *)canvas())->contentsRect());
502 }
503 
504 void ScrollZoomer::layoutScrollBars(const QRect &rect)
505 {
506  int hPos = xAxis();
507 
509  hPos = oppositeAxis(hPos);
510 
511  int vPos = yAxis();
512 
514  vPos = oppositeAxis(vPos);
515 
516  ScrollBar *hScrollBar = horizontalScrollBar();
517  ScrollBar *vScrollBar = verticalScrollBar();
518 
519  const int hdim = hScrollBar ? hScrollBar->extent() : 0;
520  const int vdim = vScrollBar ? vScrollBar->extent() : 0;
521 
522  if (hScrollBar && hScrollBar->isVisible())
523  {
524  int x = rect.x();
525  int y = (hPos == QwtPlot::xTop)
526  ? rect.top() : rect.bottom() - hdim + 1;
527  int w = rect.width();
528 
529  if (vScrollBar && vScrollBar->isVisible())
530  {
531  if (vPos == QwtPlot::yLeft)
532  x += vdim;
533 
534  w -= vdim;
535  }
536 
537  hScrollBar->setGeometry(x, y, w, hdim);
538  }
539 
540  if (vScrollBar && vScrollBar->isVisible())
541  {
542  int pos = yAxis();
543 
545  pos = oppositeAxis(pos);
546 
547  int x = (vPos == QwtPlot::yLeft)
548  ? rect.left() : rect.right() - vdim + 1;
549  int y = rect.y();
550 
551  int h = rect.height();
552 
553  if (hScrollBar && hScrollBar->isVisible())
554  {
555  if (hPos == QwtPlot::xTop)
556  y += hdim;
557 
558  h -= hdim;
559  }
560 
561  vScrollBar->setGeometry(x, y, vdim, h);
562  }
563 
564  if (hScrollBar && hScrollBar->isVisible() &&
565  vScrollBar && vScrollBar->isVisible())
566  {
567  if (d_cornerWidget)
568  {
569  QRect cornerRect(
570  vScrollBar->pos().x(), hScrollBar->pos().y(),
571  vdim, hdim);
572  d_cornerWidget->setGeometry(cornerRect);
573  }
574  }
575 }
576 
577 void ScrollZoomer::scrollBarMoved(Qt::Orientation o, double min, double)
578 {
579  if (o == Qt::Horizontal)
580  move(min, zoomRect().top());
581  else
582  move(zoomRect().left(), min);
583 
584  emit zoomed(zoomRect());
585 }
586 
587 int ScrollZoomer::oppositeAxis(int axis) const
588 {
589  switch (axis)
590  {
591  case QwtPlot::xBottom:
592  return QwtPlot::xTop;
593  case QwtPlot::xTop:
594  return QwtPlot::xBottom;
595  case QwtPlot::yLeft:
596  return QwtPlot::yRight;
597  case QwtPlot::yRight:
598  return QwtPlot::yLeft;
599  default:
600  break;
601  }
602 
603  return axis;
604 }
ScrollBar * horizontalScrollBar() const
int extent() const
Definition: scrollbar.cpp:218
void setHScrollBarPosition(ScrollBarPosition)
QWidget * d_cornerWidget
Definition: scrollzoomer.h:109
virtual void layoutScrollBars(const QRect &)
virtual void updateScrollBars()
ScrollBar * scrollBar
ScrollData * d_vScrollData
Definition: scrollzoomer.h:112
ScrollBarPosition hScrollBarPosition() const
QWidget * cornerWidget() const
virtual bool eventFilter(QObject *, QEvent *)
void setVScrollBarPosition(ScrollBarPosition)
ScrollBar * verticalScrollBar() const
Q3ScrollView::ScrollBarMode hScrollBarMode() const
void setInverted(bool)
Definition: scrollbar.cpp:68
ScrollZoomer(QwtPlotCanvas *)
virtual void rescale()
bool needScrollBar(Qt::Orientation) const
void setVScrollBarMode(Q3ScrollView::ScrollBarMode)
Q3ScrollView::ScrollBarMode mode
virtual ~ScrollZoomer()
ScrollBarPosition vScrollBarPosition() const
virtual void setCornerWidget(QWidget *)
ScrollData * d_hScrollData
Definition: scrollzoomer.h:111
int oppositeAxis(int) const
void setLogScale(bool l)
Definition: scrollbar.cpp:243
virtual void move(double x, double y)
LogPlotZoomer(QwtPlotCanvas *canvas)
virtual ScrollBar * scrollBar(Qt::Orientation)
virtual void moveSlider(double min, double max)
Definition: scrollbar.cpp:99
void setHScrollBarMode(Q3ScrollView::ScrollBarMode)
Q3ScrollView::ScrollBarMode vScrollBarMode() const
if(!yymsg) yymsg
ScrollZoomer::ScrollBarPosition position
virtual QwtText trackerText(const QwtDoublePoint &pos) const
virtual void setBase(double min, double max)
Definition: scrollbar.cpp:82
void scrollBarMoved(Qt::Orientation o, double min, double max)
#define min(a, b)
Definition: f2c.h:175