-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
326 lines (290 loc) · 8.97 KB
/
mainwindow.cpp
File metadata and controls
326 lines (290 loc) · 8.97 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
11/01/2022 https://github.com/su8/kurger
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sys/stat.h>
#include <sys/types.h>
#include <QApplication>
#include <QFileDialog>
#include <QMessageBox>
#include <QIcon>
#include <QDesktopServices>
#include <QUrl>
/* Tray icon
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
#include <QCloseEvent>*/
#include "mainwindow.h"
#include "./ui_mainwindow.h"
static void RaiseMsg(const QString &msg1, const QString &msg2, unsigned short int isWarning);
static void pdf2img(const char *str);
static size_t indexLastSep(const char *str);
#define VLA 4999
static std::string PdfFile = "";
static char lastDirectory[VLA+1] = {'\0'};
static Ui::MainWindow *UI;
/* Tray icon
static QAction *exitAction;
static QMenu *trayIconMenu;
static QSystemTrayIcon *sysTrayIcon;*/
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
UI = ui;
ui->comboBox->addItems({"png", "jpg", "bmp", "tiff"});
ui->comboBox_2->addItems({"png16m", "pngalpha", "pnggray", "jpeg", "jpegcmyk", "jpeggray", "bmp16m", "bmpgray", "tiff24nc", "tiffgray"});
/* Tray icon, based on https://amin-ahmadi.com/2019/06/20/implementing-minimize-to-tray-functionality-using-qt-c
closing = 0U;
exitAction = new QAction(tr("&Exit"), this);
connect(exitAction, &QAction::triggered, [this]() { closing = 1U; close(); });
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(exitAction);
sysTrayIcon = new QSystemTrayIcon(this);
sysTrayIcon->setContextMenu(trayIconMenu);
sysTrayIcon->setIcon(QIcon("media/icon.xpm"));
sysTrayIcon->show();
connect(sysTrayIcon, &QSystemTrayIcon::activated, [this](auto reason)
{
if (reason == QSystemTrayIcon::Trigger)
{
if (isVisible()) { hide(); }
else { show(); activateWindow(); }
}
});
*/
}
MainWindow::~MainWindow()
{
//delete UI;
/* Tray icon
delete sysTrayIcon;
delete trayIconMenu;
delete exitAction; */
delete ui;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
/* Tray icon
void MainWindow::closeEvent(QCloseEvent *event)
{
if (closing == 1U)
{
event->accept();
}
else
{
this->hide();
event->ignore();
}
}
*/
void MainWindow::on_pushButton_clicked()
{
if (UI->spinBox_2->value() > UI->spinBox_3->value())
{
RaiseMsg("Reversed Numbers", "From page can't be greater than To page.", 1U);
return;
}
if (PdfFile == "")
{
RaiseMsg("No pdf selected", "You must select some PDF file first and then click this button.", 1U);
return;
}
pdf2img(PdfFile.c_str());
}
void MainWindow::on_pushButton_2_clicked()
{
char dialogFile[VLA+1] = {'\0'};
if (UI->spinBox_2->value() > UI->spinBox_3->value())
{
RaiseMsg("Reversed Numbers", "From page can't be greater than To page.", 1U);
return;
}
QString filename = QFileDialog::getOpenFileName(
nullptr,
QObject::tr("Open PDF file"),
(*lastDirectory) ? lastDirectory : QDir::currentPath(),
QObject::tr("PDF File (*.pdf)"));
if ((!filename.isEmpty()) && (!filename.isNull()))
{
snprintf(dialogFile, VLA, "%s", filename.toStdString().c_str());
snprintf(lastDirectory, VLA, "%s", dialogFile);
lastDirectory[indexLastSep(dialogFile)] = '\0';
PdfFile = filename.toStdString();
pdf2img(dialogFile);
}
}
static void RaiseMsg(const QString &msg1, const QString &msg2, unsigned short int isWarning)
{
QMessageBox msg;
msg.setText(msg2);
msg.setIcon((isWarning == 1U) ? QMessageBox::Warning : QMessageBox::Information);
msg.setWindowTitle(msg1);
msg.setWindowIcon(static_cast<QIcon>("media/icon.xpm"));
msg.exec();
}
void MainWindow::on_comboBox_currentIndexChanged(int index)
{
unsigned short int sdevCombo = 0;
if (index == 0)
{
sdevCombo = 0;
}
else if (index == 1)
{
sdevCombo = 3;
}
else
{
sdevCombo = (index == 2) ? 6 : 8;
}
UI->comboBox_2->setCurrentIndex(sdevCombo);
}
void MainWindow::on_comboBox_2_currentIndexChanged(int index)
{
unsigned short int sdevCombo = 0, imageCombo = 0;
if (index == 0)
sdevCombo = 0;
else if (index == 1)
sdevCombo = 1;
else if (index == 2)
sdevCombo = 2;
else /* 'active' not in png group */
{
imageCombo = 1; /* The jpg group */
if (index == 3)
sdevCombo = 3;
else if (index == 4)
sdevCombo = 4;
else if (index == 5)
sdevCombo = 5;
else /* 'active' not in jpg group */
{
imageCombo = 2; /* The bmp group */
if (index == 6)
sdevCombo = 6;
else if (index == 7)
sdevCombo = 7;
else /* 'active' not in bmp group */
{
imageCombo = 3; /* The tiff group */
sdevCombo = (index == 8) ? 8 : 9;
}
}
}
UI->comboBox->setCurrentIndex(imageCombo);
UI->comboBox_2->setCurrentIndex(sdevCombo);
}
/*
return the last separator '/' index number,
we will use this index number to create our own
`basename' alternative in C++
*/
static size_t indexLastSep(const char *str)
{
const char *ptr = str;
size_t sepIndex = 0;
size_t x = 0;
for (; *ptr; x++, ptr++)
{
if ('/' == *ptr)
{
sepIndex = x; /* keep in mind that we use loop */
}
}
return sepIndex;
}
static void pdf2img(const char *str)
{
char imageCombo[10] = {'\0'};
char sdeviceCombo[10] = {'\0'};
char pdfName[VLA+1] = {'\0'};
char BaseName[VLA+1] = {'\0'};
char params[VLA+1] = {'\0'};
char ren1[VLA+1] = {'\0'};
char ren2[VLA+1] = {'\0'};
char created_dir[VLA+1] = {'\0'};
int spin1 = UI->spinBox_2->value();
int spin2 = UI->spinBox_3->value();
int smallRange = (spin2 - spin1) + 2;
int bigRange = spin1;
int y = 0;
size_t dirnameLen = indexLastSep(str);
size_t x = 0;
size_t z = 0;
size_t fit2 = strlen(str) - 4; /* exclude the .pdf file extension */
struct stat DiR;
if (1850U <= fit2)
{
RaiseMsg("Warning!", "The given filename is too long!", 1U);
return;
}
if (spin1 > spin2)
{
RaiseMsg("Reversed Numbers", "From page can't be greater than To page.", 1U);
return;
}
snprintf(imageCombo, 10, "%s", UI->comboBox->currentText().toStdString().c_str());
snprintf(sdeviceCombo, 10, "%s", UI->comboBox_2->currentText().toStdString().c_str());
snprintf(pdfName, VLA, "%s", str);
for (z = 0, x = dirnameLen+1; x < fit2; x++, z++)
{
BaseName[z] = pdfName[x]; /* /path/to/some.pdf -> some */
}
BaseName[z] = '\0';
if (250U < z)
{
RaiseMsg("Warning!", "The given filename is too long!", 1U);
return;
}
snprintf(created_dir, VLA, "%s_converted", pdfName);
stat(created_dir, &DiR);
if (0 == S_ISDIR(DiR.st_mode))
{
#if defined(__OpenBSD__) || defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
mkdir(created_dir, 0700);
#define GS "gs"
#else
mkdir(created_dir);
#define GS "C:\\gs\\bin\\gswin64.exe"
#endif /*__OpenBSD__ || __linux__ || __FreeBSD__ || __NetBSD__ */
}
snprintf(params, VLA, GS " -dBATCH -dNOPAUSE -dQUIET -dFirstPage=%d -dLastPage=%d "
"-sOutputFile=\"%s\"_pAge_%%01d.%s -sDEVICE=%s -r%d "
"-dGraphicsAlphaBits=4 -sBandListStorage=memory "
"-dBufferSpace=99000 -dNumRenderingThreads=8 \"%s\"",
spin1, spin2, pdfName, imageCombo, sdeviceCombo, UI->spinBox->value(), str);
RaiseMsg("Please wait...", "Please wait until we convert the requested images.", 0U);
system(params);
for (y = 1; y < smallRange; y++, bigRange++)
{
snprintf(ren1, VLA, "%s_pAge_%d.%s", pdfName, y, imageCombo);
snprintf(ren2, VLA, "%s/%s_page_%d.%s", created_dir, BaseName, bigRange, imageCombo);
remove(ren2);
rename(ren1, ren2);
}
QDesktopServices::openUrl(QUrl::fromLocalFile(static_cast<QString>(created_dir)));
}