-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpages.h
More file actions
132 lines (117 loc) · 4.89 KB
/
Copy pathpages.h
File metadata and controls
132 lines (117 loc) · 4.89 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
// ---------------------------------------------------------------------
//
// qtgrader - the interactive high speed grading assistant
// Copyright (C) 2012-2013 Wayne Piekarski
// wayne@tinmith.net http://tinmith.net/wayne
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
//
// ---------------------------------------------------------------------
#pragma once
#include <QProcess>
#include <QImage>
#include <QPixmap>
#include <QDir>
#include "debug.h"
class Pages {
friend class Global;
private:
Pages(QString path)
{
if(path.contains(' '))
GEXITDIALOG(QString("Cannot process path %1 that contains a space in it").arg(path));
if(path.contains('\''))
GEXITDIALOG(QString("Cannot process path %1 that contains a single quote in it").arg(path));
if(path.contains('"'))
GEXITDIALOG(QString("Cannot process path %1 that contains a double quote in it").arg(path));
if(path.contains('\\'))
GEXITDIALOG(QString("Cannot process path %1 that contains a backslash in it").arg(path));
/* See if we need to firstly convert PDF to JPG */
preparePDFtoJPG(path);
/* The JPEG files should be there, so we can load them in now */
_path = path;
QDir dir (path, "*.jpg");
dir.setFilter(QDir::Files);
GDEBUG("Loading JPG images from dir [%s]",qPrintable(dir.canonicalPath()));
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); i++)
{
QFileInfo f = list.at(i);
GDEBUG("Found image %d from file [%s]", i, qPrintable(f.fileName()));
filenames.push_back(f.filePath());
}
if (list.size() == 0)
GEXITDIALOG(QString("No images found in scan directory [%1]").arg(qPrintable(path)));
GASSERT(size() > 0, "Size %zu is invalid", size());
}
void preparePDFtoJPG (QString path)
{
QDir dir (path, "*.jpg");
dir.setFilter(QDir::Files);
GDEBUG("Scanning for JPG images in dir [%s]",qPrintable(dir.canonicalPath()));
if (dir.entryInfoList().size() != 0)
{
GDEBUG("Found %d JPG files in dir [%s], so will not perform another PDF conversion", dir.entryInfoList().size(), qPrintable(path));
return;
}
QDir pdfs (path, "*.pdf");
pdfs.setFilter(QDir::Files);
QFileInfoList list = pdfs.entryInfoList();
size_t found_pdfs = 0;
for (int i = 0; i < list.size(); i++)
{
QString pdfname = list.at(i).absoluteFilePath();
if (pdfname.contains("report-"))
GEXITDIALOG(QString("Found generated PDF file %1 but there are no JPG images, and this should not be possible").arg(pdfname));
found_pdfs++;
QString jpegprefix = pdfname;
jpegprefix.replace(".pdf", "");
GASSERT(jpegprefix.size()+4 == pdfname.size(), "Was not able to replace .pdf in file name [%s]", qPrintable(pdfname));
// Use pdfimages to do the work. Note that QProcess only executes a single program, and you
// need to know the location of the executable. So we use a bash wrapper to do this. You also
// need to include --login so the PATH is set correctly to find the pdfimages program.
QString cmd = QString("/bin/bash --login -c \"pdfimages -j %1 %2\"").arg(pdfname).arg(jpegprefix);
QProcess pdfimages;
GDEBUG("Processing PDF file [%s] with cmd [%s]", qPrintable(pdfname), qPrintable(cmd));
pdfimages.start(cmd);
if (!pdfimages.waitForFinished())
GEXITDIALOG(QString("An error %1 occurred running command [%2], make sure you have pdfimages installed in your PATH").arg(pdfimages.errorString()).arg(cmd));
}
if (found_pdfs == 0)
GEXITDIALOG(QString("No PDF or JPG files found in scan directory [%1]").arg(qPrintable(path)));
}
private:
QString& getFilename(size_t elem)
{
GASSERT(elem < size(), "Requested elem %zu not less than %zu", elem, size());
return filenames[elem];
}
QPixmap getQPixmap(size_t elem)
{
GASSERT(elem < size(), "Requested elem %zu not less than %zu", elem, size());
GDEBUG("Loading image %zu from file [%s]", elem, qPrintable(getFilename(elem)));
QImage img (getFilename(elem));
QPixmap pix = QPixmap::fromImage (img);
return pix;
}
size_t size()
{
GASSERT(filenames.size() > 0, "Filenames size %zu is not greater than zero", filenames.size());
return filenames.size();
}
QString& getPath() { return _path; }
private:
std::vector<QString> filenames;
QString _path;
};