-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagearea.cpp
146 lines (130 loc) · 3.86 KB
/
imagearea.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
#include "imagearea.hpp"
#include "ui_imagearea.h"
#include <QMessageBox>
/*!
* \brief ImageArea::ImageArea
* \param parent
*/
ImageArea::ImageArea(QWidget* parent)
: QWidget(parent)
, ui(new Ui::ImageArea)
, rectnotdrawing(true)
{
setX1(0);
setX2(ImageStorage::getInstance().getImageWidth() - 1);
setY1(0);
setX2(ImageStorage::getInstance().getImageHeight() - 1);
ui->setupUi(this);
update();
}
/*!
* \brief ImageArea::~ImageArea
*/
ImageArea::~ImageArea()
{
delete ui;
}
/*!
* \brief ImageArea::rectRecv
* \param rect
*/
void ImageArea::boundsChanged(QRect _bounds)
{
//assert(_bounds.isValid());
bounds = _bounds.normalized();
update();
}
/*!
* \brief ImageArea::paintEvent
* \param e
*/
void ImageArea::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
painter.setPen(Qt::green);
if(!ImageStorage::getInstance().isImageNull()){
if (!rectnotdrawing) {
painter.drawImage(0, 0, ImageStorage::getInstance().getImage());
painter.drawRect(bounds);
} else {
painter.drawImage(0, 0, ImageStorage::getInstance().getImage());
painter.setPen(Qt::green);
for(auto contourIt = contours.cbegin(); contourIt != contours.cend(); ++contourIt){
for(auto pointIt = contourIt->cbegin(); pointIt != (contourIt->cend() - 1); ++pointIt){
painter.drawLine(pointIt->x, pointIt->y, (pointIt + 1)->x, (pointIt + 1)->y);
}
painter.drawLine((contourIt->cend() - 1)->x,
(contourIt->cend() - 1)->y,
(contourIt->cbegin())->x,
(contourIt->cbegin())->y);
}
painter.setPen(Qt::red);
painter.drawLine(x1() - 4, y1(), x1() + 4, y1());
painter.drawLine(x1(), y1() - 4, x1(), y1() + 4);
painter.drawLine(x2() - 4, y2(), x2() + 4, y2());
painter.drawLine(x2(), y2() - 4, x2(), y2() + 4);
painter.drawLine(x2() - 4, y1(), x2() + 4, y1());
painter.drawLine(x2(), y1() - 4, x2(), y1() + 4);
painter.drawLine(x1() - 4, y2(), x1() + 4, y2());
painter.drawLine(x1(), y2() - 4, x1(), y2() + 4);
}
}
e->accept();
}
/*!
* \brief ImageArea::mousePressEvent
* \param e
*/
void ImageArea::mousePressEvent(QMouseEvent* e)
{
if (e->button() == Qt::LeftButton) {
rectnotdrawing = false;
setX1(e->x());
setY1(e->y());
setX1(std::min(e->x(), ImageStorage::getInstance().getImageWidth() - 1));
setY1(std::min(e->y(), ImageStorage::getInstance().getImageHeight() - 1));
}
}
/*!
* \brief ImageArea::mouseReleaseEvent
* \param e
*/
void ImageArea::mouseReleaseEvent(QMouseEvent* e)
{
if (e->button() == Qt::LeftButton) {
setX2(e->x());
setY2(e->y());
setX2(std::min(e->x(), ImageStorage::getInstance().getImageWidth() - 1));
setY2(std::min(e->y(), ImageStorage::getInstance().getImageHeight() - 1));
rectnotdrawing = true;
emit rectChanged(bounds);
update();
}
}
/*!
* \brief ImageArea::mouseMoveEvent
* \param e
*/
void ImageArea::mouseMoveEvent(QMouseEvent* e)
{
if (e->buttons() & Qt::LeftButton) {
setX2(e->x());
setY2(e->y());
setX2(std::min(e->x(), ImageStorage::getInstance().getImageWidth() - 1));
setY2(std::min(e->y(), ImageStorage::getInstance().getImageHeight() - 1));
update();
} else {
}
}
/*!
* \brief ImageArea::frameChanged
* \param _image
*/
void ImageArea::frameChanged(QImage image, Contours _contours)
{
assert(!image.isNull());
ImageStorage::getInstance().setImage(image);
contours = _contours;
rectnotdrawing = true;
update();
}