• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

knotes

  • sources
  • kde-4.12
  • kdepim
  • knotes
  • print
knoteprinter.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013 Montel Laurent <montel@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "knoteprinter.h"
19 #include "print/knoteprintobject.h"
20 
21 #include <QPainter>
22 #include <QTextDocument>
23 #include <QTextDocumentFragment>
24 #include <QAbstractTextDocumentLayout>
25 #include <QPrinter>
26 #include <QPrintDialog>
27 
28 #include <kcal/journal.h>
29 #include <kdeprintdialog.h>
30 #include <KMessageBox>
31 
32 #include <klocale.h>
33 #include <kdebug.h>
34 #include <KPrintPreview>
35 
36 #include <grantlee/context.h>
37 #include <grantlee/engine.h>
38 #include <grantlee/templateloader.h>
39 
40 
41 KNotePrinter::KNotePrinter()
42  : mEngine(new Grantlee::Engine)
43 {
44  mTemplateLoader = Grantlee::FileSystemTemplateLoader::Ptr( new Grantlee::FileSystemTemplateLoader );
45 }
46 
47 KNotePrinter::~KNotePrinter()
48 {
49  mEngine->deleteLater();
50 }
51 
52 void KNotePrinter::setDefaultFont( const QFont &font )
53 {
54  m_defaultFont = font;
55 }
56 
57 QFont KNotePrinter::defaultFont() const
58 {
59  return m_defaultFont;
60 }
61 
62 void KNotePrinter::doPrintPreview(const QString &htmlText)
63 {
64  QPrinter printer( QPrinter::HighResolution );
65  printer.setOutputFormat( QPrinter::PdfFormat );
66  printer.setCollateCopies( true );
67 
68  KPrintPreview previewdlg( &printer, 0 );
69  print(printer, htmlText);
70  previewdlg.exec();
71 }
72 
73 void KNotePrinter::doPrint( const QString &htmlText,
74  const QString &dialogCaption )
75 {
76  QPrinter printer( QPrinter::HighResolution );
77  //printer.setFullPage( true ); //disabled, causes asymmetric margins
78  QPrintDialog printDialog(/*KdePrint::createPrintDialog*/(&printer));
79  printDialog.setWindowTitle( dialogCaption );
80  if ( !printDialog.exec() ) {
81  return;
82  }
83  print(printer, htmlText);
84 }
85 
86 void KNotePrinter::print(QPrinter &printer, const QString &htmlText)
87 {
88  const int margin = 30; //pt //set to 40 when setFullPage() works again
89  int marginX = margin * printer.logicalDpiX() / 72;
90  int marginY = margin * printer.logicalDpiY() / 72;
91 
92  QRect typeArea( marginX, marginY,
93  printer.width() - marginX * 2,
94  printer.height() - marginY * 2 );
95 
96  QTextDocument textDoc;
97  textDoc.setHtml( htmlText );
98  textDoc.documentLayout()->setPaintDevice( &printer );
99  textDoc.setPageSize( typeArea.size() );
100  textDoc.setDefaultFont( m_defaultFont );
101 
102  QPainter painter( &printer );
103  QRect clip( typeArea );
104  painter.translate( marginX, marginY );
105  clip.translate( -marginX, -marginY );
106 
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() );
111 
112  painter.setFont( m_defaultFont );
113  const QString pageNumber(QString::number( page ));
114  painter.drawText(
115  clip.right() - painter.fontMetrics().width( pageNumber ),
116  clip.bottom() + painter.fontMetrics().ascent() + 5,
117  pageNumber );
118 
119  if ( page < textDoc.pageCount() ) {
120  printer.newPage();
121  }
122  }
123 }
124 
125 void KNotePrinter::printNotes(const QList<KNotePrintObject *> lst, const QString &themePath, bool preview)
126 {
127  mTemplateLoader->setTemplateDirs( QStringList() << themePath );
128  mEngine->addTemplateLoader( mTemplateLoader );
129 
130  mSelfcontainedTemplate = mEngine->loadByName( QLatin1String("theme.html") );
131  QString mErrorMessage;
132  if ( mSelfcontainedTemplate->error() ) {
133  mErrorMessage += mSelfcontainedTemplate->errorString() + QLatin1String("<br>");
134  }
135 
136  if (mErrorMessage.isEmpty()) {
137  QVariantList notes;
138  Q_FOREACH(KNotePrintObject *n, lst)
139  notes << QVariant::fromValue(static_cast<QObject*>(n));
140  Grantlee::Context c;
141  c.insert(QLatin1String("notes"), notes);
142 
143  const QString htmlText = mSelfcontainedTemplate->render(&c);
144  const QString dialogCaption = i18np( "Print Note", "Print %1 notes",
145  lst.count() );
146  if (preview)
147  doPrintPreview(htmlText);
148  else
149  doPrint( htmlText, dialogCaption );
150  } else {
151  KMessageBox::error(0, i18n("Printing theme was not found."), i18n("Printing error"));
152  }
153 }
154 
KNotePrinter::~KNotePrinter
~KNotePrinter()
Definition: knoteprinter.cpp:47
KNotePrintObject
Definition: knoteprintobject.h:29
KNotePrinter::printNotes
void printNotes(const QList< KNotePrintObject * > lst, const QString &themePath, bool preview)
Definition: knoteprinter.cpp:125
KNotePrinter::setDefaultFont
void setDefaultFont(const QFont &font)
Definition: knoteprinter.cpp:52
knoteprintobject.h
knoteprinter.h
KNotePrinter::defaultFont
QFont defaultFont() const
Definition: knoteprinter.cpp:57
KNotePrinter::KNotePrinter
KNotePrinter()
Definition: knoteprinter.cpp:41
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:33 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

knotes

Skip menu "knotes"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal