-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.cpp
151 lines (127 loc) · 6.58 KB
/
MainWindow.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
#include "MainWindow.h"
#include "./ui_MainWindow.h"
#include "TargetImageWidget.h"
#include "AlgorithmSinglePixelSwapRgb.h"
#include "AlgorithmSinglePixelSwapHsv.h"
#include "AlgorithmAreaAverageRgb.h"
#include "AlgorithmAreaAverageHsv.h"
#include "AlgorithmExhaustiveRgb.h""
#include <QLocale>
#include <QDesktopServices>
#include <QScrollArea>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowIcon(QIcon(":/Logo.png"));
setWindowTitle("All RGB v2.1");
///
/// Load Image Tab
///
// save / load / preview / begin
connect(ui->targetImageSaveButton, &QPushButton::pressed, ui->allRgbWidget, &TargetImageWidget::onSaveImageRequested);
connect(ui->targetImageLoadButton, &QPushButton::pressed, ui->allRgbWidget, &TargetImageWidget::onLoadImageRequested);
connect(ui->targetImagePreviewButton, &QPushButton::pressed, this, &MainWindow::onPreviewTargetImageRequested);
connect(ui->setTargetImageButton, &QPushButton::pressed, this, &MainWindow::onSetTargetImageFromViewRequested);
connect(ui->allRgbWidget, &TargetImageWidget::onUseWholeImageRequested, this, &MainWindow::onSetTargetImageFromViewRequested);
// rotation
connect(ui->targetImageRotationSlider, &QSlider::sliderMoved, ui->allRgbWidget, &TargetImageWidget::SetRotation);
connect(ui->targetImageRotate90DegreesButton, &QPushButton::pressed, ui->allRgbWidget, &TargetImageWidget::onRotateRequested);
connect(ui->allRgbWidget, &TargetImageWidget::onRotationUpdated, ui->targetImageRotationSlider, &QSlider::setValue);
// scale
connect(ui->targetImageFitToAreaButton, &QPushButton::pressed, ui->allRgbWidget, &TargetImageWidget::onFitToTargetRequested);
connect(ui->targetImageScaleSpinBox, &QDoubleSpinBox::valueChanged, ui->allRgbWidget, &TargetImageWidget::SetScale);
connect(ui->allRgbWidget, &TargetImageWidget::onScaleUpdated, ui->targetImageScaleSpinBox, &QDoubleSpinBox::setValue);
// transform
connect(ui->targetImageResetTranslationButton, &QPushButton::pressed, ui->allRgbWidget, &TargetImageWidget::ResetTranslation);
connect(ui->targetImageXOffsetSpinBox, &QDoubleSpinBox::valueChanged, ui->allRgbWidget, &TargetImageWidget::SetTranslationX);
connect(ui->targetImageYOffsetSpinBox, &QDoubleSpinBox::valueChanged, ui->allRgbWidget, &TargetImageWidget::SetTranslationY);
connect(ui->allRgbWidget, &TargetImageWidget::onTranslationUpdated, this, [ui = ui](QPointF translation)
{
ui->targetImageXOffsetSpinBox->setValue(translation.rx());
ui->targetImageYOffsetSpinBox->setValue(translation.ry());
});
// Link to website
connect(ui->moreByTroyDevButton, &QPushButton::pressed, this, []()
{
QDesktopServices::openUrl(QUrl("http://www.TroyDev.co.uk"));
});
///
/// All RGB preview tab
///
// View
connect(ui->allRgbUpdateTargetButton, &QPushButton::pressed, this, &MainWindow::onSetTargetImageFromViewRequested);
connect(ui->allRgbResetViewButton, &QPushButton::pressed, ui->allRgbPreviewWidget, &AllRgbWidget::onResetViewRequested);
// Starting pixels
connect(ui->allRgbResetPixelsButton, &QPushButton::pressed, ui->allRgbPreviewWidget, &AllRgbWidget::onResetPixelsRequested);
connect(ui->allRgbLoadButton, &QPushButton::pressed, ui->allRgbPreviewWidget, &AllRgbWidget::onLoadPixelsRequested);
// All RGB
connect(ui->allRgbStartButton, &QPushButton::pressed, ui->allRgbPreviewWidget, &AllRgbWidget::onStartRequested);
connect(ui->allRgbStopButton, &QPushButton::pressed, ui->allRgbPreviewWidget, &AllRgbWidget::onStopRequested);
connect(ui->allRgbPreviewWidget, &AllRgbWidget::onIterationsChanged, this, [ui = ui](quint64 iterations)
{
ui->allRGBIterationsValueLabel->setText(QLocale::system().toString(iterations));
});
connect(ui->allRgbPreviewWidget, &AllRgbWidget::onImprovementsChanged, this, [ui = ui](quint64 improvements)
{
ui->allRGBImprovementsValueLabel->setText(QLocale::system().toString(improvements));
});
// Algorithm
std::vector<std::function<std::unique_ptr<AlgorithmBase>()>> algorithmFactories;
algorithmFactories.push_back([]() { return std::make_unique<AlgorithmSinglePixelSwapRgb>(); });
algorithmFactories.push_back([]() { return std::make_unique<AlgorithmSinglePixelSwapHsv>(); });
algorithmFactories.push_back([]() { return std::make_unique<AlgorithmAreaAverageRgb>(); });
algorithmFactories.push_back([]() { return std::make_unique<AlgorithmAreaAverageHsv>(); });
algorithmFactories.push_back([]() { return std::make_unique<AlgorithmExhaustiveRgb>(); });
connect(ui->allRgbSolverComboBox, &QComboBox::currentIndexChanged, this, [ui = ui, factory = algorithmFactories](int index)
{
if (size_t{ 0 } + index < factory.size()) {
auto algorithm = std::invoke(factory[index]);
ui->allRgbSolverDetailsTextBrowser->setText(algorithm->GetDescription());
ui->allRgbPreviewWidget->SetAlgorithm(std::move(algorithm));
}
}, Qt::QueuedConnection);
// Doing this after the connection ensures we start having connected up the currently selected item
for (const auto& factory : algorithmFactories) {
auto algorithm = std::invoke(factory);
ui->allRgbSolverComboBox->addItem(algorithm->GetName());
}
// Result
connect(ui->allRgbPreviewButton, &QPushButton::pressed, this, &MainWindow::onPreviewAllRgbRequested);
connect(ui->allRgbSaveButton, &QPushButton::pressed, ui->allRgbPreviewWidget, &AllRgbWidget::onSaveImageRequested);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onPreviewTargetImageRequested()
{
OpenImagePreviewWindow(ui->allRgbWidget->GetImageInViewTargetArea());
}
void MainWindow::onPreviewAllRgbRequested()
{
OpenImagePreviewWindow(ui->allRgbPreviewWidget->GetAllRgbImage());
}
void MainWindow::onSetTargetImageFromViewRequested()
{
ui->allRgbPreviewWidget->SetTargetImage(ui->allRgbWidget->GetImageInViewTargetArea());
ui->tabWidget->setCurrentIndex(1);
}
void MainWindow::onSetTargetImageFromEntireImageRequested()
{
ui->allRgbPreviewWidget->SetTargetImage(ui->allRgbWidget->GetWholeImage());
ui->tabWidget->setCurrentIndex(1);
}
void MainWindow::showEvent(QShowEvent* /*event*/)
{
ui->allRgbWidget->LoadImage(":DefaultImage.jpg");
}
void MainWindow::OpenImagePreviewWindow(const QImage& toPreview)
{
QScrollArea* previewArea = new QScrollArea(nullptr);
QLabel* previewImage = new QLabel();
previewImage->setPixmap(QPixmap::fromImage(toPreview));
previewArea->setWidget(previewImage);
previewArea->showMaximized();
}