• 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
  • detailled
detailledstyle.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  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 "detailledstyle.h"
26 
27 #include "printingwizard.h"
28 #include "printprogress.h"
29 #include "printstyle.h"
30 #include "ui_ds_appearance.h"
31 
32 #include <KApplication>
33 #include <KColorButton>
34 #include <KConfig>
35 #include <KDebug>
36 #include <KDialog>
37 #include <KGlobal>
38 #include <KLocale>
39 #include <KStandardDirs>
40 
41 #include <QCheckBox>
42 #include <QPrinter>
43 #include <QTextDocument>
44 
45 using namespace KABPrinting;
46 
47 const char *ConfigSectionName = "DetailedPrintStyle";
48 const char *ContactHeaderForeColor = "ContactHeaderForeColor";
49 const char *ContactHeaderBGColor = "ContactHeaderBGColor";
50 
51 struct ContactBlock
52 {
53  typedef QList<ContactBlock> List;
54 
55  QString header;
56  QStringList entries;
57 };
58 
59 struct ColorSettings
60 {
61  QString headerTextColor;
62  QString headerBackgroundColor;
63 };
64 
65 QString contactsToHtml( const KABC::Addressee::List &contacts, const ColorSettings &settings )
66 {
67  QString content;
68 
69  content += QLatin1String("<html>\n");
70  content += QLatin1String(" <head>\n");
71  content += QLatin1String(" <style type=\"text/css\">\n");
72  content += QLatin1String(" td.indented {\n");
73  content += QLatin1String(" padding-left: 20px;\n");
74  content += QLatin1String(" font-family: Fixed, monospace;\n");
75  content += QLatin1String(" }\n");
76  content += QLatin1String(" </style>\n");
77  content += QLatin1String(" </head>\n");
78  content += QLatin1String(" <body>\n");
79  foreach ( const KABC::Addressee &contact, contacts ) {
80  QString name = contact.realName();
81  if ( !contact.title().isEmpty() || !contact.role().isEmpty() ) {
82  QStringList content;
83  if ( !contact.title().isEmpty() ) {
84  content << contact.title();
85  }
86  if ( !contact.role().isEmpty() ) {
87  content << contact.role();
88  }
89  name += QString::fromLatin1( " (%1)" ).arg( content.join( QLatin1String( ", " ) ) );
90  }
91 
92  const QString birthday = KGlobal::locale()->formatDate( contact.birthday().date(),
93  KLocale::ShortDate );
94 
95  ContactBlock::List blocks;
96 
97  if ( !contact.organization().isEmpty() ) {
98  ContactBlock block;
99  block.header = i18n( "Organization:" );
100  block.entries.append( contact.organization() );
101 
102  blocks.append( block );
103  }
104 
105  if ( !contact.emails().isEmpty() ) {
106  ContactBlock block;
107  block.header = ( contact.emails().count() == 1 ?
108  i18n( "Email address:" ) :
109  i18n( "Email addresses:" ) );
110  block.entries = contact.emails();
111 
112  blocks.append( block );
113  }
114 
115  if ( !contact.phoneNumbers().isEmpty() ) {
116  const KABC::PhoneNumber::List numbers = contact.phoneNumbers();
117 
118  ContactBlock block;
119  block.header = ( numbers.count() == 1 ?
120  i18n( "Telephone:" ) :
121  i18n( "Telephones:" ) );
122 
123  foreach ( const KABC::PhoneNumber &number, numbers ) {
124  const QString line = number.typeLabel() + QLatin1String(": ") + number.number();
125  block.entries.append( line );
126  }
127 
128  blocks.append( block );
129  }
130 
131  if ( contact.url().isValid() ) {
132  ContactBlock block;
133  block.header = i18n( "Web page:" );
134  block.entries.append( contact.url().prettyUrl() );
135 
136  blocks.append( block );
137  }
138 
139  if ( !contact.addresses().isEmpty() ) {
140  const KABC::Address::List addresses = contact.addresses();
141 
142  foreach ( const KABC::Address &address, addresses ) {
143  ContactBlock block;
144 
145  switch ( address.type() ) {
146  case KABC::Address::Dom:
147  block.header = i18n( "Domestic Address" );
148  break;
149  case KABC::Address::Intl:
150  block.header = i18n( "International Address" );
151  break;
152  case KABC::Address::Postal:
153  block.header = i18n( "Postal Address" );
154  break;
155  case KABC::Address::Parcel:
156  block.header = i18n( "Parcel Address" );
157  break;
158  case KABC::Address::Home:
159  block.header = i18n( "Home Address" );
160  break;
161  case KABC::Address::Work:
162  block.header = i18n( "Work Address" );
163  break;
164  case KABC::Address::Pref:
165  default:
166  block.header = i18n( "Preferred Address" );
167  }
168  block.header += QLatin1Char(':');
169 
170  block.entries = address.formattedAddress().split( QLatin1Char('\n'), QString::KeepEmptyParts );
171  blocks.append( block );
172  }
173  }
174 
175  if ( !contact.note().isEmpty() ) {
176  ContactBlock block;
177  block.header = i18n( "Notes:" );
178  block.entries = contact.note().split( QLatin1Char('\n'), QString::KeepEmptyParts );
179 
180  blocks.append( block );
181  }
182 
183  // add header
184  content += QLatin1String(" <table style=\"border-width: 0px; border-spacing: 0px; "
185  "page-break-inside: avoid\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n");
186  content += QLatin1String(" <tr>\n");
187  content += QLatin1String(" <td style=\"color: ") + settings.headerTextColor +
188  QLatin1String(";\" bgcolor=\"") + settings.headerBackgroundColor +
189  QLatin1String("\" style=\"padding-left: 20px\">") +
190  name + QLatin1String("</td>\n");
191  content += QLatin1String(" <td style=\"color: ") + settings.headerTextColor +
192  QLatin1String(";\" align=\"right\" bgcolor=\"") + settings.headerBackgroundColor +
193  QLatin1String("\" style=\"padding-right: 20px\">") +
194  birthday + QLatin1String("</td>\n");
195  content += QLatin1String(" </tr>\n");
196 
197  for ( int i = 0; i < blocks.count(); i += 2 ) {
198  // add empty line for spacing
199  content += QLatin1String(" <tr>\n");
200  content += QLatin1String(" <td>&nbsp;</td>\n");
201  content += QLatin1String(" <td>&nbsp;</td>\n");
202  content += QLatin1String(" </tr>\n");
203 
204  // add real block data
205  const ContactBlock leftBlock = blocks.at( i );
206  const ContactBlock rightBlock = ( ( i + 1 < blocks.count() ) ?
207  blocks.at( i + 1 ) :
208  ContactBlock() );
209 
210  content += QLatin1String(" <tr>\n");
211  content += QLatin1String(" <td>") + leftBlock.header + QLatin1String("</td>\n");
212  content += QLatin1String(" <td>") + rightBlock.header + QLatin1String("</td>\n");
213  content += QLatin1String(" </tr>\n");
214 
215  const int maxLines = qMax( leftBlock.entries.count(), rightBlock.entries.count() );
216  for ( int j = 0; j < maxLines; ++j ) {
217  QString leftLine, rightLine;
218 
219  if ( j < leftBlock.entries.count() ) {
220  leftLine = leftBlock.entries.at( j );
221  }
222 
223  if ( j < rightBlock.entries.count() ) {
224  rightLine = rightBlock.entries.at( j );
225  }
226 
227  content += QLatin1String(" <tr>\n");
228  content += QLatin1String(" <td class=\"indented\">") + leftLine + QLatin1String("</td>\n");
229  content += QLatin1String(" <td class=\"indented\">") + rightLine + QLatin1String("</td>\n");
230  content += QLatin1String(" </tr>\n");
231  }
232  }
233 
234  // add empty line for spacing
235  content += QLatin1String(" <tr>\n");
236  content += QLatin1String(" <td>&nbsp;</td>\n");
237  content += QLatin1String(" <td>&nbsp;</td>\n");
238  content += QLatin1String(" </tr>\n");
239  content += QLatin1String(" </table>\n");
240  }
241  content += QLatin1String(" </body>\n");
242  content += QLatin1String("</html>\n");
243 
244  return content;
245 }
246 
247 class KABPrinting::AppearancePage : public QWidget, public Ui::AppearancePage_Base
248 {
249  public:
250  AppearancePage( QWidget *parent )
251  : QWidget( parent )
252  {
253  setupUi( this );
254  setObjectName( QLatin1String("AppearancePage") );
255  }
256 };
257 
258 DetailledPrintStyle::DetailledPrintStyle( PrintingWizard *parent )
259  : PrintStyle( parent ), mPageAppearance( new AppearancePage( parent ) )
260 {
261  setPreview( QLatin1String("detailed-style.png") );
262  setPreferredSortOptions( ContactFields::FormattedName, Qt::AscendingOrder );
263 
264  addPage( mPageAppearance, i18n( "Detailed Print Style - Appearance" ) );
265 
266  KConfigGroup config( KGlobal::config(), ConfigSectionName );
267 
268  mPageAppearance->kcbHeaderBGColor->
269  setColor( config.readEntry( ContactHeaderBGColor, QColor( Qt::black ) ) );
270 
271  mPageAppearance->kcbHeaderTextColor->
272  setColor( config.readEntry( ContactHeaderForeColor, QColor( Qt::white ) ) );
273 
274  mPageAppearance->layout()->setMargin( KDialog::marginHint() );
275  mPageAppearance->layout()->setSpacing( KDialog::spacingHint() );
276 }
277 
278 DetailledPrintStyle::~DetailledPrintStyle()
279 {
280 }
281 
282 void DetailledPrintStyle::print( const KABC::Addressee::List &contacts, PrintProgress *progress )
283 {
284  progress->addMessage( i18n( "Setting up colors" ) );
285  progress->setProgress( 0 );
286 
287  const QColor headerBackgroundColor = mPageAppearance->kcbHeaderBGColor->color();
288  const QColor headerForegroundColor = mPageAppearance->kcbHeaderTextColor->color();
289 
290  KConfigGroup config( KGlobal::config(), ConfigSectionName );
291  config.writeEntry( ContactHeaderForeColor, headerForegroundColor );
292  config.writeEntry( ContactHeaderBGColor, headerBackgroundColor );
293  config.sync();
294 
295  ColorSettings settings;
296  settings.headerBackgroundColor = headerBackgroundColor.name();
297  settings.headerTextColor = headerForegroundColor.name();
298 
299  QPrinter *printer = wizard()->printer();
300 
301  progress->addMessage( i18n( "Setting up document" ) );
302 
303  const QString html = contactsToHtml( contacts, settings );
304 
305  QTextDocument document;
306  document.setHtml( html );
307 
308  progress->addMessage( i18n( "Printing" ) );
309 
310  document.print( printer );
311 
312  progress->addMessage( i18nc( "Finished printing", "Done" ) );
313 }
314 
315 DetailledPrintStyleFactory::DetailledPrintStyleFactory( PrintingWizard *parent )
316  : PrintStyleFactory( parent )
317 {
318 }
319 
320 PrintStyle *DetailledPrintStyleFactory::create() const
321 {
322  return new DetailledPrintStyle( mParent );
323 }
324 
325 QString DetailledPrintStyleFactory::description() const
326 {
327  return i18n( "Detailed Style" );
328 }
329 
330 #include "detailledstyle.moc"
KABPrinting::PrintStyleFactory::mParent
PrintingWizard * mParent
Definition: printstyle.h:186
KABPrinting::DetailledPrintStyleFactory::create
PrintStyle * create() const
Definition: detailledstyle.cpp:320
ConfigSectionName
const char * ConfigSectionName
Definition: detailledstyle.cpp:47
KABPrinting::DetailledPrintStyleFactory::description
QString description() const
Overload this method to provide a one-liner description for your print style.
Definition: detailledstyle.cpp:325
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
contactsToHtml
QString contactsToHtml(const KABC::Addressee::List &contacts, const ColorSettings &settings)
Definition: detailledstyle.cpp:65
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::PrintStyle::setPreview
bool setPreview(const QString &fileName)
Loads the preview image from the kaddressbook data directory.
Definition: printstyle.cpp:56
ContactHeaderBGColor
const char * ContactHeaderBGColor
Definition: detailledstyle.cpp:49
KABPrinting::PrintStyle
The abstract interface to the PrintingWizards style objects.
Definition: printstyle.h:67
ContactFields::FormattedName
Definition: contactfields.h:36
printstyle.h
KABPrinting::DetailledPrintStyle
Definition: detailledstyle.h:35
KABPrinting::DetailledPrintStyleFactory::DetailledPrintStyleFactory
DetailledPrintStyleFactory(PrintingWizard *parent)
Definition: detailledstyle.cpp:315
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
KABPrinting::DetailledPrintStyle::print
void print(const KABC::Addressee::List &contacts, PrintProgress *)
This method must be reimplemented to actually print something.
Definition: detailledstyle.cpp:282
KABPrinting::PrintProgress::setProgress
void setProgress(int)
Set the progress to a certain amount.
Definition: printprogress.cpp:81
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::DetailledPrintStyle::~DetailledPrintStyle
~DetailledPrintStyle()
Definition: detailledstyle.cpp:278
detailledstyle.h
ContactHeaderForeColor
const char * ContactHeaderForeColor
Definition: detailledstyle.cpp:48
KABPrinting::PrintingWizard::printer
QPrinter * printer()
Returns the printer to use for printing.
Definition: printingwizard.cpp:203
KABPrinting::DetailledPrintStyle::DetailledPrintStyle
DetailledPrintStyle(PrintingWizard *parent)
Definition: detailledstyle.cpp:258
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