• 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
  • mike
mikesstyle.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 1996-2002 Mirko Boehm <mirko@kde.org>
4  2002 Mike Pilone <mpilone@slac.com>
5  2009 Tobias Koenig <tokoe@kde.org>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License along
18  with this program; if not, write to the Free Software Foundation, Inc.,
19  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, permission is given to link this program
22  with any edition of Qt, and distribute the resulting executable,
23  without including the source code for Qt in the source distribution.
24 */
25 
26 #include "mikesstyle.h"
27 #include "contactfields.h"
28 #include "printingwizard.h"
29 #include "printprogress.h"
30 #include "printstyle.h"
31 
32 #include <KABC/Addressee>
33 
34 #include <KDebug>
35 #include <KLocale>
36 
37 #include <QPrinter>
38 #include <QTextDocument>
39 
40 using namespace KABPrinting;
41 
42 static QString contactsToHtml( const KABC::Addressee::List &contacts )
43 {
44  QString content;
45 
46  ContactFields::Fields leftFields, rightFields;
47  ContactFields::Fields allFields = ContactFields::allFields();
48  allFields.remove( 0 ); // drop 'Undefined' field
49 
50  const int middle = allFields.count() / 2;
51 
52  for ( int i = 0; i < middle; ++i ) {
53  leftFields.append( allFields.at( i ) );
54  }
55 
56  for ( int i = middle; i < allFields.count(); ++i ) {
57  rightFields.append( allFields.at( i ) );
58  }
59 
60  int counter = 0;
61  content += QLatin1String("<html>\n");
62  content += QLatin1String(" <body>\n");
63  foreach ( const KABC::Addressee &contact, contacts ) {
64  const int max = qMax( leftFields.count(), rightFields.count() );
65 
66  const QString name = contact.realName();
67 
68  if ( counter % 2 ) {
69  content += QLatin1String(" <br/><br/>\n");
70  }
71 
72  // start a new page after every second table
73  const QString pageBreak = ( ( counter % 2 ) ? QLatin1String("page-break-after: always;" ): QString() );
74 
75  content += QLatin1String(" <table style=\"border-width: 0px; ") + pageBreak + QLatin1String("\" width=\"100%\">\n");
76  content += QLatin1String(" <tr>\n");
77  content += QLatin1String(" <th align=\"left\" style=\"color: black;\" bgcolor=\"gray\" "
78  "style=\"padding-left: 20px\" colspan=\"4\">") + name + QLatin1String("</th>\n");
79  content += QLatin1String(" </tr>\n");
80 
81  for ( int i = 0; i < max; i ++ ) {
82  QString leftTitle, leftValue, rightTitle, rightValue;
83 
84  if ( i < leftFields.count() ) {
85  leftTitle = ContactFields::label( leftFields.at( i ) ) + QLatin1Char(':');
86  leftTitle = leftTitle.replace( QLatin1Char(' '), QLatin1String("&nbsp;") );
87  leftValue = ContactFields::value( leftFields.at( i ), contact );
88  }
89 
90  if ( i < rightFields.count() ) {
91  rightTitle = ContactFields::label( rightFields.at( i ) ) + QLatin1Char(':');
92  rightTitle = rightTitle.replace( QLatin1Char(' '), QLatin1String("&nbsp;") );
93  rightValue = ContactFields::value( rightFields.at( i ), contact );
94  }
95 
96  content += QLatin1String(" <tr>\n");
97  content += QLatin1String(" <td>") + leftTitle + QLatin1String("</td>\n");
98  content += QLatin1String(" <td>") + leftValue + QLatin1String("</td>\n");
99  content += QLatin1String(" <td>") + rightTitle + QLatin1String("</td>\n");
100  content += QLatin1String(" <td>") + rightValue + QLatin1String("</td>\n");
101  content += QLatin1String(" </tr>\n");
102  }
103  content += QLatin1String(" </table>\n");
104 
105  counter++;
106  }
107  content += QLatin1String(" </body>\n");
108  content += QLatin1String("</html>\n");
109 
110  return content;
111 }
112 
113 MikesStyle::MikesStyle( PrintingWizard *parent )
114  : PrintStyle( parent )
115 {
116  setPreview( QLatin1String("mike-style.png") );
117  setPreferredSortOptions( ContactFields::FormattedName, Qt::AscendingOrder );
118 }
119 
120 MikesStyle::~MikesStyle()
121 {
122 }
123 
124 void MikesStyle::print( const KABC::Addressee::List &contacts, PrintProgress *progress )
125 {
126  QPrinter *printer = wizard()->printer();
127  printer->setPageMargins( 20, 20, 20, 20, QPrinter::DevicePixel );
128 
129  progress->addMessage( i18n( "Setting up document" ) );
130 
131  const QString html = contactsToHtml( contacts );
132 
133  QTextDocument document;
134  document.setHtml( html );
135 
136  progress->addMessage( i18n( "Printing" ) );
137 
138  document.print( printer );
139 
140  progress->addMessage( i18nc( "Finished printing", "Done" ) );
141 }
142 
143 MikesStyleFactory::MikesStyleFactory( PrintingWizard *parent )
144  : PrintStyleFactory( parent )
145 {
146 }
147 
148 PrintStyle *MikesStyleFactory::create() const
149 {
150  return new MikesStyle( mParent );
151 }
152 
153 QString MikesStyleFactory::description() const
154 {
155  return i18n( "Mike's Printing Style" );
156 }
157 
158 #include "mikesstyle.moc"
mikesstyle.h
KABPrinting::MikesStyleFactory::create
PrintStyle * create() const
Definition: mikesstyle.cpp:148
KABPrinting::PrintStyleFactory::mParent
PrintingWizard * mParent
Definition: printstyle.h:186
KABPrinting::MikesStyleFactory::description
QString description() const
Overload this method to provide a one-liner description for your print style.
Definition: mikesstyle.cpp:153
contactfields.h
KABPrinting::PrintingWizard
The PrintingWizard combines pages common for all print styles and those provided by the respective st...
Definition: printingwizard.h:55
KABPrinting::MikesStyle::MikesStyle
MikesStyle(PrintingWizard *parent)
Definition: mikesstyle.cpp:113
KABPrinting::MikesStyle
Definition: mikesstyle.h:36
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::MikesStyle::print
void print(const KABC::Addressee::List &, PrintProgress *)
This method must be reimplemented to actually print something.
Definition: mikesstyle.cpp:124
KABPrinting::PrintStyle::setPreview
bool setPreview(const QString &fileName)
Loads the preview image from the kaddressbook data directory.
Definition: printstyle.cpp:56
KABPrinting::MikesStyleFactory::MikesStyleFactory
MikesStyleFactory(PrintingWizard *parent)
Definition: mikesstyle.cpp:143
KABPrinting::PrintStyle
The abstract interface to the PrintingWizards style objects.
Definition: printstyle.h:67
contactsToHtml
static QString contactsToHtml(const KABC::Addressee::List &contacts)
Definition: mikesstyle.cpp:42
ContactFields::FormattedName
Definition: contactfields.h:36
ContactFields::Fields
QVector< Field > Fields
Defines a list of Field enums.
Definition: contactfields.h:95
printstyle.h
KABPrinting::MikesStyle::~MikesStyle
~MikesStyle()
Definition: mikesstyle.cpp:120
ContactFields::label
static QString label(Field field)
Returns the i18n label for the field.
Definition: contactfields.cpp:25
KABPrinting::PrintStyle::wizard
PrintingWizard * wizard() const
Returns the printing wizard that is responsible for this style.
Definition: printstyle.cpp:75
printprogress.h
printingwizard.h
KABPrinting::PrintProgress::addMessage
void addMessage(const QString &)
Add a message to the message log.
Definition: printprogress.cpp:61
ContactFields::value
static QString value(Field field, const KABC::Addressee &contact)
Returns the value for the field of the contact.
Definition: contactfields.cpp:480
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::PrintingWizard::printer
QPrinter * printer()
Returns the printer to use for printing.
Definition: printingwizard.cpp:203
ContactFields::allFields
static Fields allFields()
Returns a list of all available fields.
Definition: contactfields.cpp:177
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