diff --git a/src/pdf_jam.cpp b/src/pdf_jam.cpp index 236a734..44a62bd 100644 --- a/src/pdf_jam.cpp +++ b/src/pdf_jam.cpp @@ -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");*/ } diff --git a/src/pdf_jam.h b/src/pdf_jam.h index 548f9fd..14b8252 100644 --- a/src/pdf_jam.h +++ b/src/pdf_jam.h @@ -1,14 +1,28 @@ #pragma once +#include +#include +#include +#include -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 cmdQueue; + QString nextCommand(); + bool isQueueEmpty(); + QMutex mutex; }; diff --git a/src/pdf_preview_widget.cpp b/src/pdf_preview_widget.cpp index 5d8b1d7..b75fd32 100644 --- a/src/pdf_preview_widget.cpp +++ b/src/pdf_preview_widget.cpp @@ -12,21 +12,20 @@ void PDFPreviewWidget::setImage(QImage image) { } void PDFPreviewWidget::regenImage(void) { - if(pPage != NULL) { - double dpi; - double dpi2; - QSize targetSize = this->size(); - QSizeF oriSize = pPage->pageSizeF(); - double oriDpi = 72; - dpi = targetSize.height() / (float)oriSize.height()*72; - dpi2 = targetSize.width() / (float)oriSize.width()*72; - dpi = dpirenderToImage(dpi, dpi); - } + double dpi; + double dpi2; + QSize targetSize = this->size(); + QSizeF oriSize = pPage->pageSizeF(); + double oriDpi = 72; + dpi = targetSize.height() / (float)oriSize.height()*72; + dpi2 = targetSize.width() / (float)oriSize.width()*72; + dpi = dpirenderToImage(dpi, dpi); } void PDFPreviewWidget::previewUpdate(Poppler::Page* pp) { pPage = pp; + qDebug() << "Set new popler page" << pp; regenImage(); update(); } @@ -36,7 +35,8 @@ void PDFPreviewWidget::pageClicked(QMouseEvent* mouseEvent, QImage image) { } void PDFPreviewWidget::resizeEvent(QResizeEvent* event) { - regenImage(); + if(pPage != NULL) + regenImage(); } void PDFPreviewWidget::paintEvent(QPaintEvent* event) { diff --git a/src/pdf_table_widget.cpp b/src/pdf_table_widget.cpp index b8d349b..e3e48ed 100644 --- a/src/pdf_table_widget.cpp +++ b/src/pdf_table_widget.cpp @@ -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, diff --git a/src/pdf_table_widget.h b/src/pdf_table_widget.h index fd254ea..185e743 100644 --- a/src/pdf_table_widget.h +++ b/src/pdf_table_widget.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include "pdf_jam.h" class QImage; class QVBoxLayout; @@ -16,6 +17,7 @@ public: void loadFile(QString fileName); protected: + PDFJam pdfJam; private: QVBoxLayout* outerLayout;