-
Notifications
You must be signed in to change notification settings - Fork 2
/
qgraphics.cpp
92 lines (78 loc) · 1.84 KB
/
qgraphics.cpp
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
#include <QPainter>
#include <QMouseEvent>
#include "qgraphics.h"
QGraphics::QGraphics(QWidget *parent) : QWidget(parent), s_type(Options::Cardinal)
{
QPalette p;
p.setBrush(this->backgroundRole(),QBrush(QColor(255,255,255)));
this->setPalette(p);
this->setAutoFillBackground(true);
spline = new CardinalSpline();
//spline = new BezierSpline();
setMouseTracking(true);
}
void QGraphics::genSpline(bool fi)
{
if (options.sline_t != s_type) {
delete spline; spline = nullptr;
spline = SplineBuilder::Build(options.sline_t);
s_type = options.sline_t;
}
if (fi)
spline->finish();
else
spline->genSpline();
}
void QGraphics::repaint()
{
spline->clear();
}
void QGraphics::paintEvent(QPaintEvent *event)
{
QPainter pir(this);
spline->draw(pir.device(), options);
QWidget::paintEvent(event);
}
void QGraphics::mousePressEvent(QMouseEvent *event)
{
had_press = true;
setCursor(Qt::ClosedHandCursor);
last_pos = event->pos();
if(options.add_cp_mode == Options::AdjustCtrlPos)
adj_p_idx = spline->at(last_pos);
update();
}
void QGraphics::mouseDoubleClickEvent(QMouseEvent * event)
{
if (options.add_cp_mode == Options::AddCtrlPos) {
setCursor(Qt::CrossCursor);
spline->add(event->pos());
}
update();
}
void QGraphics::mouseMoveEvent(QMouseEvent *event)
{
auto pos = event->pos();
if(had_press) {
if (adj_p_idx != -1 && options.add_cp_mode == Options::AdjustCtrlPos) {
(*spline)[adj_p_idx] = pos;
//是否完成绘制,若完成,则更新整个图,否则只更新点
if (spline->isFinished())
spline->genSpline();
}
}
last_pos = pos;
update();
}
void QGraphics::mouseReleaseEvent(QMouseEvent *event)
{
had_press = false;
adj_p_idx = -1;
setCursor(Qt::ArrowCursor);
}
void QGraphics::wheelEvent(QWheelEvent *event)
{
auto delta = event->angleDelta();
if(delta.rx() > 0) {
}
}