Skip to content

Commit

Permalink
Rework/expose format conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Jun 4, 2020
1 parent 28bb06d commit 09fbb96
Show file tree
Hide file tree
Showing 12 changed files with 405 additions and 100 deletions.
9 changes: 6 additions & 3 deletions qml/components/ChoiceSetting.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
}
}
}
Expand Down
22 changes: 13 additions & 9 deletions qml/pages/PrinterPage.qml
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
53 changes: 53 additions & 0 deletions qml/pages/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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″",
Expand Down
117 changes: 86 additions & 31 deletions src/convertworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();

outImage.save(tmpImage.fileName(), imageFormat.toStdString().c_str());
QFile tempfileAsFile(tempfile->fileName());
tempfileAsFile.open(QIODevice::Append);
tempfileAsFile.write(tmpImage.readAll());
tempfileAsFile.close();
tmpImage.close();
}
else
{ // We are converting to a raster format
QTemporaryFile tmpImage;
tmpImage.open();
qDebug() << "Raw image: " << tmpImage.fileName();

outImage.save(tmpImage.fileName(), Colors == 1 ? "pgm" : "ppm");
tmpImage.close();

QProcess* ppm2pwg = new QProcess(this);
// Yo dawg, I heard you like programs...
ppm2pwg->setProgram("harbour-seaprint");
ppm2pwg->setArguments({"ppm2pwg"});
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;
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);
ppm2pwg->setEnvironment(env);
ppm2pwg->setStandardInputFile(tmpImage.fileName());
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);

connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));

qDebug() << "All connected";
ppm2pwg->start();
qDebug() << "All connected";
ppm2pwg->start();

qDebug() << "Starting";
qDebug() << "Starting";

if(!ppm2pwg->waitForStarted())
{
qDebug() << "ppm2pwg died";
tempfile->deleteLater();
emit failed(tr("Conversion error"));
return;
}
qDebug() << "All started";
if(!ppm2pwg->waitForStarted())
{
qDebug() << "ppm2pwg died";
tempfile->deleteLater();
emit failed(tr("Conversion error"));
return;
}
qDebug() << "All started";

ppm2pwg->waitForFinished();
ppm2pwg->waitForFinished();

qDebug() << "Finished";
qDebug() << "Finished";
}

emit done(request, tempfile);
qDebug() << "posted";
Expand Down
4 changes: 2 additions & 2 deletions src/convertworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 09fbb96

Please sign in to comment.