-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
209 lines (182 loc) · 7.1 KB
/
mainwindow.cpp
File metadata and controls
209 lines (182 loc) · 7.1 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSvg>
#include <QPdfWriter>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
validator = new QIntValidator(this);
validator->setTop(1000);
validator->setBottom(0);
ui->lineEdit_ID ->setValidator(validator);
ui->lineEdit_Border ->setValidator(validator);
ui->lineEdit_CornerBox ->setValidator(validator);
ui->lineEdit_Cols ->setValidator(validator);
ui->lineEdit_Rows ->setValidator(validator);
ui->lineEdit_UnitPixels ->setValidator(validator);
ui->lineEdit_Padding ->setValidator(validator);
ui->lineEdit_DPI ->setValidator(validator);
ui->comboBox_Family->addItem("36h11", TagFamilyName::TAG36H11);
ui->comboBox_Family->addItem("36h9", TagFamilyName::TAG36H9);
ui->comboBox_Family->addItem("25h9", TagFamilyName::TAG25H9);
ui->comboBox_Family->addItem("25h7", TagFamilyName::TAG25H7);
ui->comboBox_Family->addItem("16h5", TagFamilyName::TAG16H5);
start_id_ = 0;
border_ = 2;
corner_box_ = 3;
cols_ = 4;
rows_ = 4;
unit_pixels_ = 80;
padding_pixels_ = 300;
tag_family_ = TagFamilyName::TAG36H11;
// for the convenience of converting inches to cm
file_dpi_ = 254;
reset_params();
paint_on_label();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::reset_params()
{
ui->lineEdit_ID ->setText(QString::number(start_id_));
ui->lineEdit_Border ->setText(QString::number(border_));
ui->lineEdit_CornerBox ->setText(QString::number(corner_box_));
ui->lineEdit_Cols ->setText(QString::number(cols_));
ui->lineEdit_Rows ->setText(QString::number(rows_));
ui->lineEdit_UnitPixels ->setText(QString::number(unit_pixels_));
ui->lineEdit_Padding ->setText(QString::number(padding_pixels_));
ui->lineEdit_DPI ->setText(QString::number(file_dpi_));
ui->comboBox_Family ->setCurrentIndex(static_cast<int>(tag_family_));
}
void MainWindow::paint_on_label()
{
p_tag_painter_ = std::make_shared<TagPainter>(tag_family_, start_id_,
border_, corner_box_,
padding_pixels_,
cols_, rows_, unit_pixels_);
auto& tp = p_tag_painter_;
QPixmap pixmap(tp->pixel_count_width(), tp->pixel_count_height());
{
QPainter painter(&pixmap);
tp->PaintTagBoard(painter);
}
// scale the image if needed
double total_ctrl_size = 800;
double total_scale =
total_ctrl_size / static_cast<double>(std::max(pixmap.width(), pixmap.height()));
double tag_ctrl_size = 150;
double tag_scale =
tag_ctrl_size / static_cast<double>(pixmap.width()) * cols_;
if ((total_scale < 1) || (tag_scale < 1)) {
// image size is larger than controlled size
double scale = std::min(total_scale, tag_scale);
pixmap = pixmap.scaled(static_cast<int>(pixmap.width() * scale),
static_cast<int>(pixmap.height() * scale),
Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
ui->label->setPixmap(pixmap);
QString status =
tr("tag_size: ") + QString::number(pixel_to_cm(tp->pixel_count_tag_size())) +
tr(" cm, corner_box: ") + QString::number(pixel_to_cm(tp->pixel_count_corner_box_size())) +
tr(" cm, width: ") + QString::number(pixel_to_cm(tp->pixel_count_width())) +
tr(" cm, height: ") + QString::number(pixel_to_cm(tp->pixel_count_height())) +
tr(" cm.\n") +
tr("tag_size: ") + QString::number(tp->pixel_count_tag_size()) +
tr(" px, corner_box: ") + QString::number(tp->pixel_count_corner_box_size()) +
tr(" px, width: ") + QString::number(tp->pixel_count_width()) +
tr(" px, height: ") + QString::number(tp->pixel_count_height()) +
tr(" px.");
ui->label_status->setText(status);
}
double MainWindow::pixel_to_cm(int pixel) {
return pixel * 1.0 / file_dpi_ * 2.54;
}
void MainWindow::on_pushButtonClose_clicked()
{
close();
}
void MainWindow::on_pushButtonReset_clicked()
{
reset_params();
paint_on_label();
}
void MainWindow::on_pushButtonApply_clicked()
{
start_id_ = ui->lineEdit_ID ->text().toULongLong();
border_ = ui->lineEdit_Border ->text().toInt();
corner_box_ = ui->lineEdit_CornerBox ->text().toInt();
cols_ = ui->lineEdit_Cols ->text().toInt();
rows_ = ui->lineEdit_Rows ->text().toInt();
unit_pixels_ = ui->lineEdit_UnitPixels ->text().toInt();
padding_pixels_ = ui->lineEdit_Padding ->text().toInt();
file_dpi_ = ui->lineEdit_DPI ->text().toInt();
tag_family_ = *(TagFamilyName*) (
ui->comboBox_Family->currentData().data());
paint_on_label();
}
void MainWindow::on_pushButtonSave_clicked()
{
// save to file
double width = pixel_to_cm(p_tag_painter_->pixel_count_width());
double height = pixel_to_cm(p_tag_painter_->pixel_count_height());
int first_id = static_cast<int>(start_id_);
int last_id = static_cast<int>(cols_ * rows_ + start_id_ - 1);
char filename_cstr[80];
std::sprintf(filename_cstr, "id_%d-%d_size_%.2fcmx%.2fcm.svg",
first_id, last_id, width, height);
path_ = QString::fromStdString(filename_cstr);
QString new_path = QFileDialog::getSaveFileName(this, tr("Save to PDF or SVG"),
path_, tr("PDF (*.pdf);;SVG (*.svg)"));
if (new_path.isEmpty()) {
return;
}
enum {
PDF = 0,
SVG = 1,
};
int filetype = PDF;
if (new_path.length() < 4) {
path_ = new_path + ".pdf";
} else {
if (new_path.endsWith(".pdf", Qt::CaseInsensitive)) {
path_ = new_path;
} else if (new_path.endsWith(".svg", Qt::CaseInsensitive)) {
path_ = new_path;
filetype = SVG;
} else {
path_ = new_path + ".pdf";
}
}
qDebug() << "path to save: " << path_;
QString title = tr("AprilTags, ID from ") + QString::number(first_id) +
tr(" to ") + QString::number(last_id);
if (filetype == SVG) {
QSvgGenerator svg_generator;
svg_generator.setFileName(path_);
svg_generator.setSize(QSize(p_tag_painter_->pixel_count_width(),
p_tag_painter_->pixel_count_height()));
svg_generator.setViewBox(QRect(0, 0,
p_tag_painter_->pixel_count_width(),
p_tag_painter_->pixel_count_height()));
svg_generator.setTitle(title);
svg_generator.setResolution(file_dpi_);
QPainter painter(&svg_generator);
p_tag_painter_->PaintTagBoard(painter);
} else if (filetype == PDF) {
QPdfWriter pdf_writer(path_);
pdf_writer.setTitle(title);
pdf_writer.setResolution(file_dpi_);
QPageSize page_size(QSizeF(width * 10.0,
height * 10.0),
QPageSize::Millimeter, "", QPageSize::FuzzyMatch);
pdf_writer.setPageSize(page_size);
pdf_writer.setPageMargins(QMarginsF(0, 0, 0, 0));
pdf_writer.newPage();
QPainter painter(&pdf_writer);
p_tag_painter_->PaintTagBoard(painter);
}
}