kaddressbook Library API Documentation

detailledstyle.cpp

00001 /* -*- C++ -*-
00002    This file implements the detailed print style.
00003 
00004    the KDE addressbook
00005 
00006    $ Author: Mirko Boehm $
00007    $ Copyright: (C) 1996-2002, Mirko Boehm $
00008    $ Contact: mirko@kde.org
00009          http://www.kde.org $
00010    $ License: LGPL 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.11 $
00015 */
00016 
00017 #include <kdebug.h>
00018 #include <kapplication.h>
00019 #include <kstandarddirs.h>
00020 #include <kcolorbutton.h>
00021 #include <klocale.h>
00022 #include <kprinter.h>
00023 #include <kfontcombo.h>
00024 #include <kglobalsettings.h>
00025 #include <knuminput.h>
00026 #include <kdialog.h>
00027 #include <kconfig.h>
00028 #include <qpainter.h>
00029 #include <qcheckbox.h>
00030 #include <qlayout.h>
00031 #include <qpaintdevicemetrics.h>
00032 
00033 #include "detailledstyle.h"
00034 #include "printingwizard.h"
00035 #include "printstyle.h"
00036 #include "printprogress.h"
00037 
00038 // ----- the wizard pages:
00039 #include "ds_appearance.h"
00040 
00041 namespace KABPrinting {
00042 
00043     const char* ConfigSectionName="DetailedPrintStyle";
00044     const char* UseKDEFonts="UseKDEFonts";
00045     const char* HeaderFont="HeaderFont";
00046     const char* HeaderFontSize="HeaderFontSize";
00047     const char* HeadlinesFont="HeadlineFont";
00048     const char* HeadlinesFontSize="HeadlineFontSize";
00049     const char* BodyFont="BodyFont";
00050     const char* BodyFontSize="BodyFontSize";
00051     const char* DetailsFont="DetailsFont";
00052     const char* DetailsFontSize="DetailsFontSize";
00053     const char* FixedFont="FixedFont";
00054     const char* FixedFontSize="FixedFontSize";
00055     const char* ColoredContactHeaders="UseColoredContactHeaders";
00056     const char* ContactHeaderForeColor="ContactHeaderForeColor";
00057     const char* ContactHeaderBGColor="ContactHeaderBGColor";
00058 
00059 
00060     DetailledPrintStyle::DetailledPrintStyle(PrintingWizard* parent,
00061                                              const char* name)
00062         : PrintStyle(parent, name),
00063           mPageAppearance(new AppearancePage(parent, "AppearancePage")),
00064           mEPntr(0),
00065           mPrintProgress(0)
00066     {
00067         KConfig *config;
00068         QFont font;
00069         bool kdeFonts;
00070         QFont standard=KGlobalSettings::generalFont();
00071         QFont fixed=KGlobalSettings::fixedFont();
00072 
00073         setPreview( "detailed-style.png" );
00074 
00075         addPage( mPageAppearance, i18n("Appearance") );
00076         // ----- set some values in the pages:
00077         // ----- now try to load previous settings from the
00078         // configuration file and set the UI items accordingly:
00079         config=kapp->config();
00080         config->setGroup(ConfigSectionName);
00081         //       use KDE fonts box:
00082         kdeFonts=config->readBoolEntry(UseKDEFonts, true);
00083         mPageAppearance->cbStandardFonts->setChecked(kdeFonts);
00084         //       header font:
00085         font=config->readFontEntry(HeaderFont, &standard);
00086         mPageAppearance->kfcHeaderFont->setCurrentFont(font.family());
00087         mPageAppearance->kisbHeaderFontSize->setValue(font.pointSize());
00088         font=config->readFontEntry(HeadlinesFont, &standard);
00089         mPageAppearance->kfcHeadlineFont->setCurrentFont(font.family());
00090         mPageAppearance->kisbHeadlineFontSize->setValue(font.pointSize());
00091         font=config->readFontEntry(BodyFont, &standard);
00092         mPageAppearance->kfcBodyFont->setCurrentFont(font.family());
00093         mPageAppearance->kisbBodyFontSize->setValue(font.pointSize());
00094         font=config->readFontEntry(DetailsFont, &standard);
00095         mPageAppearance->kfcDetailsFont->setCurrentFont(font.family());
00096         mPageAppearance->kisbDetailsFontSize->setValue(font.pointSize());
00097         font=config->readFontEntry(FixedFont, &fixed);
00098         mPageAppearance->kfcFixedFont->setCurrentFont(font.family());
00099         mPageAppearance->kisbFixedFontSize->setValue(font.pointSize());
00100         // ----- contact header design:
00101         mPageAppearance->cbBackgroundColor
00102             ->setChecked(config->readBoolEntry(ColoredContactHeaders, true));
00103         mPageAppearance->kcbHeaderBGColor
00104             ->setColor(config->readColorEntry(ContactHeaderBGColor, &Qt::black));
00105         mPageAppearance->kcbHeaderTextColor
00106             ->setColor(config->readColorEntry(ContactHeaderForeColor, &Qt::white));
00107         // ----- work around Qt Designer's fixed widget spacing:
00108         mPageAppearance->layout()->setMargin(KDialog::marginHint());
00109         mPageAppearance->layout()->setSpacing(KDialog::spacingHint());
00110         // ----- enable finish in our page:
00111     }
00112 
00113     DetailledPrintStyle::~DetailledPrintStyle()
00114     {
00115         if( mEPntr != 0 )
00116           delete mEPntr;
00117         mEPntr = 0;
00118     }
00119 
00120     void DetailledPrintStyle::print(QStringList contacts, PrintProgress *progress)
00121     {
00122         mPrintProgress=progress;
00123         progress->addMessage(i18n("Setting up fonts and colors"));
00124         progress->setProgress(0);
00125         bool useKDEFonts;
00126         KConfig *config;
00127         QFont font;
00128         QColor foreColor=Qt::black;
00129         QColor headerColor=Qt::white;
00130         bool useHeaderColor=true;
00131         QColor backColor=Qt::black;
00132         bool useBGColor;
00133         // save, always available defaults:
00134         QFont header=QFont("Helvetica", 12, QFont::Normal);
00135         QFont headlines=QFont("Helvetica", 12, QFont::Normal, true);
00136         QFont body=QFont("Helvetica", 12, QFont::Normal);
00137         QFont fixed=QFont("Courier", 12, QFont::Normal);
00138         QFont comment=QFont("Helvetica", 10, QFont::Normal);
00139         // ----- store the configuration settings:
00140         config=kapp->config();
00141         config->setGroup(ConfigSectionName);
00142         useKDEFonts=mPageAppearance->cbStandardFonts->isChecked();
00143         config->writeEntry(UseKDEFonts, useKDEFonts);
00144         // ----- read the font and color selections from the wizard pages:
00145         useBGColor=mPageAppearance->cbBackgroundColor->isChecked();
00146         config->writeEntry(ColoredContactHeaders, useBGColor);
00147         if(useBGColor)
00148         { // use colored contact headers, otherwise use plain black and white):
00149             headerColor=mPageAppearance->kcbHeaderTextColor->color();
00150             backColor=mPageAppearance->kcbHeaderBGColor->color();
00151             config->writeEntry(ContactHeaderForeColor, headerColor);
00152             config->writeEntry(ContactHeaderBGColor, backColor);
00153         }
00154         if(mPageAppearance->cbStandardFonts->isChecked())
00155         {
00156             QFont standard=KGlobalSettings::generalFont();
00157             header=standard;
00158             headlines=standard;
00159             body=standard;
00160             fixed=KGlobalSettings::fixedFont();
00161             comment=standard;
00162         } else {
00163             // ----- get header font settings and save for later
00164             // sessions:
00165             header.setFamily(mPageAppearance->kfcHeaderFont->currentText());
00166             header.setPointSize(mPageAppearance->kisbHeaderFontSize->value());
00167             config->writeEntry(HeaderFont, header);
00168             // ----- headlines:
00169             headlines.setFamily(mPageAppearance->kfcHeadlineFont->currentText());
00170             headlines.setPointSize(mPageAppearance->kisbHeadlineFontSize->value());
00171             config->writeEntry(HeadlinesFont, headlines);
00172             // ----- body:
00173             body.setFamily(mPageAppearance->kfcBodyFont->currentText());
00174             body.setPointSize(mPageAppearance->kisbBodyFontSize->value());
00175             config->writeEntry(BodyFont, body);
00176             // ----- details:
00177             comment.setFamily(mPageAppearance->kfcDetailsFont->currentText());
00178             comment.setPointSize(mPageAppearance->kisbDetailsFontSize->value());
00179             config->writeEntry(DetailsFont, comment);
00180             // ----- fixed:
00181             fixed.setFamily(mPageAppearance->kfcFixedFont->currentText());
00182             fixed.setPointSize(mPageAppearance->kisbFixedFontSize->value());
00183             config->writeEntry(FixedFont, fixed);
00184         }
00185         kdDebug() << "DetailledPrintStyle::print: printing using" << endl
00186                   << "    header:   " << header.family() << "("
00187                   << header.pointSize() << ")" << endl
00188                   << "    headline: " << headlines.family() << "("
00189                   << headlines.pointSize() << ")" << endl
00190                   << "    body:     " << body.family() << "("
00191                   << body.pointSize() << ")" << endl
00192                   << "    fixed:    " << fixed.family() << "("
00193                   << fixed.pointSize() << ")" << endl
00194                   << "    comment:  " << comment.family() << "("
00195                   << comment.pointSize() << ")" << endl;
00196 
00197         mEPntr=new KABEntryPainter(foreColor, headerColor, useHeaderColor,
00198                                    backColor,
00199                                    header, headlines, body, fixed, comment);
00200         KPrinter *printer=wizard()->printer();
00201         QPainter painter;
00202         // ----- variables used to define MINIMAL MARGINS entered by the user:
00203         progress->addMessage(i18n("Setting up margins and spacing"));
00204         int marginTop=0,
00205            marginLeft=64, // to allow stapling, need refinement with two-side prints
00206           marginRight=0,
00207          marginBottom=0;
00208         register int left, top, width, height;
00209         // ----- we expect the printer to be set up (it is, before the wizard is started):
00210         painter.begin(printer);
00211         printer->setFullPage(true); // use whole page
00212         QPaintDeviceMetrics metrics(printer);
00213         kdDebug() << "DetailledPrintStyle::print: printing on a "
00214                   << metrics.width() << "x" << metrics.height()
00215                   << " size area," << endl << "   "
00216                   << "margins are "
00217                   << printer->margins().width() << " (left/right) and "
00218                   << printer->margins().height() << " (top/bottom)." << endl;
00219         left=QMAX(printer->margins().width(), marginLeft); // left margin
00220         top=QMAX(printer->margins().height(), marginTop); // top margin
00221         width=metrics.width()-left
00222               -QMAX(printer->margins().width(), marginRight); // page width
00223         height=metrics.height()-top
00224                -QMAX(printer->margins().height(), marginBottom); // page height
00225         // ----- now do the printing:
00226         // this prepares for, like, two-up etc:
00227         painter.setViewport(left, top, width, height);
00228         progress->addMessage(i18n("Printing"));
00229         printEntries(contacts, printer, &painter,
00230                      QRect(0, 0, metrics.width(), metrics.height()));
00231         progress->addMessage(i18n("Done"));
00232         painter.end();
00233         config->sync();
00234     }
00235 
00236     bool DetailledPrintStyle::printEntries(const QStringList& contacts,
00237                                            KPrinter *printer,
00238                                            QPainter *painter,
00239                                            const QRect& window)
00240     {
00241         KABC::Addressee addressee;
00242         QStringList::ConstIterator it;
00243         QRect brect;
00244         int ypos=0, count=0;
00245         // -----
00246         for(it=contacts.begin(); it!=contacts.end(); ++it)
00247         {
00248             addressee=wizard()->document()->findByUid(*it);
00249             if(!addressee.isEmpty())
00250             { // print it:
00251                 kdDebug() << "DetailledPrintStyle::printEntries: printing addressee "
00252                           << addressee.realName() << endl;
00253                 // ----- do a faked print to get the bounding rect:
00254                 if(!mEPntr->printEntry(addressee, window, painter, ypos, true, &brect))
00255                 { // it does not fit on the page beginning at ypos:
00256                     printer->newPage();
00257                     // WORK_TO_DO: this assumes the entry fits on the whole page
00258                     // (dunno how to fix this without being illogical)
00259                     ypos=0;
00260                 }
00261                 mEPntr->printEntry(addressee, window, painter, ypos, false, &brect);
00262                 ypos+=brect.height();
00263             } else {
00264                 kdDebug() << "DetailledPrintStyle::printEntries: strange, addressee "
00265                           << "with UID " << *it << " not available." << endl;
00266             }
00267             // ----- set progress:
00268             mPrintProgress->setProgress((count++*100)/contacts.count());
00269         }
00270         mPrintProgress->setProgress(100);
00271         return true;
00272     }
00273 
00274     DetailledPrintStyleFactory::DetailledPrintStyleFactory(
00275         PrintingWizard* parent,
00276         const char* name)
00277         : PrintStyleFactory(parent, name)
00278     {
00279     }
00280 
00281 
00282     PrintStyle *DetailledPrintStyleFactory::create()
00283     {
00284         return new DetailledPrintStyle( mParent, mName );
00285     }
00286 
00287     QString DetailledPrintStyleFactory::description()
00288     {
00289         return i18n("Detailed Style");
00290     }
00291 
00292 }
00293 
00294 #include "detailledstyle.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:09 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001