• 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
  • compact
compactstyle.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2011 Mario Scheel <zweistein12@gmx.de>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19  As a special exception, permission is given to link this program
20  with any edition of Qt, and distribute the resulting executable,
21  without including the source code for Qt in the source distribution.
22 */
23 
24 #include "compactstyle.h"
25 #include "contactfields.h"
26 #include "printprogress.h"
27 #include "printstyle.h"
28 #include "printingwizard.h"
29 #include "ui_compactstyle.h"
30 
31 #include <KABC/Addressee>
32 
33 #include <KLocale>
34 
35 #include <QCheckBox>
36 #include <QPrinter>
37 #include <QTextDocument>
38 
39 using namespace KABPrinting;
40 
41 const char *CompactStyleConfigSectionName = "CompactStyle";
42 const char *WithAlternating = "WithAlternating";
43 const char *WithHomeAddress = "WithHomeAddress";
44 const char *WithBusinessAddress = "WithBusinessAddress";
45 const char *WithBirthday = "WithBirthday";
46 const char *WithEMail = "WithEMail";
47 const char *FirstColor = "FirstColor";
48 const char *SecondColor = "SecondColor";
49 
50 namespace KABPrinting {
51 
52 class CompactStyleForm : public QWidget, public Ui::CompactStyleForm_Base
53 {
54  public:
55  explicit CompactStyleForm( QWidget *parent )
56  : QWidget( parent )
57  {
58  setObjectName( QLatin1String("AppearancePage") );
59  setupUi( this );
60  }
61 };
62 
63 }
64 
65 QString CompactStyle::contactsToHtml( const KABC::Addressee::List &contacts ) const
66 {
67  // collect the fields are need to print
68  ContactFields::Fields fields;
69  fields << ContactFields::FormattedName;
70  if (this->withHomeAddress) {
71  fields << ContactFields::HomeAddressStreet
72  << ContactFields::HomeAddressPostalCode
73  << ContactFields::HomeAddressLocality
74  << ContactFields::HomePhone
75  << ContactFields::MobilePhone;
76  }
77  if (this->withBusinessAddress) {
78  fields << ContactFields::BusinessAddressStreet
79  << ContactFields::BusinessAddressPostalCode
80  << ContactFields::BusinessAddressLocality
81  << ContactFields::BusinessPhone;
82  }
83  if (this->withEMail) {
84  fields << ContactFields::PreferredEmail
85  << ContactFields::Email2;
86  }
87  if (this->withBirthday) {
88  fields << ContactFields::Birthday;
89  }
90 
91  QString content;
92 
93  content += QLatin1String("<html>\n");
94  content += QLatin1String(" <body>\n");
95  content += QLatin1String(" <table style=\"font-size:50%; border-width: 0px; \"width=\"100%\">\n");
96 
97  bool odd = false;
98  foreach ( const KABC::Addressee &contact, contacts ) {
99  // get the values
100  QStringList values;
101  foreach ( const ContactFields::Field &field, fields ) {
102  // we need only values with content
103  QString value = ContactFields::value( field, contact ).trimmed();
104  if ( value.size() > 0 ) {
105  values << value;
106  }
107  }
108 
109  content += QLatin1String(" <tr>\n");
110  QString style = QLatin1String("background-color:");
111  if ( this->withAlternating ) {
112  style += ( odd ) ? this->firstColor.name() : this->secondColor.name();
113  } else {
114  style += QLatin1String("#ffffff");
115  }
116  content += QLatin1String(" <td style=\"")+ style + QLatin1String(";\">") + values.join(QLatin1String("; ")) + QLatin1String("</td>\n");
117  content += QLatin1String(" </tr>\n");
118  odd = !odd;
119  }
120 
121  content += QLatin1String(" </table>\n");
122  content += QLatin1String(" </body>\n");
123  content += QLatin1String("</html>\n");
124 
125  return content;
126 }
127 
128 CompactStyle::CompactStyle( PrintingWizard *parent )
129  : PrintStyle( parent ),
130  mPageSettings( new CompactStyleForm( parent ) )
131 {
132  setPreview( QLatin1String("compact-style.png") );
133  setPreferredSortOptions( ContactFields::FormattedName, Qt::AscendingOrder );
134 
135  addPage( mPageSettings, i18n( "Compact Style" ) );
136 
137  connect( mPageSettings->cbAlternating, SIGNAL(clicked()),
138  this, SLOT(setAlternatingColors()) );
139 
140  // set the controls, with the values in config
141  KConfigGroup config( KGlobal::config(), CompactStyleConfigSectionName );
142 
143  withAlternating = config.readEntry( WithAlternating, true );
144  withHomeAddress = config.readEntry( WithHomeAddress, true );
145  withBusinessAddress = config.readEntry( WithBusinessAddress, false );
146  withBirthday = config.readEntry( WithBirthday, true );
147  withEMail = config.readEntry( WithEMail, true );
148 
149  mPageSettings->cbFirst->setColor( config.readEntry( FirstColor, QColor( 220, 220, 220 ) ) );
150  mPageSettings->cbSecond->setColor( config.readEntry( SecondColor, QColor( 255, 255, 255 ) ) );
151  mPageSettings->cbAlternating->setChecked( withAlternating );
152  mPageSettings->cbWithHomeAddress->setChecked( withHomeAddress );
153  mPageSettings->cbWithBusinessAddress->setChecked( withBusinessAddress );
154  mPageSettings->cbWithBirthday->setChecked( withBirthday );
155  mPageSettings->cbWithEMail->setChecked( withEMail );
156 
157  // set up the color boxes
158  setAlternatingColors();
159 }
160 
161 CompactStyle::~CompactStyle()
162 {
163 }
164 
165 void CompactStyle::print( const KABC::Addressee::List &contacts, PrintProgress *progress )
166 {
167  // from UI to members
168  withAlternating = mPageSettings->cbAlternating->isChecked();
169  firstColor = mPageSettings->cbFirst->color();
170  secondColor = mPageSettings->cbSecond->color();
171  withHomeAddress = mPageSettings->cbWithHomeAddress->isChecked();
172  withBusinessAddress = mPageSettings->cbWithBusinessAddress->isChecked();
173  withBirthday = mPageSettings->cbWithBirthday->isChecked();
174  withEMail = mPageSettings->cbWithEMail->isChecked();
175 
176  // to config
177  KConfigGroup config( KGlobal::config(), CompactStyleConfigSectionName );
178 
179  config.writeEntry( WithAlternating, withAlternating );
180  config.writeEntry( FirstColor, firstColor );
181  config.writeEntry( SecondColor, secondColor );
182  config.writeEntry( WithHomeAddress, withHomeAddress );
183  config.writeEntry( WithBusinessAddress, withBusinessAddress );
184  config.writeEntry( WithBirthday, withBirthday );
185  config.writeEntry( WithEMail, withEMail );
186  config.sync();
187 
188  // print
189  QPrinter *printer = wizard()->printer();
190  printer->setPageMargins( 20, 20, 20, 20, QPrinter::DevicePixel );
191 
192  progress->addMessage( i18n( "Setting up document" ) );
193 
194  const QString html = contactsToHtml( contacts );
195 
196  QTextDocument document;
197  document.setHtml( html );
198 
199  progress->addMessage( i18n( "Printing" ) );
200 
201  document.print( printer );
202 
203  progress->addMessage( i18nc( "Finished printing", "Done" ) );
204 }
205 
206 void CompactStyle::setAlternatingColors()
207 {
208  mPageSettings->cbFirst->setEnabled( mPageSettings->cbAlternating->isChecked() );
209  mPageSettings->lbCbFirst->setEnabled( mPageSettings->cbAlternating->isChecked() );
210  mPageSettings->cbSecond->setEnabled( mPageSettings->cbAlternating->isChecked() );
211  mPageSettings->lbCbSecond->setEnabled( mPageSettings->cbAlternating->isChecked() );
212 }
213 
214 CompactStyleFactory::CompactStyleFactory( PrintingWizard *parent )
215  : PrintStyleFactory( parent )
216 {
217 }
218 
219 PrintStyle *CompactStyleFactory::create() const
220 {
221  return new CompactStyle( mParent );
222 }
223 
224 QString CompactStyleFactory::description() const
225 {
226  return i18n( "Compact Printing Style" );
227 }
228 
229 #include "compactstyle.moc"
KABPrinting::CompactStyleFactory::description
QString description() const
Overload this method to provide a one-liner description for your print style.
Definition: compactstyle.cpp:224
KABPrinting::CompactStyleFactory::create
PrintStyle * create() const
Definition: compactstyle.cpp:219
CompactStyleConfigSectionName
const char * CompactStyleConfigSectionName
Definition: compactstyle.cpp:41
ContactFields::Email2
Definition: contactfields.h:73
KABPrinting::PrintStyleFactory::mParent
PrintingWizard * mParent
Definition: printstyle.h:186
contactfields.h
compactstyle.h
KABPrinting::PrintingWizard
The PrintingWizard combines pages common for all print styles and those provided by the respective st...
Definition: printingwizard.h:55
WithEMail
const char * WithEMail
Definition: compactstyle.cpp:46
SecondColor
const char * SecondColor
Definition: compactstyle.cpp:48
KABPrinting::CompactStyle
Definition: compactstyle.h:33
ContactFields::HomeAddressLocality
Definition: contactfields.h:49
ContactFields::HomePhone
Definition: contactfields.h:63
FirstColor
const char * FirstColor
Definition: compactstyle.cpp:47
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::PrintStyle::addPage
void addPage(QWidget *page, const QString &title)
Adds an additional page to the printing wizard, e.g.
Definition: printstyle.cpp:80
ContactFields::Birthday
Definition: contactfields.h:44
WithBirthday
const char * WithBirthday
Definition: compactstyle.cpp:45
WithBusinessAddress
const char * WithBusinessAddress
Definition: compactstyle.cpp:44
WithAlternating
const char * WithAlternating
Definition: compactstyle.cpp:42
KABPrinting::PrintStyle::setPreview
bool setPreview(const QString &fileName)
Loads the preview image from the kaddressbook data directory.
Definition: printstyle.cpp:56
KABPrinting::PrintStyle
The abstract interface to the PrintingWizards style objects.
Definition: printstyle.h:67
ContactFields::FormattedName
Definition: contactfields.h:36
ContactFields::BusinessAddressStreet
Definition: contactfields.h:55
KABPrinting::CompactStyle::CompactStyle
CompactStyle(PrintingWizard *parent)
Definition: compactstyle.cpp:128
ContactFields::Fields
QVector< Field > Fields
Defines a list of Field enums.
Definition: contactfields.h:95
KABPrinting::CompactStyleFactory::CompactStyleFactory
CompactStyleFactory(PrintingWizard *parent)
Definition: compactstyle.cpp:214
printstyle.h
ContactFields::BusinessPhone
Definition: contactfields.h:64
KABPrinting::CompactStyle::~CompactStyle
~CompactStyle()
Definition: compactstyle.cpp:161
ContactFields::MobilePhone
Definition: contactfields.h:65
ContactFields::BusinessAddressLocality
Definition: contactfields.h:57
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::CompactStyle::print
void print(const KABC::Addressee::List &, PrintProgress *)
prints the contacts
Definition: compactstyle.cpp:165
ContactFields::HomeAddressStreet
Definition: contactfields.h:47
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
WithHomeAddress
const char * WithHomeAddress
Definition: compactstyle.cpp:43
ContactFields::Field
Field
Describes the standard fields that are available for every contact.
Definition: contactfields.h:33
KABPrinting::PrintingWizard::printer
QPrinter * printer()
Returns the printer to use for printing.
Definition: printingwizard.cpp:203
ContactFields::HomeAddressPostalCode
Definition: contactfields.h:51
ContactFields::BusinessAddressPostalCode
Definition: contactfields.h:59
ContactFields::PreferredEmail
Definition: contactfields.h:72
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