4 #include <QApplication>
6 GraphView::GraphView(QWidget* parent) :
22 m_selectionState(NULL)
24 memset(&m_previous, -1, sizeof(m_previous));
27 void GraphView::update()
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;
33 emit horizontalRangeChanged(m_graphLeft, m_graphRight);
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;
40 emit horizontalViewChanged(m_viewLeft, m_viewRight);
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;
47 emit verticalRangeChanged(m_graphBottom, m_graphTop);
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;
54 emit verticalViewChanged(m_viewBottom, m_viewTop);
60 void GraphView::resizeEvent(QResizeEvent *)
62 m_viewHeight = height();
63 m_viewHeightMin = m_viewHeight;
64 m_viewHeightMax = m_viewHeight;
66 m_viewTop = m_viewBottom + m_viewHeight;
71 void GraphView::wheelEvent(QWheelEvent *e)
75 /* If holding Ctrl key then zoom 2x faster */
76 if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
80 /* Zoom view by adjusting width */
81 double dt = m_viewWidth;
85 /* Zoom deltas normally come in increments of 120 */
86 size /= 120 * (100 / zoomPercent);
89 m_viewWidth = qBound(m_viewWidthMin, m_viewWidth, m_viewWidthMax);
91 /* Scroll view to zoom around mouse */
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;
103 void GraphView::mouseMoveEvent(QMouseEvent *e)
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();
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;
114 /* Vertical scroll */
115 double dvdy = m_viewHeight / (double)height();
116 dvdy *= m_mousePressPosition.y() - e->pos().y();
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;
126 void GraphView::mousePressEvent(QMouseEvent *e)
128 m_mousePressPosition = e->pos();
129 m_mousePressViewLeft = m_viewLeft;
130 m_mousePressViewBottom = m_viewBottom;
133 void GraphView::mouseDoubleClickEvent(QMouseEvent *e)
135 if (m_selectionState) {
136 m_selectionState->type = SelectionState::None;
137 emit selectionChanged();
141 void GraphView::setSelectionState(SelectionState* state)
143 m_selectionState = state;
146 void GraphView::setHorizontalView(qint64 start, qint64 end)
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;
154 void GraphView::setVerticalView(qint64 start, qint64 end)
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;
162 void GraphView::setDefaultView(qint64 min, qint64 max)
166 m_viewWidth = max - min;
169 m_viewWidthMax = m_viewWidth;
174 m_viewHeight = height();
175 m_viewHeightMin = m_viewHeight;
176 m_viewHeightMax = m_viewHeight;
179 m_viewTop = m_viewHeight;
182 m_graphTop = m_viewHeight;
187 #include "graphview.moc"