-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutils.cpp
128 lines (102 loc) · 3.07 KB
/
utils.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
#include <QtWidgets>
#include <QtGui>
#include <QtCore>
#include <cstdlib>
#include "platefile.h"
#include "utils.h"
constexpr double BOUNDING_ENLARGE_WIDTH_FACTOR = 0.15;
constexpr double BOUNDING_ENLARGE_HEIGHT_FACTOR = 0.25;
QString Utils::helpString()
{
return QObject::tr("Help");
}
QString Utils::informationString()
{
return QObject::tr("Information");
}
QString Utils::warningString()
{
return QObject::tr("Warning");
}
QString Utils::errorString()
{
return QObject::tr("Error");
}
QString Utils::fatalErrorString()
{
return QObject::tr("Fatal error");
}
QPoint Utils::centroid(const QPolygon &polygon)
{
QPoint centroid{0,0};
foreach(const auto &point, polygon)
{
centroid.rx() += point.x();
centroid.ry() += point.y();
}
return centroid / polygon.size();
}
void Utils::setShortcutTooltips(const QList<QPushButton *> &buttons)
{
// set tooltips
foreach(auto *button, buttons)
{
button->setToolTip(QObject::tr("Shortcut: %1").arg(button->shortcut().toString()));
}
}
QStringList Utils::imageMatchingWildcard()
{
return QStringList()
<< "*.jpg"
<< "*.JPG"
<< "*.jpeg"
<< "*.JPEG"
<< "*.png"
<< "*.PNG"
;
}
QPoint Utils::fitPoint(const QRect &m_referenceRect, const QPoint &_pos)
{
QPoint pos = _pos;
if(pos.x() > m_referenceRect.topRight().x())
pos.setX(m_referenceRect.topRight().x());
else if(pos.x() < m_referenceRect.topLeft().x())
pos.setX(m_referenceRect.topLeft().x());
if(pos.y() > m_referenceRect.bottomLeft().y())
pos.setY(m_referenceRect.bottomLeft().y());
else if(pos.y() < m_referenceRect.topLeft().y())
pos.setY(m_referenceRect.topLeft().y());
return pos;
}
QRect Utils::selectionForPlate(const PlateFile &plateFile, const QImage &inImage)
{
const QRect &boundingRect = plateFile.plateCorners().boundingRect();
// enlarge
QRect selection = boundingRect.adjusted(-BOUNDING_ENLARGE_WIDTH_FACTOR * boundingRect.width(),
-BOUNDING_ENLARGE_HEIGHT_FACTOR * boundingRect.height(),
BOUNDING_ENLARGE_WIDTH_FACTOR * boundingRect.width(),
BOUNDING_ENLARGE_HEIGHT_FACTOR * boundingRect.height());
selection &= inImage.rect();
return selection;
}
void Utils::help(const QString &message, QWidget *parent)
{
QMessageBox::information(parent, Utils::helpString(), message);
}
void Utils::message(const QString &message, QWidget *parent)
{
QMessageBox::information(parent, Utils::informationString(), message);
}
void Utils::warning(const QString &message, QWidget *parent)
{
QMessageBox::warning(parent, Utils::warningString(), message);
}
void Utils::error(const QString &message, QWidget *parent)
{
QMessageBox::critical(parent, Utils::errorString(), message);
}
void Utils::fatalError(const QString &message, QWidget *parent)
{
QMessageBox::critical(parent, Utils::fatalErrorString(), message);
::exit(1);
}