25 #include <tqpaintdevicemetrics.h>
26 #include <tqpainter.h>
28 #include <tdeabc/addressee.h>
29 #include <tdeapplication.h>
31 #include <tdeglobal.h>
32 #include <tdelocale.h>
34 #include <kprogress.h>
36 #include "printingwizard.h"
37 #include "printprogress.h"
38 #include "printstyle.h"
40 #include "mikesstyle.h"
42 using namespace KABPrinting;
44 const int mFieldSpacingHint = 2;
49 setPreview(
"mike-style.png" );
52 MikesStyle::~MikesStyle()
56 void MikesStyle::print(
const TDEABC::Addressee::List &contacts,
PrintProgress *progress )
62 p.begin( wizard()->printer() );
63 int yPos = 0, count = 0;
69 mBoldFont.setBold(
true );
70 TQFontMetrics fm( mFont );
71 TQPaintDeviceMetrics metrics( p.device() );
74 TDEABC::Addressee::List::ConstIterator it;
79 for ( it = contacts.begin(); it != contacts.end(); ++it ) {
80 progress->
setProgress( (count++ * 100) / contacts.count() );
81 kapp->processEvents();
84 height = calcHeight( *it, mFont, mBoldFont );
85 if ( (yPos + spacingHint + height) > (metrics.height() - fm.height() - 5) ) {
87 p.translate( 0, metrics.height() - fm.height() - 5 );
88 paintTagLine( p, mFont );
91 wizard()->printer()->newPage();
98 p.translate( 0, yPos );
99 doPaint( p, *it, height, mFont, mBoldFont );
109 p.translate( 0, metrics.height() - fm.height() - 5 );
110 paintTagLine( p, mFont );
117 TQString MikesStyle::trimString(
const TQString &text,
int width, TQFontMetrics &fm )
119 if ( fm.width( text ) <= width )
122 TQString dots =
"...";
123 int dotWidth = fm.width( dots );
127 while ( fm.width( trimmed ) + dotWidth < width ) {
128 trimmed += text[ charNum ];
133 trimmed = trimmed.left( trimmed.length() - 1 );
139 void MikesStyle::doPaint( TQPainter &painter,
const TDEABC::Addressee &addr,
140 int maxHeight,
const TQFont &font,
const TQFont &bFont )
142 TQFontMetrics fm( font );
143 TQFontMetrics bfm( bFont );
144 TQPaintDeviceMetrics metrics( painter.device() );
146 int width = metrics.width() - 10;
149 TQBrush brush( TQt::lightGray );
151 painter.setPen( TQt::black );
152 painter.drawRect( xPos, yPos, width, maxHeight );
155 painter.fillRect( xPos + 1, yPos + 1, width - 2,
156 bfm.height() + 2 * mFieldSpacingHint - 2, brush );
157 painter.setFont( bFont );
158 xPos += mFieldSpacingHint;
159 painter.drawText( xPos, yPos + bfm.height(), addr.formattedName() );
161 yPos += bfm.height() + 2 * mFieldSpacingHint;
165 painter.setFont( font );
167 TDEABC::Field::List fields = wizard()->addressBook()->fields();
168 int numFields = fields.count();
172 for (
int i = 0; i < numFields / 2; i++ ) {
173 label = fields[ i ]->label();
174 value = trimString( fields[ i ]->value( addr ), (width - 10) / 4, fm );
177 painter.drawText( xPos, yPos, label +
":" );
179 xPos += (width - (2 * margin)) / 4;
180 painter.drawText( xPos, yPos, value );
182 yPos += mFieldSpacingHint;
186 yPos = bfm.height() + 2 * mFieldSpacingHint;
187 xPos = margin + width / 2;
188 for (
int i = numFields / 2; i < numFields; i++ ) {
189 label = fields[ i ]->label();
190 value = value = trimString( fields[ i ]->value( addr ), (width - 10) / 4, fm );
193 painter.drawText( xPos, yPos, label +
":" );
195 xPos += (width - (2 * margin)) / 4;
196 painter.drawText( xPos, yPos, value );
198 yPos += mFieldSpacingHint;
199 xPos = margin + width / 2;
203 void MikesStyle::paintTagLine( TQPainter &p,
const TQFont &font )
205 TQFontMetrics fm( font );
207 TQString text = i18n(
"Printed on %1 by KAddressBook (http://www.kde.org)" )
208 .arg( TDEGlobal::locale()->formatDateTime( TQDateTime::currentDateTime() ) );
210 p.setPen( TQt::black );
211 p.drawText( 0, fm.height(), text );
214 int MikesStyle::calcHeight(
const TDEABC::Addressee &addr,
215 const TQFont &font,
const TQFont &bFont )
217 TQFontMetrics fm( font );
218 TQFontMetrics bfm( bFont );
223 TDEABC::Field::List fieldList = wizard()->addressBook()->fields();
224 int numFields = fieldList.count();
228 for (
int i = 0; i < numFields / 2; i++ )
229 halfHeight += fm.height() * (fieldList[ i ]->value( addr ).contains(
'\n' ) + 1);
235 for (
int i = numFields / 2; i < numFields; i++ )
236 halfHeight += fm.height() * (fieldList[ i ]->value( addr ).contains(
'\n' ) + 1);
238 height = TQMAX( height, halfHeight );
241 height += bfm.height() + ((numFields / 2 + 3) * mFieldSpacingHint);
247 MikesStyleFactory::MikesStyleFactory(
PrintingWizard *parent,
const char *name )
254 return new MikesStyle( mParent, mName );
257 TQString MikesStyleFactory::description()
const
259 return i18n(
"Mike's Printing Style" );
262 #include "mikesstyle.moc"