-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaintWidget.cpp
167 lines (135 loc) · 3.42 KB
/
PaintWidget.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
#include <PaintWidget.h>
#include <QPainter>
#include <QMouseEvent>
#include <iostream>
#include <assert.h>
using namespace std;
PaintWidget::PaintWidget(QWidget *parent)
:QWidget(parent)
{
setMinimumSize(800, 600); //设置绘图区域窗体的最小大小
setAutoFillBackground(true); //自动设定背景颜色
setPalette(QPalette(QColor(200,200,200))); //设置调色板的颜色为白色
m_pixmap = new QPixmap(this->size()); //这个pixmap对象用来接受准备绘制到空间的内容
m_pixmap->fill(Qt::white); //填充这个图片的背景是白色
//cout << "pixmapsize" << m_pixmap->width() << ", " << m_pixmap->height() << endl;
m_currentfig = NULL;
m_polygon = NULL;
}
PaintWidget::~PaintWidget()
{
//析构时,释放vector中每个图形
for(auto it=m_FigrueArray.begin();it!=m_FigrueArray.end();++it)
{
delete *it;
}
}
void PaintWidget::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
m_pixmap->fill(Qt::white);
//cout <<"数量: "<< m_FigrueArray.size() << endl;
//cout << "1" << endl;
for(auto it=m_FigrueArray.begin();it!=m_FigrueArray.end();++it)
{
(*it)->Draw(*m_pixmap);
}
if(m_currentfig != NULL)
{
m_currentfig->Draw(*m_pixmap);
//cout << "paintcurrent" << endl;
}
//m_pixmap->save("pixmap.jpg");
QPainter painter(this);
painter.drawPixmap(QPoint(0, 0), *m_pixmap);
//cout << "2" << endl;
}
void PaintWidget::mousePressEvent(QMouseEvent *event)
{
//cout << "mousepress" << endl;
if(event->button()==Qt::LeftButton)
{
/*m_leftpressed = true;*/
m_lastPos = event->pos();
if(m_paintmode==CFREEHAND)
{
vertex.push_back(m_lastPos);
}
else{}
if(m_paintmode==CPOLYGON)
{
m_leftpressed = false;
//vertex.push_back(m_lastPos);
if(m_polygon==NULL)
{
//cout << "m_polygon is NULL" << endl;
m_polygon = new CPolygon();
}
assert(m_polygon != NULL);
m_polygon->isclosed = false;
m_polygon->addVertex(m_lastPos);
m_currentfig = m_polygon;
m_currentfig->setPaintStyle(m_paintstyle);
update();
}
else{
m_leftpressed = true;
}
}
if(event->button() == Qt::RightButton)
{
if(m_paintmode==CPOLYGON)
{
m_polygon->isclosed = true;
m_currentfig = m_polygon;
m_currentfig->setPaintStyle(m_paintstyle);
m_FigrueArray.push_back(m_currentfig);
m_currentfig = NULL;
m_polygon = NULL;
}
update();
}
}
void PaintWidget::mouseMoveEvent(QMouseEvent *event)
{
if(m_leftpressed)
{
//cout << "mousemove" << endl;
if(m_currentfig)
{
delete m_currentfig;
m_currentfig = NULL;
}
switch (m_paintmode)
{
case CLINE: m_currentfig = new CLine(m_lastPos.x(), m_lastPos.y(), event->pos().x(), event->pos().y());
break;
case CRECT: m_currentfig = new CRect(m_lastPos.x(), m_lastPos.y(), event->pos().x(), event->pos().y());
break;
case CELLIPSE: m_currentfig = new CEllipse(m_lastPos.x(), m_lastPos.y(), event->pos().x(), event->pos().y());
break;
case CFREEHAND: vertex.push_back(event->pos()); m_currentfig = new CPolygon(vertex);
case CPOLYGON: break;
default:
break;
}
m_currentfig->setPaintStyle(m_paintstyle);
update();
}
}
void PaintWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton&&m_leftpressed)
{
//鼠标释放时,将当前绘制的图形加入到vector中
if(m_currentfig != NULL)
{
m_FigrueArray.push_back(m_currentfig);
m_currentfig = NULL;
vertex.clear();
//cout << m_FigrueArray.size() << endl;
}
m_leftpressed = false;
}
update();
}