00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kabentrypainter.h"
00020 #include "look_basic.h"
00021
00022 #include <kprinter.h>
00023 #include <qpaintdevicemetrics.h>
00024 #include <qpainter.h>
00025
00026
00027 #include <klocale.h>
00028 #include <kglobal.h>
00029 #include <kdebug.h>
00030 #include <knotifyclient.h>
00031 #include <kurl.h>
00032
00033 KABEntryPainter::KABEntryPainter(QColor foreColor_,
00034 QColor headerColor_,
00035 bool useHeaderColor_,
00036 QColor backColor_,
00037 QFont header_,
00038 QFont headlines_,
00039 QFont body_,
00040 QFont fixed_,
00041 QFont comment_,
00042 bool showAddresses_,
00043 bool showEmails_,
00044 bool showTelephones_,
00045 bool showURLs_)
00046 : foreColor(foreColor_),
00047 headerColor(headerColor_),
00048 useHeaderColor(useHeaderColor_),
00049 backColor(backColor_),
00050 header(header_),
00051 headlines(headlines_),
00052 body(body_),
00053 fixed(fixed_),
00054 comment(comment_),
00055 showAddresses(showAddresses_),
00056 showEmails(showEmails_),
00057 showTelephones(showTelephones_),
00058 showURLs(showURLs_)
00059 {
00060 }
00061
00062 KABEntryPainter::~KABEntryPainter()
00063 {
00064 emails.clear();
00065 telephones.clear();
00066 URLs.clear();
00067 }
00068
00069 void KABEntryPainter::setShowAddresses(bool b)
00070 {
00071 showAddresses=b;
00072 }
00073
00074 void KABEntryPainter::setShowEmails(bool b)
00075 {
00076 showEmails=b;
00077 }
00078
00079 void KABEntryPainter::setShowTelephones(bool b)
00080 {
00081 showTelephones=b;
00082 }
00083
00084 void KABEntryPainter::setShowURLs(bool b)
00085 {
00086 showURLs=b;
00087 }
00088
00089 int KABEntryPainter::hitsEmail(QPoint p)
00090 {
00091 return hits(emails, p);
00092 }
00093
00094 int KABEntryPainter::hitsURLs(QPoint p)
00095 {
00096 return hits(URLs, p);
00097 }
00098 int KABEntryPainter::hitsTelephones(QPoint p)
00099 {
00100 return hits(telephones, p);
00101 }
00102 int KABEntryPainter::hitsTalkAddresses(QPoint p)
00103 {
00104 return hits(talk, p);
00105 }
00106
00107 int KABEntryPainter::hits(const QRectList& list, QPoint p)
00108 {
00109 QRectList::const_iterator pos;
00110 int count=0;
00111
00112 for(pos=list.begin(); pos!=list.end(); ++pos)
00113 {
00114 if((*pos).contains(p))
00115 {
00116 return count;
00117 }
00118 ++count;
00119 }
00120 return -1;
00121 }
00122
00123 bool KABEntryPainter::printEntry(const KABC::Addressee& entry,
00124 const QRect& window,
00125 QPainter *painter,
00126 int top, bool fake, QRect *brect)
00127 {
00128
00129 const int Width=window.width();
00130 const int Height=window.height();
00131 const int Ruler1=Width/32;
00132 const int Ruler2=2*Ruler1;
00133 const int Ruler3=3*Ruler1;
00134 QString text, line1, line2, line3, line4;
00135 QRect rect;
00136
00137 QFontMetrics fmheader(header);
00138 QFontMetrics fmheadline(headlines);
00139 QFontMetrics fmbody(body);
00140 QFontMetrics fmfixed(fixed);
00141 QFontMetrics fmcomment(comment);
00142 int y=top;
00143 KABC::Address address;
00144
00145
00146 QValueList<QStringList> parts;
00147 QValueList<QRectList*> contents;
00148
00149 emails.clear(); telephones.clear(); URLs.clear();
00150
00151
00152 painter->setWindow(window);
00153
00154
00155 painter->setFont(header);
00156 painter->setBrush(QBrush(backColor));
00157 painter->setPen(backColor);
00158 text=entry.realName();
00159
00160 rect=painter->boundingRect(Ruler1, y, Width, Height,
00161 Qt::AlignVCenter | Qt::AlignLeft, text);
00162 rect.setHeight((int)(1.25*rect.height()));
00163 if(!fake && useHeaderColor)
00164 {
00165 painter->drawRect(0, y, Width, rect.height());
00166 }
00167 painter->setPen(useHeaderColor ? headerColor : foreColor);
00168 if(!fake)
00169 {
00170
00171 float ypos=y+((float)rect.height())*0.125;
00172 painter->drawText(Ruler1, (int)ypos, Width, rect.height(),
00173 Qt::AlignVCenter | Qt::AlignLeft, text);
00174 }
00175
00176 QDateTime dt=entry.birthday();
00177 if(dt.isValid())
00178 {
00179 line1=KGlobal::locale()->formatDate(dt.date(), true);
00180 if(!fake)
00181 {
00182
00183 float ypos=y+((float)rect.height())*0.125;
00184 painter->drawText(0, (int)ypos, Width-Ruler1, rect.height(),
00185 Qt::AlignVCenter | Qt::AlignRight, line1);
00186 }
00187 }
00188 y+=rect.height();
00189
00190 painter->setFont(body);
00191 y+=fmbody.lineSpacing()/2;
00192 painter->setPen(foreColor);
00193 if(!entry.prefix().isEmpty())
00194 {
00195 line1=entry.prefix();
00196 line1=line1.stripWhiteSpace();
00197 if(fake)
00198 {
00199 rect=painter->boundingRect(Ruler1, y, Width-Ruler1, Height,
00200 Qt::AlignTop | Qt::AlignLeft,
00201 line1);
00202 } else {
00203 painter->drawText(Ruler1, y, Width-Ruler1, Height,
00204 Qt::AlignTop | Qt::AlignLeft,
00205 line1, -1, &rect);
00206 }
00207 y+=rect.height();
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 if(!(entry.prefix().isEmpty())) {
00228 y+=fmbody.lineSpacing()/2;
00229 }
00230
00231
00232
00233
00234 if(!entry.emails().isEmpty() && showEmails)
00235 {
00236 contents.push_back(&emails);
00237 QStringList list;
00238
00239 list.append(entry.emails().count()==1
00240 ? i18n("Email address:")
00241 : i18n("Email addresses:"));
00242 list+=entry.emails();
00243 parts.push_back(list);
00244 }
00245
00246 KABC::PhoneNumber::List phoneNumbers=entry.phoneNumbers();
00247 if(!phoneNumbers.isEmpty() && showTelephones)
00248 {
00249 contents.push_back(&telephones);
00250 QStringList list;
00251 QString line;
00252
00253 list.append(phoneNumbers.count()==1
00254 ? i18n("Telephone:")
00255 : i18n("Telephones:"));
00256
00257 for(KABC::PhoneNumber::List::Iterator it=phoneNumbers.begin();
00258 it!=phoneNumbers.end();
00259 ++it)
00260 {
00261 line=(*it).typeLabel();
00262 line+=": " + (*it).number();
00263 list.append(line.stripWhiteSpace());
00264 }
00265 parts.push_back(list);
00266 }
00267
00268 if(!entry.url().isEmpty() && entry.url().isValid() && showURLs)
00269 {
00270 contents.push_back(&URLs);
00271 QStringList list;
00272
00273
00274
00275
00276 list.append(i18n("Web page:"));
00277 list+=entry.url().prettyURL();
00278 parts.push_back(list);
00279 }
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 QRect limits[]= {
00296 QRect(0, y, Width/2, Height),
00297 QRect(Width/2, y, Width/2, Height),
00298 QRect(0, y, Width/2, Height),
00299 QRect(Width/2, y, Width/2, Height) };
00300 int heights[4]= { 0, 0, 0, 0 };
00301
00302 QValueList<QStringList>::iterator pos=parts.begin();
00303 QValueList<QRectList*>::iterator rpos=contents.begin();
00304 for(unsigned counter=0; counter<parts.count(); ++counter)
00305 {
00306 const int Offset=counter>1 ? QMAX(heights[0], heights[1]) : 0;
00307 QStringList list=*pos;
00308
00309 painter->setFont(headlines);
00310 if(fake)
00311 {
00312 rect=painter->boundingRect
00313 (limits[counter].left(),
00314 limits[counter].top()+heights[counter]+Offset,
00315 limits[counter].width(),
00316 limits[counter].height(),
00317 Qt::AlignTop | Qt::AlignLeft,
00318 *list.at(0));
00319 } else {
00320 painter->drawText
00321 (limits[counter].left(),
00322 limits[counter].top()+heights[counter]+Offset,
00323 limits[counter].width(),
00324 limits[counter].height(),
00325 Qt::AlignTop | Qt::AlignLeft,
00326 *list.at(0), -1, &rect);
00327 }
00328 heights[counter]+=rect.height();
00329
00330 painter->setFont(fixed);
00331 for(unsigned c2=1; c2<list.count(); ++c2)
00332 {
00333 if(fake)
00334 {
00335 rect=painter->boundingRect
00336 (limits[counter].left()+Ruler1,
00337 limits[counter].top()+heights[counter]+Offset,
00338 limits[counter].width()-Ruler1,
00339 limits[counter].height(),
00340 Qt::AlignTop | Qt::AlignLeft,
00341 *list.at(c2));
00342 } else {
00343 painter->drawText
00344 (limits[counter].left()+Ruler1,
00345 limits[counter].top()+heights[counter]+Offset,
00346 limits[counter].width()-Ruler1,
00347 limits[counter].height(),
00348 Qt::AlignTop | Qt::AlignLeft,
00349 *list.at(c2), -1, &rect);
00350 }
00351 (*rpos)->push_back(rect);
00352 heights[counter]+=rect.height();
00353 }
00354
00355 ++pos;
00356 ++rpos;
00357 }
00358 y=y+QMAX(heights[0], heights[1])+QMAX(heights[2], heights[3]);
00359
00360
00361 KABC::Address::List addresses=entry.addresses();
00362 if(addresses.count()>0 && showAddresses)
00363 {
00364 y+=fmbody.lineSpacing()/2;
00365 painter->setFont(headlines);
00366 if(fake)
00367 {
00368 rect=painter->boundingRect
00369 (0, y, Width, Height,
00370 Qt::AlignTop | Qt::AlignLeft,
00371 addresses.count()==1 ? i18n("Address:") : i18n("Addresses:"));
00372 } else {
00373 painter->drawText(0, y, Width, Height,
00374 Qt::AlignTop | Qt::AlignLeft,
00375 addresses.count()==1
00376 ? i18n("Address:") : i18n("Addresses:"),
00377 -1, &rect);
00378 }
00379 y+=rect.height();
00380 y+=fmbody.lineSpacing()/4;
00381 painter->setFont(body);
00382 for(KABC::Address::List::iterator it=addresses.begin();
00383 it!=addresses.end();
00384 ++it)
00385 {
00386 bool org; org=false;
00387 address=*it;
00388 switch(address.type())
00389 {
00390 case KABC::Address::Dom:
00391 line1=i18n("Domestic Address");
00392 break;
00393 case KABC::Address::Intl:
00394 line1=i18n("International Address");
00395 break;
00396 case KABC::Address::Postal:
00397 line1=i18n("Postal Address");
00398 break;
00399 case KABC::Address::Parcel:
00400 line1=i18n("Parcel Address");
00401 break;
00402 case KABC::Address::Home:
00403 line1=i18n("Home Address");
00404 break;
00405 case KABC::Address::Work:
00406 line1=i18n("Work Address");
00407 break;
00408 case KABC::Address::Pref:
00409 default:
00410 line1=i18n("Preferred Address");
00411 };
00412 line1+=QString::fromLatin1(":");
00413 text=QString::null;
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451 if(!address.extended().isEmpty())
00452 {
00453 text=address.extended();
00454 }
00455 text=text.stripWhiteSpace();
00456 if(!text.isEmpty())
00457 {
00458 line1=line1+QString::fromLatin1(" (")
00459 +text+QString::fromLatin1(")");
00460 }
00461 line1=line1.stripWhiteSpace();
00462 line2=address.street();
00463 if(!address.postOfficeBox().isEmpty())
00464 {
00465 line2+=QString::fromLatin1(" - ") + address.postOfficeBox();
00466 }
00467
00468
00469 line3=address.locality()
00470 +(address.region().isEmpty()
00471 ? QString::fromLatin1("")
00472 : QString::fromLatin1(", ")+address.region())
00473 +(address.postalCode().isEmpty()
00474 ? QString::fromLatin1("")
00475 : QString::fromLatin1(" ")+address.postalCode());
00476 line4=address.country();
00477
00478
00479
00480
00481
00482
00483
00484 if(fake)
00485 {
00486 rect=painter->boundingRect(Ruler1, y, Width-Ruler1, Height,
00487 Qt::AlignTop | Qt::AlignLeft,
00488 line1);
00489 } else {
00490 painter->drawText(Ruler1, y, Width-Ruler1, Height,
00491 Qt::AlignTop | Qt::AlignLeft,
00492 line1, -1, &rect);
00493 }
00494 y+=rect.height();
00495 if(!line2.isEmpty())
00496 {
00497 if(fake)
00498 {
00499 rect=painter->boundingRect(Ruler2, y, Width-Ruler2, Height,
00500 Qt::AlignTop | Qt::AlignLeft,
00501 line2);
00502 } else {
00503 painter->drawText(Ruler2, y, Width-Ruler2, Height,
00504 Qt::AlignTop | Qt::AlignLeft,
00505 line2, -1, &rect);
00506 }
00507 y+=rect.height();
00508 }
00509 if(!line3.isEmpty())
00510 {
00511 if(fake)
00512 {
00513 rect=painter->boundingRect(Ruler2, y, Width-Ruler2, Height,
00514 Qt::AlignTop | Qt::AlignLeft,
00515 line3);
00516 } else {
00517 painter->drawText(Ruler2, y, Width-Ruler2, Height,
00518 Qt::AlignTop | Qt::AlignLeft,
00519 line3, -1, &rect);
00520 }
00521 y+=rect.height();
00522 }
00523 if(!line4.isEmpty())
00524 {
00525 if(fake)
00526 {
00527 rect=painter->boundingRect(Ruler2, y, Width-Ruler2, Height,
00528 Qt::AlignTop | Qt::AlignLeft,
00529 line4);
00530 } else {
00531 painter->drawText(Ruler2, y, Width-Ruler2, Height,
00532 Qt::AlignTop | Qt::AlignLeft,
00533 line4, -1, &rect);
00534 }
00535 y+=rect.height();
00536 }
00537 y+=fmbody.lineSpacing()/4;
00538 if(!address.label().isEmpty())
00539 {
00540 if(fake)
00541 {
00542 rect=painter->boundingRect(Ruler2, y, Width-Ruler2, Height,
00543 Qt::AlignTop | Qt::AlignLeft,
00544 i18n("(Deliver to:)"));
00545 } else {
00546 painter->drawText(Ruler2, y, Width-Ruler2, Height,
00547 Qt::AlignTop | Qt::AlignLeft,
00548 i18n("(Deliver to:)"), -1, &rect);
00549 }
00550 y+=rect.height();
00551 y+=fmbody.lineSpacing()/4;
00552 if(fake)
00553 {
00554 rect=painter->boundingRect(Ruler3, y, Width-Ruler3, Height,
00555 Qt::AlignTop | Qt::AlignLeft,
00556 address.label());
00557 } else {
00558 painter->drawText(Ruler3, y, Width-Ruler3, Height,
00559 Qt::AlignTop | Qt::AlignLeft,
00560 address.label(), -1, &rect);
00561 }
00562 y+=rect.height();
00563 y+=fmbody.lineSpacing()/2;
00564 }
00565 }
00566 }
00567 if(!entry.note().isEmpty())
00568 {
00569 painter->setFont(comment);
00570 y+=fmbody.lineSpacing()/2;
00571 if(fake)
00572 {
00573 rect=painter->boundingRect(0, y, Width, Height,
00574 Qt::AlignTop | Qt::AlignLeft | Qt::WordBreak,
00575 entry.note());
00576 } else {
00577 painter->drawText(0, y, Width, Height,
00578 Qt::AlignTop | Qt::AlignLeft | Qt::WordBreak,
00579 entry.note(), -1, &rect);
00580 }
00581 y+=rect.height();
00582 }
00583 y+=fmbody.lineSpacing()/2;
00584
00585 if(brect!=0)
00586 {
00587 *brect=QRect(0, top, Width, y-top);
00588 }
00589 if(y<Height)
00590 {
00591 return true;
00592 } else {
00593 return false;
00594 }
00595 return true;
00596 }
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756