-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainwindow.cpp
151 lines (122 loc) · 3.83 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 <QFormLayout>
#include <QDialog>
#include <QApplication>
#include <QIcon>
#include <QMenuBar>
#include <QMenu>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
_convertor(NULL)
{
// workaround because themed icons dont work in fluxbox as they should
if(QIcon::themeName().isEmpty() || QIcon::themeName() == "hicolor") {
QIcon::setThemeName("Tango");
}
_init_actions();
_init_gui();
_init_connections();
revalidate();
}
MainWindow::~MainWindow()
{
}
void MainWindow::_init_actions()
{
_quit = new QAction(QIcon::fromTheme("exit"), trUtf8("Quit"), this);
}
void MainWindow::_init_gui()
{
setMinimumSize(600, 200);
QMenu *menu = this->menuBar()->addMenu(trUtf8("Images2PDF"));
menu->addAction(_quit);
QWidget *cw = new QWidget();
setCentralWidget(cw);
QFormLayout *layout = new QFormLayout(cw);
_folder = new FileInput(FileInput::DirectoryMode);
_folder->dialog_caption = trUtf8("Images directory");
_file = new FileInput(FileInput::FileSaveMode);
_file->dialog_caption = trUtf8("Save as PDF");
_process = new QPushButton(trUtf8("Merge"));
_abort = new QPushButton(trUtf8("Abort"));
_status = new QLabel();
_progress = new QProgressBar();
_abort->setVisible(false);
_progress->setMinimum(0);
_progress->setVisible(false);
layout->addRow(trUtf8("Folder with images"), _folder);
layout->addRow(trUtf8("Target pdf file:"), _file);
layout->addRow(_process);
layout->addRow(_abort);
layout->addRow(_status);
layout->addRow(_progress);
}
void MainWindow::_init_connections()
{
connect(_folder, SIGNAL(valueChanged()), this, SLOT(revalidate()));
connect(_file, SIGNAL(valueChanged()), this, SLOT(revalidate()));
connect(_quit, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(_process, SIGNAL(clicked()), this, SLOT(process()));
connect(_abort, SIGNAL(clicked()), this, SLOT(processAbort()));
}
void MainWindow::revalidate()
{
if(_folder->isReadableDir() && _file->isWritableFile()) {
_status->setText(trUtf8("You can press Merge button now."));
_process->setEnabled(true);
} else {
_status->setText(trUtf8("Select path to dir with images and where to save pdf."));
_process->setEnabled(false);
}
}
void MainWindow::process()
{
if(_convertor) {
return;
}
_status->setText(trUtf8("Preparing ..."));
_folder->setEnabled(false);
_file->setEnabled(false);
_process->setEnabled(false);
_convertor = new Images2PDF(_folder->value(), _file->value());
_status->setText(trUtf8("Preparing to process %1 images").arg(_convertor->imageCount()));
_progress->setValue(0);
_progress->setMaximum(_convertor->imageCount());
_progress->setVisible(true);
_abort->setVisible(true);
connect(_convertor, SIGNAL(processing(QString)), this, SLOT(processProcessing(QString)));
connect(_convertor, SIGNAL(progress(int)), this, SLOT(processProgress(int)));
connect(_convertor, SIGNAL(finished(bool)), this, SLOT(processFinished(bool)));
_convertor->start();
}
void MainWindow::processFinished(bool success)
{
_folder->setEnabled(true);
_file->setEnabled(true);
_process->setEnabled(true);
_abort->setVisible(false);;
_progress->setVisible(false);
if(success) {
_status->setText(trUtf8("All done, enjoy your pdf ;)"));
} else if(_convertor) {
_status->setText(_convertor->error());
}
if(_convertor) {
delete _convertor;
_convertor = NULL;
}
}
void MainWindow::processProcessing(QString fileName)
{
_status->setText(trUtf8("Processing %1").arg(fileName));
}
void MainWindow::processProgress(int done)
{
_progress->setValue(done);
}
void MainWindow::processAbort()
{
if(_convertor) {
_convertor->abort();
}
}