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

knotes

  • sources
  • kde-4.14
  • kdepim
  • knotes
  • print
knoteprinter.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013-2015 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 #include "knotegrantleeprint.h"
21 
22 #include <QPainter>
23 #include <QTextDocument>
24 #include <QPrinter>
25 #include <QPrintDialog>
26 #include <QAbstractTextDocumentLayout>
27 #include <QPointer>
28 
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(QObject *parent)
42  : QObject(parent),
43  mGrantleePrint(0)
44 {
45 }
46 
47 KNotePrinter::~KNotePrinter()
48 {
49 }
50 
51 void KNotePrinter::setDefaultFont( const QFont &font )
52 {
53  m_defaultFont = font;
54 }
55 
56 QFont KNotePrinter::defaultFont() const
57 {
58  return m_defaultFont;
59 }
60 
61 void KNotePrinter::doPrintPreview(const QString &htmlText)
62 {
63  QPrinter printer( QPrinter::HighResolution );
64  printer.setOutputFormat( QPrinter::PdfFormat );
65  printer.setCollateCopies( true );
66 
67  KPrintPreview previewdlg( &printer, 0 );
68  print(printer, htmlText);
69  previewdlg.exec();
70 }
71 
72 void KNotePrinter::doPrint( const QString &htmlText,
73  const QString &dialogCaption )
74 {
75  QPrinter printer( QPrinter::HighResolution );
76  //printer.setFullPage( true ); //disabled, causes asymmetric margins
77  QPointer<QPrintDialog> printDialog = KdePrint::createPrintDialog(&printer);
78  printDialog->setWindowTitle(dialogCaption);
79  if ( !printDialog->exec() || !printDialog ) {
80  delete printDialog;
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  mGrantleePrint = new KNoteGrantleePrint(themePath, this);
128  if (mGrantleePrint->errorMessage().isEmpty()) {
129  const QString htmlText = mGrantleePrint->notesToHtml(lst);
130  const QString dialogCaption = i18np( "Print Note", "Print %1 notes",
131  lst.count() );
132  if (preview)
133  doPrintPreview(htmlText);
134  else
135  doPrint( htmlText, dialogCaption );
136  } else {
137  KMessageBox::error(0, i18n("Printing theme was not found."), i18n("Printing error"));
138  }
139 }
140 
KNotePrinter::~KNotePrinter
~KNotePrinter()
Definition: knoteprinter.cpp:47
KNotePrinter::printNotes
void printNotes(const QList< KNotePrintObject * > lst, const QString &themePath, bool preview)
Definition: knoteprinter.cpp:125
QPrinter
QFont
QPointer
KNoteGrantleePrint
Definition: knotegrantleeprint.h:28
KNotePrinter::setDefaultFont
void setDefaultFont(const QFont &font)
Definition: knoteprinter.cpp:51
QPaintDevice::width
int width() const
QRect
QString::number
QString number(int n, int base)
QList::count
int count(const T &value) const
knoteprintobject.h
QObject
QPainter
QString::isEmpty
bool isEmpty() const
knoteprinter.h
QString
QList
KNotePrinter::defaultFont
QFont defaultFont() const
Definition: knoteprinter.cpp:56
QPaintDevice::logicalDpiX
int logicalDpiX() const
QPaintDevice::logicalDpiY
int logicalDpiY() const
QPrinter::newPage
bool newPage()
QTextDocument
KNoteGrantleePrint::errorMessage
QString errorMessage() const
Definition: knotegrantleeprint.cpp:51
QTextDocument::setHtml
void setHtml(const QString &html)
KNotePrinter::KNotePrinter
KNotePrinter(QObject *parent=0)
Definition: knoteprinter.cpp:41
QPaintDevice::height
int height() const
knotegrantleeprint.h
KNoteGrantleePrint::notesToHtml
QString notesToHtml(const QList< KNotePrintObject * > lst)
Definition: knotegrantleeprint.cpp:64
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:28 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
  • pimprint

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