22 #include <QTextDocument>
23 #include <QTextDocumentFragment>
24 #include <QAbstractTextDocumentLayout>
26 #include <QPrintDialog>
28 #include <kcal/journal.h>
29 #include <kdeprintdialog.h>
30 #include <KMessageBox>
34 #include <KPrintPreview>
36 #include <grantlee/context.h>
37 #include <grantlee/engine.h>
38 #include <grantlee/templateloader.h>
42 : mEngine(new Grantlee::Engine)
44 mTemplateLoader = Grantlee::FileSystemTemplateLoader::Ptr(
new Grantlee::FileSystemTemplateLoader );
49 mEngine->deleteLater();
62 void KNotePrinter::doPrintPreview(
const QString &htmlText)
64 QPrinter printer( QPrinter::HighResolution );
65 printer.setOutputFormat( QPrinter::PdfFormat );
66 printer.setCollateCopies(
true );
68 KPrintPreview previewdlg( &printer, 0 );
69 print(printer, htmlText);
73 void KNotePrinter::doPrint(
const QString &htmlText,
74 const QString &dialogCaption )
76 QPrinter printer( QPrinter::HighResolution );
78 QPrintDialog printDialog((&printer));
79 printDialog.setWindowTitle( dialogCaption );
80 if ( !printDialog.exec() ) {
83 print(printer, htmlText);
86 void KNotePrinter::print(QPrinter &printer,
const QString &htmlText)
88 const int margin = 30;
89 int marginX = margin * printer.logicalDpiX() / 72;
90 int marginY = margin * printer.logicalDpiY() / 72;
92 QRect typeArea( marginX, marginY,
93 printer.width() - marginX * 2,
94 printer.height() - marginY * 2 );
96 QTextDocument textDoc;
97 textDoc.setHtml( htmlText );
98 textDoc.documentLayout()->setPaintDevice( &printer );
99 textDoc.setPageSize( typeArea.size() );
100 textDoc.setDefaultFont( m_defaultFont );
102 QPainter painter( &printer );
103 QRect clip( typeArea );
104 painter.translate( marginX, marginY );
105 clip.translate( -marginX, -marginY );
107 for (
int page = 1; page <= textDoc.pageCount(); ++page ) {
108 textDoc.drawContents( &painter, clip );
109 clip.translate( 0, typeArea.height() );
110 painter.translate( 0, -typeArea.height() );
112 painter.setFont( m_defaultFont );
113 const QString pageNumber(QString::number( page ));
115 clip.right() - painter.fontMetrics().width( pageNumber ),
116 clip.bottom() + painter.fontMetrics().ascent() + 5,
119 if ( page < textDoc.pageCount() ) {
127 mTemplateLoader->setTemplateDirs( QStringList() << themePath );
128 mEngine->addTemplateLoader( mTemplateLoader );
130 mSelfcontainedTemplate = mEngine->loadByName( QLatin1String(
"theme.html") );
131 QString mErrorMessage;
132 if ( mSelfcontainedTemplate->error() ) {
133 mErrorMessage += mSelfcontainedTemplate->errorString() + QLatin1String(
"<br>");
136 if (mErrorMessage.isEmpty()) {
139 notes << QVariant::fromValue(static_cast<QObject*>(n));
141 c.insert(QLatin1String(
"notes"), notes);
143 const QString htmlText = mSelfcontainedTemplate->render(&c);
144 const QString dialogCaption = i18np(
"Print Note",
"Print %1 notes",
147 doPrintPreview(htmlText);
149 doPrint( htmlText, dialogCaption );
151 KMessageBox::error(0, i18n(
"Printing theme was not found."), i18n(
"Printing error"));
void printNotes(const QList< KNotePrintObject * > lst, const QString &themePath, bool preview)
void setDefaultFont(const QFont &font)
QFont defaultFont() const