Locus No Pilotus
Project of four first grade MIPT DAFE/RSE students (for engineering practical work in the second semester) in Qt C++
Loading...
Searching...
No Matches
plot_item_arc.h
Go to the documentation of this file.
1// QCP lib:
2#include <qcustomplot.h> // base class
3
4/// @brief Value for arc degree in qPainter
5static constexpr char arc_correction_value = 16;
6
7/// @brief Class of the arc shape
8class PlotItemArc : public QCPAbstractItem {
9 public:
10 explicit PlotItemArc(QCustomPlot *parentPlot);
11 virtual ~PlotItemArc() override;
12
13 QPen Pen() const { return mPen; }
14
15 void SetPen(const QPen &pen);
16
17 virtual double selectTest(const QPointF &pos, bool onlySelectable,
18 QVariant *details = nullptr) const override;
19
20 QCPItemPosition *const topLeft;
21 QCPItemPosition *const bottomRight;
22 QCPItemAnchor *const topLeftRim;
23 QCPItemAnchor *const top;
24 QCPItemAnchor *const topRightRim;
25 QCPItemAnchor *const right;
26 QCPItemAnchor *const bottomRightRim;
27 QCPItemAnchor *const bottom;
28 QCPItemAnchor *const bottomLeftRim;
29 QCPItemAnchor *const left;
30 QCPItemAnchor *const center;
31
32 void SetCenterAndRadiusCoords(double center_x, double center_y, double rad);
33 void SetStartAndEnd(double start_angle, double end_angle);
34 void SetStartAndEnd(std::pair<double, double> start_and_end);
35
36 protected:
48
51
52 virtual void draw(QCPPainter *painter) override;
53 virtual QPointF anchorPixelPosition(int anchorId) const override;
54
55 QPen MainPen() const;
56 QBrush MainBrush() const;
57
58 private:
59 int arc_start_{0};
60 int arc_length_{90};
61};
62
63/**
64 * @brief Creates an arc item and sets default values
65 * @details The created item is automatically registered with \a parentPlot.
66 * This QCustomPlot instance takes ownership of the item, so do not delete it
67 * manually but use QCustomPlot::removeItem() instead
68 * @param parentPlot
69 */
70inline PlotItemArc::PlotItemArc(QCustomPlot *parentPlot)
71 : QCPAbstractItem(parentPlot),
72 topLeft(createPosition(QLatin1String("topLeft"))),
73 bottomRight(createPosition(QLatin1String("bottomRight"))),
74 topLeftRim(createAnchor(QLatin1String("topLeftRim"), aiTopLeftRim)),
75 top(createAnchor(QLatin1String("top"), aiTop)),
76 topRightRim(createAnchor(QLatin1String("topRightRim"), aiTopRightRim)),
77 right(createAnchor(QLatin1String("right"), aiRight)),
78 bottomRightRim(
79 createAnchor(QLatin1String("bottomRightRim"), aiBottomRightRim)),
80 bottom(createAnchor(QLatin1String("bottom"), aiBottom)),
81 bottomLeftRim(
82 createAnchor(QLatin1String("bottomLeftRim"), aiBottomLeftRim)),
83 left(createAnchor(QLatin1String("left"), aiLeft)),
84 center(createAnchor(QLatin1String("center"), aiCenter)) {
85 topLeft->setCoords(0, 1);
86 bottomRight->setCoords(1, 0);
87
88 SetPen(QPen(Qt::black));
89}
90
92
93/**
94 * @brief Sets the pen that will be used to draw the line of the arc
95 * @param pen
96 * @see setSelectedPen, setBrush
97 */
98inline void PlotItemArc::SetPen(const QPen &pen) { mPen = pen; }
99
100/* check documentation from base class */
101inline double PlotItemArc::selectTest(const QPointF &pos, bool onlySelectable,
102 QVariant *details) const {
103 // i love qcustomplot, because it has genius solutions in code
104 Q_UNUSED(details);
105 Q_UNUSED(pos);
106 Q_UNUSED(onlySelectable);
107
108 return -1.0;
109}
110
111/* check documentation from base class */
112inline void PlotItemArc::draw(QCPPainter *painter) {
113 QPointF p1 = topLeft->pixelPosition();
114 QPointF p2 = bottomRight->pixelPosition();
115
116 if (p1.toPoint() == p2.toPoint()) return;
117
118 QRectF arcRect = QRectF(p1, p2).normalized();
119
120 const int clipEnlarge = qCeil(MainPen().widthF());
121
122 QRect clip =
123 clipRect().adjusted(-clipEnlarge, -clipEnlarge, clipEnlarge, clipEnlarge);
124
125 if (arcRect.intersects(clip)) {
126 painter->setPen(MainPen());
127 painter->setBrush(MainBrush());
128#ifdef __EXCEPTIONS
129 try // drawArc sometimes throws exceptions if arc is too big
130 {
131#endif
132 painter->drawArc(arcRect, arc_start_, arc_length_);
133
134#ifdef __EXCEPTIONS
135 } catch (...) {
136 qDebug() << Q_FUNC_INFO << "Item too large for memory, setting invisible";
137 setVisible(false);
138 }
139#endif
140 }
141}
142
143/* check documentation from base class */
144inline QPointF PlotItemArc::anchorPixelPosition(int anchorId) const {
145 QRectF rect = QRectF(topLeft->pixelPosition(), bottomRight->pixelPosition());
146 switch (anchorId) {
147 case aiTopLeftRim:
148 return rect.center() + (rect.topLeft() - rect.center()) * 1 / qSqrt(2);
149 case aiTop:
150 return (rect.topLeft() + rect.topRight()) * 0.5;
151 case aiTopRightRim:
152 return rect.center() + (rect.topRight() - rect.center()) * 1 / qSqrt(2);
153 case aiRight:
154 return (rect.topRight() + rect.bottomRight()) * 0.5;
155 case aiBottomRightRim:
156 return rect.center() +
157 (rect.bottomRight() - rect.center()) * 1 / qSqrt(2);
158 case aiBottom:
159 return (rect.bottomLeft() + rect.bottomRight()) * 0.5;
160 case aiBottomLeftRim:
161 return rect.center() + (rect.bottomLeft() - rect.center()) * 1 / qSqrt(2);
162 case aiLeft:
163 return (rect.topLeft() + rect.bottomLeft()) * 0.5;
164 case aiCenter:
165 return (rect.topLeft() + rect.bottomRight()) * 0.5;
166 }
167
168 qDebug() << Q_FUNC_INFO << "invalid anchorId" << anchorId;
169 return {};
170}
171
172/**
173 * @brief Returns the pen that should be used for drawing lines
174 * Returns mPen when the item is not selected and mSelectedPen when it is
175 * @return QPen
176 */
177inline QPen PlotItemArc::MainPen() const {
178 return mSelected ? mSelectedPen : mPen;
179}
180
181/**
182 * @brief Returns the brush that should be used for drawing fills of the item
183 * Returns mBrush when the item is not selected and mSelectedBrush when it is
184 * @return QBrush
185 */
186inline QBrush PlotItemArc::MainBrush() const {
187 return mSelected ? mSelectedBrush : mBrush;
188}
189
190/**
191 * @brief Sets center and radius of the arc by setting topLeft and bottomRight
192 * @param center_x: abscissa coord of the arc
193 * @param center_y: ordinate coord of the arc
194 * @param rad: radius value of the arc
195 */
196inline void PlotItemArc::SetCenterAndRadiusCoords(double center_x,
197 double center_y, double rad) {
198 topLeft->setCoords(center_x - rad, center_y + rad);
199 bottomRight->setCoords(center_x + rad, center_y - rad);
200}
201
202/**
203 * @brief Sets start and end of current acr
204 * @param start_angle: start value in degree
205 * @param end_angle: end value in degree
206 */
207inline void PlotItemArc::SetStartAndEnd(double start_angle, double end_angle) {
208 arc_start_ = static_cast<int>(start_angle * arc_correction_value);
209 int arc_end = static_cast<int>(end_angle * arc_correction_value);
210
211 arc_length_ = static_cast<int>(arc_end - arc_start_);
212}
213
214/**
215 * @brief Sets start and end of current acr
216 * @param start_and_end: pair of start and end
217 */
219 std::pair<double, double> start_and_end) {
220 arc_start_ = static_cast<int>(start_and_end.first * arc_correction_value);
221 int arc_end = static_cast<int>(start_and_end.second * arc_correction_value);
222
223 arc_length_ = static_cast<int>(arc_end - arc_start_);
224}
Class of the arc shape.
Definition plot_item_arc.h:8
int arc_start_
Definition plot_item_arc.h:59
void SetPen(const QPen &pen)
Sets the pen that will be used to draw the line of the arc.
Definition plot_item_arc.h:98
int arc_length_
Definition plot_item_arc.h:60
virtual ~PlotItemArc() override
Definition plot_item_arc.h:91
virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=nullptr) const override
Definition plot_item_arc.h:101
QCPItemAnchor *const bottomLeftRim
Definition plot_item_arc.h:28
void SetCenterAndRadiusCoords(double center_x, double center_y, double rad)
Sets center and radius of the arc by setting topLeft and bottomRight.
Definition plot_item_arc.h:196
virtual QPointF anchorPixelPosition(int anchorId) const override
Definition plot_item_arc.h:144
QCPItemPosition *const topLeft
Definition plot_item_arc.h:20
QCPItemAnchor *const bottom
Definition plot_item_arc.h:27
QCPItemAnchor *const bottomRightRim
Definition plot_item_arc.h:26
QCPItemAnchor *const top
Definition plot_item_arc.h:23
AnchorIndex
Definition plot_item_arc.h:37
@ aiBottomLeftRim
Definition plot_item_arc.h:44
@ aiCenter
Definition plot_item_arc.h:46
@ aiBottomRightRim
Definition plot_item_arc.h:42
@ aiTopLeftRim
Definition plot_item_arc.h:38
@ aiBottom
Definition plot_item_arc.h:43
@ aiTopRightRim
Definition plot_item_arc.h:40
@ aiRight
Definition plot_item_arc.h:41
@ aiTop
Definition plot_item_arc.h:39
@ aiLeft
Definition plot_item_arc.h:45
PlotItemArc(QCustomPlot *parentPlot)
Creates an arc item and sets default values.
Definition plot_item_arc.h:70
QPen mSelectedPen
Definition plot_item_arc.h:49
void SetStartAndEnd(double start_angle, double end_angle)
Sets start and end of current acr.
Definition plot_item_arc.h:207
QCPItemAnchor *const topLeftRim
Definition plot_item_arc.h:22
QBrush mBrush
Definition plot_item_arc.h:50
QPen MainPen() const
Returns the pen that should be used for drawing lines Returns mPen when the item is not selected and ...
Definition plot_item_arc.h:177
QPen Pen() const
Definition plot_item_arc.h:13
QCPItemAnchor *const topRightRim
Definition plot_item_arc.h:24
QCPItemAnchor *const right
Definition plot_item_arc.h:25
virtual void draw(QCPPainter *painter) override
Definition plot_item_arc.h:112
QCPItemAnchor *const left
Definition plot_item_arc.h:29
QCPItemAnchor *const center
Definition plot_item_arc.h:30
QBrush MainBrush() const
Returns the brush that should be used for drawing fills of the item Returns mBrush when the item is n...
Definition plot_item_arc.h:186
QCPItemPosition *const bottomRight
Definition plot_item_arc.h:21
QBrush mSelectedBrush
Definition plot_item_arc.h:50
QPen mPen
Definition plot_item_arc.h:49
static constexpr char arc_correction_value
Value for arc degree in qPainter.
Definition plot_item_arc.h:5