-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathimageviewerobserver.cpp
254 lines (197 loc) · 7.09 KB
/
imageviewerobserver.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <QtWidgets>
#include <QtGui>
#include "imageviewerobserver.h"
#include "plateselector.h"
#include "imagefile.h"
#include "settings.h"
#include "utils.h"
ImageViewerObserver::ImageViewerObserver(QWidget *parent)
: ImageViewerBase(parent)
, m_imageFile(nullptr)
, m_plateFileIndexForEditing(-1)
{}
void ImageViewerObserver::setImageFile(ImageFile *imageFile)
{
// save image info
m_imageFile = imageFile;
// saved license plates
m_polygons = Polygons::fromPlates(m_imageFile->plates());
}
void ImageViewerObserver::reset()
{
m_imageFile = nullptr;
m_polygons.clear();
ImageViewerBase::reset();
}
void ImageViewerObserver::mousePressEvent(QMouseEvent *e)
{
e->accept();
// start to paint a selection rectangle
m_selection.reset();
// the point is outside the image or nothing to select
if(pixmap().isNull())
return;
m_selection.start(e->pos());
update();
}
void ImageViewerObserver::mouseMoveEvent(QMouseEvent *e)
{
e->accept();
// no selection or the point is outside the image
if(!m_selection.hasSelection())
return;
m_selection.move(e->pos());
update();
}
void ImageViewerObserver::mouseReleaseEvent(QMouseEvent *e)
{
e->accept();
// no selection/selection is too small, or the point is outside the image
if(!m_selection.selectionIsFine())
{
QPoint pos = (e->pos() - pixmapBoundingRect().topLeft()) / zoom();
// handle click
for(int i = 0;i < m_imageFile->plates().size();i++)
{
const PlateFile &plateFile = m_imageFile->plates().at(i);
if(plateFile.plateCorners().containsPoint(pos, Qt::OddEvenFill))
{
m_fixedSelection = Utils::selectionForPlate(plateFile, image());
m_plateFileIndexForEditing = i;
QTimer::singleShot(0, this, SLOT(slotShowPlateSelector()));
break;
}
}
return;
}
m_selection.stop();
m_fixedSelection = m_selection.selection();
// relative to the image itself
m_fixedSelection.moveTopLeft(m_fixedSelection.topLeft() - pixmapBoundingRect().topLeft());
// take zoom into account
m_fixedSelection.moveTopLeft(m_fixedSelection.topLeft() * (1 / zoom()));
m_fixedSelection.setSize(m_fixedSelection.size() * (1 / zoom()));
// fix possible division errors:
// - negative coordinates
// - too large size
m_fixedSelection &= image().rect();
if(!m_fixedSelection.isValid())
{
qWarning() << "Selection rectangle" << m_fixedSelection << "is invalid for image size" << image().size();
return;
}
qDebug() << "Actual selection: " << m_fixedSelection;
m_plateFileIndexForEditing = -1;
QTimer::singleShot(0, this, SLOT(slotShowPlateSelector()));
update();
}
bool ImageViewerObserver::event(QEvent *event)
{
if(event->type() == QEvent::ToolTip && SETTINGS_GET_BOOL(SETTING_TOOLTIPS))
{
const QPoint &pos = (mapFromGlobal(QCursor::pos()) - pixmapBoundingRect().topLeft()) / zoom();
// handle click
foreach(const PlateFile &plateFile, m_imageFile->plates())
{
if(plateFile.plateCorners().containsPoint(pos, Qt::OddEvenFill))
{
QToolTip::showText(QCursor::pos(),
QString(
// "<table cellspacing=5>"
//"<tr><td><b>%1</b></td><td>%2</td></tr>"
//"<tr><td><b>%3</b></td><td>%4</td></tr>"
//"<tr><td><b>%5</b></td><td>%6</td></tr>"
// "</table>"
"<b>%1</b> %2<br>"
"<b>%3</b> %4<br>"
"<b>%5</b> %6"
)
.arg(tr("Plate number:"))
.arg(plateFile.plateNumber())
.arg(tr("Plate region:"))
.arg(plateFile.regionCode())
.arg(tr("Light-on-Dark:"))
.arg(plateFile.plateInverted() ? tr("yes") : tr("no")),
nullptr,
QRect());
break;
}
}
event->accept();
return true;
}
return QWidget::event(event);
}
void ImageViewerObserver::pixmapAboutToBeChanged()
{
m_selection.reset();
}
void ImageViewerObserver::pixmapChanged()
{
m_selection.setReferenceRect(pixmapBoundingRect());
m_polygons.setZoom(zoom());
}
void ImageViewerObserver::draw(QPainter *painter)
{
if(!painter)
return;
painter->translate(pixmapBoundingRect().topLeft());
m_polygons.draw(painter);
painter->resetTransform();
m_selection.draw(painter);
}
void ImageViewerObserver::slotShowPlateSelector()
{
qDebug() << "Image selection:" << m_fixedSelection;
const QImage &frame = image().copy(m_fixedSelection);
if(frame.isNull())
{
qWarning() << "Cannot edit a null image from region" << m_fixedSelection;
return;
}
PlateSelector selector(frame, PlateSelector::WithDeleteButton, this);
selector.setSelectionRect(m_fixedSelection);
if(m_plateFileIndexForEditing >= 0)
selector.setPlateFile(m_imageFile->plates().at(m_plateFileIndexForEditing));
if(selector.exec() != PlateSelector::Accepted)
return;
PlateFileList plateFileList = m_imageFile->plates();
PlateFile *plateFile = nullptr;
// existing plate
if(m_plateFileIndexForEditing >= 0)
{
qDebug("Editing existing plate index %d", m_plateFileIndexForEditing);
// empty polygon -> delete it
if(selector.selectedPolygon().isEmpty())
plateFileList.removeAt(m_plateFileIndexForEditing);
else
plateFile = &plateFileList[m_plateFileIndexForEditing];
}
// new non-empty plate
else if(!selector.selectedPolygon().isEmpty())
{
qDebug("Adding new plate with index %d", plateFileList.size());
PlateFile newPlateFile(m_imageFile->plateNameTemplate(), plateFileList.size());
newPlateFile.setImageFile(m_imageFile->fileInfo().fileName());
newPlateFile.setImageWidth(image().width());
newPlateFile.setImageHeight(image().height());
plateFileList.append(newPlateFile);
plateFile = &plateFileList.last();
}
// nothing has happend, just return
else
return;
// update with entered data
if(plateFile)
{
plateFile->setPlateCorners(selector.selectedPolygon());
plateFile->setRegionCode(selector.plateRegion());
plateFile->setPlateNumber(selector.plateNumber());
plateFile->setPlateInverted(selector.lightOnDark());
}
m_imageFile->setPlates(plateFileList);
emit platesChanged();
m_polygons = Polygons::fromPlates(m_imageFile->plates());
m_polygons.setZoom(zoom());
update();
}