kaddressbook

mikesstyle.cpp
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 1996-2002 Mirko Boehm <mirko@kde.org>
4  2002 Mike Pilone <mpilone@slac.com>
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
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 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 TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include <tqpaintdevicemetrics.h>
26 #include <tqpainter.h>
27 
28 #include <tdeabc/addressee.h>
29 #include <tdeapplication.h>
30 #include <kdebug.h>
31 #include <tdeglobal.h>
32 #include <tdelocale.h>
33 #include <kprinter.h>
34 #include <kprogress.h>
35 
36 #include "printingwizard.h"
37 #include "printprogress.h"
38 #include "printstyle.h"
39 
40 #include "mikesstyle.h"
41 
42 using namespace KABPrinting;
43 
44 const int mFieldSpacingHint = 2;
45 
46 MikesStyle::MikesStyle( PrintingWizard *parent, const char *name )
47  : PrintStyle( parent, name )
48 {
49  setPreview( "mike-style.png" );
50 }
51 
52 MikesStyle::~MikesStyle()
53 {
54 }
55 
56 void MikesStyle::print( const TDEABC::Addressee::List &contacts, PrintProgress *progress )
57 {
58  TQFont mFont;
59  TQFont mBoldFont;
60  TQPainter p;
61 
62  p.begin( wizard()->printer() );
63  int yPos = 0, count = 0;
64  int spacingHint = 10;
65 
66  // Now do the actual printing
67  mFont = p.font();
68  mBoldFont = p.font();
69  mBoldFont.setBold( true );
70  TQFontMetrics fm( mFont );
71  TQPaintDeviceMetrics metrics( p.device() );
72 
73  int height = 0;
74  TDEABC::Addressee::List::ConstIterator it;
75 
76  progress->addMessage( i18n( "Preparing" ) );
77  progress->addMessage( i18n( "Printing" ) );
78 
79  for ( it = contacts.begin(); it != contacts.end(); ++it ) {
80  progress->setProgress( (count++ * 100) / contacts.count() );
81  kapp->processEvents();
82 
83  // Get the total height so we know if it will fit on the current page
84  height = calcHeight( *it, mFont, mBoldFont );
85  if ( (yPos + spacingHint + height) > (metrics.height() - fm.height() - 5) ) {
86  p.save();
87  p.translate( 0, metrics.height() - fm.height() - 5 );
88  paintTagLine( p, mFont );
89  p.restore();
90 
91  wizard()->printer()->newPage();
92  yPos = 0;
93  }
94 
95  // Move the painter to the proper position and then paint the addressee
96  yPos += spacingHint;
97  p.save();
98  p.translate( 0, yPos );
99  doPaint( p, *it, height, mFont, mBoldFont );
100  p.restore();
101 
102  yPos += height;
103  }
104 
105  progress->addMessage( i18n( "Done" ) );
106 
107  // print the tag line on the last page
108  p.save();
109  p.translate( 0, metrics.height() - fm.height() - 5 );
110  paintTagLine( p, mFont );
111  p.restore();
112 
113  // send to the printer
114  p.end();
115 }
116 
117 TQString MikesStyle::trimString( const TQString &text, int width, TQFontMetrics &fm )
118 {
119  if ( fm.width( text ) <= width )
120  return text;
121 
122  TQString dots = "...";
123  int dotWidth = fm.width( dots );
124  TQString trimmed;
125  int charNum = 0;
126 
127  while ( fm.width( trimmed ) + dotWidth < width ) {
128  trimmed += text[ charNum ];
129  charNum++;
130  }
131 
132  // Now trim the last char, since it put the width over the top
133  trimmed = trimmed.left( trimmed.length() - 1 );
134  trimmed += dots;
135 
136  return trimmed;
137 }
138 
139 void MikesStyle::doPaint( TQPainter &painter, const TDEABC::Addressee &addr,
140  int maxHeight, const TQFont &font, const TQFont &bFont )
141 {
142  TQFontMetrics fm( font );
143  TQFontMetrics bfm( bFont );
144  TQPaintDeviceMetrics metrics( painter.device() );
145  int margin = 10;
146  int width = metrics.width() - 10;
147  int xPos = 5;
148  int yPos = 0;
149  TQBrush brush( TQt::lightGray );
150 
151  painter.setPen( TQt::black );
152  painter.drawRect( xPos, yPos, width, maxHeight );
153 
154  // The header
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() );
160 
161  yPos += bfm.height() + 2 * mFieldSpacingHint;
162  xPos = margin;
163 
164  // now the fields, in two halves
165  painter.setFont( font );
166 
167  TDEABC::Field::List fields = wizard()->addressBook()->fields();
168  int numFields = fields.count();
169  TQString label;
170  TQString value;
171 
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 );
175 
176  yPos += fm.height();
177  painter.drawText( xPos, yPos, label + ":" );
178 
179  xPos += (width - (2 * margin)) / 4;
180  painter.drawText( xPos, yPos, value );
181 
182  yPos += mFieldSpacingHint;
183  xPos = margin;
184  }
185 
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 );
191 
192  yPos += fm.height();
193  painter.drawText( xPos, yPos, label + ":" );
194 
195  xPos += (width - (2 * margin)) / 4;
196  painter.drawText( xPos, yPos, value );
197 
198  yPos += mFieldSpacingHint;
199  xPos = margin + width / 2;
200  }
201 }
202 
203 void MikesStyle::paintTagLine( TQPainter &p, const TQFont &font )
204 {
205  TQFontMetrics fm( font );
206 
207  TQString text = i18n( "Printed on %1 by KAddressBook (http://www.kde.org)" )
208  .arg( TDEGlobal::locale()->formatDateTime( TQDateTime::currentDateTime() ) );
209 
210  p.setPen( TQt::black );
211  p.drawText( 0, fm.height(), text );
212 }
213 
214 int MikesStyle::calcHeight( const TDEABC::Addressee &addr,
215  const TQFont &font, const TQFont &bFont )
216 {
217  TQFontMetrics fm( font );
218  TQFontMetrics bfm( bFont );
219 
220  int height = 0;
221 
222  // get the fields
223  TDEABC::Field::List fieldList = wizard()->addressBook()->fields();
224  int numFields = fieldList.count();
225  int halfHeight = 0;
226 
227  // Determine which half of the fields is higher
228  for ( int i = 0; i < numFields / 2; i++ )
229  halfHeight += fm.height() * (fieldList[ i ]->value( addr ).contains( '\n' ) + 1);
230 
231  height = halfHeight;
232 
233  // now the second half
234  halfHeight = 0;
235  for ( int i = numFields / 2; i < numFields; i++ )
236  halfHeight += fm.height() * (fieldList[ i ]->value( addr ).contains( '\n' ) + 1);
237 
238  height = TQMAX( height, halfHeight );
239 
240  // Add the title and the spacing
241  height += bfm.height() + ((numFields / 2 + 3) * mFieldSpacingHint);
242 
243  return height;
244 }
245 
246 
247 MikesStyleFactory::MikesStyleFactory( PrintingWizard *parent, const char *name )
248  : PrintStyleFactory( parent, name )
249 {
250 }
251 
252 PrintStyle *MikesStyleFactory::create() const
253 {
254  return new MikesStyle( mParent, mName );
255 }
256 
257 TQString MikesStyleFactory::description() const
258 {
259  return i18n( "Mike's Printing Style" );
260 }
261 
262 #include "mikesstyle.moc"
This defines a simple widget to display print progress information.
Definition: printprogress.h:41
void addMessage(const TQString &)
Add a message to the message log.
void setProgress(int)
Set the progress to a certain amount.
The factories are used to have all object of the respective print style created in one place.
Definition: printstyle.h:152
The class PrintStyle implements the abstract interface to the PrintingWizards style objects.
Definition: printstyle.h:61
The PrintingWizard combines pages common for all print styles and those provided by the respective st...