• 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
  • ringbinder
ringbinderstyle.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2002 Jost Schenck <jost@schenck.de>
4  2009 Tobias Koenig <tokoe@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc.,
18  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of Qt, and distribute the resulting executable,
22  without including the source code for Qt in the source distribution.
23 */
24 
25 #include "ringbinderstyle.h"
26 #include "printingwizard.h"
27 #include "printprogress.h"
28 #include "ui_rbs_appearance.h"
29 
30 #include <KConfig>
31 #include <KGlobal>
32 #include <KLocale>
33 
34 #include <QPrinter>
35 #include <QTextDocument>
36 
37 using namespace KABPrinting;
38 
39 const char *RingBinderConfigSectionName = "RingBinderPrintStyle";
40 const char *ShowPhoneNumbers = "ShowPhoneNumbers";
41 const char *ShowEmailAddresses = "ShowEmailAddresses";
42 const char *ShowStreetAddresses = "ShowStreetAddresses";
43 const char *ShowOrganization = "ShowOrganization";
44 const char *ShowBirthday = "ShowBirthday";
45 const char *ShowNote = "ShowNote";
46 
47 enum PrintField {
48  PhoneNumbers = 1,
49  Emails = 2,
50  Addresses = 4,
51  Organization = 8,
52  Birthday = 16,
53  Note = 32
54 };
55 
56 static QString contactsToHtml( const KABC::Addressee::List &contacts, int fields )
57 {
58  QString content;
59 
60  content += QLatin1String("<html>\n");
61  content += QLatin1String(" <body>\n");
62  content += QLatin1String(" <table style=\"border-width: 1px; border-style: solid; "
63  "border-color: gray;\" width=\"100%\" cellspacing=\"0\">\n");
64  foreach ( const KABC::Addressee &contact, contacts ) {
65  QString nameString = contact.familyName() + QLatin1String(", ") + contact.givenName();
66 
67  if ( fields & Organization ) {
68  if ( !contact.organization().isEmpty() ) {
69  nameString += QLatin1String(" (") + contact.organization() + QLatin1Char(')');
70  }
71  }
72 
73  if ( fields & Birthday ) {
74  if ( contact.birthday().isValid() ) {
75  nameString += QLatin1String(" *") + KGlobal::locale()->formatDate( contact.birthday().date(),
76  KLocale::ShortDate );
77  }
78  }
79 
80  QStringList leftBlock, rightBlock;
81  if ( fields & PhoneNumbers ) {
82  const KABC::PhoneNumber::List numbers = contact.phoneNumbers();
83  foreach ( const KABC::PhoneNumber &number, numbers ) {
84  rightBlock.append( number.typeLabel() + QLatin1String(": ") + number.number() );
85  }
86  }
87  if ( fields & Emails ) {
88  const QStringList emails = contact.emails();
89  foreach ( const QString &email, emails ) {
90  rightBlock.append( email );
91  }
92  }
93  if ( fields & Note ) {
94  if ( !contact.note().isEmpty() ) {
95  const QString note = QLatin1String( "Note: " ) + contact.note().replace( QLatin1Char('\n'), QLatin1String("<br/>") );
96 
97  rightBlock.append( note );
98  }
99  }
100  if ( fields & Addresses ) {
101  const KABC::Address::List addresses = contact.addresses();
102  foreach ( const KABC::Address &address, addresses ) {
103  const QString data =
104  address.formattedAddress().replace( QLatin1String("\n\n"), QLatin1String("\n") ).replace( QLatin1Char('\n'), QLatin1String("<br/>") );
105  const QString subBlock= QLatin1String("<p style=\"margin-top: 0px; margin-left: 20px\">") + data +QLatin1String( "</p>");
106 
107  leftBlock.append( subBlock );
108  }
109  }
110 
111  content += QLatin1String(" <tr>\n");
112  content += QLatin1String(" <td style=\"padding-left: 3px; padding-top: 3px; padding-right: 3px; "
113  "padding-bottom: 3px;\">") +
114  nameString + leftBlock.join( QString() ) + QLatin1String("</td>\n");
115  content += QLatin1String(" <td style=\"padding-left: 3px; padding-top: 3px; padding-right: 3px; "
116  "padding-bottom: 3px;\">") +
117  rightBlock.join( QLatin1String("<br/>") ) + QLatin1String("</td>\n");
118  content += QLatin1String(" </tr>\n");
119  }
120  content += QLatin1String(" </table>\n");
121  content += QLatin1String(" </body>\n");
122  content += QLatin1String("</html>\n");
123 
124  return content;
125 }
126 
127 namespace KABPrinting {
128 
129 class RingBinderStyleAppearanceForm : public QWidget, public Ui::RingBinderStyleAppearanceForm_Base
130 {
131  public:
132  explicit RingBinderStyleAppearanceForm( QWidget *parent )
133  : QWidget( parent )
134  {
135  setObjectName( QLatin1String("AppearancePage") );
136  setupUi( this );
137  }
138 };
139 
140 }
141 
142 RingBinderPrintStyle::RingBinderPrintStyle( PrintingWizard *parent )
143  : PrintStyle( parent ),
144  mPageAppearance( new RingBinderStyleAppearanceForm( parent ) )
145 {
146  setPreview( QLatin1String("ringbinder-style.png") );
147  setPreferredSortOptions( ContactFields::FamilyName, Qt::AscendingOrder );
148 
149  addPage( mPageAppearance, i18n( "Ring Binder Printing Style - Appearance" ) );
150 
151  // applying previous settings
152  KConfigGroup config( KGlobal::config(), RingBinderConfigSectionName );
153  mPageAppearance->cbPhoneNumbers->setChecked( config.readEntry( ShowPhoneNumbers, true ) );
154  mPageAppearance->cbEmails->setChecked( config.readEntry( ShowEmailAddresses, true ) );
155  mPageAppearance->cbStreetAddresses->setChecked( config.readEntry( ShowStreetAddresses, true ) );
156  mPageAppearance->cbOrganization->setChecked( config.readEntry( ShowOrganization, true ) );
157  mPageAppearance->cbBirthday->setChecked( config.readEntry( ShowBirthday, false ) );
158  mPageAppearance->cbNote->setChecked( config.readEntry( ShowNote, false ) );
159 }
160 
161 RingBinderPrintStyle::~RingBinderPrintStyle()
162 {
163 }
164 
165 void RingBinderPrintStyle::print( const KABC::Addressee::List &contacts, PrintProgress *progress )
166 {
167  progress->addMessage( i18n( "Setting up fields" ) );
168  progress->setProgress( 0 );
169 
170  // first write current config settings
171  KConfigGroup config( KGlobal::config(), RingBinderConfigSectionName );
172  config.writeEntry( ShowPhoneNumbers, mPageAppearance->cbPhoneNumbers->isChecked() );
173  config.writeEntry( ShowEmailAddresses, mPageAppearance->cbEmails->isChecked() );
174  config.writeEntry( ShowStreetAddresses, mPageAppearance->cbStreetAddresses->isChecked() );
175  config.writeEntry( ShowOrganization, mPageAppearance->cbOrganization->isChecked() );
176  config.writeEntry( ShowBirthday, mPageAppearance->cbBirthday->isChecked() );
177  config.writeEntry( ShowNote, mPageAppearance->cbNote->isChecked() );
178  config.sync();
179 
180  QPrinter *printer = wizard()->printer();
181  printer->setPageMargins( 50, 20, 0, 50, QPrinter::DevicePixel );
182 
183  progress->addMessage( i18n( "Setting up document" ) );
184 
185  int fields = 0;
186 
187  if ( mPageAppearance->cbPhoneNumbers->isChecked() ) {
188  fields |= PhoneNumbers;
189  }
190 
191  if ( mPageAppearance->cbEmails->isChecked() ) {
192  fields |= Emails;
193  }
194 
195  if ( mPageAppearance->cbStreetAddresses->isChecked() ) {
196  fields |= Addresses;
197  }
198 
199  if ( mPageAppearance->cbOrganization->isChecked() ) {
200  fields |= Organization;
201  }
202 
203  if ( mPageAppearance->cbBirthday->isChecked() ) {
204  fields |= Birthday;
205  }
206 
207  if ( mPageAppearance->cbNote->isChecked() ) {
208  fields |= Note;
209  }
210 
211  const QString html = contactsToHtml( contacts, fields );
212 
213  QTextDocument document;
214  document.setHtml( html );
215 
216  progress->addMessage( i18n( "Printing" ) );
217 
218  document.print( printer );
219 
220  progress->addMessage( i18nc( "Finished printing", "Done" ) );
221 }
222 
223 RingBinderPrintStyleFactory::RingBinderPrintStyleFactory( PrintingWizard *parent )
224  : PrintStyleFactory( parent )
225 {
226 }
227 
228 PrintStyle *RingBinderPrintStyleFactory::create() const
229 {
230  return new RingBinderPrintStyle( mParent );
231 }
232 
233 QString RingBinderPrintStyleFactory::description() const
234 {
235  return i18n( "Printout for Ring Binders" );
236 }
KABPrinting::PrintStyleFactory::mParent
PrintingWizard * mParent
Definition: printstyle.h:186
Birthday
Definition: ringbinderstyle.cpp:52
KABPrinting::RingBinderPrintStyle::print
void print(const KABC::Addressee::List &contacts, PrintProgress *)
This method must be reimplemented to actually print something.
Definition: ringbinderstyle.cpp:165
KABPrinting::PrintingWizard
The PrintingWizard combines pages common for all print styles and those provided by the respective st...
Definition: printingwizard.h:55
Addresses
Definition: ringbinderstyle.cpp:50
ShowOrganization
const char * ShowOrganization
Definition: ringbinderstyle.cpp:43
contactsToHtml
static QString contactsToHtml(const KABC::Addressee::List &contacts, int fields)
Definition: ringbinderstyle.cpp:56
KABPrinting::RingBinderPrintStyle
Definition: ringbinderstyle.h:35
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
ShowNote
const char * ShowNote
Definition: ringbinderstyle.cpp:45
KABPrinting::PrintStyle::addPage
void addPage(QWidget *page, const QString &title)
Adds an additional page to the printing wizard, e.g.
Definition: printstyle.cpp:80
KABPrinting::RingBinderPrintStyle::RingBinderPrintStyle
RingBinderPrintStyle(PrintingWizard *parent)
Definition: ringbinderstyle.cpp:142
KABPrinting::PrintStyle::setPreview
bool setPreview(const QString &fileName)
Loads the preview image from the kaddressbook data directory.
Definition: printstyle.cpp:56
ShowPhoneNumbers
const char * ShowPhoneNumbers
Definition: ringbinderstyle.cpp:40
PrintField
PrintField
Definition: ringbinderstyle.cpp:47
Organization
Definition: ringbinderstyle.cpp:51
ContactFields::FamilyName
Definition: contactfields.h:40
KABPrinting::PrintStyle
The abstract interface to the PrintingWizards style objects.
Definition: printstyle.h:67
PhoneNumbers
Definition: ringbinderstyle.cpp:48
ringbinderstyle.h
KABPrinting::RingBinderPrintStyleFactory::description
QString description() const
Overload this method to provide a one-liner description for your print style.
Definition: ringbinderstyle.cpp:233
ShowEmailAddresses
const char * ShowEmailAddresses
Definition: ringbinderstyle.cpp:41
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::RingBinderPrintStyle::~RingBinderPrintStyle
~RingBinderPrintStyle()
Definition: ringbinderstyle.cpp:161
KABPrinting::PrintProgress::addMessage
void addMessage(const QString &)
Add a message to the message log.
Definition: printprogress.cpp:61
RingBinderConfigSectionName
const char * RingBinderConfigSectionName
Definition: ringbinderstyle.cpp:39
ShowBirthday
const char * ShowBirthday
Definition: ringbinderstyle.cpp:44
KABPrinting::PrintProgress::setProgress
void setProgress(int)
Set the progress to a certain amount.
Definition: printprogress.cpp:81
KABPrinting::RingBinderPrintStyleFactory::RingBinderPrintStyleFactory
RingBinderPrintStyleFactory(PrintingWizard *parent)
Definition: ringbinderstyle.cpp:223
KABPrinting::PrintProgress
This defines a simple widget to display print progress information.
Definition: printprogress.h:41
Emails
Definition: ringbinderstyle.cpp:49
KABPrinting::PrintStyleFactory
The factories are used to have all object of the respective print style created in one place...
Definition: printstyle.h:171
ShowStreetAddresses
const char * ShowStreetAddresses
Definition: ringbinderstyle.cpp:42
KABPrinting::RingBinderPrintStyleFactory::create
PrintStyle * create() const
Definition: ringbinderstyle.cpp:228
KABPrinting::PrintingWizard::printer
QPrinter * printer()
Returns the printer to use for printing.
Definition: printingwizard.cpp:203
Note
Definition: ringbinderstyle.cpp:53
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