-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_window.cpp
232 lines (205 loc) · 6.39 KB
/
main_window.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
#include <QtCore/QDebug>
#include <QMessageBox>
#include <QFileDialog>
#include <QDropEvent>
#include <QUrl>
#include <QtConcurrentRun>
#include <QMouseEvent>
#include <QSettings>
#include <QProgressDialog>
#include "main_window.h"
#include "tab_page.h"
#include "resize_dialog.h"
const char FORMAT_LIST[] = "Images (*.png *.jpg *.jpeg *.bmp *.pbm *.pgm *.ppm *.tiff *.xbm *.xpm *.nef *.cr2 *.crw *.raf *.dng *.mos *.kdc *.dcr)";
static const QString LOADDIR_KEY("init-file-choose-dir");
MainWindow::MainWindow(QWidget* parent):
QMainWindow(parent)
{
ui.setupUi(this);
setTabActionsEnabled(false);
connect(ui.actionAbout_Qt, SIGNAL(triggered()),
this, SLOT(aboutQT()));
connect(ui.actionAbout, SIGNAL(triggered()),
this, SLOT(about()));
connect(ui.action_Open, SIGNAL(triggered()),
this, SLOT(chooseFiles()));
connect(ui.action_Resize, SIGNAL(triggered()),
this, SLOT(resizeDialog()));
connect(ui.actionZoom_In, SIGNAL(triggered()),
this, SLOT(zoomIn()));
connect(ui.actionZoom_Out, SIGNAL(triggered()),
this, SLOT(zoomOut()));
connect(ui.action_Close, SIGNAL(triggered()),
this, SLOT(closeCurrentTab()));
connect(ui.actionClose_All, SIGNAL(triggered()),
this, SLOT(closeAllTabs()));
connect(ui.tabWidget, SIGNAL(tabCloseRequested(int)),
this, SLOT(closeTab(int)));
connect(ui.tabWidget, SIGNAL(currentChanged(int)),
this, SLOT(displayCurrentTab()));
}
void MainWindow::setTabIcon(int index)
{
static QIcon icon(":/icon/icons/image.svg");
ui.tabWidget->setTabIcon(index, icon);
}
void MainWindow::closeAllTabs()
{
for (int i=0; i<ui.tabWidget->count(); ++i) {
auto page = ui.tabWidget->widget(i);
delete page;
}
ui.tabWidget->clear();
}
void MainWindow::closeCurrentTab()
{
closeTab(ui.tabWidget->currentIndex());
}
void MainWindow::setTabActionsEnabled(bool flag)
{
ui.actionZoom_In->setEnabled(flag);
ui.actionZoom_Out->setEnabled(flag);
ui.action_Close->setEnabled(flag);
ui.action_Resize->setEnabled(flag);
}
void MainWindow::mouseDoubleClickEvent(QMouseEvent* event)
{
chooseFiles();
}
void MainWindow::zoomIn()
{
TabPage* page = qobject_cast<TabPage*>(ui.tabWidget->currentWidget());
page->zoomIn();
}
void MainWindow::zoomOut()
{
TabPage* page = qobject_cast<TabPage*>(ui.tabWidget->currentWidget());
page->zoomOut();
}
void MainWindow::resizeDialog()
{
TabPage* page = qobject_cast<TabPage*>(ui.tabWidget->currentWidget());
ResizeDialog dialog(page->getHeight(), page->getWidth(), this);
connect(&dialog,
SIGNAL(dialogFinished(int, int, bool, bool, const QString&,
int)),
this, SLOT(resizeOpts(int, int, bool, bool, const QString&,
int)));
dialog.exec();
}
void MainWindow::saveImage(int index, int height, int width, bool sameDir,
const QString& outdir, int quality)
{
TabPage* page =
qobject_cast<TabPage*>(ui.tabWidget->widget(index));
auto dir = sameDir ? QFileInfo(page->getFullpath()).path() : outdir;
page->save(height, width, dir, quality);
}
void MainWindow::resizeOpts(int height, int width, bool applyToAll,
bool sameDir, const QString& outdir, int quality)
{
if (applyToAll) {
auto count = ui.tabWidget->count();
QProgressDialog progress(tr("Resizing..."), tr("Abort"), 0, count,
this);
progress.setWindowModality(Qt::WindowModal);
for (int i=0; i<count; ++i) {
progress.setValue(i);
if (progress.wasCanceled())
break;
saveImage(i, height, width, sameDir, outdir, quality);
}
progress.setValue(count);
} else {
QProgressDialog progress(tr("Resizing..."), tr("Abort"), 0, 1,
this);
progress.setWindowModality(Qt::WindowModal);
saveImage(ui.tabWidget->currentIndex(), height, width, sameDir,
outdir, quality);
progress.setValue(1);
}
}
void MainWindow::dragEnterEvent(QDragEnterEvent* event)
{
if (event->mimeData()->hasFormat("text/uri-list")) {
event->acceptProposedAction();
}
}
void MainWindow::dropEvent(QDropEvent* event)
{
qDebug() << "Drop event";
auto mimeData = event->mimeData();
if (mimeData->hasUrls()) {
QStringList pathList;
auto urlList = mimeData->urls();
for (int i=0; i<urlList.size(); ++i) {
pathList.append(urlList.at(i).toLocalFile());
}
addTabs(pathList);
}
}
void MainWindow::displayCurrentTab()
{
if (ui.tabWidget->count() == 0) {
return;
}
TabPage* page = qobject_cast<TabPage*>(ui.tabWidget->currentWidget());
Q_ASSERT(page != nullptr);
page->loadImage();
setTabActionsEnabled(true);
}
void MainWindow::closeTab(int index)
{
auto page = ui.tabWidget->widget(index);
Q_ASSERT(page != nullptr);
ui.tabWidget->removeTab(index);
delete page;
if (ui.tabWidget->count() == 0) {
setTabActionsEnabled(false);
}
}
void MainWindow::loadErrorHandler(const QString& fullpath)
{
QMessageBox::information(this, tr("Error!"),
tr("Cannot load %1.").arg(fullpath));
TabPage* page = qobject_cast<TabPage*>(sender());
auto idx = ui.tabWidget->indexOf(page);
closeTab(idx);
}
void MainWindow::aboutQT()
{
QMessageBox::aboutQt(this);
}
void MainWindow::about()
{
QMessageBox::about(this, "ImgResize", tr("Resize your images."));
}
void MainWindow::addTabs(const QStringList& pathList)
{
auto it = pathList.begin();
while (it != pathList.end()) {
auto page = new TabPage(*it);
lastTabIdx = ui.tabWidget->addTab(page,
QFileInfo(*it).fileName());
connect(page, SIGNAL(loadError(const QString&)),
this, SLOT(loadErrorHandler(const QString&)));
setTabIcon(lastTabIdx);
qDebug() << "Tab added: " << *it;
++it;
}
ui.tabWidget->setCurrentIndex(lastTabIdx);
}
void MainWindow::chooseFiles()
{
QSettings settings;
auto initdir = settings.value(LOADDIR_KEY, ".").toString();
auto files = QFileDialog::getOpenFileNames(
this,
tr("Select one or more files to open"),
initdir,
FORMAT_LIST);
addTabs(files);
if (files.size() > 0) {
settings.setValue(LOADDIR_KEY, files.first());
}
}