]> git.cworth.org Git - apitrace/blob - gui/graphing/graphview.cpp
Update downloads link.
[apitrace] / gui / graphing / graphview.cpp
1 #include "graphview.h"
2
3 #include <QMouseEvent>
4 #include <QApplication>
5
6 GraphView::GraphView(QWidget* parent) :
7     QWidget(parent),
8     m_viewLeft(0),
9     m_viewRight(0),
10     m_viewBottom(0),
11     m_viewTop(0),
12     m_graphLeft(0),
13     m_graphRight(0),
14     m_graphBottom(0),
15     m_graphTop(0),
16     m_viewWidth(0),
17     m_viewWidthMin(0),
18     m_viewWidthMax(0),
19     m_viewHeight(0),
20     m_viewHeightMin(0),
21     m_viewHeightMax(0),
22     m_selectionState(NULL)
23 {
24     memset(&m_previous, -1, sizeof(m_previous));
25 }
26
27 void GraphView::update()
28 {
29     if (m_graphLeft != m_previous.m_graphLeft || m_graphRight != m_previous.m_graphRight) {
30         m_previous.m_graphLeft = m_graphLeft;
31         m_previous.m_graphRight = m_graphRight;
32
33         emit horizontalRangeChanged(m_graphLeft, m_graphRight);
34     }
35
36     if (m_viewLeft != m_previous.m_viewLeft || m_viewRight != m_previous.m_viewRight) {
37         m_previous.m_viewLeft = m_viewLeft;
38         m_previous.m_viewRight = m_viewRight;
39
40         emit horizontalViewChanged(m_viewLeft, m_viewRight);
41     }
42
43     if (m_graphBottom != m_previous.m_graphBottom || m_graphTop != m_previous.m_graphTop) {
44         m_previous.m_graphBottom = m_graphBottom;
45         m_previous.m_graphTop = m_graphTop;
46
47         emit verticalRangeChanged(m_graphBottom, m_graphTop);
48     }
49
50     if (m_viewBottom != m_previous.m_viewBottom || m_viewTop != m_previous.m_viewTop) {
51         m_previous.m_viewBottom = m_viewBottom;
52         m_previous.m_viewTop = m_viewTop;
53
54         emit verticalViewChanged(m_viewBottom, m_viewTop);
55     }
56
57     QWidget::update();
58 }
59
60 void GraphView::resizeEvent(QResizeEvent *)
61 {
62     m_viewHeight = height();
63     m_viewHeightMin = m_viewHeight;
64     m_viewHeightMax = m_viewHeight;
65
66     m_viewTop = m_viewBottom + m_viewHeight;
67
68     update();
69 }
70
71 void GraphView::wheelEvent(QWheelEvent *e)
72 {
73     int zoomPercent = 10;
74
75     /* If holding Ctrl key then zoom 2x faster */
76     if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
77         zoomPercent = 20;
78     }
79
80     /* Zoom view by adjusting width */
81     double dt = m_viewWidth;
82     double size = dt;
83     size *= -e->delta();
84
85     /* Zoom deltas normally come in increments of 120 */
86     size /= 120 * (100 / zoomPercent);
87
88     m_viewWidth += size;
89     m_viewWidth = qBound(m_viewWidthMin, m_viewWidth, m_viewWidthMax);
90
91     /* Scroll view to zoom around mouse */
92     dt -= m_viewWidth;
93     dt *= e->x();
94     dt /= width();
95
96     m_viewLeft = dt + m_viewLeft;
97     m_viewLeft = qBound(m_graphLeft, m_viewLeft, m_graphRight - m_viewWidth);
98     m_viewRight = m_viewLeft + m_viewWidth;
99
100     update();
101 }
102
103 void GraphView::mouseMoveEvent(QMouseEvent *e)
104 {
105     if (e->buttons().testFlag(Qt::LeftButton)) {
106         /* Horizontal scroll */
107         double dvdx = m_viewWidth / (double)width();
108         dvdx *= m_mousePressPosition.x() - e->pos().x();
109
110         m_viewLeft = m_mousePressViewLeft + dvdx;
111         m_viewLeft = qBound(m_graphLeft, m_viewLeft, m_graphRight - m_viewWidth);
112         m_viewRight = m_viewLeft + m_viewWidth;
113
114         /* Vertical scroll */
115         double dvdy = m_viewHeight / (double)height();
116         dvdy *= m_mousePressPosition.y() - e->pos().y();
117
118         m_viewBottom = m_mousePressViewBottom + dvdy;
119         m_viewBottom = qBound(m_graphBottom, m_viewBottom, m_graphTop - m_viewHeight);
120         m_viewTop = m_viewBottom + m_viewHeight;
121
122         update();
123     }
124 }
125
126 void GraphView::mousePressEvent(QMouseEvent *e)
127 {
128     m_mousePressPosition = e->pos();
129     m_mousePressViewLeft = m_viewLeft;
130     m_mousePressViewBottom = m_viewBottom;
131 }
132
133 void GraphView::mouseDoubleClickEvent(QMouseEvent *e)
134 {
135     if (m_selectionState) {
136         m_selectionState->type = SelectionState::None;
137         emit selectionChanged();
138     }
139 }
140
141 void GraphView::setSelectionState(SelectionState* state)
142 {
143     m_selectionState = state;
144 }
145
146 void GraphView::setHorizontalView(qint64 start, qint64 end)
147 {
148     m_viewLeft = qBound(m_graphLeft, start, m_graphRight - (end - start));
149     m_viewRight = qBound(m_graphLeft, end, m_graphRight);
150     m_viewWidth = m_viewRight - m_viewLeft;
151     update();
152 }
153
154 void GraphView::setVerticalView(qint64 start, qint64 end)
155 {
156     m_viewBottom = qBound(m_graphBottom, start, m_graphTop - (end - start));
157     m_viewTop = qBound(m_graphBottom, end, m_graphTop);
158     m_viewHeight = m_viewTop - m_viewBottom;
159     update();
160 }
161
162 void GraphView::setDefaultView(qint64 min, qint64 max)
163 {
164     m_graphLeft = min;
165     m_graphRight = max;
166     m_viewWidth = max - min;
167
168     m_viewWidthMin = 1;
169     m_viewWidthMax = m_viewWidth;
170
171     m_viewLeft = min;
172     m_viewRight = max;
173
174     m_viewHeight = height();
175     m_viewHeightMin = m_viewHeight;
176     m_viewHeightMax = m_viewHeight;
177
178     m_viewBottom = 0;
179     m_viewTop = m_viewHeight;
180
181     m_graphBottom = 0;
182     m_graphTop = m_viewHeight;
183
184     update();
185 }
186
187 #include "graphview.moc"