-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyPlot.cpp
More file actions
139 lines (100 loc) · 3.2 KB
/
Copy pathmyPlot.cpp
File metadata and controls
139 lines (100 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "myPlot.h"
#include <QTimer>
#include <QGraphicsPixmapItem>
#include <QVBoxLayout>
#include <QGuiApplication>
#include <QStyle>
#include <QDesktopServices>
#include <QScreen>
void MyPlot::appendPoints(const QVector<QPointF>& listPoints) {
appendPoints(QPolygonF(listPoints), QColorConstants::Black, QColorConstants::Black);
}
void MyPlot::appendPoints(
const QVector<QPointF>& listPoints,
QColor color) {
appendPoints(listPoints, color, color);
}
void MyPlot::appendPoints(
const QVector<QPointF>& listPoints,
QColor colorLine,
QColor colorPoints
) {
if (listPoints.size()) {
m_listPoints.append({QPolygonF(listPoints), colorLine, colorPoints});
recalcRect();
}
QTimer::singleShot(1, [this]{ repaintPlot(); });
}
void MyPlot::recalcRect() {
if (m_listPoints.empty()) {
rectPoints = QRectF();
return;
}
rectPoints = m_listPoints.first().points.boundingRect();
for (uint ii = 1; ii < m_listPoints.size(); ++ii)
rectPoints = rectPoints.united(m_listPoints[ii].points.boundingRect());
}
QPolygonF MyPlot::getListPoints(uint iList, int wi, int he) {
QPolygonF points;
for (const QPointF& p: m_listPoints[iList].points) {
double x = wi / 2.;
double y = he / 2.;
if (rectPoints.width())
x = (p.x() - rectPoints.x()) / rectPoints.width() * (wi - 20) + 10;
if (rectPoints.height())
y = (p.y() - rectPoints.y()) / rectPoints.height() * (he - 20) + 10;
y = he - y;
points.append({x,y});
}
return points;
}
void MyPlot::repaintPlot() {
if (!m_view || !m_scene) return;
QRect r = m_view->contentsRect();
m_view->setSceneRect(0, 0, r.width(), r.height());
QSize sz = m_view->viewport()->size();
int wi = sz.width();
int he = sz.height();
QPixmap pixmap(wi, he);
pixmap.fill();
QPainter painter(&pixmap);
for (uint ii = 0; ii < m_listPoints.size(); ++ii) {
QPolygonF points = getListPoints(ii, wi, he);
const Poly& pl = m_listPoints.at(ii);
painter.setPen(pl.colorLine);
painter.drawPolyline(points.data(), points.size());
painter.setPen(pl.colorPoints);
painter.setBrush(pl.colorPoints);
for (const QPointF& p: points)
painter.drawEllipse(p, 2, 2);
}
paintPixmap(pixmap);
}
void MyPlot::paintPixmap(const QPixmap& pixmap) {
auto list = m_scene->items();
if (list.size() == 0) {
auto* item = new QGraphicsPixmapItem(pixmap);
m_scene->addItem(item);
} else {
auto* gpi = static_cast<QGraphicsPixmapItem*>(list.first());
gpi->setPixmap(pixmap);
}
}
MyPlot::MyPlot(QWidget* parent)
: QWidget(parent) {
setAttribute(Qt::WA_DeleteOnClose);
QScreen* screen = QGuiApplication::primaryScreen();
auto srect = screen->availableGeometry();
setGeometry(
QStyle::alignedRect(
Qt::LeftToRight,
Qt::AlignCenter,
{600,400}, srect)
);
auto* lt = new QVBoxLayout;
setLayout(lt);
m_view = new QGraphicsView;
m_scene = new QGraphicsScene;
m_view->setScene(m_scene);
lt->addWidget(m_view);
}