-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawingWidgetItems.cpp
140 lines (103 loc) · 3.57 KB
/
drawingWidgetItems.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
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
139
140
#include "drawingWidgetItems.h"
#include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <Eigen/Dense>
#include <iostream>
#include "scene.h"
#include "curve2D.h"
using namespace std;
void Curve2DDrawer::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) {
Scene *sce = Scene::get();
Curve2D *curve = sce->getCurve(_curveInd);
// draw lines between control points
QBrush b(curve->brushColor());
QPen p(Qt::black,1,Qt::DashLine,Qt::RoundCap,Qt::RoundJoin);
QPointF points[curve->nbPts()];
painter->setPen(p);
for(unsigned int i=0;i<curve->nbPts();++i) {
Vector2f pt = curve->get(i)->evalAt((float)sce->currentFrame());
points[i].setX(pt[0]);
points[i].setY(pt[1]);
}
painter->drawPolyline(points,curve->nbPts());
p.setColor(curve->penColor());
p.setWidth(curve->penWidth());
p.setStyle(Qt::SolidLine);
//QPen p(curve->penColor(),curve->penWidth(),Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin);
QPainterPath pts = curve->path((float)(sce->currentFrame()));
painter->setPen(p);
painter->drawPath(pts);
painter->fillPath(pts,b);
}
QRectF Curve2DDrawer::boundingRect() const {
Scene *sce = Scene::get();
return QRectF(0,0,sce->width(),sce->height());
}
ControlPoint2D::ControlPoint2D(DrawingWidget *drawingWidget,unsigned int curveInd,unsigned int ptInd)
: _drawingWidget(drawingWidget),_curveInd(curveInd),_ptInd(ptInd) {
Scene *sce = Scene::get();
setFlags(ItemIsMovable | ItemIgnoresTransformations | ItemIsSelectable);
setZValue(1);
Curve2D *c = sce->getCurve(_curveInd);
Vector2f pt = c->evalAnimPt(c->get(_ptInd),(float)(sce->currentFrame()));
setPos(pt[0],pt[1]);
}
void ControlPoint2D::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) {
Scene *sce = Scene::get();
QPen p(Qt::black,_size, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
// test selection
bool curveSelected = sce->isCurveSelected(_curveInd);
bool pointSelected = sce->isPointSelected(_curveInd,_ptInd);
if(curveSelected) {
p.setWidth(_size+_size/3);
}
if(pointSelected) {
p.setWidth(_size+_size/2);
}
painter->setPen(p);
painter->drawPoint(0,0);
p.setWidth(_size-2);
if(pointSelected)
p.setColor(Qt::red);
else if(curveSelected)
p.setColor(Qt::yellow);
else
p.setColor(Qt::blue);
painter->setPen(p);
painter->drawPoint(0,0);
}
QRectF ControlPoint2D::boundingRect() const {
qreal s= _size/2;
return QRectF(-s,-s,_size,_size);
}
void ControlPoint2D::mousePressEvent(QGraphicsSceneMouseEvent *event) {
QGraphicsItem::mousePressEvent(event);
Scene *sce = Scene::get();
if(sce->currentTool()==Scene::EDIT_CURVE && !sce->isAnimated() &&
(event->buttons() & Qt::LeftButton)) {
sce->setSelectedCurve(_curveInd);
sce->setSelectedPoint(_ptInd);
setSelected(true);
_drawingWidget->selectionChanged();
_drawingWidget->refresh();
_drawingWidget->refreshAnimation();
} else {
setFlag(QGraphicsItem::ItemIsMovable,false);
}
}
void ControlPoint2D::mouseMoveEvent (QGraphicsSceneMouseEvent * event) {
QGraphicsItem::mouseMoveEvent(event);
Scene *sce = Scene::get();
if(sce->currentTool()==Scene::EDIT_CURVE && !sce->isAnimated() &&
(event->buttons() & Qt::LeftButton)) {
QPointF p = event->scenePos();
sce->getCurve(_curveInd)->set(_ptInd,p.x(),p.y());
_drawingWidget->refresh();
_drawingWidget->refreshAnimation();
}
}
void ControlPoint2D::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
setSelected(false);
setFlag(QGraphicsItem::ItemIsMovable,true);
QGraphicsItem::mouseReleaseEvent(event);
}