00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
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
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
00105 p.save();
00106 p.translate( 0, p.device()->height() - fm.height() - 5 );
00107 paintTagLine( p, mFont );
00108 p.restore();
00109
00110
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
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
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
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
00219 KABC::Field::List fieldList = wizard()->addressBook()->fields();
00220 int numFields = fieldList.count();
00221 int halfHeight = 0;
00222
00223
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
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
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"