kaddressbook Library API Documentation

look_details.cpp

00001 /* -*- C++ -*-
00002    This file implements the business card look.
00003 
00004    the KDE addressbook
00005 
00006    $ Author: Mirko Boehm $
00007    $ Copyright: (C) 1996-2001, Mirko Boehm $
00008    $ Contact: mirko@kde.org
00009    http://www.kde.org $
00010    $ License: GPL with the following explicit clarification:
00011    This code may be linked against any version of the Qt toolkit
00012    from Troll Tech, Norway. $
00013 
00014    $Revision: 1.3 $
00015 */
00016 
00017 #include "look_details.h"
00018 #include <qdir.h>
00019 #include <qcursor.h>
00020 #include "kabentrypainter.h"
00021 #include "global.h"
00022 #include <kdebug.h>
00023 #include <kglobalsettings.h>
00024 #include <kinstance.h>
00025 #include <kstandarddirs.h>
00026 #include <klocale.h>
00027 #include <kconfig.h>
00028 #include <qpainter.h>
00029 #include <qpopupmenu.h>
00030 
00031 #define GRID 5
00032 
00033 const QString KABDetailedView::BorderedBGDir="kab3part/backgrounds/bordered/";
00034 const QString KABDetailedView::TiledBGDir="kab3part/backgrounds/tiled/";
00035 
00036 KABDetailedView::KABDetailedView(QWidget* parent, const char* name)
00037     : KABBasicLook(parent, name),
00038       epainter(0),
00039       bgStyle(None),
00040       defaultBGColor(white),
00041       headlineBGColor(darkBlue),
00042       headlineTextColor(yellow),
00043       Grid(3),
00044       menuBorderedBG(0),
00045       menuTiledBG(0)
00046 {
00047     KToggleAction** actions[]= {
00048         &actionShowAddresses,
00049         &actionShowEmails,
00050         &actionShowTelephones,
00051         &actionShowURLs
00052     };
00053     QString texts[]= {
00054         i18n("Show Postal Addresses"),
00055         i18n("Show Email Addresses"),
00056         i18n("Show Telephone Numbers"),
00057         i18n("Show Web Pages (URLs)")
00058     };
00059     QFont general=KGlobalSettings::generalFont();
00060     QFont fixed=KGlobalSettings::fixedFont();
00061     QString gfont=general.family();
00062     QString ffont=fixed.family();
00063     int gpointsize=general.pixelSize();
00064     if ( gpointsize == -1 )
00065       gpointsize = general.pointSize();
00066 
00067     int fpointsize=fixed.pixelSize();
00068     if ( fpointsize == -1 )
00069       fpointsize = fixed.pointSize();
00070 
00071     epainter=new KABEntryPainter
00072              (Qt::black, headlineTextColor,
00073               useHeadlineBGColor, headlineBGColor,
00074               QFont(gfont, gpointsize+4, QFont::Bold, true),
00075               QFont(gfont, gpointsize+2, QFont::Bold, true),
00076               QFont(gfont, gpointsize, QFont::Normal, false),
00077               QFont(ffont, fpointsize, QFont::Normal, false),
00078               QFont(gfont, gpointsize, QFont::Normal, false));
00079     const int Size=sizeof(actions)/sizeof(actions[0]);
00080     // ----- create some actions:
00081     for(int count=0; count<Size; ++count)
00082     {
00083         *actions[count]=new KToggleAction(texts[count]);
00084         (*actions[count])->setChecked(true);
00085     }
00086     // ----- we would like to track mouse movement:
00087     setMouseTracking(true);
00088     /*
00089     // ----- find preferred size:
00090     // Since the detailed look does not prefer a size it uses the
00091     // preferred size of the editing look to avoid flickering resizing.
00092     KABEditLook *editlook=new KABEditLook(api, this);
00093     setMinimumSize(editlook->minimumSizeHint());
00094     delete editlook;
00095     */
00096 }
00097 
00098 KABDetailedView::~KABDetailedView()
00099 {
00100     if(epainter!=0) delete epainter;
00101 }
00102 
00103 bool KABDetailedView::getBackground(QString path, QPixmap& image)
00104 {
00105     QMap<QString, QPixmap>::iterator pos;
00106     pos=backgrounds.find(path);
00107     if(pos==backgrounds.end())
00108     { // the image has not been loaded previously
00109         if(image.load(path))
00110         {
00111             backgrounds[path]=image;
00112             return true;
00113         } else {
00114             return false;
00115         }
00116     } else {
00117         // image found in cache
00118         image=pos.data();
00119         return true;
00120     }
00121 }
00122 
00123 void KABDetailedView::paintEvent(QPaintEvent*)
00124 {
00125     const int BorderSpace=Grid;
00126     QPixmap pm(width(), height());
00127     QPainter p;
00128     QRect entryArea=QRect(BorderSpace, Grid, width()-Grid-BorderSpace,
00129                           height()-2*Grid);
00130     p.begin(&pm);
00131     // ----- load the background pattern, or clear the painter:
00132     p.setPen(darkBlue);
00133     p.setBrush(defaultBGColor);
00134     p.drawRect(0, 0, width(), height());
00135     switch(bgStyle)
00136     {
00137     case Tiled:
00138         p.drawTiledPixmap(1, 1, width()-2, height()-2, background);
00139         break;
00140     case Bordered:
00141         p.drawTiledPixmap(1, 1,
00142                           QMIN(width()-2,
00143                                background.width()),
00144                           height()-2, background);
00145         break;
00146     case None: // no BG image defined for this entry:
00147     default:
00148         if(useDefaultBGImage)
00149         {
00150             p.drawTiledPixmap(1, 1, width()-2, height()-2, defaultBGImage);
00151         }
00152         break;
00153     };
00154     p.setViewport(entryArea);
00155     epainter->setShowAddresses(actionShowAddresses->isChecked());
00156     epainter->setShowEmails(actionShowEmails->isChecked());
00157     epainter->setShowTelephones(actionShowTelephones->isChecked());
00158     epainter->setShowURLs(actionShowURLs->isChecked());
00159     epainter->printEntry(current,
00160                          QRect(0, 0, entryArea.width(), entryArea.height()),
00161                          &p);
00162     p.end();
00163     bitBlt(this, 0, 0, &pm);
00164 }
00165 
00166 void KABDetailedView::mouseMoveEvent(QMouseEvent *e)
00167 {
00168     QPoint bias(Grid, Grid);
00169     int rc;
00170     bool hit=false;
00171     // -----
00172     if((rc=epainter->hitsEmail(e->pos()-bias))!=-1)
00173     {
00174         //       kdDebug() << "KABDetailedView::mouseMoveEvent: "
00175         // << "pointer touches email "
00176         //              << rc << endl;
00177         hit=true;
00178     }
00179     else
00180         if((rc=epainter->hitsURLs(e->pos()-bias))!=-1)
00181         {
00182             //  kdDebug() << "KABDetailedView::mouseMoveEvent: "
00183             // << "pointer touches URL "
00184             //            << rc << endl;
00185             hit=true;
00186         }
00187         else
00188             if((rc=epainter->hitsTelephones(e->pos()-bias))!=-1)
00189             {
00190                 //        kdDebug() << "KABDetailedView::mouseMoveEvent: "
00191                 // << "pointer touches telephone no. "
00192                 //                  << rc << endl;
00193                 hit=true;
00194             }
00195             else
00196                 if((rc=epainter->hitsTalkAddresses(e->pos()-bias))!=-1)
00197                 {
00198                     //      kdDebug() << "KABDetailedView::mouseMoveEvent: "
00199                     // << "pointer touches talk address "
00200                     //                << rc << endl;
00201                     hit=true;
00202                 }
00203     if(hit)
00204     {
00205         if(cursor().shape()!=PointingHandCursor)
00206         {
00207             setCursor(PointingHandCursor);
00208         }
00209     } else {
00210         if(cursor().shape()!=ArrowCursor)
00211         {
00212             setCursor(ArrowCursor);
00213         }
00214     }
00215 }
00216 
00217 void KABDetailedView::mousePressEvent(QMouseEvent *e)
00218 {
00219     QPopupMenu menu(this);
00220     QPopupMenu *menuBG=new QPopupMenu(&menu);
00221     menuBorderedBG=new QPopupMenu(&menu);
00222     menuTiledBG=new QPopupMenu(&menu);
00223     menu.insertItem(i18n("Select Background"), menuBG);
00224     menuBG->insertItem(i18n("Bordered Backgrounds"), menuBorderedBG);
00225     menuBG->insertItem(i18n("Tiled Backgrounds"), menuTiledBG);
00226     menu.insertSeparator();
00227     QPoint point=e->pos()-QPoint(Grid, Grid);
00228     int rc;
00229     QStringList dirsBorderedBG, dirsTiledBG;
00230     QDir dir;
00231     // -----
00232     switch(e->button())
00233     {
00234     case QMouseEvent::RightButton:
00235         if(m_ro)
00236         {
00237             menu.setItemEnabled(menu.idAt(0), false);
00238         } else {
00239             // @todo: settings need to be saved in view options
00240             // ----- load background options:
00241             dirsBorderedBG=KGlobal::instance()->dirs()->findDirs
00242                            ("data", BorderedBGDir);
00243             if(dirsBorderedBG.count()>0)
00244             {
00245                 dir.setPath(dirsBorderedBG[0]);
00246                 borders=dir.entryList(QDir::Files);
00247                 for(unsigned count=0; count<borders.count(); ++count)
00248                 {
00249                     menuBorderedBG->insertItem(borders[count], count);
00250                 }
00251                 connect(menuBorderedBG, SIGNAL(activated(int)),
00252                         this, SLOT(slotBorderedBGSelected(int)));
00253             } else {
00254                 menuBG->setItemEnabled(menuBG->idAt(0), false);
00255             }
00256             dirsTiledBG=KGlobal::instance()->dirs()->findDirs
00257                         ("data", TiledBGDir);
00258             if(dirsTiledBG.count()>0)
00259             {
00260                 dir.setPath(dirsTiledBG[0]);
00261                 tiles=dir.entryList(QDir::Files);
00262                 for(unsigned count=0; count<tiles.count(); ++count)
00263                 {
00264                     menuTiledBG->insertItem(tiles[count], count);
00265                 }
00266                 connect(menuTiledBG, SIGNAL(activated(int)),
00267                         this, SLOT(slotTiledBGSelected(int)));
00268             } else {
00269                 menuBG->setItemEnabled(menuBG->idAt(1), false);
00270             }
00271         }
00272         // ----- done, plug actions:
00273         actionShowAddresses->plug(&menu);
00274         actionShowEmails->plug(&menu);
00275         actionShowTelephones->plug(&menu);
00276         actionShowURLs->plug(&menu);
00277         // ----- done, execute menu:
00278         menu.exec(e->globalPos());
00279         break;
00280     case QMouseEvent::LeftButton:
00281         // ----- find whether the pointer touches an email address, URL,
00282         // talk address or telephone number:
00283         if((rc=epainter->hitsEmail(point))!=-1)
00284         {
00285             emit(sendEmail(current.emails()[rc]));
00286             break;
00287         }
00288         if((rc=epainter->hitsURLs(point))!=-1)
00289         {
00290             emit(browse(current.url().prettyURL()));
00291             break;
00292         }
00293         if((rc=epainter->hitsTelephones(point))!=-1)
00294         {
00295             /* emit(call(current.telephone.at(2*rc),
00296                current.telephone.at(2*rc+1))); */
00297             kdDebug() << "KABDetailedView::mousePressEvent: ni (calling)."
00298                       << endl;
00299             break;
00300         }
00301         if((rc=epainter->hitsTalkAddresses(point))!=-1)
00302         {
00303             /* emit(talk(current.talk.at(rc))); */
00304             kdDebug() << "KABDetailedView::mousePressEvent: ni (invoking ktalk)."
00305                       << endl;
00306             break;
00307         }
00308         kdDebug() << "KABDetailedView::mousePressEvent: not over active item."
00309                   << endl;
00310         break;
00311     default:
00312         break;
00313     };
00314     menuBorderedBG=0;
00315     menuTiledBG=0;
00316 }
00317 
00318 void KABDetailedView::setEntry(const KABC::Addressee& e)
00319 {
00320 
00321     BackgroundStyle style=None;
00322     QString dir, file, styleSetting;
00323     KABBasicLook::setEntry(e);
00324     // @todo: preload path and styleSetting with possible preference values
00325     // ----- load the background image:
00326     styleSetting=current.custom("kab", "BackgroundStyle");
00327     style=(BackgroundStyle)styleSetting.toInt();
00328     file=current.custom("kab", "BackgroundImage");
00329     if(!file.isEmpty())
00330     {
00331         switch(style)
00332         {
00333         case Tiled:
00334             dir=TiledBGDir;
00335             break;
00336         case Bordered:
00337             dir=BorderedBGDir;
00338             break;
00339         case None:
00340         default:
00341             break;
00342         }
00343         // ----- path is located under KDEDIR/share:
00344         QStringList dirs;
00345         dirs=KGlobal::instance()->dirs()->findDirs("data", dir);
00346         bgStyle=None;
00347         if(!dirs.isEmpty())
00348         {
00349             unsigned count;
00350             for(count=0; count<dirs.count(); ++count)
00351             {
00352                 QDir folder;
00353                 folder.setPath(dirs[count]);
00354                 file=folder.absPath()+"/"+file;
00355                 if(getBackground(file, background))
00356                 {
00357                     bgStyle=style;
00358                     break;
00359                 }
00360             }
00361             if(count==dirs.count())
00362             {   // not found:
00363                 kdDebug() << "KABDetailedView::setEntry: " << file
00364                           << " not locatable." << endl;
00365             }
00366         }
00367     } else { // no background here:
00368         bgStyle=None;
00369         background.resize(0,0);
00370     }
00371     repaint(false);
00372 }
00373 
00374 void KABDetailedView::slotBorderedBGSelected(int index)
00375 {
00376     if(index>=0 && (unsigned)index<borders.count() && !readonly())
00377     {
00378         // ----- get the selection and make it a full path:
00379         QString path=borders[index];
00380         bgStyle=Bordered;
00381         current.insertCustom("kab", "BackgroundStyle", QString().setNum(bgStyle));
00382         current.insertCustom("kab", "BackgroundImage", path);
00383         setEntry(current);
00384         emit(entryChanged());
00385     }
00386 }
00387 
00388 void KABDetailedView::slotTiledBGSelected(int index)
00389 {
00390     if(index>=0 && (unsigned)index<tiles.count() && !readonly())
00391     {
00392         QString path=tiles[index];
00393         bgStyle=Tiled;
00394         current.insertCustom("kab", "BackgroundStyle", QString().setNum(bgStyle));
00395         current.insertCustom("kab", "BackgroundImage", path);
00396         setEntry(current);
00397         emit(entryChanged());
00398     }
00399 }
00400 
00401 
00402 void KABDetailedView::setReadonly(bool state)
00403 {
00404     KABBasicLook::setReadonly(state);
00405     repaint(false);
00406 }
00407 
00408 void KABDetailedView::configure(KConfig *config)
00409 {
00410     QFont general=KGlobalSettings::generalFont();
00411     QFont fixed=KGlobalSettings::fixedFont();
00412     QString gfont=general.family();
00413     QString ffont=fixed.family();
00414 
00415     int gpointsize=general.pixelSize();
00416     if ( gpointsize == -1 )
00417       gpointsize = general.pointSize();
00418 
00419     int fpointsize=fixed.pixelSize();
00420     if ( fpointsize == -1 )
00421       fpointsize = fixed.pointSize();
00422 
00423     // -----
00424     bool useBGImage=true;
00425     config->setGroup(ConfigView);
00426     // ----- load the default background image:
00427     QString bgImage;
00428     useDefaultBGImage=config->readBoolEntry
00429                       (ConfigView_UseDefaultBackground, useBGImage);
00430     defaultBGColor=config->readColorEntry
00431                    (ConfigView_DefaultBackgroundColor, &white);
00432     bgImage=config->readEntry
00433             (ConfigView_DefaultBackgroundImage,
00434              "konqueror/tiles/kenwimer.png");
00435     if(useDefaultBGImage)
00436     {
00437         unsigned count=0;
00438         QStringList dirs=KGlobal::instance()->dirs()->findDirs("data", "/");
00439         if(!dirs.isEmpty())
00440         {
00441             for(count=0; count<dirs.count(); ++count)
00442             {
00443                 // kdDebug() << "Trying " << dirs[count] + "/" + bgImage << endl;
00444                 if(getBackground(dirs[count] + "/" + bgImage, defaultBGImage))
00445                 {
00446                     break;
00447                 }
00448             }
00449         }
00450         if(count==dirs.count())
00451         {
00452             useDefaultBGImage=getBackground(bgImage, defaultBGImage);
00453             if(!useDefaultBGImage)
00454             {
00455                 kdDebug() << "KABDetailedView::configure: "
00456                           << "default BG image selected, but could not be loaded."
00457                           << endl;
00458             }
00459         }
00460     }
00461 //     kdDebug() << "KABDetailedView::configure: "
00462 //               << "default BG color is " << defaultBGColor.name()
00463 //               << ", default BG image is " << bgImage
00464 //               << ", BG image is "
00465 //               << (useDefaultBGImage ? "in use." : "not used.")
00466 //               << endl;
00467     // ----- default background color:
00468     defaultBGColor=config->readColorEntry
00469                    (ConfigView_DefaultBackgroundColor, &white);
00470     // -----
00471     headlineBGColor=config->readColorEntry
00472                     (ConfigView_HeadlineBGColor, &darkBlue);
00473     headlineTextColor=config->readColorEntry
00474                       (ConfigView_HeadlineTextColor, &yellow);
00475     useHeadlineBGColor=config->readBoolEntry(ConfigView_UseHeadlineBGColor, true);
00476     // -----
00477     if(epainter!=0)
00478     {
00479         delete epainter;
00480         epainter=0;
00481     }
00482     epainter=new KABEntryPainter
00483              (Qt::black, headlineTextColor,
00484               useHeadlineBGColor, headlineBGColor,
00485               QFont(gfont, gpointsize+4, QFont::Bold, true),
00486               QFont(gfont, gpointsize+2, QFont::Bold, true),
00487               QFont(gfont, gpointsize, QFont::Normal, false),
00488               QFont(ffont, fpointsize, QFont::Normal, false),
00489               QFont(gfont, gpointsize, QFont::Normal, false));
00490 }
00491 
00492 
00493 #include "look_details.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:37 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001