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

kaddressbook

  • sources
  • kde-4.12
  • kdepim
  • kaddressbook
  • printing
  • grantlee
grantleeprintstyle.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 "grantleeprintstyle.h"
19 #include "contactfields.h"
20 #include "printingwizard.h"
21 #include "printprogress.h"
22 #include "printstyle.h"
23 #include "contactgrantleeprintobject.h"
24 
25 #include "kaddressbookgrantlee/formatter/grantleecontactutils.h"
26 
27 #include <grantlee/context.h>
28 #include <grantlee/engine.h>
29 #include <grantlee/templateloader.h>
30 
31 #include <KABC/Addressee>
32 
33 #include <KDebug>
34 #include <KLocale>
35 
36 #include <QPrinter>
37 #include <QTextDocument>
38 
39 using namespace KABPrinting;
40 
41 QString GrantleePrintStyle::contactsToHtml( const KABC::Addressee::List &contacts )
42 {
43  if (!mErrorMessage.isEmpty())
44  return mErrorMessage;
45 
46  QVariantList contactsList;
47  QList<ContactGrantleePrintObject*> lst;
48  Q_FOREACH (const KABC::Addressee &address, contacts) {
49  ContactGrantleePrintObject *contactPrintObject = new ContactGrantleePrintObject(address);
50  lst.append(contactPrintObject);
51  contactsList << QVariant::fromValue(static_cast<QObject*>(contactPrintObject));
52  }
53  QVariantHash mapping;
54  QVariantHash contactI18n;
55  contactI18n.insert( QLatin1String( "birthdayi18n" ), GrantleeContactUtils::variableI18n(QLatin1String("birthdayi18n") ) );
56  contactI18n.insert( QLatin1String("anniversaryi18n"), GrantleeContactUtils::variableI18n(QLatin1String("anniversaryi18n") ) );
57  contactI18n.insert( QLatin1String( "emailsi18n" ), GrantleeContactUtils::variableI18n(QLatin1String("emailsi18n") ) );
58  contactI18n.insert( QLatin1String( "websitei18n" ), GrantleeContactUtils::variableI18n(QLatin1String("websitei18n") ) );
59  contactI18n.insert( QLatin1String( "blogUrli18n" ), GrantleeContactUtils::variableI18n(QLatin1String("blogUrli18n")) );
60  contactI18n.insert( QLatin1String( "addressBookNamei18n" ), GrantleeContactUtils::variableI18n(QLatin1String("addressBookNamei18n") ));
61  contactI18n.insert( QLatin1String( "notei18n" ),GrantleeContactUtils::variableI18n(QLatin1String("notei18n") ) );
62  contactI18n.insert( QLatin1String( "departmenti18n" ),GrantleeContactUtils::variableI18n(QLatin1String("departmenti18n") ) );
63  contactI18n.insert( QLatin1String( "Professioni18n" ),GrantleeContactUtils::variableI18n(QLatin1String("Professioni18n") ) );
64  contactI18n.insert( QLatin1String( "officei18n" ),GrantleeContactUtils::variableI18n(QLatin1String("officei18n") ) );
65  contactI18n.insert( QLatin1String( "manageri18n" ),GrantleeContactUtils::variableI18n(QLatin1String("manageri18n") ) );
66  contactI18n.insert( QLatin1String( "assistanti18n" ),GrantleeContactUtils::variableI18n(QLatin1String("assistanti18n") ) );
67  contactI18n.insert( QLatin1String( "spousei18n" ),GrantleeContactUtils::variableI18n(QLatin1String("spousei18n") ) );
68  contactI18n.insert( QLatin1String( "imAddressi18n" ), GrantleeContactUtils::variableI18n(QLatin1String("imAddressi18n") ));
69  mapping.insert( QLatin1String("contacti18n"), contactI18n );
70 
71  Grantlee::Context context( mapping );
72  context.insert(QLatin1String("contacts"), contactsList);
73  const QString content = mSelfcontainedTemplate->render( &context );
74  qDeleteAll(lst);
75  return content;
76 }
77 
78 GrantleePrintStyle::GrantleePrintStyle( const QString &themePath, PrintingWizard *parent )
79  : PrintStyle( parent )
80 {
81  mEngine = new Grantlee::Engine;
82  mTemplateLoader = Grantlee::FileSystemTemplateLoader::Ptr( new Grantlee::FileSystemTemplateLoader );
83  //TODO themePath + preview.png ?
84  //setPreview( QLatin1String("") );
85 
86  mTemplateLoader->setTemplateDirs( QStringList() << themePath );
87  mEngine->addTemplateLoader( mTemplateLoader );
88 
89  mSelfcontainedTemplate = mEngine->loadByName( QLatin1String("theme.html") );
90  if ( mSelfcontainedTemplate->error() ) {
91  mErrorMessage = mSelfcontainedTemplate->errorString() + QLatin1String("<br>");
92  }
93 
94  setPreferredSortOptions( ContactFields::FormattedName, Qt::AscendingOrder );
95 }
96 
97 GrantleePrintStyle::~GrantleePrintStyle()
98 {
99  delete mEngine;
100 }
101 
102 void GrantleePrintStyle::print( const KABC::Addressee::List &contacts, PrintProgress *progress )
103 {
104  QPrinter *printer = wizard()->printer();
105  printer->setPageMargins( 20, 20, 20, 20, QPrinter::DevicePixel );
106 
107  progress->addMessage( i18n( "Setting up document" ) );
108 
109  const QString html = contactsToHtml( contacts );
110 
111  QTextDocument document;
112  document.setHtml( html );
113 
114  progress->addMessage( i18n( "Printing" ) );
115 
116  document.print( printer );
117 
118  progress->addMessage( i18nc( "Finished printing", "Done" ) );
119 }
120 
121 GrantleeStyleFactory::GrantleeStyleFactory( const QString &name,const QString &themePath, PrintingWizard *parent )
122  : PrintStyleFactory( parent ),
123  mThemePath(themePath),
124  mName(name)
125 {
126 }
127 
128 PrintStyle *GrantleeStyleFactory::create() const
129 {
130  return new GrantleePrintStyle( mThemePath, mParent );
131 }
132 
133 QString GrantleeStyleFactory::description() const
134 {
135  return mName;
136 }
137 
138 #include "grantleeprintstyle.moc"
KABPrinting::PrintStyleFactory::mParent
PrintingWizard * mParent
Definition: printstyle.h:186
contactfields.h
KABPrinting::GrantleeStyleFactory::GrantleeStyleFactory
GrantleeStyleFactory(const QString &name, const QString &themePath, PrintingWizard *parent)
Definition: grantleeprintstyle.cpp:121
KABPrinting::PrintingWizard
The PrintingWizard combines pages common for all print styles and those provided by the respective st...
Definition: printingwizard.h:55
KABPrinting::PrintStyle::setPreferredSortOptions
void setPreferredSortOptions(ContactFields::Field, Qt::SortOrder sortOrder=Qt::AscendingOrder)
Sets the preferred sort options for this printing style.
Definition: printstyle.cpp:112
KABPrinting::ContactGrantleePrintObject
Definition: contactgrantleeprintobject.h:25
KABPrinting::GrantleePrintStyle::GrantleePrintStyle
GrantleePrintStyle(const QString &themePath, PrintingWizard *parent)
Definition: grantleeprintstyle.cpp:78
KABPrinting::PrintStyle
The abstract interface to the PrintingWizards style objects.
Definition: printstyle.h:67
ContactFields::FormattedName
Definition: contactfields.h:36
printstyle.h
KABPrinting::GrantleeStyleFactory::create
PrintStyle * create() const
Definition: grantleeprintstyle.cpp:128
KABPrinting::PrintStyle::wizard
PrintingWizard * wizard() const
Returns the printing wizard that is responsible for this style.
Definition: printstyle.cpp:75
KABPrinting::GrantleeStyleFactory::description
QString description() const
Overload this method to provide a one-liner description for your print style.
Definition: grantleeprintstyle.cpp:133
grantleeprintstyle.h
printprogress.h
printingwizard.h
KABPrinting::GrantleePrintStyle::print
void print(const KABC::Addressee::List &, PrintProgress *)
This method must be reimplemented to actually print something.
Definition: grantleeprintstyle.cpp:102
KABPrinting::PrintProgress::addMessage
void addMessage(const QString &)
Add a message to the message log.
Definition: printprogress.cpp:61
KABPrinting::PrintProgress
This defines a simple widget to display print progress information.
Definition: printprogress.h:41
KABPrinting::PrintStyleFactory
The factories are used to have all object of the respective print style created in one place...
Definition: printstyle.h:171
KABPrinting::GrantleePrintStyle
Definition: grantleeprintstyle.h:32
KABPrinting::GrantleePrintStyle::~GrantleePrintStyle
~GrantleePrintStyle()
Definition: grantleeprintstyle.cpp:97
contactgrantleeprintobject.h
KABPrinting::PrintingWizard::printer
QPrinter * printer()
Returns the printer to use for printing.
Definition: printingwizard.cpp:203
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kaddressbook

Skip menu "kaddressbook"
  • 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