-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrawwidget.cpp
99 lines (86 loc) · 2.42 KB
/
drawwidget.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
#include "drawwidget.h"
#include<QMouseEvent>
#include <QPainter>
#include <qfiledialog.h>
#include <qmessagebox.h>
DrawWidget::DrawWidget(){
setAutoFillBackground(true);
setPalette(QPalette(Qt::white));
pix=new QPixmap(size());
pix->fill(Qt::white);
ShapeDetecter shapedetecter;
setMinimumSize(600,400);
}
DrawWidget::~DrawWidget(){
delete pix;
delete shapedetecter;
}
void DrawWidget::mousePressEvent(QMouseEvent *e)
{
startPos=e->pos();
}
void DrawWidget::mouseMoveEvent(QMouseEvent *e)
{
QPainter *painter=new QPainter;
QPen pen;
painter->begin(pix);
painter->setPen(pen);
painter->drawLine(startPos,e->pos());
painter->end();
startPos=e->pos();
this->update();
}
void DrawWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawPixmap(QPoint(0,0),*pix);
}
void DrawWidget::resizeEvent(QResizeEvent *event)
{
if(height()>pix->height()||width()>pix->width())
{
QPixmap *newPix=new QPixmap(size());
newPix->fill(Qt::white);
QPainter p(newPix);
p.drawPixmap(QPoint(0,0),*pix);
pix=newPix;
}
QWidget::resizeEvent(event);
}
void DrawWidget::save(){
QString fileName=QFileDialog::getSaveFileName(this,tr("Save File"),"",tr("PNG files(*.png)"));
if(fileName!=""){
if(!pix->save(fileName)){
QMessageBox::critical(this,tr("error"),tr("Could not save file!"));
};
}
}
void DrawWidget::open(){
QString fileName=QFileDialog::getOpenFileName(this,tr("Load File"),"",tr("PNG files(*.png)"));
if(fileName!=""){
if(!pix->load(fileName)){
QMessageBox::critical(this,tr("error"),tr("Could not load file!"));
};
}
}
void DrawWidget::clear(){
QPixmap *newPix=new QPixmap(size());
newPix->fill(Qt::white);
QPainter p(newPix);
p.drawPixmap(QPoint(0,0),*newPix);
pix=newPix;
QWidget::update();
}
void DrawWidget::autoDetect(){
QString currentPath=QDir::currentPath()+"/temp.png";
if(!pix->save(currentPath)){
QMessageBox::critical(this,tr("error"),tr("Could not save file!"));
};
QString fileName=QFileDialog::getSaveFileName(this,tr("output file"),"",tr("PNG files(*.png)"));
if(fileName!=""){
shapedetecter->shapeDetect(currentPath.toStdString(),fileName.toStdString());
}
if(!pix->load(fileName)){
QMessageBox::critical(this,tr("error"),tr("Could not load file!"));
};
}