-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
212 lines (162 loc) · 5.5 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->textBrowser->append("Application started");
ui->textBrowser->append("Start by selecting folders with images");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updateListWidget()
{
QListWidget *widget = ui->imageList;
widget->clear();
for(vector<QString> &vec: fileDirectories__){
for(QString const &str : vec){
new QListWidgetItem(str, widget);
}
}
}
bool MainWindow::combineImages()
{
if(saveFilePath__.size() == 0){
ui->textBrowser->append("Select save file before creating images!");
return false;
}
// number of arrays
vector<vector<QString>> combinations;
int n = fileDirectories__.size();
// to keep track of next element in each of
// the n arrays
int* indices = new int[n];
// initialize with first element's index
for (int i = 0; i < n; i++)
indices[i] = 0;
while (1) {
vector<QString> vec;
// print current combination
for (int i = 0; i < n; i++){
vec.push_back(fileDirectories__[i][indices[i]]);
}
combinations.push_back(vec);
// find the rightmost array that has more
// elements left after the current element
// in that array
int next = n - 1;
while (next >= 0 &&
(indices[next] + 1 >= int(fileDirectories__[next].size())))
next--;
// no such array is found so no more
// combinations left
if (next < 0)
break;
// if found move to next element in that
// array
indices[next]++;
// for all arrays to the right of this
// array current index again points to
// first element
for (int i = next + 1; i < n; i++)
indices[i] = 0;
}
delete[] indices;
int counter = 1;
// update text browser
ui->textBrowser->append(QString::number(combinations.size()) + " combinations created");
// all created images will have the same size as first image
QSize size = QImage(combinations[0][0]).size();
// creating combinations
for(vector<QString> &vec : combinations){
// determine savefile name
QString saveFileName = saveFilePath__+"/"+QString::number(counter)+".png";
QImage resultImage;
resultImage = QImage(size, QImage::Format_ARGB32_Premultiplied);
QPainter painter(&resultImage);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
// combine all pngs
for(QString &path : vec){
qDebug() << path << Qt::endl;
QImage img(path);
painter.drawImage(0, 0, img);
}
painter.end();
// save image to file
resultImage.save(saveFileName);
allImages__.push_back(resultImage);
ui->textBrowser->append("#"+QString::number(counter) + " image saved");
counter += 1;
}
ui->textBrowser->append("All images created");
return true;
}
void MainWindow::on_fileselectButton_clicked()
{
// set file to save generated images
QDir dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
QStringList image_list = dir.entryList(QStringList() << "*.jpg" << "*.JPG" << "*.png", QDir::Files);
vector<QString> vec;
for(QString& str : image_list){
vec.push_back(dir.absolutePath()+"/"+str);
}
if(!vec.empty()){
fileDirectories__.push_back(vec);
ui->textBrowser->append(QString::number(vec.size()) + " images added from " + dir.path());
}
if(!fileDirectories__.empty()){
updateListWidget();
}
}
void MainWindow::initImageViewer()
{
// start image browser
if(allImages__.size() == 0){
return;
}
QImage firstImage = allImages__[0];
ui->imageLabel->setPixmap(QPixmap::fromImage(firstImage));
ui->imageLabel->setScaledContents(true);
ui->imageLabel->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
updateImageIndexLabel();
}
void MainWindow::on_createImagesButton_clicked()
{
if(combineImages()){
initImageViewer();
}
}
void MainWindow::on_nextImageButton_clicked()
{
if(imageViewerIndex < int(allImages__.size() - 1)){
imageViewerIndex += 1;
ui->imageLabel->setPixmap(QPixmap::fromImage(allImages__[imageViewerIndex]));
updateImageIndexLabel();
}
}
void MainWindow::on_prevImageButton_clicked()
{
if(imageViewerIndex > 0){
imageViewerIndex -= 1;
ui->imageLabel->setPixmap(QPixmap::fromImage(allImages__[imageViewerIndex]));
updateImageIndexLabel();
}
}
void MainWindow::updateImageIndexLabel()
{
QString labelText = QString::number(imageViewerIndex+1) + " of " + QString::number(allImages__.size());
ui->imageIndexLabel->setText(labelText);
}
void MainWindow::on_saveFileButton_clicked()
{
saveFilePath__ = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
}