Rework/expose format conversions

This commit is contained in:
Anton Thomasson 2020-06-04 21:31:46 +02:00
parent 28bb06d18e
commit 09fbb96a7e
12 changed files with 410 additions and 105 deletions

View file

@ -4,6 +4,9 @@ import "../pages/utils.js" as Utils
Setting {
property var choices
property string mime_type
property var limited_choices: Utils.limitChoices(name, choices, mime_type)
ValueButton {
enabled: valid
@ -17,12 +20,12 @@ Setting {
id: menu
enabled: valid
Repeater {
model: choices
model: limited_choices
MenuItem {
text: Utils.ippName(name, choices[index])
text: Utils.ippName(name, limited_choices[index])
onClicked:
{
choice = choices[index];
choice = limited_choices[index];
}
}
}

View file

@ -1,5 +1,6 @@
import QtQuick 2.0
import Sailfish.Silica 1.0
import seaprint.mimer 1.0
import "utils.js" as Utils
import Nemo.Configuration 1.0
@ -39,14 +40,15 @@ Page {
ListModel {
id:mod
ListElement {name: "sides"; prettyName: qsTr("Sides"); tag: 0x23}
ListElement {name: "media"; prettyName: qsTr("Print media"); tag: 0x44}
ListElement {name: "copies"; prettyName: qsTr("Copies"); tag: 0x21}
// ListElement {name: "page-ranges"; prettyName: qsTr("Page range"); tag: 0x33}
ListElement {name: "print-color-mode"; prettyName: qsTr("Color mode"); tag: 0x23}
// ListElement {name: "orientation-requested"; prettyName: qsTr("Orientation"); tag: 0x23}
ListElement {name: "print-quality"; prettyName: qsTr("Quality"); tag: 0x23}
ListElement {name: "printer-resolution"; prettyName: qsTr("Resolution"); tag: 0x32}
ListElement {name: "sides"; prettyName: qsTr("Sides"); tag: 0x23}
ListElement {name: "media"; prettyName: qsTr("Print media"); tag: 0x44}
ListElement {name: "copies"; prettyName: qsTr("Copies"); tag: 0x21}
// ListElement {name: "page-ranges"; prettyName: qsTr("Page range"); tag: 0x33}
ListElement {name: "print-color-mode"; prettyName: qsTr("Color mode"); tag: 0x23}
// ListElement {name: "orientation-requested"; prettyName: qsTr("Orientation"); tag: 0x23}
ListElement {name: "print-quality"; prettyName: qsTr("Quality"); tag: 0x23}
ListElement {name: "printer-resolution"; prettyName: qsTr("Resolution"); tag: 0x32}
ListElement {name: "document-format"; prettyName: qsTr("Transfer format"); tag: 0x49}
}
SilicaListView {
@ -100,13 +102,15 @@ Page {
case 0x32:
case 0x23:
case 0x44:
case 0x49:
loader.setSource("../components/ChoiceSetting.qml",
{name: name,
prettyName: prettyName,
tag: tag,
valid: printer.attrs.hasOwnProperty(name+"-supported"),
choices: printer.attrs[name+"-supported"].value,
default_choice: printer.attrs[name+"-default"].value
default_choice: printer.attrs[name+"-default"].value,
mime_type: Mimer.get_type(selectedFile)
})
break
}

View file

@ -114,6 +114,27 @@ function ippName(name, value)
{
return value;
}
case "document-format":
switch(value) {
case "application/octet-stream":
return qsTr("Auto-sense");
case "application/pdf":
return qsTr("PDF");
case "application/postscript":
return qsTr("Postscript");
case "image/pwg-raster":
return qsTr("PWG-raster");
case "image/urf":
return qsTr("URF-raster");
case "image/png":
return qsTr("PNG");
case "image/jpeg":
return qsTr("JPEG");
case "image/gif":
return qsTr("GIF");
default:
return value;
}
}
return value;
}
@ -123,6 +144,38 @@ function endsWith(ending, string)
return string.lastIndexOf(ending) == (string.length - ending.length);
}
function canConvertPdfTo(type)
{
var targets = ["application/octet-stream", "application/pdf", "image/pwg-raster", "image/urf"];
return has(targets, type)
}
function canConvertImageTo(type)
{
var targets = ["application/octet-stream", "image/jpeg", "image/png", "image/pwg-raster", "image/urf", "image/gif"];
return has(targets, type)
}
function limitChoices(name, choices, mimeType)
{
switch(name) {
case "document-format":
if(mimeType == "application/pdf")
{
return choices.filter(canConvertPdfTo)
}
else if(mimeType == "image/jpeg" || mimeType == "image/png")
{
return choices.filter(canConvertImageTo);
}
else
{
return ["application/octet-stream"];
}
default:
return choices;
}
}
var media =
{"asme_f_28x40in": "28 x 40″",

View file

@ -32,9 +32,25 @@ void ppm2PwgEnv(QStringList& env, bool urf, quint32 Quality, QString PaperSize,
}
void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
bool urf, quint32 Colors, quint32 Quality, QString PaperSize,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble)
{
bool urf = false;
if(targetFormat == "image/urf")
{
urf = true;
}
else if(targetFormat == "image/pwg-raster")
{
//ok
}
else
{
emit failed(tr("Unsupported target format"));
return;
}
if(urf && (HwResX != HwResY))
{ // URF only supports symmetric resolutions
qDebug() << "Unsupported URF resolution" << PaperSize;
@ -141,9 +157,31 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
}
void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
bool urf, quint32 Colors, quint32 Quality, QString PaperSize,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY)
{
bool urf = false;
QString imageFormat = "";
QStringList supportedImageFormats = {"image/jpeg", "image/png", "image/gif"};
if(targetFormat == "image/urf")
{
urf = true;
}
else if(targetFormat == "image/pwg-raster")
{
//ok
}
else if(supportedImageFormats.contains(targetFormat))
{
imageFormat = targetFormat.split("/")[1];
}
else
{
emit failed(tr("Unsupported target format"));
return;
}
if(urf && (HwResX != HwResY))
{ // URF only supports symmetric resolutions
qDebug() << "Unsupported URF resolution" << PaperSize;
@ -184,44 +222,61 @@ void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTem
painter.drawImage(0, (outImage.height()-inImage.height())/2, inImage);
painter.end();
QTemporaryFile tmpImage;
tmpImage.open();
qDebug() << "Raw image: " << tmpImage.fileName();
outImage.save(tmpImage.fileName(), Colors == 1 ? "pgm" : "ppm");
tmpImage.close();
if(imageFormat != "")
{ // We are converting to a supported image format
QTemporaryFile tmpImage;
tmpImage.open();
qDebug() << "Raw image: " << tmpImage.fileName();
QProcess* ppm2pwg = new QProcess(this);
// Yo dawg, I heard you like programs...
ppm2pwg->setProgram("harbour-seaprint");
ppm2pwg->setArguments({"ppm2pwg"});
QStringList env;
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, false, false);
qDebug() << "ppm2pwg env is " << env;
ppm2pwg->setEnvironment(env);
ppm2pwg->setStandardInputFile(tmpImage.fileName());
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
qDebug() << "All connected";
ppm2pwg->start();
qDebug() << "Starting";
if(!ppm2pwg->waitForStarted())
{
qDebug() << "ppm2pwg died";
tempfile->deleteLater();
emit failed(tr("Conversion error"));
return;
outImage.save(tmpImage.fileName(), imageFormat.toStdString().c_str());
QFile tempfileAsFile(tempfile->fileName());
tempfileAsFile.open(QIODevice::Append);
tempfileAsFile.write(tmpImage.readAll());
tempfileAsFile.close();
tmpImage.close();
}
qDebug() << "All started";
else
{ // We are converting to a raster format
QTemporaryFile tmpImage;
tmpImage.open();
qDebug() << "Raw image: " << tmpImage.fileName();
ppm2pwg->waitForFinished();
outImage.save(tmpImage.fileName(), Colors == 1 ? "pgm" : "ppm");
tmpImage.close();
qDebug() << "Finished";
QProcess* ppm2pwg = new QProcess(this);
// Yo dawg, I heard you like programs...
ppm2pwg->setProgram("harbour-seaprint");
ppm2pwg->setArguments({"ppm2pwg"});
QStringList env;
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, false, false);
qDebug() << "ppm2pwg env is " << env;
ppm2pwg->setEnvironment(env);
ppm2pwg->setStandardInputFile(tmpImage.fileName());
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
qDebug() << "All connected";
ppm2pwg->start();
qDebug() << "Starting";
if(!ppm2pwg->waitForStarted())
{
qDebug() << "ppm2pwg died";
tempfile->deleteLater();
emit failed(tr("Conversion error"));
return;
}
qDebug() << "All started";
ppm2pwg->waitForFinished();
qDebug() << "Finished";
}
emit done(request, tempfile);
qDebug() << "posted";

View file

@ -9,11 +9,11 @@ class ConvertWorker : public QObject
public slots:
void convertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
bool urf, quint32 Colors, quint32 Quality, QString PaperSize,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble);
void convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
bool urf, quint32 Colors, quint32 Quality, QString PaperSize,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY);
signals:

View file

@ -250,8 +250,9 @@ bool IppPrinter::hasPrinterDeviceIdCmd(QString cmd)
return false;
}
void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){
// TODO: make alwaysConvert force ratser format
void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert)
{
qDebug() << "printing" << filename << attrs;
_progress = "";
@ -270,15 +271,20 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){
QJsonObject o = opAttrs();
o.insert("job-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", fileinfo.fileName()}});
QJsonArray jobCreationAttributes = _attrs["job-creation-attributes-supported"].toObject()["value"].toArray();
// Only include if printer supports it
// if (filename.endsWith("pdf"))
// {
// attrs.insert("document-format", QJsonObject {{"tag", 73}, {"value", "application/pdf"}});
// }
// else if (filename.endsWith("jpg")) {
// attrs.insert("document-format", QJsonObject {{"tag", 73}, {"value", "image/jpeg"}});
// }
QString documentFormat = getAttrOrDefault(attrs, "document-format").toString();
if(documentFormat == "")
{
emit convertFailed(tr("Unknown document format"));
return;
}
if(!jobCreationAttributes.contains("document-format"))
{ // Only include if printer supports it
attrs.remove("document-format");
}
qDebug() << "Printing job" << o << attrs;
IppMsg job = IppMsg(o, attrs);
@ -294,16 +300,6 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){
Mimer* mimer = Mimer::instance();
QString mimeType = mimer->get_type(filename);
ConvertFrom from = NotConvertable;
ConvertTarget target = NoConvert;
if(mimeType == "application/pdf")
{
from = Pdf;
}
else if (mimeType.contains("image")) {
from = Image;
}
QJsonArray supportedMimeTypes = _attrs["document-format-supported"].toObject()["value"].toArray();
@ -313,23 +309,11 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){
qDebug() << "supportsPdf" << supportsPdf;
if(alwaysConvert || from == Image || (from == Pdf && !supportsPdf))
{
if(supportedMimeTypes.contains("image/pwg-raster"))
{
target = PwgConvert;
}
else if (supportedMimeTypes.contains("image/urf"))
{
target = UrfConvert;
}
}
QJsonValue PrinterResolutionRef = getAttrOrDefault(attrs, "printer-resolution");
quint32 HwResX = PrinterResolutionRef.toObject()["x"].toInt();
quint32 HwResY = PrinterResolutionRef.toObject()["y"].toInt();
if(target == UrfConvert)
if(documentFormat == "image/urf")
{ // Ensure symmetric resolution for URF
if(HwResX < HwResY)
{
@ -358,7 +342,7 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){
return;
}
if(target != NoConvert)
if(documentFormat != mimeType)
{
file.close();
@ -370,7 +354,7 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){
setBusyMessage("Converting");
if(from == Pdf )
if(mimeType == "application/pdf")
{
QString Sides = getAttrOrDefault(attrs, "sides").toString();
@ -386,12 +370,12 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert){
Tumble = true;
}
emit doConvertPdf(request, filename, tempfile, target==UrfConvert, Colors, Quality,
emit doConvertPdf(request, filename, tempfile, documentFormat, Colors, Quality,
PaperSize, HwResX, HwResY, TwoSided, Tumble);
}
else if (from == Image)
else if (mimeType.contains("image"))
{
emit doConvertImage(request, filename, tempfile, target==UrfConvert, Colors, Quality,
emit doConvertImage(request, filename, tempfile, documentFormat, Colors, Quality,
PaperSize, HwResX, HwResY);
}
else

View file

@ -32,20 +32,6 @@ public:
bool hasPrinterDeviceIdCmd(QString cmd);
enum ConvertTarget
{
NoConvert,
PwgConvert,
UrfConvert
};
enum ConvertFrom
{
NotConvertable,
Pdf,
Image
};
signals:
void urlChanged();
void attrsChanged();
@ -56,11 +42,11 @@ signals:
void cancelStatus(bool status);
void doConvertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
bool urf, quint32 Colors, quint32 Quality, QString PaperSize,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble);
void doConvertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, bool urf,
quint32 Colors, quint32 Quality, QString PaperSize,
void doConvertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY);
void busyMessageChanged();

View file

@ -140,6 +140,10 @@
<source>Unsupported resolution (dpi)</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unsupported target format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>
@ -209,6 +213,10 @@
<source>Cannot convert this file format</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unknown document format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>JobsPage</name>
@ -271,6 +279,10 @@
<source>Print media</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Transfer format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>utils</name>
@ -350,5 +362,37 @@
<source>dots/cm</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Auto-sense</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PDF</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Postscript</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PWG-raster</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>URF-raster</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PNG</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>JPEG</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>GIF</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>

View file

@ -140,6 +140,10 @@
<source>Unsupported resolution (dpi)</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unsupported target format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>
@ -209,6 +213,10 @@
<source>Cannot convert this file format</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unknown document format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>JobsPage</name>
@ -271,6 +279,10 @@
<source>Print media</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Transfer format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>utils</name>
@ -350,5 +362,37 @@
<source>dots/cm</source>
<translation>ppp</translation>
</message>
<message>
<source>Auto-sense</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PDF</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Postscript</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PWG-raster</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>URF-raster</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PNG</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>JPEG</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>GIF</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>

View file

@ -140,6 +140,10 @@
<source>Unsupported resolution (dpi)</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unsupported target format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>
@ -209,6 +213,10 @@
<source>Cannot convert this file format</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unknown document format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>JobsPage</name>
@ -271,6 +279,10 @@
<source>Print media</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Transfer format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>utils</name>
@ -350,5 +362,37 @@
<source>dots/cm</source>
<translation>pts/cm</translation>
</message>
<message>
<source>Auto-sense</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PDF</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Postscript</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PWG-raster</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>URF-raster</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PNG</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>JPEG</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>GIF</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>

View file

@ -140,6 +140,10 @@
<source>Unsupported resolution (dpi)</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unsupported target format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>
@ -209,6 +213,10 @@
<source>Cannot convert this file format</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unknown document format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>JobsPage</name>
@ -271,6 +279,10 @@
<source>Print media</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Transfer format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>utils</name>
@ -350,5 +362,37 @@
<source>dots/cm</source>
<translation>/</translation>
</message>
<message>
<source>Auto-sense</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PDF</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Postscript</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PWG-raster</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>URF-raster</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PNG</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>JPEG</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>GIF</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>

View file

@ -140,6 +140,10 @@
<source>Unsupported resolution (dpi)</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unsupported target format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>
@ -209,6 +213,10 @@
<source>Cannot convert this file format</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unknown document format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>JobsPage</name>
@ -271,6 +279,10 @@
<source>Print media</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Transfer format</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>utils</name>
@ -350,5 +362,37 @@
<source>dots/cm</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Auto-sense</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PDF</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Postscript</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PWG-raster</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>URF-raster</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>PNG</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>JPEG</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>GIF</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>