@@ -1,163 +1,163 | |||
|
1 | 1 | #include <Common/DateUtils.h> |
|
2 | 2 | #include <Visualization/VisualizationCursorItem.h> |
|
3 | 3 | #include <Visualization/qcustomplot.h> |
|
4 | 4 | |
|
5 | 5 | /// Width of the cursor in pixel |
|
6 | 6 | const auto CURSOR_WIDTH = 3; |
|
7 | 7 | |
|
8 | 8 | /// Color of the cursor in the graph |
|
9 | 9 | const auto CURSOR_COLOR = QColor{68, 114, 196}; |
|
10 | 10 | |
|
11 | 11 | /// Line style of the cursor in the graph |
|
12 | 12 | auto CURSOR_PEN_STYLE = Qt::DotLine; |
|
13 | 13 | |
|
14 | 14 | struct VisualizationCursorItem::VisualizationCursorItemPrivate { |
|
15 | 15 | |
|
16 | 16 | QCustomPlot *m_Plot = nullptr; |
|
17 | 17 | |
|
18 | 18 | QCPItemStraightLine *m_LineItem = nullptr; |
|
19 | 19 | QCPItemText *m_LabelItem = nullptr; |
|
20 | 20 | |
|
21 | 21 | Qt::Orientation m_Orientation; |
|
22 | 22 | double m_Position = 0.0; |
|
23 | 23 | bool m_IsAbsolutePosition = false; |
|
24 | 24 | QString m_LabelText; |
|
25 | 25 | |
|
26 | 26 | explicit VisualizationCursorItemPrivate(QCustomPlot *plot) |
|
27 | 27 | : m_Plot(plot), m_Orientation(Qt::Vertical) |
|
28 | 28 | { |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | void updateOrientation() |
|
32 | 32 | { |
|
33 | 33 | if (m_LineItem) { |
|
34 | 34 | switch (m_Orientation) { |
|
35 | 35 | case Qt::Vertical: |
|
36 | 36 | m_LineItem->point1->setTypeX(m_IsAbsolutePosition |
|
37 | 37 | ? QCPItemPosition::ptAbsolute |
|
38 | 38 | : QCPItemPosition::ptPlotCoords); |
|
39 | 39 | m_LineItem->point1->setTypeY(QCPItemPosition::ptAxisRectRatio); |
|
40 | 40 | m_LineItem->point2->setTypeX(m_IsAbsolutePosition |
|
41 | 41 | ? QCPItemPosition::ptAbsolute |
|
42 | 42 | : QCPItemPosition::ptPlotCoords); |
|
43 | 43 | m_LineItem->point2->setTypeY(QCPItemPosition::ptAxisRectRatio); |
|
44 | 44 | m_LabelItem->setPositionAlignment(Qt::AlignLeft | Qt::AlignTop); |
|
45 | 45 | break; |
|
46 | 46 | case Qt::Horizontal: |
|
47 | 47 | m_LineItem->point1->setTypeX(QCPItemPosition::ptAxisRectRatio); |
|
48 | 48 | m_LineItem->point1->setTypeY(m_IsAbsolutePosition |
|
49 | 49 | ? QCPItemPosition::ptAbsolute |
|
50 | 50 | : QCPItemPosition::ptPlotCoords); |
|
51 | 51 | m_LineItem->point2->setTypeX(QCPItemPosition::ptAxisRectRatio); |
|
52 | 52 | m_LineItem->point2->setTypeY(m_IsAbsolutePosition |
|
53 | 53 | ? QCPItemPosition::ptAbsolute |
|
54 | 54 | : QCPItemPosition::ptPlotCoords); |
|
55 | 55 | m_LabelItem->setPositionAlignment(Qt::AlignRight | Qt::AlignBottom); |
|
56 | 56 | } |
|
57 | 57 | } |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | void updateCursorPosition() |
|
61 | 61 | { |
|
62 | 62 | if (m_LineItem) { |
|
63 | 63 | switch (m_Orientation) { |
|
64 | 64 | case Qt::Vertical: |
|
65 | 65 | m_LineItem->point1->setCoords(m_Position, 0); |
|
66 | 66 | m_LineItem->point2->setCoords(m_Position, 1); |
|
67 | 67 | m_LabelItem->position->setCoords(5, 5); |
|
68 | 68 | break; |
|
69 | 69 | case Qt::Horizontal: |
|
70 | 70 | m_LineItem->point1->setCoords(1, m_Position); |
|
71 | 71 | m_LineItem->point2->setCoords(0, m_Position); |
|
72 | 72 | m_LabelItem->position->setCoords(-5, -5); |
|
73 | 73 | } |
|
74 | 74 | } |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | void updateLabelText() |
|
78 | 78 | { |
|
79 | 79 | if (m_LabelItem) { |
|
80 | 80 | m_LabelItem->setText(m_LabelText); |
|
81 | 81 | } |
|
82 | 82 | } |
|
83 | 83 | }; |
|
84 | 84 | |
|
85 | 85 | VisualizationCursorItem::VisualizationCursorItem(QCustomPlot *plot) |
|
86 | 86 | : impl{spimpl::make_unique_impl<VisualizationCursorItemPrivate>(plot)} |
|
87 | 87 | { |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | void VisualizationCursorItem::setVisible(bool value) |
|
91 | 91 | { |
|
92 | 92 | if (value != isVisible()) { |
|
93 | 93 | |
|
94 | 94 | if (value) { |
|
95 |
Q_ASSERT(!impl->m_LineItem && !impl->m_ |
|
|
95 | Q_ASSERT(!impl->m_LineItem && !impl->m_LabelItem); | |
|
96 | 96 | |
|
97 | 97 | impl->m_LineItem = new QCPItemStraightLine{impl->m_Plot}; |
|
98 | 98 | auto pen = QPen{CURSOR_PEN_STYLE}; |
|
99 | 99 | pen.setColor(CURSOR_COLOR); |
|
100 | 100 | pen.setWidth(CURSOR_WIDTH); |
|
101 | 101 | impl->m_LineItem->setPen(pen); |
|
102 | 102 | impl->m_LineItem->setSelectable(false); |
|
103 | 103 | |
|
104 | 104 | impl->m_LabelItem = new QCPItemText{impl->m_Plot}; |
|
105 | 105 | impl->m_LabelItem->setColor(CURSOR_COLOR); |
|
106 | 106 | impl->m_LabelItem->setSelectable(false); |
|
107 | 107 | impl->m_LabelItem->position->setParentAnchor(impl->m_LineItem->point1); |
|
108 | 108 | impl->m_LabelItem->position->setTypeX(QCPItemPosition::ptAbsolute); |
|
109 | 109 | impl->m_LabelItem->position->setTypeY(QCPItemPosition::ptAbsolute); |
|
110 | 110 | |
|
111 | 111 | auto font = impl->m_LabelItem->font(); |
|
112 | 112 | font.setPointSize(10); |
|
113 | 113 | font.setBold(true); |
|
114 | 114 | impl->m_LabelItem->setFont(font); |
|
115 | 115 | |
|
116 | 116 | impl->updateOrientation(); |
|
117 | 117 | impl->updateLabelText(); |
|
118 | 118 | impl->updateCursorPosition(); |
|
119 | 119 | } |
|
120 | 120 | else { |
|
121 |
Q_ASSERT(impl->m_LineItem && impl->m_ |
|
|
121 | Q_ASSERT(impl->m_LineItem && impl->m_LabelItem); | |
|
122 | 122 | |
|
123 | 123 | // Note: the items are destroyed by QCustomPlot in removeItem |
|
124 | 124 | impl->m_Plot->removeItem(impl->m_LineItem); |
|
125 | 125 | impl->m_LineItem = nullptr; |
|
126 | 126 | impl->m_Plot->removeItem(impl->m_LabelItem); |
|
127 | 127 | impl->m_LabelItem = nullptr; |
|
128 | 128 | } |
|
129 | 129 | } |
|
130 | 130 | } |
|
131 | 131 | |
|
132 | 132 | bool VisualizationCursorItem::isVisible() const |
|
133 | 133 | { |
|
134 | 134 | return impl->m_LineItem != nullptr; |
|
135 | 135 | } |
|
136 | 136 | |
|
137 | 137 | void VisualizationCursorItem::setPosition(double value) |
|
138 | 138 | { |
|
139 | 139 | impl->m_Position = value; |
|
140 | 140 | impl->m_IsAbsolutePosition = false; |
|
141 | 141 | impl->updateLabelText(); |
|
142 | 142 | impl->updateCursorPosition(); |
|
143 | 143 | } |
|
144 | 144 | |
|
145 | 145 | void VisualizationCursorItem::setAbsolutePosition(double value) |
|
146 | 146 | { |
|
147 | 147 | setPosition(value); |
|
148 | 148 | impl->m_IsAbsolutePosition = true; |
|
149 | 149 | } |
|
150 | 150 | |
|
151 | 151 | void VisualizationCursorItem::setOrientation(Qt::Orientation orientation) |
|
152 | 152 | { |
|
153 | 153 | impl->m_Orientation = orientation; |
|
154 | 154 | impl->updateLabelText(); |
|
155 | 155 | impl->updateOrientation(); |
|
156 | 156 | impl->updateCursorPosition(); |
|
157 | 157 | } |
|
158 | 158 | |
|
159 | 159 | void VisualizationCursorItem::setLabelText(const QString &text) |
|
160 | 160 | { |
|
161 | 161 | impl->m_LabelText = text; |
|
162 | 162 | impl->updateLabelText(); |
|
163 | 163 | } |
General Comments 0
You need to be logged in to leave comments.
Login now