-
Notifications
You must be signed in to change notification settings - Fork 3
/
imageviewer.cpp
566 lines (495 loc) · 17.4 KB
/
imageviewer.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#include "imageviewer.h"
#include "ui_imageviewer.h"
#include "pcolor.h"
#include <QtWidgets>
#include <QFileDialog>
#include <QMessageBox>
#include <QPrintDialog>
/* Qt 5.15
* WARNING: 'pixmap' is deprecated: Use the other overload which returns QPixmap by-value
* replace imageLabel->pixmap() with imageLabel->pixmap(Qt::ReturnByValue)
* replace
* Q_ASSERT(imageLabel->pixmap());
* with
* Q_ASSERT(!imageLabel->pixmap(Qt::ReturnByValue).isNull());
**/
static void initializeImageFileDialog(QFileDialog &dialog, QFileDialog::AcceptMode acceptMode)
{
static bool firstDialog = true;
if (firstDialog) {
firstDialog = false;
const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
dialog.setDirectory(picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.last());
}
QStringList mimeTypeFilters;
const QByteArrayList supportedMimeTypes = acceptMode == QFileDialog::AcceptOpen
? QImageReader::supportedMimeTypes() : QImageWriter::supportedMimeTypes();
foreach (const QByteArray &mimeTypeName, supportedMimeTypes)
mimeTypeFilters.append(mimeTypeName);
mimeTypeFilters.sort();
dialog.setMimeTypeFilters(mimeTypeFilters);
dialog.selectMimeTypeFilter("image/jpeg");
if (acceptMode == QFileDialog::AcceptSave)
dialog.setDefaultSuffix("jpg");
}
ImageViewer::ImageViewer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ImageViewer)
{
ui->setupUi(this);
this->setAcceptDrops(true);
imageLabel = new QLabel;
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true);
QFont font("Microsoft YaHei", 10, 75);
imageLabel->setFont(font);
imageLabel->setAlignment(Qt::AlignCenter);
imageLabel->setText("Start by dragging images here");
imageLabel->resize(500, 400);
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel);
setCentralWidget(scrollArea);
createDockWindows();
icon = new QIcon(":/icon/images/icon/colormap.png");
trayMenu = new QMenu;
systemTray = new QSystemTrayIcon;
systemTray->setIcon(*icon);
systemTray->setToolTip("RangeMap Visualization"); // context help
minimumAct = new QAction("Minimum Window");
maximumAct = new QAction("Maximum Window");
restoreAct = new QAction("Display");
quitAct = new QAction("Exit");
connect(minimumAct, SIGNAL(triggered()), this, SLOT(hide()));
connect(maximumAct, SIGNAL(triggered()), this, SLOT(showMaximized()));
connect(restoreAct, SIGNAL(triggered()), this, SLOT(showNormal()));
connect(quitAct, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(systemTray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason)));
trayMenu->addAction(minimumAct);
trayMenu->addAction(maximumAct);
trayMenu->addAction(restoreAct);
trayMenu->addSeparator();
trayMenu->addAction(quitAct);
systemTray->setContextMenu(trayMenu);
systemTray->show();
setWindowIcon(*icon);
setWindowTitle(tr("RangeMap Visualization"));
resize(1000, 400);
openAct = ui->actionOpen;
saveAsAct = ui->actionSaveAs;
printAct = ui->actionPrint;
exitAct = ui->actionExit;
zoomInAct = ui->actionZoomIn;
zoomOutAct = ui->actionZoomOut;
normalSizeAct = ui->actionNormalSize;
fitToWindowAct = ui->actionFitToWindow;
aboutQtAct = ui->actionAboutQt;
// connect(openAct,SIGNAL(triggered()),this,SLOT(open()));
// connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
// connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
// connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
// connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
// connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
// connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
// connect(aboutQtAct, SIGNAL(triggered()), this, SLOT(about()));
}
ImageViewer::~ImageViewer()
{
delete ui;
}
void ImageViewer::optionGroups_clicked() {
if (raw_image.isNull()) return;
if (channelsMormThan1(raw_image)) return;
QImage copy_img = raw_image.copy();
// bugs when load kitti.png
if (copy_img.format() == QImage::Format_Grayscale16)
copy_img = convertGray16ToGray8(copy_img);
switch (optionGroups->checkedId()) {
case 0:
setImage(raw_image);
break;
case 1:
setImage(image2autumn(copy_img));
break;
case 2:
setImage(image2bone(copy_img));
break;
case 3:
setImage(image2cool(copy_img));
break;
case 4:
setImage(image2hot(copy_img));
break;
case 5:
setImage(image2hsv(copy_img));
break;
case 6:
setImage(image2jet(copy_img));
break;
case 7:
setImage(image2ocean(copy_img));
break;
case 8:
setImage(image2parula(copy_img));
break;
case 9:
setImage(image2pink(copy_img));
break;
case 10:
setImage(image2rainbow(copy_img));
break;
case 11:
setImage(image2spring(copy_img));
break;
case 12:
setImage(image2summer(copy_img));
break;
case 13:
setImage(image2turbo(copy_img));
break;
case 14:
setImage(image2winter(copy_img));
break;
}
}
void ImageViewer::createDockWindows() {
dock = new QDockWidget(tr("Options"), this);
dock->setFeatures(QDockWidget::DockWidgetMovable);
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea, dock);
const int options = 15;
// init QRadioButton
QRadioButton * radioButton[options];
for (int i = 0; i < options; ++i) {
radioButton[i] = new QRadioButton(dock);
}
radioButton[0]->setText("Raw");
radioButton[1]->setText("Autumn");
radioButton[2]->setText("Bone");
radioButton[3]->setText("Cool");
radioButton[4]->setText("Hot");
radioButton[5]->setText("Hsv");
radioButton[6]->setText("Jet");
radioButton[7]->setText("Ocean");
radioButton[8]->setText("Parula");
radioButton[9]->setText("Pink");
radioButton[10]->setText("Rainbow");
radioButton[11]->setText("Spring");
radioButton[12]->setText("Summer");
radioButton[13]->setText("Turbo");
radioButton[14]->setText("Winter");
radioButton[0]->setChecked(true);
// init colorbar
QImage grayscale = QImage(256, 10, QImage::Format_Indexed8);
uchar* line;
for(int y = 0; y < grayscale.height(); y++) {
line = grayscale.scanLine(y);
for(int x = 0; x < grayscale.width(); x++) {
line[x] = x;
}
}
QLabel* colorbar[options];
for (int i = 0; i < options; ++i) {
colorbar[i] = new QLabel(this);
}
colorbar[0]->setPixmap(QPixmap::fromImage(grayscale));
colorbar[1]->setPixmap(QPixmap::fromImage(image2autumn(grayscale)));
colorbar[2]->setPixmap(QPixmap::fromImage(image2bone(grayscale)));
colorbar[3]->setPixmap(QPixmap::fromImage(image2cool(grayscale)));
colorbar[4]->setPixmap(QPixmap::fromImage(image2hot(grayscale)));
colorbar[5]->setPixmap(QPixmap::fromImage(image2hsv(grayscale)));
colorbar[6]->setPixmap(QPixmap::fromImage(image2jet(grayscale)));
colorbar[7]->setPixmap(QPixmap::fromImage(image2ocean(grayscale)));
colorbar[8]->setPixmap(QPixmap::fromImage(image2parula(grayscale)));
colorbar[9]->setPixmap(QPixmap::fromImage(image2pink(grayscale)));
colorbar[10]->setPixmap(QPixmap::fromImage(image2rainbow(grayscale)));
colorbar[11]->setPixmap(QPixmap::fromImage(image2spring(grayscale)));
colorbar[12]->setPixmap(QPixmap::fromImage(image2summer(grayscale)));
colorbar[13]->setPixmap(QPixmap::fromImage(image2turbo(grayscale)));
colorbar[14]->setPixmap(QPixmap::fromImage(image2winter(grayscale)));
// init widget
QWidget * mywid = new QWidget(this);
QVBoxLayout *vLayout = new QVBoxLayout;
optionGroups = new QButtonGroup(this);
for (int i = 0; i < options; ++i) {
optionGroups->addButton(radioButton[i], i);
connect(radioButton[i], SIGNAL(clicked()), this, SLOT(optionGroups_clicked()));
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(radioButton[i]);
hLayout->addWidget(colorbar[i]);
vLayout->addLayout(hLayout);
}
mywid->setLayout(vLayout);
dock->setWidget(mywid);
// QWidget * mywid = new QWidget(this);
// QGridLayout *gridLayout = new QGridLayout;
// optionGroups = new QButtonGroup(this);
// for (int i = 0; i < options; ++i) {
// optionGroups->addButton(radioButton[i], i);
// connect(radioButton[i], SIGNAL(clicked()), this, SLOT(optionGroups_clicked()));
// gridLayout->addWidget(radioButton[i]);
// }
// mywid->setLayout(gridLayout);
// dock->setWidget(mywid);
}
void ImageViewer::idPcolor() {
if (raw_image.isNull()) return;
if (channelsMormThan1(raw_image)) return;
QImage copy_img = raw_image.copy();
// bugs when load kitti.png
if (copy_img.format() == QImage::Format_Grayscale16)
copy_img = convertGray16ToGray8(copy_img);
switch (optionGroups->checkedId()) {
case 0:
setImage(raw_image);
break;
case 1:
setImage(image2autumn(copy_img));
break;
case 2:
setImage(image2bone(copy_img));
break;
case 3:
setImage(image2cool(copy_img));
break;
case 4:
setImage(image2hot(copy_img));
break;
case 5:
setImage(image2hsv(copy_img));
break;
case 6:
setImage(image2jet(copy_img));
break;
case 7:
setImage(image2ocean(copy_img));
break;
case 8:
setImage(image2parula(copy_img));
break;
case 9:
setImage(image2pink(copy_img));
break;
case 10:
setImage(image2rainbow(copy_img));
break;
case 11:
setImage(image2spring(copy_img));
break;
case 12:
setImage(image2summer(copy_img));
break;
case 13:
setImage(image2turbo(copy_img));
break;
case 14:
setImage(image2winter(copy_img));
break;
}
}
void ImageViewer::setImage(const QImage &newImage)
{
image = newImage;
imageLabel->setPixmap(QPixmap::fromImage(image));
scaleFactor = 1.0;
scrollArea->setVisible(true);
printAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();
if (!fitToWindowAct->isChecked())
imageLabel->adjustSize();
}
bool ImageViewer::loadFile(const QString &fileName)
{
QImageReader reader(fileName);
reader.setAutoTransform(true);
const QImage newImage = reader.read();
if (newImage.isNull()) {
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
tr("Cannot load %1: %2")
.arg(QDir::toNativeSeparators(fileName), reader.errorString()));
return false;
}
raw_image = newImage;
setImage(newImage);
idPcolor();
setWindowFilePath(fileName);
const QString message = tr("Opened \"%1\", %2x%3, Depth: %4")
.arg(QDir::toNativeSeparators(fileName)).arg(image.width()).arg(image.height()).arg(image.depth());
statusBar()->showMessage(message);
return true;
}
void ImageViewer::updateActions()
{
saveAsAct->setEnabled(!image.isNull());
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
}
void ImageViewer::scaleImage(double factor)
{
Q_ASSERT(imageLabel->pixmap());
scaleFactor *= factor;
imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
adjustScrollBar(scrollArea->verticalScrollBar(), factor);
zoomInAct->setEnabled(scaleFactor < 3.0);
zoomOutAct->setEnabled(scaleFactor > 0.333);
}
void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
{
scrollBar->setValue(int(factor * scrollBar->value()
+ ((factor - 1) * scrollBar->pageStep()/2)));
}
bool ImageViewer::saveFile(const QString &fileName)
{
QImageWriter writer(fileName);
if (!writer.write(image)) {
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
tr("Cannot write %1: %2")
.arg(QDir::toNativeSeparators(fileName)), writer.errorString());
return false;
}
const QString message = tr("Wrote \"%1\"").arg(QDir::toNativeSeparators(fileName));
statusBar()->showMessage(message);
return true;
}
void ImageViewer::dragEnterEvent(QDragEnterEvent *event)
{
if (event->type() != QEvent::DragEnter) return;
// for linux:event->mimeData()->urls()[0].toLocalFile().right(3).compare("bmp")
if (!event->mimeData()->urls()[0].toLocalFile().right(3).compare("bmp") ||
!event->mimeData()->urls()[0].toLocalFile().right(3).compare("jpg") ||
!event->mimeData()->urls()[0].toLocalFile().right(3).compare("pbm") ||
!event->mimeData()->urls()[0].toLocalFile().right(3).compare("pgm") ||
!event->mimeData()->urls()[0].toLocalFile().right(3).compare("png") ||
!event->mimeData()->urls()[0].toLocalFile().right(3).compare("ppm") ||
!event->mimeData()->urls()[0].toLocalFile().right(3).compare("xbm") ||
!event->mimeData()->urls()[0].toLocalFile().right(3).compare("xpm"))
{
event->acceptProposedAction();
}
else
{
event->ignore();
}
}
void ImageViewer::dropEvent(QDropEvent *event) {
if (event->type() != QEvent::Drop) return;
QList<QUrl> urls = event->mimeData()->urls();
QString fileName = urls.first().toLocalFile();
if (!loadFile(fileName)) {
imageLabel->setPixmap(QPixmap::fromImage(image));
scaleFactor = 1.0;
printAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();
if (!fitToWindowAct->isChecked())
imageLabel->adjustSize();
}
}
void ImageViewer::on_actionOpen_triggered()
{
QFileDialog dialog(this, tr("Open File"));
initializeImageFileDialog(dialog, QFileDialog::AcceptOpen);
while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {
}
}
void ImageViewer::on_actionPrint_triggered()
{
Q_ASSERT(imageLabel->pixmap());
#ifndef QT_NO_PRINTER
QPrintDialog dialog(&printer, this);
if (dialog.exec()) {
QPainter painter(&printer);
QRect rect = painter.viewport();
QSize size = imageLabel->pixmap()->size();
size.scale(rect.size(), Qt::KeepAspectRatio);
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
painter.setWindow(imageLabel->pixmap()->rect());
painter.drawPixmap(0, 0, *imageLabel->pixmap());
}
#endif
}
void ImageViewer::wheelEvent(QWheelEvent *event)
{
if (raw_image.isNull()) return;
if(event->delta() > 0) // default 15 degree once
{
scaleImage(1.25);
} else
{
scaleImage(0.8);
}
}
void ImageViewer::closeEvent(QCloseEvent *event)
{
if (systemTray->isVisible())
{
hide();
// display 500ms
// systemTray->showMessage(QString("Tray mode"), QString("Running in tray mode"), QSystemTrayIcon::Information, 500);
event->ignore();
}
}
void ImageViewer::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
case QSystemTrayIcon::Trigger:
// click tray icon
break;
case QSystemTrayIcon::DoubleClick:
// double tray icon, show window
this->show();
break;
default:
break;
}
}
void ImageViewer::on_actionExit_triggered()
{
this->close();
}
void ImageViewer::on_actionZoomIn_triggered()
{
scaleImage(1.25);
}
void ImageViewer::on_actionZoomOut_triggered()
{
scaleImage(0.8);
}
void ImageViewer::on_actionNormalSize_triggered()
{
imageLabel->adjustSize();
scaleFactor = 1.0;
}
void ImageViewer::on_actionFitToWindow_triggered()
{
bool fitToWindow = fitToWindowAct->isChecked();
scrollArea->setWidgetResizable(fitToWindow);
if (!fitToWindow) {
on_actionNormalSize_triggered();
}
updateActions();
}
void ImageViewer::on_actionAboutQt_triggered()
{
QApplication::aboutQt();
}
void ImageViewer::on_actionSaveAs_triggered()
{
QFileDialog dialog(this, tr("Save File As"));
initializeImageFileDialog(dialog, QFileDialog::AcceptSave);
while (dialog.exec() == QDialog::Accepted && !saveFile(dialog.selectedFiles().first())) {}
}
void ImageViewer::on_actionAbout_triggered() {
// text with html, <br> instead of \n
QString info;
info += "Version: 2.1<br>";
info += "Author: Steve Canvas<br>";
info += "Emails: [email protected]<br>";
info += "<a href='https://github.com/renhaofan/colormap/'>Github Repo</a>";
QMessageBox::about(this, tr("About"), info);
}