-
Notifications
You must be signed in to change notification settings - Fork 0
/
painttree_one.cpp
242 lines (230 loc) · 8.34 KB
/
painttree_one.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "painttree_one.h"
#include <QPainter>
#include <QPixmap>
int paintTree_one::iCount;//已经画出的步骤
paintTree_one::paintTree_one(QWidget *parent) :
QFrame(parent)
{
this->iCount=-1;
this->setStyleSheet ("border: 1px solid gray;background-color:white"); //设置边框颜色
// this->setAutoFillBackground(true);
// this->setPalette(Qt::white); //背景色为白色
}
void paintTree_one::paintEvent(QPaintEvent *event)
{
paintNext();//画下一步
}
void paintTree_one::resizeEvent(QResizeEvent *event) //获得子控件大小,随窗口变化而变化
{
iWidth=this->width();//画图窗口宽度
iHeight=this->height();//画图窗口高度
//this->resize(QSize(iWidth,iHeight));
}
QString paintTree_one::charToName(char &cCh)
{
QString temp;
if(cCh>='a'&&cCh<='e')
temp.sprintf(":/image/lowercase/%c.png",cCh);
else
temp.sprintf(":/image/uppercase/%c.png",cCh);
return temp;
}
void paintTree_one::setTreeNode(treeNode *t)
{
this->m_tree=t;
}
void paintTree_one::paintNext()
{
vector<Node> nTemp;
nTemp=this->m_tree->tree;
int space_x, space_y;
int lowCount=0;//树中出现终结符的个数,方便计算空间间隔宽度
int upCount=0;//树中出现终结符的个数,方便计算空间间隔高度
for(int j=nTemp.size()-1;j>=0;j--)
{
if(nTemp[j].y==0)
{
lowCount=nTemp[j].x+1;//x坐标从0开始的,所以要加一
break;
}
else
{
lowCount=0;
continue;
}
}
for(int j=nTemp.size()-1;j>=0;j--)
{
char temp=nTemp[j].key;
if(temp=='A' || temp=='B' || temp=='S' )
{
upCount=nTemp[j].y+1; //y坐标从0开始的,所以要加一
break;
}
else
{
upCount=0;
continue;
}
}
// int space_x=iWidth/(lowCount+1);//横向间隔
// int space_y=iHeight/(upCount+1);//纵向间隔
space_x=iWidth/(lowCount+1);//横向间隔
if(upCount==0)
space_y=iHeight/3;
else
space_y=iHeight/(upCount+1);//纵向间隔
QString fileName;
QPainter painter;
//QPen pen;
//pen.setWidth(2);//设置线的宽度为2个像素
int currentX;
int currentY;
int x,y,nY;
if(iCount<nTemp.size()&&iCount>=0)
{
for(int i=0;i<=iCount;i++)
{
painter.begin(this);
//painter.setPen(pen);
fileName=charToName(nTemp[i].key);
currentX=space_x*(nTemp[i].x+1);
currentY=iHeight-space_y*(nTemp[i].y+1);
QPixmap picture;
picture.load(fileName);
// painter.drawPixmap(currentX,currentY,picture);
if(nTemp[i].key=='A')//画A->Ab或A->b的直线
{
if(nTemp[i].y>1)
{
painter.drawPixmap(currentX,currentY,picture);
painter.drawLine(space_x*(nTemp[i-2].x+1)+16,iHeight-space_y*(nTemp[i-2].y+1)+16,currentX+16,currentY+16);
painter.drawLine(space_x*(nTemp[i-1].x+1)+16,iHeight-space_y*(nTemp[i-1].y+1)+16,currentX+16,currentY+16);
painter.end();
}
else
{
painter.drawPixmap(currentX,currentY,picture);
painter.drawLine(currentX+16,currentY+space_y*nTemp[i].y+16,currentX+16,currentY+16);
painter.end();
}
}
else if(nTemp[i].key=='B')//画B->d的直线
{
findXY('A',x,y);
nY=y;
painter.drawPixmap(currentX,iHeight-space_y*(nY+1),picture);
painter.drawLine(currentX+16,currentY+space_y*nTemp[i].y+16,currentX+16,iHeight-space_y*(nY+1)+16);
painter.end();
}
else if(nTemp[i].key=='S')//画S->aAcBe的所有直线
{
painter.drawPixmap(currentX,currentY,picture);
findXY('A',x,y);
nY=y;
findXY('a',x,y);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(nY+1)+16,currentX+16,currentY+16);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(nY+1)+16,space_x*(x+1)+16,iHeight-space_y*(y+1)+16);
findXY('A',x,y);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(y+1)+16,currentX+16,currentY+16);
findXY('c',x,y);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(y+1)+16,currentX+16,currentY+16);
findXY('B',x,y);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(nY+1)+16,currentX+16,currentY+16);
findXY('e',x,y);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(nY+1)+16,currentX+16,currentY+16);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(nY+1)+16,space_x*(x+1)+16,iHeight-space_y*(y+1)+16);
painter.end();
}
else
{
painter.drawPixmap(currentX,currentY,picture);
painter.end();
}
}
}
else if(iCount>=0)
{
painter.begin(this);
for(int i=0;i<=nTemp.size();i++)
{
fileName=charToName(nTemp[i].key);
currentX=space_x*(nTemp[i].x+1);
currentY=iHeight-space_y*(nTemp[i].y+1);
QPixmap picture;
picture.load(fileName);
// painter.drawPixmap(currentX,currentY,picture);
if(nTemp[i].key=='A')//画A->Ab或A->b的直线
{
if(nTemp[i].y>1)
{
painter.drawPixmap(currentX,currentY,picture);
painter.drawLine(space_x*(nTemp[i-2].x+1)+16,iHeight-space_y*(nTemp[i-2].y+1)+16,currentX+16,currentY+16);
painter.drawLine(space_x*(nTemp[i-1].x+1)+16,iHeight-space_y*(nTemp[i-1].y+1)+16,currentX+16,currentY+16);
}
else
{
painter.drawPixmap(currentX,currentY,picture);
painter.drawLine(currentX+16,currentY+space_y*nTemp[i].y+16,currentX+16,currentY+16);
}
}
else if(nTemp[i].key=='B')//画B->d的直线
{
findXY('A',x,y);
nY=y;
painter.drawPixmap(currentX,iHeight-space_y*(nY+1),picture);
painter.drawLine(currentX+16,currentY+space_y*nTemp[i].y+16,currentX+16,iHeight-space_y*(nY+1)+16);
}
else if(nTemp[i].key=='S')//画S->aAcBe的所有直线
{
painter.drawPixmap(currentX,currentY,picture);
findXY('A',x,y);
nY=y;
findXY('a',x,y);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(nY+1)+16,currentX+16,currentY+16);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(nY+1)+16,space_x*(x+1)+16,iHeight-space_y*(y+1)+16);
findXY('A',x,y);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(y+1)+16,currentX+16,currentY+16);
findXY('c',x,y);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(y+1)+16,currentX+16,currentY+16);
findXY('B',x,y);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(nY+1)+16,currentX+16,currentY+16);
findXY('e',x,y);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(nY+1)+16,currentX+16,currentY+16);
painter.drawLine(space_x*(x+1)+16,iHeight-space_y*(nY+1)+16,space_x*(x+1)+16,iHeight-space_y*(y+1)+16);
}
else
painter.drawPixmap(currentX,currentY,picture);
}
painter.end();
}
}
void paintTree_one::findXY(const char cCh, int &x, int &y)
{
vector<Node> nTemp;
nTemp=m_tree->tree;
if(cCh>='a'&&cCh<='e')//从左到右查找
{
for(int i=0;i<nTemp.size();i++)
{
if(nTemp[i].key==cCh)
{
x=nTemp[i].x;
y=nTemp[i].y;
return;
}
}
}
else
{
for(int i=nTemp.size();i>=0;i--)//从右到左查找
{
if(nTemp[i].key==cCh)
{
x=nTemp[i].x;
y=nTemp[i].y;
return;
}
}
}
}