00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <qpainter.h>
00019 #include <qpaintdevicemetrics.h>
00020
00021 #include <kapplication.h>
00022 #include <kprinter.h>
00023 #include <klocale.h>
00024 #include <kglobal.h>
00025 #include <kdebug.h>
00026 #include <kprogress.h>
00027 #include <kabc/addressee.h>
00028
00029 #include "printingwizard.h"
00030 #include "printstyle.h"
00031 #include "printprogress.h"
00032 #include "mikesstyle.h"
00033
00034 namespace KABPrinting
00035 {
00036
00037 const int mFieldSpacingHint=2;
00038
00039 MikesStyle::MikesStyle(PrintingWizard* parent, const char* name)
00040 : PrintStyle(parent, name)
00041 {
00042 setPreview("mike-style.png");
00043 }
00044
00045 MikesStyle::~MikesStyle()
00046 {
00047 }
00048
00049 void MikesStyle::print(QStringList printUids, PrintProgress *progress)
00050 {
00051 QFont mFont;
00052 QFont mBoldFont;
00053 QPainter p;
00054 p.begin(wizard()->printer());
00055 int yPos = 0, count=0;
00056 int spacingHint = 10;
00057
00058 mFont = p.font();
00059 mBoldFont = p.font();
00060 mBoldFont.setBold(true);
00061 QFontMetrics fm(mFont);
00062 QPaintDeviceMetrics metrics(p.device());
00063
00064 int height = 0;
00065 KABC::Addressee a;
00066 QStringList::ConstIterator iter;
00067
00068 progress->addMessage(i18n("Preparing"));
00069 progress->addMessage(i18n("Printing"));
00070
00071 for (iter = printUids.begin(); iter != printUids.end(); ++iter)
00072 {
00073 progress->setProgress((count++*100)/printUids.count());
00074 kapp->processEvents();
00075
00076
00077 a = wizard()->document()->findByUid(*iter);
00078
00079
00080
00081 height = calcHeight(a, mFont, mBoldFont);
00082 if ((yPos + spacingHint + height)
00083 > (metrics.height()-fm.height()-5))
00084 {
00085 p.save();
00086 p.translate(0, metrics.height()-fm.height()-5);
00087 paintTagLine(p, mFont);
00088 p.restore();
00089
00090 wizard()->printer()->newPage();
00091 yPos = 0;
00092 }
00093
00094
00095
00096 yPos += spacingHint;
00097 p.save();
00098 p.translate(0, yPos);
00099 doPaint(p, a, height, mFont, mBoldFont);
00100 p.restore();
00101 yPos += height;
00102
00103
00104 }
00105 progress->addMessage(i18n("Done"));
00106
00107 p.save();
00108 p.translate(0, metrics.height()-fm.height()-5);
00109 paintTagLine(p, mFont);
00110 p.restore();
00111
00112
00113 p.end();
00114 }
00115
00116 QString MikesStyle::trimString(const QString &text, int width,
00117 QFontMetrics &fm)
00118 {
00119 if (fm.width(text) <= width)
00120 return text;
00121
00122 QString dots = "...";
00123 int dotWidth = fm.width(dots);
00124 QString trimmed;
00125 int charNum = 0;
00126
00127 while (fm.width(trimmed) + dotWidth < width)
00128 {
00129 trimmed += text[charNum];
00130 charNum++;
00131 }
00132
00133
00134 trimmed = trimmed.left(trimmed.length()-1);
00135 trimmed += dots;
00136
00137 return trimmed;
00138 }
00139
00140 void MikesStyle::doPaint(QPainter &painter, const KABC::Addressee &a,
00141 int maxHeight,
00142 const QFont& font, const QFont& bFont)
00143 {
00144 QFontMetrics fm(font);
00145 QFontMetrics bfm(bFont);
00146 QPaintDeviceMetrics metrics(painter.device());
00147 int margin = 10;
00148 int width = metrics.width() - 10;
00149 int xPos = 5;
00150 int yPos = 0;
00151 QBrush brush(Qt::lightGray);
00152
00153 painter.setPen(Qt::black);
00154 painter.drawRect(xPos, yPos, width, maxHeight);
00155
00156
00157 painter.fillRect(xPos+1, yPos+1, width-2,
00158 bfm.height() + 2*mFieldSpacingHint - 2, brush);
00159 painter.setFont(bFont);
00160 xPos += mFieldSpacingHint;
00161 painter.drawText(xPos, yPos+bfm.height(),
00162 a.formattedName());
00163
00164 yPos += bfm.height() + 2*mFieldSpacingHint;
00165 xPos = margin;
00166
00167
00168 painter.setFont(font);
00169
00170 KABC::Field::List fields = wizard()->document()->fields();
00171 int numFields = fields.count();
00172 QString label;
00173 QString value;
00174
00175 for (int i = 0; i < numFields/2; i++)
00176 {
00177 label = fields[i]->label();
00178 value = trimString(fields[i]->value(a), (width-10)/4, fm);
00179
00180 yPos += fm.height();
00181 painter.drawText(xPos, yPos, label + ":");
00182
00183 xPos += (width - (2 * margin))/4;
00184 painter.drawText(xPos, yPos, value);
00185
00186 yPos += mFieldSpacingHint;
00187 xPos = margin;
00188 }
00189
00190 yPos = bfm.height() + 2*mFieldSpacingHint;
00191 xPos = margin + width/2;
00192 for (int i = numFields/2; i < numFields; i++)
00193 {
00194 label = fields[i]->label();
00195 value = value = trimString(fields[i]->value(a), (width-10)/4, fm);
00196
00197 yPos += fm.height();
00198 painter.drawText(xPos, yPos, label + ":");
00199
00200 xPos += (width - (2 * margin))/4;
00201 painter.drawText(xPos, yPos, value);
00202
00203 yPos += mFieldSpacingHint;
00204 xPos = margin + width/2;
00205 }
00206
00207 }
00208
00209 void MikesStyle::paintTagLine(QPainter &p, const QFont& font)
00210 {
00211 QFontMetrics fm(font);
00212
00213 QString text =
00214 i18n("Printed on %1 by KAddressBook (http://www.kde.org)")
00215 .arg(KGlobal::locale()->formatDateTime(QDateTime::currentDateTime()));
00216
00217 p.setPen(Qt::black);
00218 p.drawText(0, fm.height(), text);
00219
00220 }
00221
00222 int MikesStyle::calcHeight(const KABC::Addressee &a,
00223 const QFont& font, const QFont& bFont)
00224 {
00225
00226 QFontMetrics fm(font);
00227 QFontMetrics bfm(bFont);
00228
00229 int height = 0;
00230
00231
00232 KABC::Field::List fieldList = wizard()->document()->fields();
00233 int numFields = fieldList.count();
00234 int halfHeight = 0;
00235
00236
00237 for (int i = 0; i < numFields/2; i++)
00238 {
00239 halfHeight +=
00240 fm.height() * (fieldList[i]->value(a).contains('\n') + 1);
00241 }
00242 height = halfHeight;
00243
00244
00245 halfHeight = 0;
00246 for (int i = numFields/2; i < numFields; i++)
00247 {
00248 halfHeight +=
00249 fm.height() * (fieldList[i]->value(a).contains('\n') + 1);
00250 }
00251
00252 height = QMAX(height, halfHeight);
00253
00254
00255 height += bfm.height() + ((numFields/2 + 3)*mFieldSpacingHint);
00256
00257 return height;
00258
00259 }
00260
00261
00262
00263 MikesStyleFactory::MikesStyleFactory(PrintingWizard* parent,
00264 const char* name)
00265 : PrintStyleFactory(parent, name)
00266 {
00267 }
00268
00269 PrintStyle *MikesStyleFactory::create()
00270 {
00271 return new MikesStyle( mParent, mName );
00272 }
00273
00274 QString MikesStyleFactory::description()
00275 {
00276 return i18n("Mike's Printing Style");
00277 }
00278
00279 }
00280
00281 #include "mikesstyle.moc"
00282