]> git.cworth.org Git - apitrace/blob - gui/graphing/graphaxiswidget.cpp
Update downloads link.
[apitrace] / gui / graphing / graphaxiswidget.cpp
1 #include "graphaxiswidget.h"
2
3 #include <QMouseEvent>
4
5 GraphAxisWidget::GraphAxisWidget(QWidget* parent) :
6     QWidget(parent),
7     m_selectable(None),
8     m_selectionState(NULL)
9 {
10 }
11
12
13 bool GraphAxisWidget::hasSelection()
14 {
15     if (!m_selectionState) {
16         return false;
17     }
18
19     if (m_selectionState->type == SelectionState::Horizontal && m_orientation == GraphAxisWidget::Horizontal) {
20         return true;
21     }
22
23     if (m_selectionState->type == SelectionState::Vertical && m_orientation == GraphAxisWidget::Vertical) {
24         return true;
25     }
26
27     return false;
28 }
29
30
31 void GraphAxisWidget::setSelectable(SelectionStyle selectable)
32 {
33     m_selectable = selectable;
34 }
35
36
37 void GraphAxisWidget::setSelectionState(SelectionState* state)
38 {
39     m_selectionState = state;
40 }
41
42
43 void GraphAxisWidget::setOrientation(Orientation v)
44 {
45     m_orientation = v;
46
47     if (m_orientation == Horizontal) {
48         setMinimumWidth(60);
49     } else {
50         setMinimumHeight(60);
51     }
52 }
53
54
55 void GraphAxisWidget::mouseMoveEvent(QMouseEvent *e)
56 {
57     if (m_selectable == None) {
58         return;
59     }
60
61     int pos, max;
62
63     if (m_orientation == Horizontal) {
64         pos = e->x();
65         max = width();
66     } else {
67         pos = e->y();
68         max = height();
69     }
70
71     double value = m_valueEnd - m_valueBegin;
72     value *= pos / (double)max;
73     value += m_valueBegin;
74
75     if (e->buttons().testFlag(Qt::LeftButton)) {
76         m_selectionState->start = qMin<qint64>(m_mousePressValue, value);
77         m_selectionState->end = qMax<qint64>(m_mousePressValue, value);
78         m_selectionState->type = m_orientation == Horizontal ? SelectionState::Horizontal : SelectionState::Vertical;
79         emit selectionChanged();
80         update();
81     }
82 }
83
84
85 void GraphAxisWidget::mousePressEvent(QMouseEvent *e)
86 {
87     if (m_selectable == None) {
88         return;
89     }
90
91     int pos, max;
92
93     if (m_orientation == Horizontal) {
94         pos = e->x();
95         max = width();
96     } else {
97         pos = e->y();
98         max = height();
99     }
100
101     double value = m_valueEnd - m_valueBegin;
102     value *= pos / (double)max;
103     value += m_valueBegin;
104
105     m_mousePressPosition = e->pos();
106     m_mousePressValue = value;
107 }
108
109
110 void GraphAxisWidget::mouseReleaseEvent(QMouseEvent *e)
111 {
112     if (m_selectable == None) {
113         return;
114     }
115
116     int dx = qAbs(m_mousePressPosition.x() - e->x());
117     int dy = qAbs(m_mousePressPosition.y() - e->y());
118
119     if (dx + dy < 2) {
120         m_selectionState->type = SelectionState::None;
121         emit selectionChanged();
122     }
123 }
124
125
126 void GraphAxisWidget::setRange(qint64 min, qint64 max)
127 {
128     m_valueMin = min;
129     m_valueMax = max;
130     update();
131 }
132
133
134 void GraphAxisWidget::setView(qint64 start, qint64 end)
135 {
136     m_valueBegin = start;
137     m_valueEnd = end;
138     update();
139 }
140
141 #include "graphaxiswidget.moc"