Changeset View
Changeset View
Standalone View
Standalone View
src/part/pannerview.cpp
Show All 18 Lines | |||||
19 | /* This file was callgraphview.cpp, part of KCachegrind. | 19 | /* This file was callgraphview.cpp, part of KCachegrind. | ||
20 | Copyright (C) 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de> | 20 | Copyright (C) 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de> | ||
21 | 21 | | |||
22 | KCachegrind is free software; you can redistribute it and/or | 22 | KCachegrind is free software; you can redistribute it and/or | ||
23 | modify it under the terms of the GNU General Public | 23 | modify it under the terms of the GNU General Public | ||
24 | License as published by the Free Software Foundation, version 2. | 24 | License as published by the Free Software Foundation, version 2. | ||
25 | */ | 25 | */ | ||
26 | 26 | | |||
27 | | ||||
28 | /* | 27 | /* | ||
29 | * Callgraph View | 28 | * Callgraph View | ||
30 | */ | 29 | */ | ||
31 | 30 | | |||
32 | #include "dotgraphview.h" | | |||
33 | #include "pannerview.h" | 31 | #include "pannerview.h" | ||
32 | #include "dotgraphview.h" | ||||
34 | #include "kgraphviewerlib_debug.h" | 33 | #include "kgraphviewerlib_debug.h" | ||
35 | 34 | | |||
36 | #include <stdlib.h> | | |||
37 | #include <math.h> | | |||
38 | #include <iostream> | 35 | #include <iostream> | ||
36 | #include <math.h> | ||||
37 | #include <stdlib.h> | ||||
39 | 38 | | |||
39 | #include <QDebug> | ||||
40 | #include <QGraphicsScene> | 40 | #include <QGraphicsScene> | ||
41 | #include <QPainter> | | |||
42 | #include <QMouseEvent> | 41 | #include <QMouseEvent> | ||
43 | #include <QDebug> | 42 | #include <QPainter> | ||
44 | 43 | | |||
45 | #include <klocalizedstring.h> | 44 | #include <klocalizedstring.h> | ||
46 | 45 | | |||
47 | namespace KGraphViewer | 46 | namespace KGraphViewer | ||
48 | { | 47 | { | ||
49 | // | 48 | // | ||
50 | // PannerView | 49 | // PannerView | ||
51 | // | 50 | // | ||
52 | PannerView::PannerView(DotGraphView * parent) | 51 | PannerView::PannerView(DotGraphView *parent) | ||
53 | : QGraphicsView(parent), m_drawContents(true), m_parent(parent) | 52 | : QGraphicsView(parent) | ||
53 | , m_drawContents(true) | ||||
54 | , m_parent(parent) | ||||
54 | { | 55 | { | ||
55 | m_movingZoomRect = false; | 56 | m_movingZoomRect = false; | ||
56 | 57 | | |||
57 | // why doesn't this avoid flicker ? | 58 | // why doesn't this avoid flicker ? | ||
58 | // viewport()->setBackgroundMode(Qt::NoBackground); | 59 | // viewport()->setBackgroundMode(Qt::NoBackground); | ||
59 | 60 | | |||
60 | // if there are ever graphic glitches to be found, remove this again | 61 | // if there are ever graphic glitches to be found, remove this again | ||
61 | setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing | QGraphicsView::DontClipPainter | | 62 | setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing | QGraphicsView::DontClipPainter | QGraphicsView::DontSavePainterState); | ||
62 | QGraphicsView::DontSavePainterState); | | |||
63 | 63 | | |||
64 | setToolTip(i18n("View of the complete graph. Click and drag to move the visible part.")); | 64 | setToolTip(i18n("View of the complete graph. Click and drag to move the visible part.")); | ||
65 | setWhatsThis(i18n("<h1>View of the Complete Graph</h1>" | 65 | setWhatsThis( | ||
66 | i18n("<h1>View of the Complete Graph</h1>" | ||||
66 | "<p>Single clicking somewhere without the red square will move the center of the " | 67 | "<p>Single clicking somewhere without the red square will move the center of the " | ||
67 | "view to where the mouse was clicked.</p><p>Clicking and dragging within the red square " | 68 | "view to where the mouse was clicked.</p><p>Clicking and dragging within the red square " | ||
68 | "will cause the view to follow the movement.</p>")); | 69 | "will cause the view to follow the movement.</p>")); | ||
69 | } | 70 | } | ||
70 | 71 | | |||
71 | void PannerView::setZoomRect(QRectF r) | 72 | void PannerView::setZoomRect(QRectF r) | ||
72 | { | 73 | { | ||
73 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "PannerView::setZoomRect " << r; | 74 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "PannerView::setZoomRect " << r; | ||
74 | if (r == m_zoomRect) { | 75 | if (r == m_zoomRect) { | ||
75 | return; | 76 | return; | ||
76 | } | 77 | } | ||
77 | scene()->invalidate(m_zoomRect, QGraphicsScene::ForegroundLayer); | 78 | scene()->invalidate(m_zoomRect, QGraphicsScene::ForegroundLayer); | ||
78 | // get new zoom rect | 79 | // get new zoom rect | ||
79 | m_zoomRect = r; | 80 | m_zoomRect = r; | ||
80 | qreal q = mapToScene(15,0).x(); | 81 | qreal q = mapToScene(15, 0).x(); | ||
81 | if (!m_zoomRect.isValid() || m_zoomRect.width() < q || m_zoomRect.height() < q) | 82 | if (!m_zoomRect.isValid() || m_zoomRect.width() < q || m_zoomRect.height() < q) { | ||
82 | { | | |||
83 | double factor = ((double)m_zoomRect.width())/m_zoomRect.height(); | 83 | double factor = ((double)m_zoomRect.width()) / m_zoomRect.height(); | ||
84 | qreal newWidth, newHeight; | 84 | qreal newWidth, newHeight; | ||
85 | if (factor < 1.0) | 85 | if (factor < 1.0) { | ||
86 | { | | |||
87 | newWidth = q; | 86 | newWidth = q; | ||
88 | newHeight = newWidth/factor; | 87 | newHeight = newWidth / factor; | ||
89 | } | 88 | } else { | ||
90 | else | | |||
91 | { | | |||
92 | newHeight = q; | 89 | newHeight = q; | ||
93 | newWidth = newHeight*factor; | 90 | newWidth = newHeight * factor; | ||
94 | } | 91 | } | ||
95 | qreal newXPos = m_zoomRect.x() + (m_zoomRect.width() - newWidth)/2; | 92 | qreal newXPos = m_zoomRect.x() + (m_zoomRect.width() - newWidth) / 2; | ||
96 | qreal newYPos = m_zoomRect.y() + (m_zoomRect.height() - newHeight)/2; | 93 | qreal newYPos = m_zoomRect.y() + (m_zoomRect.height() - newHeight) / 2; | ||
97 | m_zoomRect.setX(newXPos); | 94 | m_zoomRect.setX(newXPos); | ||
98 | m_zoomRect.setY(newYPos); | 95 | m_zoomRect.setY(newYPos); | ||
99 | m_zoomRect.setWidth(newWidth); | 96 | m_zoomRect.setWidth(newWidth); | ||
100 | m_zoomRect.setHeight(newHeight); | 97 | m_zoomRect.setHeight(newHeight); | ||
101 | } | 98 | } | ||
102 | scene()->invalidate(m_zoomRect, QGraphicsScene::ForegroundLayer); | 99 | scene()->invalidate(m_zoomRect, QGraphicsScene::ForegroundLayer); | ||
103 | } | 100 | } | ||
104 | 101 | | |||
105 | void PannerView::moveZoomRectTo(const QPointF& newPos, bool notify) | 102 | void PannerView::moveZoomRectTo(const QPointF &newPos, bool notify) | ||
106 | { | 103 | { | ||
107 | if (!m_zoomRect.isValid()) { | 104 | if (!m_zoomRect.isValid()) { | ||
108 | return; | 105 | return; | ||
109 | } | 106 | } | ||
110 | 107 | | |||
111 | if (m_zoomRect.center() == newPos) { | 108 | if (m_zoomRect.center() == newPos) { | ||
112 | qCDebug(KGRAPHVIEWERLIB_LOG) << "same pos, don't do anything"; | 109 | qCDebug(KGRAPHVIEWERLIB_LOG) << "same pos, don't do anything"; | ||
113 | return; | 110 | return; | ||
114 | } | 111 | } | ||
115 | 112 | | |||
116 | scene()->invalidate(m_zoomRect, QGraphicsScene::ForegroundLayer); | 113 | scene()->invalidate(m_zoomRect, QGraphicsScene::ForegroundLayer); | ||
117 | m_zoomRect.moveCenter(newPos); | 114 | m_zoomRect.moveCenter(newPos); | ||
118 | scene()->invalidate(m_zoomRect, QGraphicsScene::ForegroundLayer); | 115 | scene()->invalidate(m_zoomRect, QGraphicsScene::ForegroundLayer); | ||
119 | 116 | | |||
120 | if (m_zoomRect.isValid() && notify) { | 117 | if (m_zoomRect.isValid() && notify) { | ||
121 | emit zoomRectMovedTo(newPos); | 118 | emit zoomRectMovedTo(newPos); | ||
122 | m_lastPos = newPos; | 119 | m_lastPos = newPos; | ||
123 | } | 120 | } | ||
124 | } | 121 | } | ||
125 | 122 | | |||
126 | void PannerView::drawForeground(QPainter * p, const QRectF & rect ) | 123 | void PannerView::drawForeground(QPainter *p, const QRectF &rect) | ||
127 | { | 124 | { | ||
128 | if (m_zoomRect.isValid() && rect.intersects(m_zoomRect)) | 125 | if (m_zoomRect.isValid() && rect.intersects(m_zoomRect)) { | ||
129 | { | | |||
130 | p->save(); | 126 | p->save(); | ||
131 | if (m_zoomRect.width() > 10 && m_zoomRect.height() > 10) | 127 | if (m_zoomRect.width() > 10 && m_zoomRect.height() > 10) { | ||
132 | { | | |||
133 | p->setPen(Qt::red); | 128 | p->setPen(Qt::red); | ||
134 | // subtract pen width, i.e. draw inside | 129 | // subtract pen width, i.e. draw inside | ||
135 | qreal penWidth = p->pen().widthF(); | 130 | qreal penWidth = p->pen().widthF(); | ||
136 | p->drawRect(m_zoomRect.adjusted(-penWidth, -penWidth, -penWidth, -penWidth)); | 131 | p->drawRect(m_zoomRect.adjusted(-penWidth, -penWidth, -penWidth, -penWidth)); | ||
137 | } | 132 | } else { | ||
138 | else | | |||
139 | { | | |||
140 | QBrush brush(Qt::red); | 133 | QBrush brush(Qt::red); | ||
141 | p->fillRect(m_zoomRect, brush); | 134 | p->fillRect(m_zoomRect, brush); | ||
142 | } | 135 | } | ||
143 | p->restore(); | 136 | p->restore(); | ||
144 | } | 137 | } | ||
145 | } | 138 | } | ||
146 | 139 | | |||
147 | void PannerView::mousePressEvent(QMouseEvent* e) | 140 | void PannerView::mousePressEvent(QMouseEvent *e) | ||
148 | { | 141 | { | ||
149 | if (e->button() != Qt::LeftButton) { | 142 | if (e->button() != Qt::LeftButton) { | ||
150 | return; | 143 | return; | ||
151 | } | 144 | } | ||
152 | /* qCDebug(KGRAPHVIEWERLIB_LOG) << "PannerView::mousePressEvent " | 145 | /* qCDebug(KGRAPHVIEWERLIB_LOG) << "PannerView::mousePressEvent " | ||
153 | << mapToScene(e->pos()) << " / " << m_zoomRect << " / " << m_zoomRect.center() <<endl;*/ | 146 | << mapToScene(e->pos()) << " / " << m_zoomRect << " / " << m_zoomRect.center() <<endl;*/ | ||
154 | moveZoomRectTo(mapToScene(e->pos())); | 147 | moveZoomRectTo(mapToScene(e->pos())); | ||
155 | m_movingZoomRect = true; | 148 | m_movingZoomRect = true; | ||
156 | } | 149 | } | ||
157 | 150 | | |||
158 | void PannerView::mouseMoveEvent(QMouseEvent* e) | 151 | void PannerView::mouseMoveEvent(QMouseEvent *e) | ||
159 | { | 152 | { | ||
160 | if (!m_movingZoomRect) { | 153 | if (!m_movingZoomRect) { | ||
161 | return; | 154 | return; | ||
162 | } | 155 | } | ||
163 | 156 | | |||
164 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "PannerView::mouseMoveEvent " << pos; | 157 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "PannerView::mouseMoveEvent " << pos; | ||
165 | moveZoomRectTo(mapToScene(e->pos())); | 158 | moveZoomRectTo(mapToScene(e->pos())); | ||
166 | } | 159 | } | ||
167 | 160 | | |||
168 | void PannerView::mouseReleaseEvent(QMouseEvent* e) | 161 | void PannerView::mouseReleaseEvent(QMouseEvent *e) | ||
169 | { | 162 | { | ||
170 | if (e->button() != Qt::LeftButton) { | 163 | if (e->button() != Qt::LeftButton) { | ||
171 | return; | 164 | return; | ||
172 | } | 165 | } | ||
173 | moveZoomRectTo(mapToScene(e->pos())); | 166 | moveZoomRectTo(mapToScene(e->pos())); | ||
174 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "PannerView::mouseReleaseEvent " << pos; | 167 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "PannerView::mouseReleaseEvent " << pos; | ||
175 | m_movingZoomRect = false; | 168 | m_movingZoomRect = false; | ||
176 | emit zoomRectMoveFinished(); | 169 | emit zoomRectMoveFinished(); | ||
177 | } | 170 | } | ||
178 | 171 | | |||
179 | void PannerView::contextMenuEvent(QContextMenuEvent* event) | 172 | void PannerView::contextMenuEvent(QContextMenuEvent *event) | ||
180 | { | 173 | { | ||
181 | m_parent->contextMenuEvent(event); | 174 | m_parent->contextMenuEvent(event); | ||
182 | } | 175 | } | ||
183 | 176 | | |||
184 | } | 177 | } |