kaddressbook Library API Documentation

mikesstyle.cpp

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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <qpainter.h> 00026 #include <qpaintdevicemetrics.h> 00027 00028 #include <kapplication.h> 00029 #include <kprinter.h> 00030 #include <klocale.h> 00031 #include <kglobal.h> 00032 #include <kdebug.h> 00033 #include <kprogress.h> 00034 #include <kabc/addressee.h> 00035 00036 #include "printingwizard.h" 00037 #include "printstyle.h" 00038 #include "printprogress.h" 00039 #include "mikesstyle.h" 00040 00041 namespace KABPrinting 00042 { 00043 00044 const int mFieldSpacingHint=2; 00045 00046 MikesStyle::MikesStyle(PrintingWizard* parent, const char* name) 00047 : PrintStyle(parent, name) 00048 { 00049 setPreview("mike-style.png"); 00050 } 00051 00052 MikesStyle::~MikesStyle() 00053 { 00054 } 00055 00056 void MikesStyle::print( KABC::Addressee::List &contacts, PrintProgress *progress) 00057 { 00058 QFont mFont; 00059 QFont mBoldFont; 00060 QPainter p; 00061 p.begin(wizard()->printer()); 00062 int yPos = 0, count=0; 00063 int spacingHint = 10; 00064 // Now do the actual printing 00065 mFont = p.font(); 00066 mBoldFont = p.font(); 00067 mBoldFont.setBold(true); 00068 QFontMetrics fm(mFont); 00069 QPaintDeviceMetrics metrics(p.device()); 00070 00071 int height = 0; 00072 KABC::Addressee::List::Iterator it; 00073 00074 progress->addMessage(i18n("Preparing")); 00075 progress->addMessage(i18n("Printing")); 00076 00077 for ( it = contacts.begin(); it != contacts.end(); ++it ) 00078 { 00079 progress->setProgress((count++*100)/contacts.count()); 00080 kapp->processEvents(); 00081 00082 // Get the total height so we know if it will fit on the 00083 // current page 00084 height = calcHeight((*it), mFont, mBoldFont); 00085 if ((yPos + spacingHint + height) 00086 > (metrics.height()-fm.height()-5)) 00087 { 00088 p.save(); 00089 p.translate(0, metrics.height()-fm.height()-5); 00090 paintTagLine(p, mFont); 00091 p.restore(); 00092 00093 wizard()->printer()->newPage(); 00094 yPos = 0; 00095 } 00096 00097 // Move the painter to the proper position and then paint the 00098 // addressee 00099 yPos += spacingHint; 00100 p.save(); 00101 p.translate(0, yPos); 00102 doPaint(p, (*it), height, mFont, mBoldFont); 00103 p.restore(); 00104 yPos += height; 00105 // ----- set progress bar: 00106 // WORK_TO_DO: port to common progress display scheme 00107 } 00108 progress->addMessage(i18n("Done")); 00109 // print the tag line on the last page 00110 p.save(); 00111 p.translate(0, metrics.height()-fm.height()-5); 00112 paintTagLine(p, mFont); 00113 p.restore(); 00114 00115 // send to the printer 00116 p.end(); 00117 } 00118 00119 QString MikesStyle::trimString(const QString &text, int width, 00120 QFontMetrics &fm) 00121 { 00122 if (fm.width(text) <= width) 00123 return text; 00124 00125 QString dots = "..."; 00126 int dotWidth = fm.width(dots); 00127 QString trimmed; 00128 int charNum = 0; 00129 00130 while (fm.width(trimmed) + dotWidth < width) 00131 { 00132 trimmed += text[charNum]; 00133 charNum++; 00134 } 00135 00136 // Now trim the last char, since it put the width over the top 00137 trimmed = trimmed.left(trimmed.length()-1); 00138 trimmed += dots; 00139 00140 return trimmed; 00141 } 00142 00143 void MikesStyle::doPaint(QPainter &painter, const KABC::Addressee &a, 00144 int maxHeight, 00145 const QFont& font, const QFont& bFont) 00146 { 00147 QFontMetrics fm(font); 00148 QFontMetrics bfm(bFont); 00149 QPaintDeviceMetrics metrics(painter.device()); 00150 int margin = 10; 00151 int width = metrics.width() - 10; 00152 int xPos = 5; 00153 int yPos = 0; 00154 QBrush brush(Qt::lightGray); 00155 00156 painter.setPen(Qt::black); 00157 painter.drawRect(xPos, yPos, width, maxHeight); 00158 00159 // The header 00160 painter.fillRect(xPos+1, yPos+1, width-2, 00161 bfm.height() + 2*mFieldSpacingHint - 2, brush); 00162 painter.setFont(bFont); 00163 xPos += mFieldSpacingHint; 00164 painter.drawText(xPos, yPos+bfm.height(), 00165 a.formattedName()); 00166 00167 yPos += bfm.height() + 2*mFieldSpacingHint; 00168 xPos = margin; 00169 00170 // now the fields, in two halves 00171 painter.setFont(font); 00172 00173 KABC::Field::List fields = wizard()->addressBook()->fields(); 00174 int numFields = fields.count(); 00175 QString label; 00176 QString value; 00177 00178 for (int i = 0; i < numFields/2; i++) 00179 { 00180 label = fields[i]->label(); 00181 value = trimString(fields[i]->value(a), (width-10)/4, fm); 00182 00183 yPos += fm.height(); 00184 painter.drawText(xPos, yPos, label + ":"); 00185 00186 xPos += (width - (2 * margin))/4; 00187 painter.drawText(xPos, yPos, value); 00188 00189 yPos += mFieldSpacingHint; 00190 xPos = margin; 00191 } 00192 00193 yPos = bfm.height() + 2*mFieldSpacingHint; 00194 xPos = margin + width/2; 00195 for (int i = numFields/2; i < numFields; i++) 00196 { 00197 label = fields[i]->label(); 00198 value = value = trimString(fields[i]->value(a), (width-10)/4, fm); 00199 00200 yPos += fm.height(); 00201 painter.drawText(xPos, yPos, label + ":"); 00202 00203 xPos += (width - (2 * margin))/4; 00204 painter.drawText(xPos, yPos, value); 00205 00206 yPos += mFieldSpacingHint; 00207 xPos = margin + width/2; 00208 } 00209 00210 } 00211 00212 void MikesStyle::paintTagLine(QPainter &p, const QFont& font) 00213 { 00214 QFontMetrics fm(font); 00215 00216 QString text = 00217 i18n("Printed on %1 by KAddressBook (http://www.kde.org)") 00218 .arg(KGlobal::locale()->formatDateTime(QDateTime::currentDateTime())); 00219 00220 p.setPen(Qt::black); 00221 p.drawText(0, fm.height(), text); 00222 00223 } 00224 00225 int MikesStyle::calcHeight(const KABC::Addressee &a, 00226 const QFont& font, const QFont& bFont) 00227 { 00228 00229 QFontMetrics fm(font); 00230 QFontMetrics bfm(bFont); 00231 00232 int height = 0; 00233 00234 // get the fields 00235 KABC::Field::List fieldList = wizard()->addressBook()->fields(); 00236 int numFields = fieldList.count(); 00237 int halfHeight = 0; 00238 00239 // Determine which half of the fields is higher 00240 for (int i = 0; i < numFields/2; i++) 00241 { 00242 halfHeight += 00243 fm.height() * (fieldList[i]->value(a).contains('\n') + 1); 00244 } 00245 height = halfHeight; 00246 00247 // now the second half 00248 halfHeight = 0; 00249 for (int i = numFields/2; i < numFields; i++) 00250 { 00251 halfHeight += 00252 fm.height() * (fieldList[i]->value(a).contains('\n') + 1); 00253 } 00254 00255 height = QMAX(height, halfHeight); 00256 00257 // Add the title and the spacing 00258 height += bfm.height() + ((numFields/2 + 3)*mFieldSpacingHint); 00259 00260 return height; 00261 00262 } 00263 00264 // The factory class: 00265 00266 MikesStyleFactory::MikesStyleFactory(PrintingWizard* parent, 00267 const char* name) 00268 : PrintStyleFactory(parent, name) 00269 { 00270 } 00271 00272 PrintStyle *MikesStyleFactory::create() 00273 { 00274 return new MikesStyle( mParent, mName ); 00275 } 00276 00277 QString MikesStyleFactory::description() 00278 { 00279 return i18n("Mike's Printing Style"); 00280 } 00281 00282 } 00283 00284 #include "mikesstyle.moc" 00285
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Oct 1 15:19:05 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003