-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathplateselector.cpp
177 lines (147 loc) · 6.31 KB
/
plateselector.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
#include <QtWidgets>
#include <QtCore>
#include "plateselector.h"
#include "platefile.h"
#include "utils.h"
#include "ui_plateselector.h"
PlateSelector::PlateSelector(const QImage &image, DeleteButtonExistense deleteButtonExistense, QWidget *parent)
: QDialog(parent)
, ui(new Ui::PlateSelector)
{
ui->setupUi(this);
setWindowFlags(windowFlags() | Qt::CustomizeWindowHint | Qt::WindowMaximizeButtonHint);
ui->imageViewerPlateSelector->setImage(image);
ui->plainTextPlateNumber->ensurePolished();
ui->plainTextPlateNumber->setFixedHeight(
(ui->plainTextPlateNumber->document()->documentMargin()
+ QFontMetrics(ui->plainTextPlateNumber->font()).lineSpacing()) * 2);
ui->buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
ui->buttonBox->button(QDialogButtonBox::Reset)->setText(tr("Reset corners"));
ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->setText(tr("Delete && Save"));
// shortcuts
ui->buttonBox->button(QDialogButtonBox::Reset)->setShortcut(Qt::CTRL+Qt::Key_Q);
ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->setShortcut(Qt::CTRL+Qt::Key_D);
ui->buttonBox->button(QDialogButtonBox::Save)->setShortcut(Qt::CTRL+Qt::Key_S);
ui->buttonBox->button(QDialogButtonBox::Help)->setShortcut(QKeySequence::HelpContents);
m_shortCutPlateNumber = new QShortcut(Qt::CTRL+Qt::Key_N, ui->plainTextPlateNumber, SLOT(setFocus()));
m_shortCutPlateRegion = new QShortcut(Qt::CTRL+Qt::Key_R, ui->linePlateRegion, SLOT(setFocus()));
m_shortCutLightOnDark = new QShortcut(Qt::CTRL+Qt::Key_L, ui->checkLightOnDark, SLOT(toggle()));
// set tooltips
Utils::setShortcutTooltips(QList<QPushButton *>()
<< ui->buttonBox->button(QDialogButtonBox::Reset)
<< ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
<< ui->buttonBox->button(QDialogButtonBox::Save)
<< ui->buttonBox->button(QDialogButtonBox::Help));
connect(ui->imageViewerPlateSelector, SIGNAL(dotsChanged(int)), this, SLOT(slotCheckData()));
if(deleteButtonExistense == WithoutDeleteButton)
{
ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(false);
ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->hide();
}
}
PlateSelector::~PlateSelector()
{
delete ui;
}
void PlateSelector::setPlateFile(const PlateFile &plateFile)
{
qDebug() << "Existing polygon:" << plateFile.plateCorners();
foreach(const QPoint &point, plateFile.plateCorners())
{
ui->imageViewerPlateSelector->addDotInImageCoordinates(point - m_selection.topLeft());
}
ui->plainTextPlateNumber->setPlainText(plateFile.plateNumber());
ui->linePlateRegion->setText(plateFile.regionCode());
ui->checkLightOnDark->setChecked(plateFile.plateInverted());
}
QString PlateSelector::plateNumber() const
{
QString result;
const QStringList plainLines = ui->plainTextPlateNumber->toPlainText().trimmed().split('\n', QString::SkipEmptyParts);
foreach(QString plainLine, plainLines)
{
plainLine = plainLine.trimmed();
if(!plainLine.isEmpty())
result += plainLine + '\n';
}
// cut the last '\n'
result.chop(1);
return result;
}
QString PlateSelector::plateRegion() const
{
return ui->linePlateRegion->text().trimmed();
}
bool PlateSelector::lightOnDark() const
{
return ui->checkLightOnDark->isChecked();
}
void PlateSelector::slotCheckData()
{
ui->buttonBox->button(QDialogButtonBox::Save)->setEnabled(
!plateNumber().isEmpty()
&& !ui->imageViewerPlateSelector->selectedPolygon().isEmpty()
);
}
void PlateSelector::slotSave()
{
m_selectedPolygon = ui->imageViewerPlateSelector->selectedPolygon();
for(int i = 0;i < m_selectedPolygon.size();i++)
{
m_selectedPolygon[i] += m_selection.topLeft();
}
qDebug() << "Selected polygon:" << m_selectedPolygon;
accept();
}
void PlateSelector::slotHelp()
{
qDebug("Help");
Utils::help(tr("Click the four corners of the license plate. "
"The lines should align with the edge of the license plate.<br><br>"
"Required fields:"
"<ul>"
"<li>Plate corners - the four corners of the license plate in the image"
"<li>Plate number - the plate number of the license plate"
"</ul>"
"Optional:"
"<ul>"
"<li>Plate region - the country/state of issuance (e.g. California)"
"<li>Light-on-Dark letters - the plate background is darker than the letters (e.g. white characters on a dark blue background)"
"</ul>"
"Hotkeys:"
"<ul>"
"<li>Tab - Move to the next field"
"<li>%1 - Jump to the plate number field"
"<li>%2 - Jump to the plate region field"
"<li>%3 - Toggle Light-on-Dark letters"
"<li>%4 - Reset corners"
"<li>%5 - Delete the corners and save"
"<li>%6 - Save"
"<li>Escape - Cancel"
"<li>%7 - Help"
"</ul>")
.arg(m_shortCutPlateNumber->key().toString())
.arg(m_shortCutPlateRegion->key().toString())
.arg(m_shortCutLightOnDark->key().toString())
.arg(ui->buttonBox->button(QDialogButtonBox::Reset)->shortcut().toString())
.arg(ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->shortcut().toString())
.arg(ui->buttonBox->button(QDialogButtonBox::Save)->shortcut().toString())
.arg(ui->buttonBox->button(QDialogButtonBox::Help)->shortcut().toString())
, this);
}
void PlateSelector::slotClicked(QAbstractButton *button)
{
// reset
if(ui->buttonBox->button(QDialogButtonBox::Reset) == button)
{
qDebug("Reset corners");
ui->imageViewerPlateSelector->clearDots();
}
// delete
else if(ui->buttonBox->button(QDialogButtonBox::RestoreDefaults) == button)
{
qDebug("Delete plate");
ui->imageViewerPlateSelector->clearDots();
accept();
}
}