-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
182 lines (141 loc) · 5.57 KB
/
mainwindow.cpp
File metadata and controls
182 lines (141 loc) · 5.57 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//connects fileSelectButton to its function
//connects convertButton to its function
connect(ui->fileSelect, &QPushButton::clicked, this, &MainWindow::selectFiles);
connect(ui->convert , &QPushButton::clicked, this, &MainWindow::convertSelectedFiles);
//creates a new converter object and moves it to a seperate thread
converter = new Convert();
converter->moveToThread(&thread);
//connects converter object to progress bar
//connects converter finished event to clearing the file list
//connects the request convert event to the converters conversion function
connect(converter, SIGNAL(progressChanged(int)), ui->progressBar, SLOT(setValue(int)));
connect (converter, SIGNAL(finished()), this, SLOT(clearFileList()));
connect(this, SIGNAL(requestConvert(QStringList,QString)), converter, SLOT(handleConvert(QStringList,QString)));
//connects converter object to gui dialog box for jpeg
connect(converter, SIGNAL(openJpegOptionsMenu(std::vector<uint>, bool)), this, SLOT(getJpegOptions(std::vector<uint>, bool)));
//afters connections are made, the thread is started and seperated
thread.start();
//set default values for screen
ui->selected->setText("");
ui->progressBar->setValue(0);
//adds items to output select list
for (int i = 0; i<Convert::acceptedOutputs.size(); i++) {
QString val = Convert::acceptedOutputs.at(i);
ui->outputType->addItem(val);
}
//create a settings object and handle default values
QSettings settings("Ethan", "Image Converter");
//set default vals if they dont exist
if (settings.contains("gui/lastpath"))
settings.setValue("gui/lastpath", "/");
setAcceptDrops(true);
}
MainWindow::~MainWindow()
{
delete ui;
delete converter;
thread.quit();
thread.wait(5);
}
void MainWindow::selectFiles() {
//condense list of input types into a string
QString types = "Images (";
for (int i = 0; i<Convert::acceptedInputs.size(); i++) {
QString val = Convert::acceptedInputs.at(i);
types += "*." + val;
if (i != Convert::acceptedInputs.size()-1) {
types += " ";
}
}
types += ")";
//open file select window
selectedFiles = QFileDialog::getOpenFileNames(
this,
"Select one or more images to open",
settings.value("gui/lastpath").toString(),
types);
//display the chosen file names
displayFileNames(selectedFiles);
ui->progressBar->setValue(0);
//update settings to the last selected folder
if (selectedFiles.size() > 0) {
QRegularExpression re("\\w+\\.\\w*$");
QString f = selectedFiles[0];
QString pth = f.replace(re, "");
settings.setValue("gui/lastpath", pth);
settings.sync();
}
}
QString MainWindow::sanitizeFileName(QString file) {
//uses regex to remove everything before the last "\"
QRegularExpression re(".*\\/");
file.replace(re, "");
return file;
}
void MainWindow::displayFileNames(QStringList files) {
//takes each selected filename, removes extra path and adds it to a list
// only allows three files to be displayed before adding "and x more"
QString out = "";
int range = (files.size()<3)? files.size() : 3;
for (int i = 0; i<range; i++) {
out += sanitizeFileName(files.at(i));
if (i != files.size()-1) {
out += ", ";
}
}
if (files.size() > 3) {
out += "and " + QString::number(files.size() - 3) + " more";
}
ui->selected->setText(out);
}
void MainWindow::clearFileNames() {
//removes the displayed filenames
ui->selected->setText("");
}
void MainWindow::clearFileList() {
//clears filenames and clears all selected files
this->clearFileNames();
this->selectedFiles = {};
}
void MainWindow::convertSelectedFiles() {
//called when convert button is pressed
//sets progress bar to 0 and requests a conversion from the converter
ui->progressBar->setValue(0);
QString outType = ui->outputType->itemText(ui->outputType->currentIndex()).toLower();
emit requestConvert(selectedFiles, outType);
}
void MainWindow::getJpegOptions(std::vector<uint> sizes, bool isChecked) {
//create a new dialog box
JpegOptions dialog(sizes, isChecked);
//connect dialog output with converter input
connect(&dialog, SIGNAL(userInput(std::vector<uint>)), converter, SLOT(saveJpegSettings(std::vector<uint>)));
dialog.exec();
}
void MainWindow::dragEnterEvent(QDragEnterEvent *e)
{
if (e->mimeData()->hasUrls()) {
e->acceptProposedAction();
}
}
void MainWindow::dropEvent(QDropEvent *e)
{
foreach (const QUrl &url, e->mimeData()->urls()) {
QString fileName = url.toLocalFile();
foreach (const QString ext, converter->acceptedInputs) {
qDebug() << sanitizeFileName(fileName) << ext << converter->getFileExtension(fileName).toLower();
if (ext == converter->getFileExtension(fileName).toLower()) {
qDebug() << "match";
selectedFiles.append(fileName);
}
}
}
displayFileNames(selectedFiles);
ui->progressBar->setValue(0);
}