-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcenterwidget.cpp
More file actions
227 lines (224 loc) · 6.13 KB
/
centerwidget.cpp
File metadata and controls
227 lines (224 loc) · 6.13 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
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
#include "centerwidget.h"
#include <QPainter>
#include <QStatusBar>
#include "mainwindow.h"
#include <QFileDialog>
#include <QTextStream>
#include <QDebug>
#include <QColorDialog>
#include <QPen>
#include "shape.h"
CenterWidget::CenterWidget(QWidget *parent) :
QWidget(parent) {
shapes.clear();
beginDraw=false;
isModified=false; //还没有绘制图形,未修改状态
fileName=tr("");
mousePosLabel=new QLabel;
mousePosLabel->setText("");
mousePosLabel->setFixedWidth(110);
MainWindow *p=(MainWindow *)parent;
p->statusBar()->addPermanentWidget(mousePosLabel);
setMinimumSize(500,400);
setMouseTracking(true);
lineWidth=3;
}
CenterWidget::~CenterWidget() {
for(auto shape:shapes)
delete shape;
}
void CenterWidget::paintEvent(QPaintEvent *) {
QPainter p(this);
qDebug()<<lineWidth;
QPen pen(currentColor,lineWidth,Qt::SolidLine);
for(auto shape:shapes)
shape->draw(&p);
if(beginDraw==true){
if(3==drawType)
return;
Shape *temp=nullptr;
switch(drawType){
case 0:
temp=new Line(p1,p2,currentColor,lineWidth);
break;
case 1:
temp=new Ellipse(p1,p2,currentColor,lineWidth);
break;
case 2:
temp=new Rectangle(p1,p2,currentColor,lineWidth);
break;
}
temp->draw(&p);
delete temp;
}
}
void CenterWidget::setDrawType(int type) {
drawType=type;
}
void CenterWidget::setLineWidth(int lw){
lineWidth=lw;
}
void CenterWidget::mousePressEvent(QMouseEvent *e) {
p1=e->pos();
p2=p1;
beginDraw=true;
if(3==drawType)
return;
Shape *temp=nullptr;
QPainter p(this);
switch(drawType) {
case 0:
temp=new Line(p1,p2,currentColor,lineWidth);
break;
case 1:
temp=new Ellipse(p1,p2,currentColor,lineWidth);
break;
case 2:
temp=new Rectangle(p1,p2,currentColor,lineWidth);
break;
default:
;
}
qDebug()<<"xiangpi";
temp->draw(&p);
delete temp;
}
bool CenterWidget::getModifiedFlag() {
return isModified;
}
void CenterWidget::mouseReleaseEvent(QMouseEvent *e) {
p2=e->pos();
beginDraw=false;
Shape *shape=nullptr;
if(p1==p2){
if(3==drawType){
shape=new Point(p2,currentColor,lineWidth);
shapes.append(shape);
isModified=true;
update();
waitingShapes.clear();
return;
}
return;
}
switch(drawType) {
case 0:
shape=new Line(p1,p2,currentColor,lineWidth);
break;
case 1:
shape=new Ellipse(p1,p2,currentColor,lineWidth);
break;
case 2:
shape=new Rectangle(p1,p2,currentColor,lineWidth);
break;
default:
;
}
if(shape){
shapes.append(shape);
isModified=true;
}
waitingShapes.clear();
update();
}
void CenterWidget::mouseMoveEvent(QMouseEvent *e) {
mousePosLabel->setText("X:"+QString::number(e->x())+",Y:"
+QString::number(e->y()));
p2=e->pos();
if(beginDraw==false)
return;
p2=e->pos(); //更新 p2
update(); //绘制 p1 和 p2 之间构成的图形
}
void CenterWidget::newDrawing() { //新建绘图
for(auto shape:shapes)
delete shape;
shapes.clear();
beginDraw=false;
isModified=false;
fileName=tr("");
parentWidget()->setWindowTitle(tr("简单绘图程序 未命名"));
update(); //更新窗口显示
}
void CenterWidget::openDrawing() { //打开绘图
fileName=QFileDialog::getOpenFileName(this,
tr("打开文件对话框"),"/","绘图文件(*.draw);;所有文件(*.*)");
if(fileName==tr(""))
return;
for(auto shape:shapes)
delete shape;
shapes.clear();
beginDraw=false;
isModified=false;
openFile(fileName); //存储文件
parentWidget()->setWindowTitle(tr("简单绘图程序 ")+fileName);
update();
}
void CenterWidget::saveDrawing() { //存储绘图
if(fileName==tr("")) { //文件名为空,弹出文件选择对话框
fileName=QFileDialog::getSaveFileName(this,
tr("保存文件对话框"),"/","绘图文件(*.draw);;所有文件(*.*)");
if(fileName==tr(""))
return;
}
saveFile(fileName); //存储文件
parentWidget()->setWindowTitle(tr("简单绘图程序 ")+fileName);
isModified=false;
}
void CenterWidget::saveFile(QString fileName) { //写文件操作
QFile file(fileName);
if(file.open(QFile::WriteOnly|QFile::Truncate)) {
QTextStream out(&file);
out<<shapes.length()<<endl;
for(auto shape:shapes)
shape->save(out);
file.close();
}
}
void CenterWidget::openFile(QString fileName) { //读文件操作
QFile file(fileName);
if(file.open(QFile::ReadOnly)) { //只读方式打开
QTextStream in(&file);
int nums;
in>>nums;
int type;
Shape *curShape;
for(int i=0;i<nums;++i){
in>>type;
switch(type){
case 0:
curShape=Line::read(in);
break;
case 1:
curShape=Ellipse::read(in);
break;
case 2:
curShape=Rectangle::read(in);
break;
case 3:
curShape=Point::read(in);
}
shapes.append(curShape);
}
file.close();
}
}
void CenterWidget::setCurrentColor(const QColor &color){
currentColor=color;
}
void CenterWidget::undo(){
if(shapes.empty())
return;
Shape* temp=shapes.top();
shapes.pop();
waitingShapes.push(temp);
update();
}
void CenterWidget::redo(){
if(waitingShapes.empty())
return;
Shape* temp=waitingShapes.top();
waitingShapes.pop();
shapes.push(temp);
update();
}