Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Michael Winkelmann
QmlSlidePrinter
Commits
33b57f62
Commit
33b57f62
authored
Nov 02, 2018
by
Michael Winkelmann
Browse files
Code clean up
parent
e3ae4729
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
main.cpp
View file @
33b57f62
#include <QDesktopServices>
#include <QDir>
#include <QGuiApplication>
#include <QQuickView>
#include <QQmlEngine>
#include <QQmlContext>
#include <QDir>
#include <QQmlEngine>
#include <QQuickItem>
#include <QQuickView>
#include <QCommandLineParser>
#include "qmlprinter.h"
int
main
(
int
ac
,
char
*
av
[])
{
QGuiApplication
app
(
ac
,
av
);
int
main
(
int
ac
,
char
*
av
[])
{
QGuiApplication
app
(
ac
,
av
);
QCommandLineParser
parser
;
parser
.
setApplicationDescription
(
"QML PDF generator"
);
parser
.
addHelpOption
();
parser
.
addVersionOption
();
QCommandLineParser
parser
;
parser
.
setApplicationDescription
(
"QML PDF generator"
);
parser
.
addHelpOption
();
parser
.
addVersionOption
();
parser
.
addOptions
({
{
{
"input"
,
"i"
},
"Input QML file"
,
"input"
},
{
{
"output"
,
"o"
},
"Output PDF file"
,
"output"
,
"output.pdf"
}
});
parser
.
addOptions
(
{{{
"input"
,
"i"
},
"Input QML file"
,
"input"
},
{{
"output"
,
"o"
},
"Output PDF file"
,
"output"
,
"output.pdf"
}});
QCommandLineOption
showOption
({
"show"
,
"s"
},
"Show PDF file"
,
"show"
);
parser
.
addOption
(
showOption
);
QCommandLineOption
showOption
({
"show"
,
"s"
},
"Show PDF file"
,
"show"
);
parser
.
addOption
(
showOption
);
QCommandLineOption
showPresentation
({
"presentation"
,
"p"
},
"Show Presentation"
,
"presentation"
);
parser
.
addOption
(
showPresentation
);
QCommandLineOption
showPresentation
({
"presentation"
,
"p"
},
"Show Presentation"
,
"presentation"
);
parser
.
addOption
(
showPresentation
);
parser
.
process
(
app
);
parser
.
process
(
app
);
auto
*
view
=
new
QQuickView
;
auto
*
view
=
new
QQuickView
;
auto
inputQml
=
parser
.
value
(
"input"
);
auto
outputPdf
=
parser
.
value
(
"output"
);
auto
showPdf
=
parser
.
isSet
(
showOption
);
auto
showPres
=
parser
.
isSet
(
showPresentation
);
auto
inputQml
=
parser
.
value
(
"input"
);
auto
outputPdf
=
parser
.
value
(
"output"
);
auto
showPdf
=
parser
.
isSet
(
showOption
);
auto
showPres
=
parser
.
isSet
(
showPresentation
);
QDir
outputPdfDir
;
QDir
outputPdfDir
;
if
(
QDir
::
isRelativePath
(
outputPdf
))
{
outputPdfDir
=
QDir
(
qApp
->
applicationDirPath
()
+
QDir
::
separator
()
+
outputPdf
);
}
else
{
outputPdfDir
=
QDir
(
outputPdf
);
}
if
(
QDir
::
isRelativePath
(
outputPdf
))
{
outputPdfDir
=
QDir
(
qApp
->
applicationDirPath
()
+
QDir
::
separator
()
+
outputPdf
);
}
else
{
outputPdfDir
=
QDir
(
outputPdf
);
}
view
->
setSource
(
QUrl
::
fromLocalFile
(
inputQml
));
view
->
setSource
(
QUrl
::
fromLocalFile
(
inputQml
));
QmlPrinter
printer
;
printer
.
printPdf
(
outputPdfDir
.
absolutePath
(),
view
->
rootObject
());
QmlPrinter
printer
;
printer
.
printPdf
(
outputPdfDir
.
absolutePath
(),
view
->
rootObject
());
qInfo
()
<<
app
.
tr
(
"Writing PDF to %1"
).
arg
(
outputPdfDir
.
absolutePath
());
qInfo
()
<<
app
.
tr
(
"Writing PDF to %1"
).
arg
(
outputPdfDir
.
absolutePath
());
if
(
showPres
)
{
view
->
rootObject
()
->
setProperty
(
"currentSlide"
,
0
);
view
->
show
();
}
if
(
showPres
)
{
view
->
rootObject
()
->
setProperty
(
"currentSlide"
,
0
);
view
->
show
();
}
if
(
showPdf
)
QDesktopServices
::
openUrl
(
QUrl
::
fromLocalFile
(
outputPdfDir
.
absolutePath
()));
if
(
showPdf
)
QDesktopServices
::
openUrl
(
QUrl
::
fromLocalFile
(
outputPdfDir
.
absolutePath
()));
return
showPres
?
app
.
exec
()
:
EXIT_SUCCESS
;
return
showPres
?
app
.
exec
()
:
EXIT_SUCCESS
;
}
qmlprinter.cpp
View file @
33b57f62
This diff is collapsed.
Click to expand it.
qmlprinter.h
View file @
33b57f62
...
...
@@ -9,44 +9,39 @@
#define QMLPRINTER_H
#include <QObject>
#include <QQuickItem>
#include <QQuickWindow>
#include <QPainter>
#include <QPrinter>
#include <QPrintDialog>
#include <QDesktopServices>
#include <QAbstractTextDocumentLayout>
#include <QTextDocument>
#include <QPrinterInfo>
class
QmlPrinter
:
public
QObject
{
Q_OBJECT
private:
QList
<
QString
>
printableItems
;
void
paintItem
(
QQuickItem
*
item
,
QQuickWindow
*
window
,
QPainter
*
painter
);
void
paintQQuickRectangle
(
QQuickItem
*
item
,
QPainter
*
painter
);
void
paintQQuickText
(
QQuickItem
*
item
,
QPainter
*
painter
);
void
paintQQuickImage
(
QQuickItem
*
item
,
QPainter
*
painter
);
void
paintQQuickCanvasItem
(
QQuickItem
*
item
,
QQuickWindow
*
window
,
QPainter
*
painter
);
QT_BEGIN_NAMESPACE
class
QQuickItem
;
class
QQuickWindow
;
class
QPainter
;
class
QPrinter
;
class
QPrinterInfo
;
QT_END_NAMESPACE
bool
inherits
(
const
QMetaObject
*
metaObject
,
const
QString
&
name
);
bool
isCustomPrintItem
(
const
QString
&
item
);
class
QmlPrinter
{
public:
explicit
QmlPrinter
();
virtual
~
QmlPrinter
();
void
changePrinterOrientation
(
QPrinter
&
printer
,
const
int
&
width
,
const
int
&
height
);
bool
printPdf
(
const
QString
&
location
,
QQuickItem
*
presentation
);
void
addPrintableItem
(
const
QString
&
item
);
private:
QList
<
QString
>
printableItems
;
public:
explicit
QmlPrinter
(
QObject
*
parent
=
0
);
virtual
~
QmlPrinter
();
static
QMatrix
transformMatrix
(
QQuickItem
*
);
bool
printPdf
(
const
QString
&
location
,
QQuickItem
*
presentation
);
void
addPrintableItem
(
const
QString
&
item
);
signals:
void
paintItem
(
QQuickItem
*
item
,
QQuickWindow
*
window
,
QPainter
*
painter
);
void
paintQQuickRectangle
(
QQuickItem
*
item
,
QPainter
*
painter
);
void
paintQQuickText
(
QQuickItem
*
item
,
QPainter
*
painter
);
void
paintQQuickImage
(
QQuickItem
*
item
,
QPainter
*
painter
);
void
paintQQuickCanvasItem
(
QQuickItem
*
item
,
QQuickWindow
*
window
,
QPainter
*
painter
);
public
slots
:
bool
inherits
(
const
QMetaObject
*
metaObject
,
const
QString
&
name
);
bool
isCustomPrintItem
(
const
QString
&
item
);
void
changePrinterOrientation
(
QPrinter
&
printer
,
const
int
&
width
,
const
int
&
height
);
};
#endif // HURPRINTER_H
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment