[Add] Implented async file splitting to multiple single page pdfs
This commit is contained in:
parent
9d1e82deb4
commit
0cbe75daac
@ -8,17 +8,57 @@ PDFJam::PDFJam(void) {
|
||||
|
||||
}
|
||||
|
||||
void PDFJam::run(void) {
|
||||
#if 0
|
||||
if((pid = fork()) < 0) {
|
||||
|
||||
}
|
||||
else if(pid > 0) {
|
||||
/* Parent. */
|
||||
} else {
|
||||
/* Child. */
|
||||
system("echo 'tata' > /tmp/test.txt");
|
||||
}
|
||||
#endif
|
||||
/* Ensure the folder exists. */
|
||||
void PDFJam::makeFolder(QString path) {
|
||||
QString tmp = "mkdir -p %1";
|
||||
int value = system(tmp.arg(path).toStdString().c_str());
|
||||
if(value != 0)
|
||||
qDebug() << "ERROR: Failed to make " << path;
|
||||
}
|
||||
|
||||
void PDFJam::pushCommand(QString cmd) {
|
||||
mutex.lock();
|
||||
cmdQueue.push_back(cmd);
|
||||
mutex.unlock();
|
||||
if(!isRunning()) start();
|
||||
}
|
||||
|
||||
void PDFJam::loadFile(QString fileName, int fileNum, int numPages) {
|
||||
QString path = "/tmp/hpdf/%1/";
|
||||
path = path.arg(QString::number(fileNum));
|
||||
makeFolder(path);
|
||||
|
||||
QString tmp = "pdfjam %1 %2 -- outfile %3%4.pdf";
|
||||
QString cmd = "";
|
||||
for(int i = 1; i <= numPages; i++) {
|
||||
cmd += tmp.arg(fileName).arg(QString::number(i)).arg(path).arg(QString::number(i)) + ";";
|
||||
}
|
||||
pushCommand(cmd);
|
||||
}
|
||||
|
||||
QString PDFJam::nextCommand(void) {
|
||||
QString cmd;
|
||||
mutex.lock();
|
||||
cmd = cmdQueue.first();
|
||||
cmdQueue.pop_front();
|
||||
mutex.unlock();
|
||||
return cmd;
|
||||
}
|
||||
|
||||
bool PDFJam::isQueueEmpty(void) {
|
||||
return (cmdQueue.size()==0);
|
||||
}
|
||||
|
||||
void PDFJam::run(void) {
|
||||
while(!isQueueEmpty()) {
|
||||
QString cmd = nextCommand();
|
||||
int value = system(cmd.toStdString().c_str());
|
||||
if(value != 0)
|
||||
qDebug() << "ERROR: Failed to execute " << cmd;
|
||||
else
|
||||
qDebug() << "SUCCESS: executed " << cmd;
|
||||
}
|
||||
|
||||
/*system("echo 'tata' > /tmp/test.txt");*/
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,28 @@
|
||||
#pragma once
|
||||
#include <QThread>
|
||||
#include <QMutex>
|
||||
#include <poppler-qt5.h>
|
||||
#include <QImage>
|
||||
|
||||
class PDFJam { /*:public QThread {*/
|
||||
class PDFPageWidget;
|
||||
class QImage;
|
||||
|
||||
class PDFJam : public QThread {
|
||||
Q_OBJECT
|
||||
public:
|
||||
PDFJam(void);
|
||||
|
||||
void pushCommand(QString);
|
||||
void loadFile(QString fileName, int, int);
|
||||
void makeFolder(QString);
|
||||
|
||||
protected:
|
||||
void run(void);
|
||||
|
||||
private:
|
||||
volatile bool stopped;
|
||||
|
||||
QVector<QString> cmdQueue;
|
||||
QString nextCommand();
|
||||
bool isQueueEmpty();
|
||||
QMutex mutex;
|
||||
};
|
||||
|
||||
|
@ -12,7 +12,6 @@ void PDFPreviewWidget::setImage(QImage image) {
|
||||
}
|
||||
|
||||
void PDFPreviewWidget::regenImage(void) {
|
||||
if(pPage != NULL) {
|
||||
double dpi;
|
||||
double dpi2;
|
||||
QSize targetSize = this->size();
|
||||
@ -22,11 +21,11 @@ void PDFPreviewWidget::regenImage(void) {
|
||||
dpi2 = targetSize.width() / (float)oriSize.width()*72;
|
||||
dpi = dpi<dpi2?dpi:dpi2;
|
||||
previewImage = pPage->renderToImage(dpi, dpi);
|
||||
}
|
||||
}
|
||||
|
||||
void PDFPreviewWidget::previewUpdate(Poppler::Page* pp) {
|
||||
pPage = pp;
|
||||
qDebug() << "Set new popler page" << pp;
|
||||
regenImage();
|
||||
update();
|
||||
}
|
||||
@ -36,6 +35,7 @@ void PDFPreviewWidget::pageClicked(QMouseEvent* mouseEvent, QImage image) {
|
||||
}
|
||||
|
||||
void PDFPreviewWidget::resizeEvent(QResizeEvent* event) {
|
||||
if(pPage != NULL)
|
||||
regenImage();
|
||||
}
|
||||
|
||||
|
@ -39,6 +39,7 @@ void PDFTableWidget::loadFile(QString fileName) {
|
||||
PDFFileWidget* fileWidget = new PDFFileWidget();
|
||||
fileWidget->setAncestor(this);
|
||||
fileWidget->setDocument(doc, fileName);
|
||||
pdfJam.loadFile(fileName, files.size()-1, doc->numPages());
|
||||
connect(fileWidget, SIGNAL(pageClicked(QMouseEvent*,QImage)), this,
|
||||
SIGNAL(pageClicked(QMouseEvent*,QImage)));
|
||||
connect(fileWidget, SIGNAL(previewUpdate(Poppler::Page*)), this,
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <QFrame>
|
||||
#include <poppler-qt5.h>
|
||||
#include "pdf_jam.h"
|
||||
|
||||
class QImage;
|
||||
class QVBoxLayout;
|
||||
@ -16,6 +17,7 @@ public:
|
||||
void loadFile(QString fileName);
|
||||
|
||||
protected:
|
||||
PDFJam pdfJam;
|
||||
|
||||
private:
|
||||
QVBoxLayout* outerLayout;
|
||||
|
Loading…
Reference in New Issue
Block a user