-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyPainter.cpp
151 lines (133 loc) · 3.65 KB
/
MyPainter.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
141
142
143
144
145
146
147
148
149
150
151
#include "MyPainter.h"
#include <QtMath>
#include <QDebug>
MyPainter::MyPainter(QObject *parent) : QObject(parent)
{
timer=new QTimer(this);
connect(timer,&QTimer::timeout,this,[=]{
drawLength+=10;
update();
//时长超过表示线条绘制完了
if(drawEnd)
timer->stop();
});
}
MyPainter::~MyPainter()
{
qDeleteAll(dataList);
dataList.clear();
}
void MyPainter::draw(QPaintDevice *device)
{
if(drawLength<=0)
return;
QPainter painter(device);
//目前没有设置路径填充规则 TODO
qint64 progress_temp=0;
int i=0;
for(i=0;i<dataList.count()&&progress_temp<drawLength;i++)
{
double len=drawLength-progress_temp;
AbstractElement* item=dataList[i];
//判断begin和end fill,执行到endfill才开始填充begin中的路径
switch (item->type()) {
case 3:
{
const int ele_index=static_cast<BeginFillElement*>(item)->index();
if(drawEndIndex>=ele_index){
item->draw(&painter,0);
}
}
break;
case 4:
drawEndIndex=static_cast<EndFillElement*>(item)->index();
break;
default:
item->draw(&painter,len>item->length()?1:len/item->length());
break;
}
//len不够就是还没画完,后面的就不画了
if(len<item->length())
break;
progress_temp+=item->length();
}
drawEnd=bool(i>=dataList.count()&&progress_temp>=dataLength);
}
void MyPainter::start()
{
timer->start(timerInterval);
drawLength=0;
drawEndIndex=-1;
drawEnd=false;
startTime=QTime::currentTime();
}
void MyPainter::begin(const QPointF &pos)
{
//begin为路径规划开始,状态复位
currentPos=pos;
dataLength=0;
dataList.clear();
}
void MyPainter::end()
{
//end暂无操作
}
void MyPainter::setPenColor(const QColor &color)
{
PenColorElement *ele=new PenColorElement(color);
dataList.push_back(ele);
}
void MyPainter::setPenWidth(int width)
{
PenWidthElement *ele=new PenWidthElement(width);
dataList.push_back(ele);
}
void MyPainter::beginFill(const QColor &color)
{
//开始填充后,beginElement!=null,path在别的函数判断中就能追加路径
fillBeginIndex++;
fillBeginPath=QPainterPath();
fillBeginPath.moveTo(currentPos);
fillBeginElement=new BeginFillElement(fillBeginIndex,color);
dataList.push_back(fillBeginElement);
}
void MyPainter::endFill()
{
if(fillBeginElement){
//end后路径就完成了
fillBeginElement->setFillPath(fillBeginPath);
EndFillElement *ele=new EndFillElement(fillBeginIndex);
dataList.push_back(ele);
fillBeginElement=nullptr;
}
}
void MyPainter::moveTo(const QPointF &pos)
{
if(fillBeginElement){
fillBeginPath.moveTo(pos);
}
currentPos=pos;
}
void MyPainter::lineTo(const QPointF &pos)
{
if(fillBeginElement){
fillBeginPath.lineTo(pos);
}
LineElement *ele=new LineElement(QLineF(currentPos,pos));
dataList.push_back(ele);
dataLength+=ele->length();
currentPos=pos;
}
void MyPainter::arc(const QPointF ¢er, int radius, int startAngle, int spanAngle)
{
QRectF ele_rect=QRectF(center.x()-radius,center.y()-radius,
radius*2,radius*2);
if(fillBeginElement){
//const int len=std::ceil(std::abs(spanAngle)*M_PI*radius/180.0);
fillBeginPath.arcTo(ele_rect,startAngle,spanAngle);
}
ArcElement *ele=new ArcElement(ele_rect,startAngle,spanAngle);
dataList.push_back(ele);
dataLength+=ele->length();
//currentPos=stop;
}