| 1 |
#include "interactiveview.hpp" |
| 2 |
|
| 3 |
#define VIEW_CENTER viewport()->rect().center() |
| 4 |
#define VIEW_WIDTH viewport()->rect().width() |
| 5 |
#define VIEW_HEIGHT viewport()->rect().height() |
| 6 |
|
| 7 |
InteractiveView::InteractiveView(QWidget * parent) : |
| 8 |
QGraphicsView(parent) |
| 9 |
{ |
| 10 |
// setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 11 |
// setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 12 |
|
| 13 |
setMaxSize(); |
| 14 |
centerOn(0, 0); |
| 15 |
|
| 16 |
zoomDelta = 0.1; |
| 17 |
panSpeed = 4; |
| 18 |
_doMousePanning = false; |
| 19 |
_doKeyZoom = false; |
| 20 |
_scale = 1.0; |
| 21 |
|
| 22 |
panButton = Qt::MiddleButton; |
| 23 |
zoomKey = Qt::Key_Z; |
| 24 |
|
| 25 |
/* QGraphicsView turns it on for some reason, |
| 26 |
* switching it back off to let BrowserTab handle it -krkk */ |
| 27 |
setAcceptDrops(false); |
| 28 |
} |
| 29 |
|
| 30 |
qreal InteractiveView::getScale() const |
| 31 |
{ |
| 32 |
return _scale; |
| 33 |
} |
| 34 |
|
| 35 |
void InteractiveView::keyPressEvent(QKeyEvent * event) |
| 36 |
{ |
| 37 |
qint32 key = event->key(); |
| 38 |
|
| 39 |
if(key == zoomKey){ |
| 40 |
_doKeyZoom = true; |
| 41 |
} |
| 42 |
|
| 43 |
if (_doKeyZoom){ |
| 44 |
if (key == Qt::Key_Up) |
| 45 |
zoomIn(); |
| 46 |
|
| 47 |
else if (key == Qt::Key_Down) |
| 48 |
zoomOut(); |
| 49 |
} |
| 50 |
|
| 51 |
else{ |
| 52 |
QGraphicsView::keyPressEvent(event); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
void InteractiveView::keyReleaseEvent(QKeyEvent * event) |
| 57 |
{ |
| 58 |
if (event->key() == zoomKey){ |
| 59 |
_doKeyZoom = false; |
| 60 |
} |
| 61 |
|
| 62 |
QGraphicsView::keyReleaseEvent(event); |
| 63 |
} |
| 64 |
|
| 65 |
void InteractiveView::mouseMoveEvent(QMouseEvent * event) |
| 66 |
{ |
| 67 |
if (_doMousePanning){ |
| 68 |
QPointF mouseDelta = mapToScene(event->pos()) - mapToScene(_lastMousePos); |
| 69 |
pan(mouseDelta); |
| 70 |
} |
| 71 |
|
| 72 |
QGraphicsView::mouseMoveEvent(event); |
| 73 |
_lastMousePos = event->pos(); |
| 74 |
} |
| 75 |
|
| 76 |
void InteractiveView::mousePressEvent(QMouseEvent * event) |
| 77 |
{ |
| 78 |
if (event->button() == panButton){ |
| 79 |
_lastMousePos = event->pos(); |
| 80 |
_doMousePanning = true; |
| 81 |
} |
| 82 |
|
| 83 |
QGraphicsView::mousePressEvent(event); |
| 84 |
} |
| 85 |
|
| 86 |
void InteractiveView::mouseReleaseEvent(QMouseEvent * event) |
| 87 |
{ |
| 88 |
if (event->button() == panButton){ |
| 89 |
_doMousePanning = false; |
| 90 |
} |
| 91 |
|
| 92 |
QGraphicsView::mouseReleaseEvent(event); |
| 93 |
} |
| 94 |
|
| 95 |
void InteractiveView::wheelEvent(QWheelEvent *event) |
| 96 |
{ |
| 97 |
QPoint scrollAmount = event->angleDelta(); |
| 98 |
|
| 99 |
// Apply zoom. |
| 100 |
scrollAmount.y() > 0 ? zoomIn() : zoomOut(); |
| 101 |
} |
| 102 |
|
| 103 |
void InteractiveView::setMaxSize() |
| 104 |
{ |
| 105 |
setSceneRect(INT_MIN/2, INT_MIN/2, INT_MAX, INT_MAX); |
| 106 |
} |
| 107 |
|
| 108 |
void InteractiveView::zoom(float scaleFactor) |
| 109 |
{ |
| 110 |
scale(scaleFactor, scaleFactor); |
| 111 |
_scale *= scaleFactor; |
| 112 |
} |
| 113 |
|
| 114 |
void InteractiveView::zoomIn() |
| 115 |
{ |
| 116 |
zoom(1 + zoomDelta); |
| 117 |
} |
| 118 |
|
| 119 |
void InteractiveView::zoomOut() |
| 120 |
{ |
| 121 |
zoom (1 - zoomDelta); |
| 122 |
} |
| 123 |
|
| 124 |
void InteractiveView::pan(QPointF delta) |
| 125 |
{ |
| 126 |
// Scale the pan amount by the current zoom. |
| 127 |
delta *= _scale; |
| 128 |
delta *= panSpeed; |
| 129 |
|
| 130 |
// Have panning be anchored from the mouse. |
| 131 |
setTransformationAnchor(QGraphicsView::AnchorUnderMouse); |
| 132 |
QPoint newCenter(VIEW_WIDTH / 2 - delta.x(), VIEW_HEIGHT / 2 - delta.y()); |
| 133 |
centerOn(mapToScene(newCenter)); |
| 134 |
|
| 135 |
// For zooming to anchor from the view center. |
| 136 |
setTransformationAnchor(QGraphicsView::AnchorViewCenter); |
| 137 |
} |