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