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

kaddressbook

mikesstyle.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 1996-2002 Mirko Boehm <mirko@kde.org>
00004                        2002 Mike Pilone <mpilone@slac.com>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include <QtGui/QPainter>
00026 #include <QtGui/QPrinter>
00027 
00028 #include <kabc/addressee.h>
00029 #include <kapplication.h>
00030 #include <kdebug.h>
00031 #include <kglobal.h>
00032 #include <klocale.h>
00033 
00034 #include "printingwizard.h"
00035 #include "printprogress.h"
00036 #include "printstyle.h"
00037 
00038 #include "mikesstyle.h"
00039 
00040 using namespace KABPrinting;
00041 
00042 const int mFieldSpacingHint = 2;
00043 
00044 MikesStyle::MikesStyle( PrintingWizard *parent, const char *name )
00045   : PrintStyle( parent, name )
00046 {
00047   setPreview( "mike-style.png" );
00048 }
00049 
00050 MikesStyle::~MikesStyle()
00051 {
00052 }
00053 
00054 void MikesStyle::print( const KABC::Addressee::List &contacts, PrintProgress *progress )
00055 {
00056   QFont mFont;
00057   QFont mBoldFont;
00058   QPainter p;
00059 
00060   p.begin( wizard()->printer() );
00061   int yPos = 0, count = 0;
00062   int spacingHint = 10;
00063 
00064   // Now do the actual printing
00065   mFont = p.font();
00066   mBoldFont = p.font();
00067   mBoldFont.setBold( true );
00068   QFontMetrics fm( mFont );
00069 
00070   int height = 0;
00071   KABC::Addressee::List::ConstIterator it;
00072 
00073   progress->addMessage( i18n( "Preparing" ) );
00074   progress->addMessage( i18n( "Printing" ) );
00075 
00076   for ( it = contacts.begin(); it != contacts.end(); ++it ) {
00077     progress->setProgress( (count++ * 100) / contacts.count() );
00078     kapp->processEvents();
00079 
00080     // Get the total height so we know if it will fit on the current page
00081     height = calcHeight( *it, mFont, mBoldFont );
00082     if ( (yPos + spacingHint + height) > (p.device()->height() - fm.height() - 5) ) {
00083       p.save();
00084       p.translate( 0, p.device()->height() - fm.height() - 5 );
00085       paintTagLine( p, mFont );
00086       p.restore();
00087 
00088       wizard()->printer()->newPage();
00089       yPos = 0;
00090     }
00091 
00092     // Move the painter to the proper position and then paint the addressee
00093     yPos += spacingHint;
00094     p.save();
00095     p.translate( 0, yPos );
00096     doPaint( p, *it, height, mFont, mBoldFont );
00097     p.restore();
00098 
00099     yPos += height;
00100   }
00101 
00102   progress->addMessage( i18n( "Done" ) );
00103 
00104   // print the tag line on the last page
00105   p.save();
00106   p.translate( 0, p.device()->height() - fm.height() - 5 );
00107   paintTagLine( p, mFont );
00108   p.restore();
00109 
00110   // send to the printer
00111   p.end();
00112 }
00113 
00114 QString MikesStyle::trimString( const QString &text, int width, QFontMetrics &fm )
00115 {
00116   if ( fm.width( text ) <= width )
00117     return text;
00118 
00119   QString dots = "...";
00120   int dotWidth = fm.width( dots );
00121   QString trimmed;
00122   int charNum = 0;
00123 
00124   while ( fm.width( trimmed ) + dotWidth < width ) {
00125     trimmed += text[ charNum ];
00126     charNum++;
00127   }
00128 
00129   // Now trim the last char, since it put the width over the top
00130   trimmed = trimmed.left( trimmed.length() - 1 );
00131   trimmed += dots;
00132 
00133   return trimmed;
00134 }
00135 
00136 void MikesStyle::doPaint( QPainter &painter, const KABC::Addressee &addr,
00137                           int maxHeight, const QFont &font, const QFont &bFont )
00138 {
00139   QFontMetrics fm( font );
00140   QFontMetrics bfm( bFont );
00141   int margin = 10;
00142   int width = painter.device()->width() - 10;
00143   int xPos = 5;
00144   int yPos = 0;
00145   QBrush brush( Qt::lightGray );
00146 
00147   painter.setPen( Qt::black );
00148   painter.drawRect( xPos, yPos, width, maxHeight );
00149 
00150   // The header
00151   painter.fillRect( xPos + 1, yPos + 1, width - 2,
00152                     bfm.height() + 2 * mFieldSpacingHint - 2, brush );
00153   painter.setFont( bFont );
00154   xPos += mFieldSpacingHint;
00155   painter.drawText( xPos, yPos + bfm.height(), addr.formattedName() );
00156 
00157   yPos += bfm.height() + 2 * mFieldSpacingHint;
00158   xPos = margin;
00159 
00160   // now the fields, in two halves
00161   painter.setFont( font );
00162 
00163   KABC::Field::List fields = wizard()->addressBook()->fields();
00164   int numFields = fields.count();
00165   QString label;
00166   QString value;
00167 
00168   for ( int i = 0; i < numFields / 2; ++i ) {
00169     label = fields[ i ]->label();
00170     value = trimString( fields[ i ]->value( addr ), (width - 10) / 4, fm );
00171 
00172     yPos += fm.height();
00173     painter.drawText( xPos, yPos, label + ':' );
00174 
00175     xPos += (width - (2 * margin)) / 4;
00176     painter.drawText( xPos, yPos, value );
00177 
00178     yPos += mFieldSpacingHint;
00179     xPos = margin;
00180   }
00181 
00182   yPos = bfm.height() + 2 * mFieldSpacingHint;
00183   xPos = margin + width / 2;
00184   for ( int i = numFields / 2; i < numFields; ++i ) {
00185     label = fields[ i ]->label();
00186     value = value = trimString( fields[ i ]->value( addr ), (width - 10) / 4, fm );
00187 
00188     yPos += fm.height();
00189     painter.drawText( xPos, yPos, label + ':' );
00190 
00191     xPos += (width - (2 * margin)) / 4;
00192     painter.drawText( xPos, yPos, value );
00193 
00194     yPos += mFieldSpacingHint;
00195     xPos = margin + width / 2;
00196   }
00197 }
00198 
00199 void MikesStyle::paintTagLine( QPainter &p, const QFont &font )
00200 {
00201   QFontMetrics fm( font );
00202 
00203   QString text = i18n( "Printed on %1 by KAddressBook (http://www.kde.org)" ,
00204                    KGlobal::locale()->formatDateTime( QDateTime::currentDateTime() ) );
00205 
00206   p.setPen( Qt::black );
00207   p.drawText( 0, fm.height(), text );
00208 }
00209 
00210 int MikesStyle::calcHeight( const KABC::Addressee &addr,
00211                             const QFont &font, const QFont &bFont )
00212 {
00213   QFontMetrics fm( font );
00214   QFontMetrics bfm( bFont );
00215 
00216   int height = 0;
00217 
00218   // get the fields
00219   KABC::Field::List fieldList = wizard()->addressBook()->fields();
00220   int numFields = fieldList.count();
00221   int halfHeight = 0;
00222 
00223   // Determine which half of the fields is higher
00224   for ( int i = 0; i < numFields / 2; ++i )
00225     halfHeight += fm.height() * (fieldList[ i ]->value( addr ).count( '\n' ) + 1);
00226 
00227   height = halfHeight;
00228 
00229   // now the second half
00230   halfHeight = 0;
00231   for ( int i = numFields / 2; i < numFields; ++i )
00232     halfHeight += fm.height() * (fieldList[ i ]->value( addr ).count( '\n' ) + 1);
00233 
00234   height = qMax( height, halfHeight );
00235 
00236   // Add the title and the spacing
00237   height += bfm.height() + ((numFields / 2 + 3) * mFieldSpacingHint);
00238 
00239   return height;
00240 }
00241 
00242 
00243 MikesStyleFactory::MikesStyleFactory( PrintingWizard *parent, const char *name )
00244   : PrintStyleFactory( parent, name )
00245 {
00246 }
00247 
00248 PrintStyle *MikesStyleFactory::create() const
00249 {
00250   return new MikesStyle( mParent, mName );
00251 }
00252 
00253 QString MikesStyleFactory::description() const
00254 {
00255   return i18n( "Mike's Printing Style" );
00256 }
00257 
00258 #include "mikesstyle.moc"

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal