-
Notifications
You must be signed in to change notification settings - Fork 0
/
photoshopgui.cpp
893 lines (784 loc) · 31.2 KB
/
photoshopgui.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
#include <QtWidgets>
#ifndef QT_NO_PRINTER
#include <QPrintDialog>
#endif
#include <iostream>
#include "photoshopgui.h"
PhotoshopGUI::PhotoshopGUI()
: imageLabel(new QLabel)
, positionLabel(new QLabel)
, pixelColorLabel(new QLabel)
, pixelRGBLabel(new QLabel)
, scrollArea(new QScrollArea)
, scaleFactor(1)
, log(new Log)
, imageOperation(new ImageOperations)
{
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true);
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel);
scrollArea->setVisible(false);
setCentralWidget(scrollArea);
positionLabel->setText(tr(""));
statusBar()->addPermanentWidget(pixelColorLabel);
statusBar()->addPermanentWidget(pixelRGBLabel);
statusBar()->addPermanentWidget(positionLabel);
this->setMouseTracking(true);
scrollArea->setMouseTracking(true);
imageLabel->setMouseTracking(true);
initializeViews();
resize(QGuiApplication::primaryScreen()->availableSize() * 0.8);
}
void PhotoshopGUI::initializeViews(){
initializeFileMenu();
initializeEditMenu();
initializeImageMenu();
initializeViewMenu();
initializeAboutMenu();
}
void PhotoshopGUI::initializeFileMenu(){
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &PhotoshopGUI::open);
openAct->setShortcut(QKeySequence::Open);
saveAsAct = fileMenu->addAction(tr("&Save As..."), this, &PhotoshopGUI::saveAs);
saveAsAct->setEnabled(false);
printAct = fileMenu->addAction(tr("&Print..."), this, &PhotoshopGUI::print);
printAct->setShortcut(QKeySequence::Print);
printAct->setEnabled(false);
fileMenu->addSeparator();
QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close);
exitAct->setShortcut(tr("Ctrl+Q"));
}
void PhotoshopGUI::initializeEditMenu(){
QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
undoAct = editMenu->addAction(tr("&Undo"), this, &PhotoshopGUI::undo);
undoAct->setShortcut(QKeySequence::Undo);
undoAct->setEnabled(false);
redoAct = editMenu->addAction(tr("&Redo"), this, &PhotoshopGUI::redo);
redoAct->setShortcut(QKeySequence::Redo);
redoAct->setEnabled(false);
editMenu->addSeparator();
copyAct = editMenu->addAction(tr("&Copy"), this, &PhotoshopGUI::copy);
copyAct->setShortcut(QKeySequence::Copy);
copyAct->setEnabled(false);
QAction *pasteAct = editMenu->addAction(tr("&Paste"), this, &PhotoshopGUI::paste);
pasteAct->setShortcut(QKeySequence::Paste);
}
void PhotoshopGUI::initializeImageMenu(){
imageMenu = menuBar()->addMenu(tr("&Image"));
imageMenu->setEnabled(false);
QMenu *channelSubMenu = imageMenu->addMenu(tr("&Channel Separation"));
channelSubMenu->addAction(tr("&Red Channel"), this, SLOT(redChannel()));
channelSubMenu->addAction(tr("&Green Channel"), this, SLOT(greenChannel()));
channelSubMenu->addAction(tr("&Blue Channel"), this, SLOT(blueChannel()));
imageMenu->addAction(tr("&Gray-Scale Transfer"), this, SLOT(grayScaleTransfer()));
QMenu *adjustSubMenu = imageMenu->addMenu(tr("&Adjust Image"));
adjustSubMenu->addAction(tr("&Hue/Saturation/Lightness..."), this, SLOT(hueSaturationLightness()));
adjustSubMenu->addAction(tr("&Contrast Curve..."), this, SLOT(curve()));
adjustSubMenu->addAction(tr("&Level..."), this, SLOT(level()));
adjustSubMenu->addAction(tr("&Clip..."), this, SLOT(clip()));
adjustSubMenu->addAction(tr("&Scale/Rotation..."), this, SLOT(scale()));
imageMenu->addAction(tr("&Histogram..."), this, SLOT(histogram()));
imageMenu->addSeparator();
QMenu *imageBinarizationSubMenu = imageMenu->addMenu(tr("&Image Binarization"));
imageBinarizationSubMenu->addAction(tr("&OTSU"), this, SLOT(otsu()));
imageBinarizationSubMenu->addAction(tr("Dual Threshold"), this, SLOT(dualThreshold()));
imageMenu->addAction(tr("&Algebraic Operations..."), this, &PhotoshopGUI::algebraic);
imageMenu->addAction(tr("&Filtering..."), this, SLOT(filter()));
imageMenu->addAction(tr("&Edge Detection..."), this, SLOT(edgeDetection()));
QMenu *houghTransformSubMenu = imageMenu->addMenu(tr("&Hough Transform"));
houghTransformSubMenu->addAction(tr("Detect &Line"), this, SLOT(houghLineDetect()));
houghTransformSubMenu->addAction(tr("Detect &Circle..."), this, SLOT(houghCircleDetect()));
QMenu *mathematicalMorphologySubMenu = imageMenu->addMenu(tr("&Mathematical Morphology"));
mathematicalMorphologySubMenu->addAction(tr("&Binary Morphology..."), this, SLOT(binaryMorphology()));
mathematicalMorphologySubMenu->addAction(tr("Gray-scale Morphology..."), this, SLOT(grayMorphology()));
}
void PhotoshopGUI::initializeViewMenu(){
QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
zoomInAct = viewMenu->addAction(tr("Zoom &In (25%)"), this, &PhotoshopGUI::zoomIn);
zoomInAct->setShortcut(QKeySequence::ZoomIn);
zoomInAct->setEnabled(false);
zoomOutAct = viewMenu->addAction(tr("Zoom &Out (25%)"), this, &PhotoshopGUI::zoomOut);
zoomOutAct->setShortcut(QKeySequence::ZoomOut);
zoomOutAct->setEnabled(false);
normalSizeAct = viewMenu->addAction(tr("&Normal Size"), this, &PhotoshopGUI::normalSize);
normalSizeAct->setShortcut(tr("Ctrl+S"));
normalSizeAct->setEnabled(false);
viewMenu->addSeparator();
fitToWindowAct = viewMenu->addAction(tr("&Fit to Window"), this, &PhotoshopGUI::fitToWindow);
fitToWindowAct->setEnabled(false);
fitToWindowAct->setCheckable(true);
fitToWindowAct->setShortcut(tr("Ctrl+F"));
}
void PhotoshopGUI::initializeAboutMenu(){
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(tr("&About"), this, &PhotoshopGUI::about);
helpMenu->addAction(tr("About &Qt"), &QApplication::aboutQt);
}
void PhotoshopGUI::mouseMoveEvent(QMouseEvent *e){
QString posMsg;
QString rgbMsg;
int x = e->x();
int y = e->y();
QRect imageRect = QRect(imageLabel->pos() + scrollArea->pos(), imageLabel->size());
if(image.isNull()
|| !imageRect.contains(e->pos())){
posMsg = " Windows position: ";
posMsg += "(" + QString::number(x) + "," + QString::number(y) + ")";
rgbMsg = " RGB(#,#,#)";
pixelColorLabel->clear();
}
else {
QSize initSize = imageLabel->pixmap()->size();
posMsg = " Canvas position: ";
int imageX = (x - imageRect.x()) * initSize.width() / imageRect.width();
int imageY = (y - imageRect.y()) * initSize.height() / imageRect.height();
posMsg += "(" + QString::number(imageX) + "," + QString::number(imageY) + ")";
QRgb rgb = image.pixel(imageX, imageY);
rgbMsg = " RGB(" + QString::number(qRed(rgb)) + ","
+ QString::number(qGreen(rgb)) + ","
+ QString::number(qBlue(rgb)) + ")";
QPixmap pixelColor(20, 20);
pixelColor.fill(QColor(rgb));
pixelColorLabel->setPixmap(pixelColor);
}
positionLabel->setText(posMsg);
pixelRGBLabel->setText(rgbMsg);
}
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");
}
void PhotoshopGUI::open(){
QFileDialog dialog(this, tr("Open File"));
initializeImageFileDialog(dialog, QFileDialog::AcceptOpen);
while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {}
}
bool PhotoshopGUI::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;
}
//! [2]
clearLog();
setImage(newImage);
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 PhotoshopGUI::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();
}
void PhotoshopGUI::updateImage(const QImage &newImage){
image = newImage;
imageLabel->setPixmap(QPixmap::fromImage(image));
if(!fitToWindowAct->isChecked())imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
}
bool PhotoshopGUI::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 PhotoshopGUI::saveAs()
{
QFileDialog dialog(this, tr("Save File As"));
initializeImageFileDialog(dialog, QFileDialog::AcceptSave);
while (dialog.exec() == QDialog::Accepted && !saveFile(dialog.selectedFiles().first())) {}
}
void PhotoshopGUI::print()
{
Q_ASSERT(imageLabel->pixmap());
#if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG)
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 PhotoshopGUI::undo(){
QImage img = log->undo(&image);
updateImage(img);
updateCommitManager();
}
void PhotoshopGUI::redo(){
QImage img = log->redo();
updateImage(img);
updateCommitManager();
}
void PhotoshopGUI::copy()
{
#ifndef QT_NO_CLIPBOARD
QGuiApplication::clipboard()->setImage(image);
#endif // !QT_NO_CLIPBOARD
}
#ifndef QT_NO_CLIPBOARD
static QImage clipboardImage()
{
if (const QMimeData *mimeData = QGuiApplication::clipboard()->mimeData()) {
if (mimeData->hasImage()) {
const QImage image = qvariant_cast<QImage>(mimeData->imageData());
if (!image.isNull())
return image;
}
}
return QImage();
}
#endif // !QT_NO_CLIPBOARD
void PhotoshopGUI::paste()
{
#ifndef QT_NO_CLIPBOARD
const QImage newImage = clipboardImage();
if (newImage.isNull()) {
statusBar()->showMessage(tr("No image in clipboard"));
} else {
setImage(newImage);
setWindowFilePath(QString());
const QString message = tr("Obtained image from clipboard, %1x%2, Depth: %3")
.arg(newImage.width()).arg(newImage.height()).arg(newImage.depth());
statusBar()->showMessage(message);
}
#endif // !QT_NO_CLIPBOARD
}
void PhotoshopGUI::redChannel(){
makeCommit(image, "Channel Separation: Red");
QImage newImage = imageOperation->getSingleChannel(image, ImageOperations::RED);
updateImage(newImage);
}
void PhotoshopGUI::greenChannel(){
makeCommit(image, "Channel Separation: Green");
QImage newImage = imageOperation->getSingleChannel(image, ImageOperations::GREEN);
updateImage(newImage);
}
void PhotoshopGUI::blueChannel(){
makeCommit(image, "Channel Separation: Blue");
QImage newImage = imageOperation->getSingleChannel(image, ImageOperations::BLUE);
updateImage(newImage);
}
void PhotoshopGUI::grayScaleTransfer(){
makeCommit(image, "Gray-Scale Transfer");
QImage newImage = imageOperation->grayScale(image);
updateImage(newImage);
}
void PhotoshopGUI::otsu(){
makeCommit(image, "Image Binarization: OTSU");
QImage newImage = imageOperation->otsu(image);
updateImage(newImage);
}
void PhotoshopGUI::houghLineDetect(){
setCursor(Qt::WaitCursor);
makeCommit(image, "Hough Transform: Detect Line");
QImage newImage = imageOperation->houghLineDetect(image);
updateImage(newImage);
setCursor(Qt::ArrowCursor);
}
void PhotoshopGUI::houghCircleDetect(){
HoughTransformDialog *dialog = new HoughTransformDialog();
connect(dialog, SIGNAL(sendApplyHoughTransform(QJsonObject)), this,
SLOT(receiveApplyHoughTransform(QJsonObject)));
dialog->exec();
}
void PhotoshopGUI::binaryMorphology(){
bool isBinary = imageOperation->isBinaryImage(image);
if(!isBinary){
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"),
tr("Current image is not binary image."));
msgBox.exec();
return;
}
BinaryMorphoDialog *dialog = new BinaryMorphoDialog();
connect(dialog, SIGNAL(sendApplyBinaryMorpho(QJsonObject)), this,
SLOT(receiveApplyBinaryMorpho(QJsonObject)));
dialog->exec();
}
void PhotoshopGUI::grayMorphology(){
bool isGray = imageOperation->isGrayImage(image);
if(!isGray){
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"),
tr("Current image is not gray-scale image."));
msgBox.exec();
return;
}
GrayMorphoDialog *dialog = new GrayMorphoDialog();
connect(dialog, SIGNAL(sendApplyGrayMorpho(QJsonObject)), this,
SLOT(receiveApplyGrayMorpho(QJsonObject)));
dialog->exec();
}
void PhotoshopGUI::clip(){
ClipDialog *dialog = new ClipDialog(QPixmap::fromImage(image));
connect(dialog, SIGNAL(sendApplyClip(QJsonObject)), this,
SLOT(receiveApplyClip(QJsonObject)));
dialog->exec();
}
void PhotoshopGUI::curve(){
CurveDialog *dialog = new CurveDialog(image);
connect(dialog, SIGNAL(sendApplyCurve(QJsonObject,QImage)), this,
SLOT(receiveApplyCurve(QJsonObject,QImage)));
dialog->exec();
}
void PhotoshopGUI::histogram(){
HistogramDialog *dialog = new HistogramDialog(image);
connect(dialog, SIGNAL(sendApplyHistogramEqualization(QImage)), this,
SLOT(receiveApplyHistoEqualize(QImage)));
dialog->exec();
}
void PhotoshopGUI::filter(){
FilterDialog *dialog = new FilterDialog();
connect(dialog, SIGNAL(sendApplyFilter(QJsonObject)), this,
SLOT(receiveApplyFilter(QJsonObject)));
dialog->exec();
}
void PhotoshopGUI::edgeDetection(){
EdgeDetectionDialog *dialog = new EdgeDetectionDialog();
connect(dialog, SIGNAL(sendApplyEdgeDetection(QJsonObject)), this,
SLOT(receiveApplyEdgeDetection(QJsonObject)));
dialog->exec();
}
void PhotoshopGUI::scale(){
ScaleDialog *dialog = new ScaleDialog();
connect(dialog, SIGNAL(sendApplyScale(QJsonObject)), this,
SLOT(receiveApplyScale(QJsonObject)));
dialog->exec();
}
void PhotoshopGUI::dualThreshold(){
DualThresholdDialog *dialog = new DualThresholdDialog(this, image);
connect(dialog, SIGNAL(sendApplyDualThreshold(QJsonObject,QImage)), this,
SLOT(receiveApplyDualThreshold(QJsonObject,QImage)));
dialog->exec();
}
void PhotoshopGUI::algebraic(){
AlgebraicDialog *dialog = new AlgebraicDialog(this, image);
connect(dialog, SIGNAL(sendApplyAlgebraicOperation(QJsonObject, QImage)), this,
SLOT(receiveApplyAlgebraicOperation(QJsonObject,QImage)));
dialog->exec();
}
void PhotoshopGUI::hueSaturationLightness(){
HueSaturaLightDialog *dialog = new HueSaturaLightDialog(this, image);
connect(dialog, SIGNAL(sendApplyHSLAdjust(QJsonObject,QImage)), this,
SLOT(receiveApplyHSLAdjust(QJsonObject,QImage)));
dialog->exec();
}
void PhotoshopGUI::level(){
LevelDialog *dialog = new LevelDialog(image);
connect(dialog, SIGNAL(sendApplyLevelAdjust(QImage)), this,
SLOT(receiveApplyLevelAdjust(QImage)));
dialog->exec();
}
void PhotoshopGUI::zoomIn()
{
// makeCommit(image, "Zoom In");
scaleImage(1.25);
}
void PhotoshopGUI::zoomOut()
{
// makeCommit(image, "Zoom Out");
scaleImage(0.8);
}
void PhotoshopGUI::normalSize()
{
// makeCommit(image, "Normal Size");
imageLabel->adjustSize();
scaleFactor = 1.0;
}
void PhotoshopGUI::fitToWindow()
{
bool fitToWindow = fitToWindowAct->isChecked();
scrollArea->setWidgetResizable(fitToWindow);
if (!fitToWindow)
normalSize();
updateActions();
}
void PhotoshopGUI::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 PhotoshopGUI::adjustScrollBar(QScrollBar *scrollBar, double factor)
{
scrollBar->setValue(int(factor * scrollBar->value()
+ ((factor - 1) * scrollBar->pageStep()/2)));
}
void PhotoshopGUI::about()
{
QMessageBox::about(this, tr("About Image Viewer"),
tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
"and QScrollArea to display an image. QLabel is typically used "
"for displaying a text, but it can also display an image. "
"QScrollArea provides a scrolling view around another widget. "
"If the child widget exceeds the size of the frame, QScrollArea "
"automatically provides scroll bars. </p><p>The example "
"demonstrates how QLabel's ability to scale its contents "
"(QLabel::scaledContents), and QScrollArea's ability to "
"automatically resize its contents "
"(QScrollArea::widgetResizable), can be used to implement "
"zooming and scaling features. </p><p>In addition the example "
"shows how to use QPainter to print an image.</p>"));
}
void PhotoshopGUI::updateActions()
{
imageMenu->setEnabled(!image.isNull());
saveAsAct->setEnabled(!image.isNull());
copyAct->setEnabled(!image.isNull());
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
}
void PhotoshopGUI::makeCommit(QImage image, QString operation){
log->commit(image, operation);
updateCommitManager();
}
void PhotoshopGUI::clearLog(){
log->clear();
updateCommitManager();
}
void PhotoshopGUI::updateCommitManager(){
bool undoEnabled = log->undoEnabled();
bool redoEnabled = log->redoEnabled();
undoAct->setEnabled(undoEnabled);
if(undoEnabled){
undoAct->setStatusTip("Undo: " + log->getUndoMsg());
} else {
undoAct->setStatusTip(tr(""));
}
redoAct->setEnabled(redoEnabled);
if(redoEnabled){
redoAct->setStatusTip("Redo: " + log->getRedoMsg());
} else {
redoAct->setStatusTip(tr(""));
}
}
void PhotoshopGUI::receiveApplyAlgebraicOperation(QJsonObject json, QImage img){
AlgebraicDialog::OperatorState op = (AlgebraicDialog::OperatorState)(json["operator"].toInt());
QImage newImage;
QString operationDesc;
switch(op){
case AlgebraicDialog::ADD:
if(json["default"].toBool()){
operationDesc = "image1 + image2";
newImage = imageOperation->imageAdd(image, img, 1, 1);
}
else{
operationDesc = tr("%1*image1 + %2*image2").arg(json["param1"].toDouble()).arg(json["param2"].toDouble());
newImage = imageOperation->imageAdd(image, img, json["param1"].toDouble(), json["param2"].toDouble());
}
break;
case AlgebraicDialog::SUBTRACT:
if(json["default"].toBool()){
operationDesc = "image1 - image2";
newImage = imageOperation->imageSubtract(image, img, 1, 1);
}
else{
operationDesc = tr("%1*image1 - %2*image2").arg(json["param1"].toDouble()).arg(json["param2"].toDouble());
newImage = imageOperation->imageSubtract(image, img, json["param1"].toDouble(), json["param2"].toDouble());
}
break;
case AlgebraicDialog::MULTIPLY:
operationDesc = "image1 * image2";
newImage = imageOperation->imageMultiply(image ,img);
break;
default:
break;
}
makeCommit(image, "Algebraic Operation: " + operationDesc);
updateImage(newImage);
}
void PhotoshopGUI::receiveApplyHSLAdjust(QJsonObject json, QImage img){
QString desc = "HSL Adjust: ";
desc += "Hue: (";
QJsonArray hueValues = json["hueValues"].toArray();
desc += "whole image: " + QString::number(hueValues[0].toInt()) + ", ";
desc += "red: " + QString::number(hueValues[1].toInt()) + ", ";
desc += "yellow: " + QString::number(hueValues[2].toInt()) + ", ";
desc += "green: " + QString::number(hueValues[3].toInt()) + ", ";
desc += "cyan: " + QString::number(hueValues[4].toInt()) + ", ";
desc += "blue: " + QString::number(hueValues[5].toInt()) + ", ";
desc += "magenta: " + QString::number(hueValues[6].toInt()) + "), ";
desc += "Saturation: " + json["saturation"].toString() + ", ";
desc += "Lightness: " + json["lightness"].toString();
makeCommit(image, desc);
updateImage(img);
}
void PhotoshopGUI::receiveApplyDualThreshold(QJsonObject json, QImage img){
QString desc = "Image Binarization: DualThreshold: (low threshold: ";
desc += json["low"].toString() + ", ";
desc += "high threshold: " + json["high"].toString() + ")";
makeCommit(image, desc);
updateImage(img);
}
void PhotoshopGUI::receiveApplyScale(QJsonObject json){
int type = json["algo"].toInt();
int scale = json["scale"].toInt();
int rotation = json["rotation"].toInt();
QImage newImage;
QString desc = "Scale/Rotation: Algorithms: ";
if(type == 0){
desc += "nearnest interpolation, ";
newImage = imageOperation->nearnestInterpolation(image, scale, rotation);
} else {
desc += "bilinear interpolation, ";
newImage = imageOperation->BilinearInterpolation(image, scale, rotation);
}
desc += "Scale: " + QString::number(scale) + ", ";
desc += "Rotation: " + QString::number(rotation);
makeCommit(image, desc);
updateImage(newImage);
}
void PhotoshopGUI::receiveApplyFilter(QJsonObject json){
setCursor(Qt::WaitCursor);
QString desc = "Filter: Type: ";
int type = json["type"].toInt();
int col = json["col"].toInt();
int row = json["row"].toInt();
QImage newImage;
int x = json["x"].toInt();
int y = json["y"].toInt();
if(type == 0){
desc += "mean filter, Kernel: (Size: (";
desc += QString::number(col) + ", " + QString::number(row) + "), Anchor: (";
desc += QString::number(x) + ", " + QString::number(y) + "))";
newImage = imageOperation->meanFilter(image, col, row, x, y);
} else if(type == 1){
desc += "median filter, Kernel: (Size: (";
desc += QString::number(col) + ", " + QString::number(row) + "), Anchor: (";
desc += QString::number(x) + ", " + QString::number(y) + "))";
newImage = imageOperation->medianFilter(image, col, row, x, y);
} else {
double sigma = json["sigma"].toDouble();
desc += "median filter, Kernel: (Size: (";
desc += QString::number(col) + ", " + QString::number(row) + "), Anchor: (";
desc += QString::number(x) + ", " + QString::number(y) + "), ";
desc += "Sigma: " + QString::number(sigma) + ")";
newImage = imageOperation->gaussianFilter(image, col, row, x, y, sigma);
}
makeCommit(image, desc);
updateImage(newImage);
setCursor(Qt::ArrowCursor);
}
void PhotoshopGUI::receiveApplyEdgeDetection(QJsonObject json){
setCursor(Qt::WaitCursor);
QString desc = "Edge Detection: Type:";
int type = json["type"].toInt();
QImage newImage;
if(type == 2){
desc += "canny, ";
int lo = json["lo"].toInt();
int hi = json["hi"].toInt();
desc += "Low Threshold: " + QString::number(lo) + ", ";
desc += "High Threshold: " + QString::number(hi);
newImage = imageOperation->cannyEdgeDetection(image, lo, hi);
} else{
int threshold = json["threshold"].toInt();
if(type == 0){
desc += "sobel, Threshold: " + QString::number(threshold);
newImage = imageOperation->sobelEdgeDetection(image, threshold);
} else {
desc += "laplacian, Threshold: " + QString(threshold);
newImage = imageOperation->laplacianEdgeDetection(image, threshold);
}
}
makeCommit(image, desc);
updateImage(newImage);
setCursor(Qt::ArrowCursor);
}
void PhotoshopGUI::receiveApplyHoughTransform(QJsonObject json){
setCursor(Qt::WaitCursor);
QString desc = "Hough Transformation: Detect Circle: ";
int lo = json["lo"].toInt();
int hi = json["hi"].toInt();
desc += "Radius: from " + QString::number(lo) + " to " + QString::number(hi);
QImage newImage = imageOperation->houghCircleDetect(image, lo, hi);
makeCommit(image, desc);
updateImage(newImage);
setCursor(Qt::ArrowCursor);
}
void PhotoshopGUI::receiveApplyClip(QJsonObject json){
QString desc = "Clip: ";
int left = json["left"].toInt();
int right = json["right"].toInt();
int top = json["top"].toInt();
int bottom = json["bottom"].toInt();
desc += "X: from " + QString::number(left) + " to " + QString::number(right) + ", ";
desc += "Y: from " + QString::number(top) + " to " + QString::number(bottom);
QImage newImage = imageOperation->clip(image, left, right, top, bottom);
makeCommit(image, desc);
updateImage(newImage);
}
void PhotoshopGUI::receiveApplyBinaryMorpho(QJsonObject json){
QString desc = "Binary Morphology: Operation: ";
int size = json["size"].toInt();
QJsonArray elem = json["elem"].toArray();
int array[size * size];
for(int i = 0; i < size * size; i++){
array[i] = elem[i].toInt();
}
int x = json["x"].toInt();
int y = json["y"].toInt();
QImage newImage;
BinaryMorphoDialog::OperationType operation = (BinaryMorphoDialog::OperationType)json["operation"].toInt();
switch (operation) {
case BinaryMorphoDialog::DILATION:
desc += "Dilation, ";
newImage = imageOperation->dilation(image, size, x, y, array);
break;
case BinaryMorphoDialog::EROSION:
desc += "Erosion, ";
newImage = imageOperation->erosion(image, size, x, y, array);
break;
case BinaryMorphoDialog::OPENING:
desc += "Opening, ";
newImage = imageOperation->opening(image, size, x, y, array);
break;
case BinaryMorphoDialog::CLOSING:
desc += "Closing, ";
newImage = imageOperation->closing(image, size, x, y, array);
break;
case BinaryMorphoDialog::SKELETONIZATION:
desc += "Skeletonization, ";
newImage = imageOperation->skeletonization(image, size, x, y, array);
break;
case BinaryMorphoDialog::THINING:
desc += "Thinning";
newImage = imageOperation->thinning(image);
break;
case BinaryMorphoDialog::THICKING:
desc += "Thicking";
newImage = imageOperation->thicking(image);
break;
case BinaryMorphoDialog::DISTANCE_TRANSFORM:
desc += "Distance Transform";
newImage = imageOperation->distanceTransform(image);
break;
case BinaryMorphoDialog::SKELETON_RECONSTRUCTION:
desc += "Skeleton Reconstruction, ";
newImage = imageOperation->skeletionReconstruct(image, size, x, y, array);
break;
case BinaryMorphoDialog::RECONSTRUCTION:
desc += "Reconstruction, ";
newImage = imageOperation->binaryReconstruction(image, size, x, y, array);
break;
default:
break;
}
if(operation != BinaryMorphoDialog::THINING
&& operation != BinaryMorphoDialog::THICKING
&& operation != BinaryMorphoDialog::DISTANCE_TRANSFORM){
desc += tr("Structure Element Size: %1x%1, Anchor: (%2, %3)").arg(size).arg(x).arg(y);
}
makeCommit(image, desc);
updateImage(newImage);
}
void PhotoshopGUI::receiveApplyGrayMorpho(QJsonObject json){
QString desc = "Grayscale Morphology: Operation: ";
int size = json["size"].toInt();
QJsonArray elem = json["elem"].toArray();
int array[size * size];
for(int i = 0; i < size * size; i++){
array[i] = elem[i].toInt();
}
int x = json["x"].toInt();
int y = json["y"].toInt();
QImage newImage;
GrayMorphoDialog::OperationType operation = (GrayMorphoDialog::OperationType)json["operation"].toInt();
switch (operation) {
case GrayMorphoDialog::DILATION:
desc += "Dilation, ";
newImage = imageOperation->dilation(image, size, x, y, array);
break;
case GrayMorphoDialog::EROSION:
desc += "Erosion, ";
newImage = imageOperation->erosion(image, size, x, y, array);
break;
case GrayMorphoDialog::OPENING:
desc += "Opening, ";
newImage = imageOperation->opening(image, size, x, y, array);
break;
case GrayMorphoDialog::CLOSING:
desc += "Closing, ";
newImage = imageOperation->closing(image, size, x, y, array);
break;
case GrayMorphoDialog::OBR:
desc += "Opening By Reconstruction, ";
newImage = imageOperation->obr(image, size, x, y, array);
break;
case GrayMorphoDialog::CBR:
desc += "Closing By Reconstruction, ";
newImage = imageOperation->cbr(image, size, x, y, array);
break;
case GrayMorphoDialog::WATERSHED:
desc += "Watershed, ";
newImage = imageOperation->watershed(image);
break;
default:
break;
}
if(operation != GrayMorphoDialog::WATERSHED){
desc += tr("Structure Element Size: %1x%1, Anchor: (%2, %3)").arg(size).arg(x).arg(y);
}
makeCommit(image, desc);
updateImage(newImage);
}
void PhotoshopGUI::receiveApplyCurve(QJsonObject json, QImage img){
QString desc = "Adjust Contrast Curve: Curve Type: " + json["type"].toString();
makeCommit(image, desc);
updateImage(img);
}
void PhotoshopGUI::receiveApplyHistoEqualize(QImage img){
makeCommit(image, tr("Histogram Equalization"));
updateImage(img);
}
void PhotoshopGUI::receiveApplyLevelAdjust(QImage img){
makeCommit(image, tr("Color Level Adjust"));
updateImage(img);
}