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

kaddressbook

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