From e1d34a4d1d78bfaa4d2637c8709da17ddb936b93 Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Sat, 1 Aug 2020 20:18:47 +0200 Subject: [PATCH] Add page range support --- qml/components/RangeSetting.qml | 90 ++++++++++++++++++++++++-- qml/pages/PrinterPage.qml | 8 ++- src/convertchecker.cpp | 34 ++++++++++ src/convertchecker.h | 2 + src/convertworker.cpp | 49 +++++++------- src/convertworker.h | 3 +- src/ippprinter.cpp | 13 +++- src/ippprinter.h | 3 +- translations/harbour-seaprint-de.ts | 11 ++++ translations/harbour-seaprint-es.ts | 11 ++++ translations/harbour-seaprint-fr.ts | 11 ++++ translations/harbour-seaprint-zh_CN.ts | 11 ++++ translations/harbour-seaprint.ts | 11 ++++ 13 files changed, 221 insertions(+), 36 deletions(-) diff --git a/qml/components/RangeSetting.qml b/qml/components/RangeSetting.qml index f3ee289..0c80a4d 100644 --- a/qml/components/RangeSetting.qml +++ b/qml/components/RangeSetting.qml @@ -3,25 +3,103 @@ import Sailfish.Silica 1.0 Setting { - //TODO - - property int low property int high - property int choice_low - property int choice_high + property int choice_low: 1 + property int choice_high: 0 + + + function update_choice() { + choice = new Object({low: choice_low, high: choice_high}); + } + + onChoice_highChanged: { + if(choice_high < choice_low) + { + low_slider.value = choice_high > 0 ? choice_high : 1; + } + else + { + update_choice() + } + } + onChoice_lowChanged: { + if(choice_low > choice_high) + { + high_slider.value = choice_low + } + else + { + update_choice() + } + } ValueButton { enabled: valid anchors.verticalCenter: parent.verticalCenter label: prettyName - value: choice ? choice : default_choice + value: choice_high==0 ? qsTr("all") : ""+choice_low+" - "+choice_high onClicked: parent.clicked() } property var menu: ContextMenu { id: menu enabled: valid + MenuItem { + Slider + { + id: low_slider + minimumValue: 1 + maximumValue: high + width: parent.width + stepSize: 1 + value: choice_low + onValueChanged: + { + choice_low = value; + } + } + IconButton + { + anchors.right: parent.right + icon.source: "image://theme/icon-s-edit" + onClicked: {var dialog = pageStack.push(Qt.resolvedUrl("IntegerInputDialog.qml"), + {value: choice, title: prettyName, + min: 1, max: high}); + dialog.accepted.connect(function() { + choice_low = dialog.value; + }) + } + } + + } + + MenuItem { + Slider + { + id: high_slider + minimumValue: 0 + maximumValue: high + width: parent.width + stepSize: 1 + value: choice_high + onValueChanged: + { + choice_high = value; + } + } + IconButton + { + anchors.right: parent.right + icon.source: "image://theme/icon-s-edit" + onClicked: {var dialog = pageStack.push(Qt.resolvedUrl("IntegerInputDialog.qml"), + {value: choice, title: prettyName, + min: 1, max: high}); + dialog.accepted.connect(function() { + choice_high = dialog.value; + }) + } + } } diff --git a/qml/pages/PrinterPage.qml b/qml/pages/PrinterPage.qml index 32b1eb7..6701f15 100644 --- a/qml/pages/PrinterPage.qml +++ b/qml/pages/PrinterPage.qml @@ -2,6 +2,7 @@ import QtQuick 2.0 import Sailfish.Silica 1.0 import seaprint.mimer 1.0 import seaprint.ippmsg 1.0 +import seaprint.convertchecker 1.0 import "utils.js" as Utils Page { @@ -39,7 +40,7 @@ Page { ListElement {name: "sides"; prettyName: qsTr("Sides"); tag: IppMsg.Enum} ListElement {name: "media"; prettyName: qsTr("Print media"); tag: IppMsg.Keyword} ListElement {name: "copies"; prettyName: qsTr("Copies"); tag: IppMsg.Integer} -// ListElement {name: "page-ranges"; prettyName: qsTr("Page range"); tag: IppMsg.IntegerRange} + ListElement {name: "page-ranges"; prettyName: qsTr("Page range"); tag: IppMsg.IntegerRange} ListElement {name: "print-color-mode"; prettyName: qsTr("Color mode"); tag: IppMsg.Enum} // ListElement {name: "orientation-requested"; prettyName: qsTr("Orientation"); tag: IppMsg.Enum} ListElement {name: "print-quality"; prettyName: qsTr("Quality"); tag: IppMsg.Enum} @@ -111,11 +112,14 @@ Page { }) break case IppMsg.IntegerRange: + var valid = printer.attrs.hasOwnProperty(name+"-supported") && + name=="page-ranges" && Mimer.get_type(selectedFile) == "application/pdf"; loader.setSource("../components/RangeSetting.qml", {name: name, prettyName: prettyName, tag: tag, - valid: false //TODO printer.attrs.hasOwnProperty(name+"-supported"), + valid: valid, + high: name=="page-ranges" ? ConvertChecker.pdfPages(selectedFile) : 0 }) break case IppMsg.Resolution: diff --git a/src/convertchecker.cpp b/src/convertchecker.cpp index 7a8de48..a68d4cb 100644 --- a/src/convertchecker.cpp +++ b/src/convertchecker.cpp @@ -1,5 +1,6 @@ #include "convertchecker.h" #include +#include ConvertChecker::ConvertChecker() { @@ -38,3 +39,36 @@ ConvertChecker* ConvertChecker::instance() return m_Instance; } + +quint32 ConvertChecker::pdfPages(QString filename) +{ + quint32 pages = 0; + if(!_pdf) + { + return pages; + } + + QProcess* pdfinfo = new QProcess(this); + pdfinfo->setProgram("pdfinfo"); + pdfinfo->setArguments({filename}); + pdfinfo->start(); + + if(!pdfinfo->waitForStarted(1000) || !pdfinfo->waitForFinished(1000)) + { + pdfinfo->deleteLater(); + return pages; + } + QByteArray pdfInfoOutput = pdfinfo->readAllStandardOutput(); + pdfinfo->deleteLater(); + qDebug() << pdfInfoOutput; + QList pdfInfoOutputLines = pdfInfoOutput.split('\n'); + for(QList::iterator it = pdfInfoOutputLines.begin(); it != pdfInfoOutputLines.end(); it++) + { + if(it->startsWith("Pages")) + { + QList pagesTokens = it->split(' '); + pages = pagesTokens.last().toInt(); + } + } + return pages; +} diff --git a/src/convertchecker.h b/src/convertchecker.h index 986aebb..1be0535 100644 --- a/src/convertchecker.h +++ b/src/convertchecker.h @@ -10,6 +10,8 @@ public: static ConvertChecker* instance(); Q_PROPERTY(bool pdf MEMBER _pdf) + Q_INVOKABLE quint32 pdfPages(QString pdf); + signals: protected: private: diff --git a/src/convertworker.cpp b/src/convertworker.cpp index 69d8e5e..59778d0 100644 --- a/src/convertworker.cpp +++ b/src/convertworker.cpp @@ -1,6 +1,7 @@ #include "convertworker.h" #include #include "papersizes.h" +#include "convertchecker.h" #include #include #include @@ -44,34 +45,20 @@ void ppm2PwgEnv(QStringList& env, bool urf, quint32 Quality, QString PaperSize, void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, - quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble) + quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, + quint32 PageRangeLow, quint32 PageRangeHigh) { - - QProcess* pdfinfo = new QProcess(this); - pdfinfo->setProgram("pdfinfo"); - pdfinfo->setArguments({filename}); - pdfinfo->start(); - - if(!pdfinfo->waitForStarted() || !pdfinfo->waitForFinished()) + quint32 pages = ConvertChecker::instance()->pdfPages(filename); + if (!pages) { - qDebug() << "pdfinfo died"; - pdfinfo->deleteLater(); + qDebug() << "pdfinfo returned 0 pages"; tempfile->deleteLater(); emit failed(tr("Failed to get info about PDF file")); - return; } - QByteArray pdfInfoOutput = pdfinfo->readAllStandardOutput(); - pdfinfo->deleteLater(); - qDebug() << pdfInfoOutput; - QList pdfInfoOutputLines = pdfInfoOutput.split('\n'); - quint32 pages = 0; - for(QList::iterator it = pdfInfoOutputLines.begin(); it != pdfInfoOutputLines.end(); it++) + + if(PageRangeHigh==0) { - if(it->startsWith("Pages")) - { - QList pagesTokens = it->split(' '); - pages = pagesTokens.last().toInt(); - } + PageRangeHigh=pages; } bool urf = false; @@ -132,11 +119,17 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo { QProcess* pdftops = new QProcess(this); pdftops->setProgram("pdftops"); - QStringList PdfToPsArgs = {"-paper", ShortPaperSize, filename, "-"}; + QStringList PdfToPsArgs; if(TwoSided) { - PdfToPsArgs.prepend("-duplex"); + PdfToPsArgs.append("-duplex"); } + if(PageRangeLow != 0) + { + PdfToPsArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)}; + } + PdfToPsArgs << QStringList {"-paper", ShortPaperSize, filename, "-"}; + pdftops->setArguments(PdfToPsArgs); pdftops->setStandardOutputFile(tempfile->fileName(), QIODevice::Append); @@ -169,7 +162,13 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo QProcess* pdftocairo = new QProcess(this); pdftocairo->setProgram("pdftocairo"); - pdftocairo->setArguments({"-pdf", "-paper", ShortPaperSize, filename, "-"}); + QStringList PdfToCairoArgs; + if(PageRangeLow != 0) + { + PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)}; + } + PdfToCairoArgs << QStringList {"-pdf", "-paper", ShortPaperSize, filename, "-"}; + pdftocairo->setArguments(PdfToCairoArgs); QProcess* pdftoppm = new QProcess(this); pdftoppm->setProgram("pdftoppm"); diff --git a/src/convertworker.h b/src/convertworker.h index c86a2a7..929d35f 100644 --- a/src/convertworker.h +++ b/src/convertworker.h @@ -10,7 +10,8 @@ class ConvertWorker : public QObject public slots: void convertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, - quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble); + quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, + quint32 PageRangeLow, quint32 PageRangeHigh); void convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, diff --git a/src/ippprinter.cpp b/src/ippprinter.cpp index 24d3501..6db51df 100644 --- a/src/ippprinter.cpp +++ b/src/ippprinter.cpp @@ -432,6 +432,17 @@ void IppPrinter::print(QJsonObject attrs, QString filename, { attrs.remove("sides"); } + quint32 PageRangeLow = 0; + quint32 PageRangeHigh = 0; + if(mimeType == "application/pdf" && documentFormat != "application/pdf") + { + if(attrs.contains("page-ranges")) + { + QJsonObject PageRanges = getAttrOrDefault(attrs, "page-ranges").toObject(); + PageRangeLow = PageRanges["low"].toInt(); + PageRangeHigh = PageRanges["high"].toInt(); + } + } qDebug() << "Final op attributes:" << o; qDebug() << "Final job attributes:" << attrs; @@ -476,7 +487,7 @@ void IppPrinter::print(QJsonObject attrs, QString filename, } emit doConvertPdf(request, filename, tempfile, documentFormat, Colors, Quality, - PaperSize, HwResX, HwResY, TwoSided, Tumble); + PaperSize, HwResX, HwResY, TwoSided, Tumble, PageRangeLow, PageRangeHigh); } else if (mimeType.contains("image")) { diff --git a/src/ippprinter.h b/src/ippprinter.h index 9d12cf0..a6d6794 100644 --- a/src/ippprinter.h +++ b/src/ippprinter.h @@ -42,7 +42,8 @@ signals: void doConvertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, - quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble); + quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, + quint32 PageRangeLow, quint32 PageRangeHigh); void doConvertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, diff --git a/translations/harbour-seaprint-de.ts b/translations/harbour-seaprint-de.ts index b9df653..e318917 100644 --- a/translations/harbour-seaprint-de.ts +++ b/translations/harbour-seaprint-de.ts @@ -349,6 +349,17 @@ Zero margins + + Page range + + + + + RangeSetting + + all + + SettingsPage diff --git a/translations/harbour-seaprint-es.ts b/translations/harbour-seaprint-es.ts index 048cf6d..ca328ca 100644 --- a/translations/harbour-seaprint-es.ts +++ b/translations/harbour-seaprint-es.ts @@ -349,6 +349,17 @@ Zero margins + + Page range + + + + + RangeSetting + + all + + SettingsPage diff --git a/translations/harbour-seaprint-fr.ts b/translations/harbour-seaprint-fr.ts index ef11d40..c5fff54 100644 --- a/translations/harbour-seaprint-fr.ts +++ b/translations/harbour-seaprint-fr.ts @@ -349,6 +349,17 @@ Zero margins + + Page range + + + + + RangeSetting + + all + + SettingsPage diff --git a/translations/harbour-seaprint-zh_CN.ts b/translations/harbour-seaprint-zh_CN.ts index 7318045..4c13835 100644 --- a/translations/harbour-seaprint-zh_CN.ts +++ b/translations/harbour-seaprint-zh_CN.ts @@ -349,6 +349,17 @@ Zero margins + + Page range + + + + + RangeSetting + + all + + SettingsPage diff --git a/translations/harbour-seaprint.ts b/translations/harbour-seaprint.ts index 2d3d2e6..fe2be60 100644 --- a/translations/harbour-seaprint.ts +++ b/translations/harbour-seaprint.ts @@ -349,6 +349,17 @@ Zero margins + + Page range + + + + + RangeSetting + + all + + SettingsPage