1 | /*
|
---|
2 | * QSeisScrollzoomer.cpp
|
---|
3 | *
|
---|
4 | * taken from "realtime"-example
|
---|
5 | *
|
---|
6 | */
|
---|
7 |
|
---|
8 | // include config.h
|
---|
9 | #ifdef HAVE_CONFIG_H
|
---|
10 | #include <config.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include <qevent.h>
|
---|
14 | #include <qwt_plot_canvas.h>
|
---|
15 | #include <qwt_plot_layout.h>
|
---|
16 | #include <qwt_scale_engine.h>
|
---|
17 | #include <qwt_scale_widget.h>
|
---|
18 | #include "ScrollZoomer.hpp"
|
---|
19 |
|
---|
20 | #include "UIElements/Views/Qt4/Plotting/QScrollZoomer/ScrollBar.hpp"
|
---|
21 |
|
---|
22 | // have this after(!) all Qt includes
|
---|
23 | #include "CodePatterns/MemDebug.hpp"
|
---|
24 |
|
---|
25 | class ScrollData
|
---|
26 | {
|
---|
27 | public:
|
---|
28 | ScrollData():
|
---|
29 | scrollBar(NULL),
|
---|
30 | position(ScrollZoomer::OppositeToScale),
|
---|
31 | #if QT_VERSION < 0x040000
|
---|
32 | mode(QScrollView::Auto)
|
---|
33 | #else
|
---|
34 | mode(Qt::ScrollBarAsNeeded)
|
---|
35 | #endif
|
---|
36 | {
|
---|
37 | }
|
---|
38 |
|
---|
39 | ~ScrollData()
|
---|
40 | {
|
---|
41 | delete scrollBar;
|
---|
42 | }
|
---|
43 |
|
---|
44 | ScrollBar *scrollBar;
|
---|
45 | ScrollZoomer::ScrollBarPosition position;
|
---|
46 | #if QT_VERSION < 0x040000
|
---|
47 | QScrollView::ScrollBarMode mode;
|
---|
48 | #else
|
---|
49 | Qt::ScrollBarPolicy mode;
|
---|
50 | #endif
|
---|
51 | };
|
---|
52 |
|
---|
53 | ScrollZoomer::ScrollZoomer(QwtPlotCanvas *canvas):
|
---|
54 | QwtPlotZoomer(canvas),
|
---|
55 | d_cornerWidget(NULL),
|
---|
56 | d_hScrollData(NULL),
|
---|
57 | d_vScrollData(NULL),
|
---|
58 | d_inZoom(false),
|
---|
59 | d_alignCanvasToScales(false)
|
---|
60 | {
|
---|
61 | if ( !canvas )
|
---|
62 | return;
|
---|
63 |
|
---|
64 | d_hScrollData = new ScrollData;
|
---|
65 | d_vScrollData = new ScrollData;
|
---|
66 |
|
---|
67 | //disable zoom reset on middle mouse button
|
---|
68 | setMousePattern(QwtEventPattern::MouseSelect3, Qt::NoButton);
|
---|
69 | }
|
---|
70 |
|
---|
71 | ScrollZoomer::~ScrollZoomer()
|
---|
72 | {
|
---|
73 | delete d_cornerWidget;
|
---|
74 | delete d_vScrollData;
|
---|
75 | delete d_hScrollData;
|
---|
76 | }
|
---|
77 |
|
---|
78 | void ScrollZoomer::rescale()
|
---|
79 | {
|
---|
80 | QwtScaleWidget *xScale = plot()->axisWidget(xAxis());
|
---|
81 | QwtScaleWidget *yScale = plot()->axisWidget(yAxis());
|
---|
82 |
|
---|
83 | if ( zoomRectIndex() <= 0 )
|
---|
84 | {
|
---|
85 | if ( d_inZoom )
|
---|
86 | {
|
---|
87 | xScale->setMinBorderDist(0, 0);
|
---|
88 | yScale->setMinBorderDist(0, 0);
|
---|
89 |
|
---|
90 | QwtPlotLayout *layout = plot()->plotLayout();
|
---|
91 | layout->setAlignCanvasToScales(d_alignCanvasToScales);
|
---|
92 |
|
---|
93 | d_inZoom = false;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | if ( !d_inZoom )
|
---|
99 | {
|
---|
100 | /*
|
---|
101 | We set a minimum border distance.
|
---|
102 | Otherwise the canvas size changes when scrolling,
|
---|
103 | between situations where the major ticks are at
|
---|
104 | the canvas borders (requiring extra space for the label)
|
---|
105 | and situations where all labels can be painted below/top
|
---|
106 | or left/right of the canvas.
|
---|
107 | */
|
---|
108 | int start, end;
|
---|
109 |
|
---|
110 | xScale->getBorderDistHint(start, end);
|
---|
111 | xScale->setMinBorderDist(start, end);
|
---|
112 |
|
---|
113 | yScale->getBorderDistHint(start, end);
|
---|
114 | yScale->setMinBorderDist(start, end);
|
---|
115 |
|
---|
116 | QwtPlotLayout *layout = plot()->plotLayout();
|
---|
117 | d_alignCanvasToScales = layout->alignCanvasToScales();
|
---|
118 | layout->setAlignCanvasToScales(false);
|
---|
119 |
|
---|
120 | d_inZoom = true;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | QwtPlotZoomer::rescale();
|
---|
125 | updateScrollBars();
|
---|
126 | }
|
---|
127 |
|
---|
128 | ScrollBar *ScrollZoomer::scrollBar(Qt::Orientation o)
|
---|
129 | {
|
---|
130 | ScrollBar *&sb = (o == Qt::Vertical)
|
---|
131 | ? d_vScrollData->scrollBar : d_hScrollData->scrollBar;
|
---|
132 |
|
---|
133 | if ( sb == NULL )
|
---|
134 | {
|
---|
135 | sb = new ScrollBar(o, canvas());
|
---|
136 | sb->hide();
|
---|
137 | connect(sb,
|
---|
138 | SIGNAL(valueChanged(Qt::Orientation, double, double)),
|
---|
139 | SLOT(scrollBarMoved(Qt::Orientation, double, double)));
|
---|
140 | }
|
---|
141 | return sb;
|
---|
142 | }
|
---|
143 |
|
---|
144 | ScrollBar *ScrollZoomer::horizontalScrollBar() const
|
---|
145 | {
|
---|
146 | return d_hScrollData->scrollBar;
|
---|
147 | }
|
---|
148 |
|
---|
149 | ScrollBar *ScrollZoomer::verticalScrollBar() const
|
---|
150 | {
|
---|
151 | return d_vScrollData->scrollBar;
|
---|
152 | }
|
---|
153 |
|
---|
154 | #if QT_VERSION < 0x040000
|
---|
155 | void ScrollZoomer::setHScrollBarMode(QScrollView::ScrollBarMode mode)
|
---|
156 | #else
|
---|
157 | void ScrollZoomer::setHScrollBarMode(Qt::ScrollBarPolicy mode)
|
---|
158 | #endif
|
---|
159 | {
|
---|
160 | if ( hScrollBarMode() != mode )
|
---|
161 | {
|
---|
162 | d_hScrollData->mode = mode;
|
---|
163 | updateScrollBars();
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | #if QT_VERSION < 0x040000
|
---|
168 | void ScrollZoomer::setVScrollBarMode(QScrollView::ScrollBarMode mode)
|
---|
169 | #else
|
---|
170 | void ScrollZoomer::setVScrollBarMode(Qt::ScrollBarPolicy mode)
|
---|
171 | #endif
|
---|
172 | {
|
---|
173 | if ( vScrollBarMode() != mode )
|
---|
174 | {
|
---|
175 | d_vScrollData->mode = mode;
|
---|
176 | updateScrollBars();
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | #if QT_VERSION < 0x040000
|
---|
181 | QScrollView::ScrollBarMode ScrollZoomer::hScrollBarMode() const
|
---|
182 | #else
|
---|
183 | Qt::ScrollBarPolicy ScrollZoomer::hScrollBarMode() const
|
---|
184 | #endif
|
---|
185 | {
|
---|
186 | return d_hScrollData->mode;
|
---|
187 | }
|
---|
188 |
|
---|
189 | #if QT_VERSION < 0x040000
|
---|
190 | QScrollView::ScrollBarMode ScrollZoomer::vScrollBarMode() const
|
---|
191 | #else
|
---|
192 | Qt::ScrollBarPolicy ScrollZoomer::vScrollBarMode() const
|
---|
193 | #endif
|
---|
194 | {
|
---|
195 | return d_vScrollData->mode;
|
---|
196 | }
|
---|
197 |
|
---|
198 | void ScrollZoomer::setHScrollBarPosition(ScrollBarPosition pos)
|
---|
199 | {
|
---|
200 | if ( d_hScrollData->position != pos )
|
---|
201 | {
|
---|
202 | d_hScrollData->position = pos;
|
---|
203 | updateScrollBars();
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | void ScrollZoomer::setVScrollBarPosition(ScrollBarPosition pos)
|
---|
208 | {
|
---|
209 | if ( d_vScrollData->position != pos )
|
---|
210 | {
|
---|
211 | d_vScrollData->position = pos;
|
---|
212 | updateScrollBars();
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | ScrollZoomer::ScrollBarPosition ScrollZoomer::hScrollBarPosition() const
|
---|
217 | {
|
---|
218 | return d_hScrollData->position;
|
---|
219 | }
|
---|
220 |
|
---|
221 | ScrollZoomer::ScrollBarPosition ScrollZoomer::vScrollBarPosition() const
|
---|
222 | {
|
---|
223 | return d_vScrollData->position;
|
---|
224 | }
|
---|
225 |
|
---|
226 | void ScrollZoomer::setCornerWidget(QWidget *w)
|
---|
227 | {
|
---|
228 | if ( w != d_cornerWidget )
|
---|
229 | {
|
---|
230 | if ( canvas() )
|
---|
231 | {
|
---|
232 | delete d_cornerWidget;
|
---|
233 | d_cornerWidget = w;
|
---|
234 | if ( d_cornerWidget->parent() != canvas() )
|
---|
235 | {
|
---|
236 | #if QT_VERSION < 0x040000
|
---|
237 | d_cornerWidget->reparent(canvas(), QPoint(0, 0));
|
---|
238 | #else
|
---|
239 | d_cornerWidget->setParent(canvas());
|
---|
240 | #endif
|
---|
241 | }
|
---|
242 |
|
---|
243 | updateScrollBars();
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | QWidget *ScrollZoomer::cornerWidget() const
|
---|
249 | {
|
---|
250 | return d_cornerWidget;
|
---|
251 | }
|
---|
252 |
|
---|
253 | bool ScrollZoomer::eventFilter(QObject *o, QEvent *e)
|
---|
254 | {
|
---|
255 | if ( o == canvas() )
|
---|
256 | {
|
---|
257 | switch(e->type())
|
---|
258 | {
|
---|
259 | case QEvent::Resize:
|
---|
260 | {
|
---|
261 | const int fw = ((QwtPlotCanvas *)canvas())->frameWidth();
|
---|
262 |
|
---|
263 | QRect rect;
|
---|
264 | rect.setSize(((QResizeEvent *)e)->size());
|
---|
265 | rect.setRect(rect.x() + fw, rect.y() + fw,
|
---|
266 | rect.width() - 2 * fw, rect.height() - 2 * fw);
|
---|
267 |
|
---|
268 | layoutScrollBars(rect);
|
---|
269 | break;
|
---|
270 | }
|
---|
271 | case QEvent::ChildRemoved:
|
---|
272 | {
|
---|
273 | const QObject *child = ((QChildEvent *)e)->child();
|
---|
274 | if ( child == d_cornerWidget )
|
---|
275 | d_cornerWidget = NULL;
|
---|
276 | else if ( child == d_hScrollData->scrollBar )
|
---|
277 | d_hScrollData->scrollBar = NULL;
|
---|
278 | else if ( child == d_vScrollData->scrollBar )
|
---|
279 | d_vScrollData->scrollBar = NULL;
|
---|
280 | break;
|
---|
281 | }
|
---|
282 | default:
|
---|
283 | break;
|
---|
284 | }
|
---|
285 | }
|
---|
286 | return QwtPlotZoomer::eventFilter(o, e);
|
---|
287 | }
|
---|
288 |
|
---|
289 | bool ScrollZoomer::needScrollBar(Qt::Orientation o) const
|
---|
290 | {
|
---|
291 | #if QT_VERSION < 0x040000
|
---|
292 | QScrollView::ScrollBarMode mode;
|
---|
293 | #else
|
---|
294 | Qt::ScrollBarPolicy mode;
|
---|
295 | #endif
|
---|
296 | double zoomMin, zoomMax, baseMin, baseMax;
|
---|
297 |
|
---|
298 | if ( o == Qt::Horizontal )
|
---|
299 | {
|
---|
300 | mode = d_hScrollData->mode;
|
---|
301 | baseMin = zoomBase().left();
|
---|
302 | baseMax = zoomBase().right();
|
---|
303 | zoomMin = zoomRect().left();
|
---|
304 | zoomMax = zoomRect().right();
|
---|
305 | }
|
---|
306 | else
|
---|
307 | {
|
---|
308 | mode = d_vScrollData->mode;
|
---|
309 | baseMin = zoomBase().top();
|
---|
310 | baseMax = zoomBase().bottom();
|
---|
311 | zoomMin = zoomRect().top();
|
---|
312 | zoomMax = zoomRect().bottom();
|
---|
313 | }
|
---|
314 |
|
---|
315 | bool needed = false;
|
---|
316 | switch(mode)
|
---|
317 | {
|
---|
318 | #if QT_VERSION < 0x040000
|
---|
319 | case QScrollView::AlwaysOn:
|
---|
320 | #else
|
---|
321 | case Qt::ScrollBarAlwaysOn:
|
---|
322 | #endif
|
---|
323 | needed = true;
|
---|
324 | break;
|
---|
325 | #if QT_VERSION < 0x040000
|
---|
326 | case QScrollView::AlwaysOff:
|
---|
327 | #else
|
---|
328 | case Qt::ScrollBarAlwaysOff:
|
---|
329 | #endif
|
---|
330 | needed = false;
|
---|
331 | break;
|
---|
332 | default:
|
---|
333 | {
|
---|
334 | if ( baseMin < zoomMin || baseMax > zoomMax )
|
---|
335 | needed = true;
|
---|
336 | break;
|
---|
337 | }
|
---|
338 | }
|
---|
339 | return needed;
|
---|
340 | }
|
---|
341 |
|
---|
342 | void ScrollZoomer::updateScrollBars()
|
---|
343 | {
|
---|
344 | if ( !canvas() )
|
---|
345 | return;
|
---|
346 |
|
---|
347 | const int xAxis = QwtPlotZoomer::xAxis();
|
---|
348 | const int yAxis = QwtPlotZoomer::yAxis();
|
---|
349 |
|
---|
350 | int xScrollBarAxis = xAxis;
|
---|
351 | if ( hScrollBarPosition() == OppositeToScale )
|
---|
352 | xScrollBarAxis = oppositeAxis(xScrollBarAxis);
|
---|
353 |
|
---|
354 | int yScrollBarAxis = yAxis;
|
---|
355 | if ( vScrollBarPosition() == OppositeToScale )
|
---|
356 | yScrollBarAxis = oppositeAxis(yScrollBarAxis);
|
---|
357 |
|
---|
358 |
|
---|
359 | QwtPlotLayout *layout = plot()->plotLayout();
|
---|
360 |
|
---|
361 | bool showHScrollBar = needScrollBar(Qt::Horizontal);
|
---|
362 | if ( showHScrollBar )
|
---|
363 | {
|
---|
364 | ScrollBar *sb = scrollBar(Qt::Horizontal);
|
---|
365 |
|
---|
366 | sb->setPalette(plot()->palette());
|
---|
367 |
|
---|
368 | const QwtScaleDiv *sd = plot()->axisScaleDiv(xAxis);
|
---|
369 | sb->setInverted(sd->lowerBound() > sd->upperBound() );
|
---|
370 |
|
---|
371 | sb->setBase(zoomBase().left(), zoomBase().right());
|
---|
372 | sb->moveSlider(zoomRect().left(), zoomRect().right());
|
---|
373 |
|
---|
374 | if ( !sb->isVisibleTo(canvas()) )
|
---|
375 | {
|
---|
376 | sb->show();
|
---|
377 | layout->setCanvasMargin(layout->canvasMargin(xScrollBarAxis)
|
---|
378 | + sb->extent(), xScrollBarAxis);
|
---|
379 | }
|
---|
380 | }
|
---|
381 | else
|
---|
382 | {
|
---|
383 | if ( horizontalScrollBar() )
|
---|
384 | {
|
---|
385 | horizontalScrollBar()->hide();
|
---|
386 | layout->setCanvasMargin(layout->canvasMargin(xScrollBarAxis)
|
---|
387 | - horizontalScrollBar()->extent(), xScrollBarAxis);
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | bool showVScrollBar = needScrollBar(Qt::Vertical);
|
---|
392 | if ( showVScrollBar )
|
---|
393 | {
|
---|
394 | ScrollBar *sb = scrollBar(Qt::Vertical);
|
---|
395 |
|
---|
396 | sb->setPalette(plot()->palette());
|
---|
397 |
|
---|
398 | const QwtScaleDiv *sd = plot()->axisScaleDiv(yAxis);
|
---|
399 | sb->setInverted(sd->lowerBound() < sd->upperBound() );
|
---|
400 |
|
---|
401 | sb->setBase(zoomBase().top(), zoomBase().bottom());
|
---|
402 | sb->moveSlider(zoomRect().top(), zoomRect().bottom());
|
---|
403 |
|
---|
404 | if ( !sb->isVisibleTo(canvas()) )
|
---|
405 | {
|
---|
406 | sb->show();
|
---|
407 | layout->setCanvasMargin(layout->canvasMargin(yScrollBarAxis)
|
---|
408 | + sb->extent(), yScrollBarAxis);
|
---|
409 | }
|
---|
410 | }
|
---|
411 | else
|
---|
412 | {
|
---|
413 | if ( verticalScrollBar() )
|
---|
414 | {
|
---|
415 | verticalScrollBar()->hide();
|
---|
416 | layout->setCanvasMargin(layout->canvasMargin(yScrollBarAxis)
|
---|
417 | - verticalScrollBar()->extent(), yScrollBarAxis);
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | if ( showHScrollBar && showVScrollBar )
|
---|
422 | {
|
---|
423 | if ( d_cornerWidget == NULL )
|
---|
424 | {
|
---|
425 | d_cornerWidget = new QWidget(canvas());
|
---|
426 | #if QT_VERSION >= 0x040100
|
---|
427 | d_cornerWidget->setAutoFillBackground(true);
|
---|
428 | #endif
|
---|
429 | d_cornerWidget->setPalette(plot()->palette());
|
---|
430 | }
|
---|
431 | d_cornerWidget->show();
|
---|
432 | }
|
---|
433 | else
|
---|
434 | {
|
---|
435 | if ( d_cornerWidget )
|
---|
436 | d_cornerWidget->hide();
|
---|
437 | }
|
---|
438 |
|
---|
439 | layoutScrollBars(((QwtPlotCanvas *)canvas())->contentsRect());
|
---|
440 | plot()->updateLayout();
|
---|
441 | }
|
---|
442 |
|
---|
443 | void ScrollZoomer::layoutScrollBars(const QRect &rect)
|
---|
444 | {
|
---|
445 | int hPos = xAxis();
|
---|
446 | if ( hScrollBarPosition() == OppositeToScale )
|
---|
447 | hPos = oppositeAxis(hPos);
|
---|
448 |
|
---|
449 | int vPos = yAxis();
|
---|
450 | if ( vScrollBarPosition() == OppositeToScale )
|
---|
451 | vPos = oppositeAxis(vPos);
|
---|
452 |
|
---|
453 | ScrollBar *hScrollBar = horizontalScrollBar();
|
---|
454 | ScrollBar *vScrollBar = verticalScrollBar();
|
---|
455 |
|
---|
456 | const int hdim = hScrollBar ? hScrollBar->extent() : 0;
|
---|
457 | const int vdim = vScrollBar ? vScrollBar->extent() : 0;
|
---|
458 |
|
---|
459 | if ( hScrollBar && hScrollBar->isVisible() )
|
---|
460 | {
|
---|
461 | int x = rect.x();
|
---|
462 | int y = (hPos == QwtPlot::xTop)
|
---|
463 | ? rect.top() : rect.bottom() - hdim + 1;
|
---|
464 | int w = rect.width();
|
---|
465 |
|
---|
466 | if ( vScrollBar && vScrollBar->isVisible() )
|
---|
467 | {
|
---|
468 | if ( vPos == QwtPlot::yLeft )
|
---|
469 | x += vdim;
|
---|
470 | w -= vdim;
|
---|
471 | }
|
---|
472 |
|
---|
473 | hScrollBar->setGeometry(x, y, w, hdim);
|
---|
474 | }
|
---|
475 | if ( vScrollBar && vScrollBar->isVisible() )
|
---|
476 | {
|
---|
477 | int pos = yAxis();
|
---|
478 | if ( vScrollBarPosition() == OppositeToScale )
|
---|
479 | pos = oppositeAxis(pos);
|
---|
480 |
|
---|
481 | int x = (vPos == QwtPlot::yLeft)
|
---|
482 | ? rect.left() : rect.right() - vdim + 1;
|
---|
483 | int y = rect.y();
|
---|
484 |
|
---|
485 | int h = rect.height();
|
---|
486 |
|
---|
487 | if ( hScrollBar && hScrollBar->isVisible() )
|
---|
488 | {
|
---|
489 | if ( hPos == QwtPlot::xTop )
|
---|
490 | y += hdim;
|
---|
491 |
|
---|
492 | h -= hdim;
|
---|
493 | }
|
---|
494 |
|
---|
495 | vScrollBar->setGeometry(x, y, vdim, h);
|
---|
496 | }
|
---|
497 | if ( hScrollBar && hScrollBar->isVisible() &&
|
---|
498 | vScrollBar && vScrollBar->isVisible() )
|
---|
499 | {
|
---|
500 | if ( d_cornerWidget )
|
---|
501 | {
|
---|
502 | QRect cornerRect(
|
---|
503 | vScrollBar->pos().x(), hScrollBar->pos().y(),
|
---|
504 | vdim, hdim);
|
---|
505 | d_cornerWidget->setGeometry(cornerRect);
|
---|
506 | }
|
---|
507 | }
|
---|
508 | }
|
---|
509 |
|
---|
510 | void ScrollZoomer::scrollBarMoved(Qt::Orientation o, double min, double)
|
---|
511 | {
|
---|
512 | if ( o == Qt::Horizontal )
|
---|
513 | move(min, zoomRect().top());
|
---|
514 | else
|
---|
515 | move(zoomRect().left(), min);
|
---|
516 |
|
---|
517 | emit zoomed(zoomRect());
|
---|
518 | }
|
---|
519 |
|
---|
520 | int ScrollZoomer::oppositeAxis(int axis) const
|
---|
521 | {
|
---|
522 | switch(axis)
|
---|
523 | {
|
---|
524 | case QwtPlot::xBottom:
|
---|
525 | return QwtPlot::xTop;
|
---|
526 | case QwtPlot::xTop:
|
---|
527 | return QwtPlot::xBottom;
|
---|
528 | case QwtPlot::yLeft:
|
---|
529 | return QwtPlot::yRight;
|
---|
530 | case QwtPlot::yRight:
|
---|
531 | return QwtPlot::yLeft;
|
---|
532 | default:
|
---|
533 | break;
|
---|
534 | }
|
---|
535 |
|
---|
536 | return axis;
|
---|
537 | }
|
---|