kaddressbook

detailledstyle.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 1996-2002 Mirko Boehm <mirko@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <kapplication.h>
00025 #include <qcheckbox.h>
00026 #include <kcolorbutton.h>
00027 #include <kconfig.h>
00028 #include <kdebug.h>
00029 #include <kdialog.h>
00030 #include <kfontcombo.h>
00031 #include <kglobalsettings.h>
00032 #include <qlayout.h>
00033 #include <klocale.h>
00034 #include <knuminput.h>
00035 #include <qpaintdevicemetrics.h>
00036 #include <qpainter.h>
00037 #include <kprinter.h>
00038 #include <kstandarddirs.h>
00039 
00040 #include "ds_appearance.h"
00041 #include "printingwizard.h"
00042 #include "printprogress.h"
00043 #include "printstyle.h"
00044 
00045 #include "detailledstyle.h"
00046 
00047 using namespace KABPrinting;
00048 
00049 const char *ConfigSectionName = "DetailedPrintStyle";
00050 const char *UseKDEFonts = "UseKDEFonts";
00051 const char *HeaderFont = "HeaderFont";
00052 const char *HeaderFontSize = "HeaderFontSize";
00053 const char *HeadlinesFont = "HeadlineFont";
00054 const char *HeadlinesFontSize = "HeadlineFontSize";
00055 const char *BodyFont = "BodyFont";
00056 const char *BodyFontSize = "BodyFontSize";
00057 const char *DetailsFont = "DetailsFont";
00058 const char *DetailsFontSize = "DetailsFontSize";
00059 const char *FixedFont = "FixedFont";
00060 const char *FixedFontSize = "FixedFontSize";
00061 const char *ColoredContactHeaders = "UseColoredContactHeaders";
00062 const char *ContactHeaderForeColor = "ContactHeaderForeColor";
00063 const char *ContactHeaderBGColor = "ContactHeaderBGColor";
00064 
00065 
00066 DetailledPrintStyle::DetailledPrintStyle( PrintingWizard *parent, const char *name )
00067   : PrintStyle( parent, name ),
00068     mPageAppearance( new AppearancePage( parent, "AppearancePage" ) ),
00069     mPainter( 0 ),
00070     mPrintProgress( 0 )
00071 {
00072   KConfig *config;
00073   QFont font;
00074   bool kdeFonts;
00075   QFont standard = KGlobalSettings::generalFont();
00076   QFont fixed = KGlobalSettings::fixedFont();
00077 
00078   setPreview( "detailed-style.png" );
00079 
00080   addPage( mPageAppearance, i18n( "Detailed Print Style - Appearance" ) );
00081 
00082   config = kapp->config();
00083   config->setGroup( ConfigSectionName );
00084 
00085   kdeFonts = config->readBoolEntry( UseKDEFonts, true );
00086   mPageAppearance->cbStandardFonts->setChecked( kdeFonts );
00087 
00088   font = config->readFontEntry( HeaderFont, &standard );
00089   mPageAppearance->kfcHeaderFont->setCurrentFont( font.family() );
00090   mPageAppearance->kisbHeaderFontSize->setValue( font.pointSize() );
00091 
00092   font = config->readFontEntry( HeadlinesFont, &standard );
00093   mPageAppearance->kfcHeadlineFont->setCurrentFont( font.family() );
00094   mPageAppearance->kisbHeadlineFontSize->setValue( font.pointSize() );
00095 
00096   font = config->readFontEntry( BodyFont, &standard );
00097   mPageAppearance->kfcBodyFont->setCurrentFont( font.family() );
00098   mPageAppearance->kisbBodyFontSize->setValue( font.pointSize() );
00099 
00100   font = config->readFontEntry( DetailsFont, &standard );
00101   mPageAppearance->kfcDetailsFont->setCurrentFont( font.family() );
00102   mPageAppearance->kisbDetailsFontSize->setValue( font.pointSize() );
00103 
00104   font = config->readFontEntry( FixedFont, &fixed );
00105   mPageAppearance->kfcFixedFont->setCurrentFont( font.family() );
00106   mPageAppearance->kisbFixedFontSize->setValue( font.pointSize() );
00107 
00108   mPageAppearance->cbBackgroundColor->setChecked(
00109       config->readBoolEntry( ColoredContactHeaders, true ) );
00110   mPageAppearance->kcbHeaderBGColor->setColor(
00111       config->readColorEntry( ContactHeaderBGColor, &Qt::black ) );
00112   mPageAppearance->kcbHeaderTextColor->setColor(
00113       config->readColorEntry( ContactHeaderForeColor, &Qt::white ) );
00114 
00115   mPageAppearance->layout()->setMargin( KDialog::marginHint() );
00116   mPageAppearance->layout()->setSpacing( KDialog::spacingHint() );
00117 }
00118 
00119 DetailledPrintStyle::~DetailledPrintStyle()
00120 {
00121   delete mPainter;
00122   mPainter = 0;
00123 }
00124 
00125 void DetailledPrintStyle::print( const KABC::Addressee::List &contacts, PrintProgress *progress )
00126 {
00127   mPrintProgress = progress;
00128 
00129   progress->addMessage( i18n( "Setting up fonts and colors" ) );
00130   progress->setProgress( 0 );
00131 
00132   bool useKDEFonts;
00133   QFont font;
00134   QColor foreColor = Qt::black;
00135   QColor headerColor = Qt::white;
00136   bool useHeaderColor = true;
00137   QColor backColor = Qt::black;
00138   bool useBGColor;
00139 
00140   // save, always available defaults:
00141   QFont header = QFont("Helvetica", 12, QFont::Normal);
00142   QFont headlines = QFont("Helvetica", 12, QFont::Normal, true);
00143   QFont body = QFont("Helvetica", 12, QFont::Normal);
00144   QFont fixed = QFont("Courier", 12, QFont::Normal);
00145   QFont comment = QFont("Helvetica", 10, QFont::Normal);
00146 
00147   // store the configuration settings:
00148   KConfig *config = kapp->config();
00149   config->setGroup( ConfigSectionName );
00150   useKDEFonts = mPageAppearance->cbStandardFonts->isChecked();
00151   config->writeEntry( UseKDEFonts, useKDEFonts );
00152 
00153   // read the font and color selections from the wizard pages:
00154   useBGColor=mPageAppearance->cbBackgroundColor->isChecked();
00155   config->writeEntry( ColoredContactHeaders, useBGColor );
00156 
00157   // use colored contact headers, otherwise use plain black and white):
00158   if ( useBGColor ) {
00159     headerColor = mPageAppearance->kcbHeaderTextColor->color();
00160     backColor = mPageAppearance->kcbHeaderBGColor->color();
00161     config->writeEntry( ContactHeaderForeColor, headerColor );
00162     config->writeEntry( ContactHeaderBGColor, backColor );
00163   }
00164 
00165   if ( mPageAppearance->cbStandardFonts->isChecked() ) {
00166     QFont standard = KGlobalSettings::generalFont();
00167     header = standard;
00168     headlines = standard;
00169     body = standard;
00170     fixed = KGlobalSettings::fixedFont();
00171     comment = standard;
00172   } else {
00173     header.setFamily( mPageAppearance->kfcHeaderFont->currentText() );
00174     header.setPointSize( mPageAppearance->kisbHeaderFontSize->value() );
00175     config->writeEntry( HeaderFont, header );
00176 
00177     // headlines:
00178     headlines.setFamily( mPageAppearance->kfcHeadlineFont->currentText() );
00179     headlines.setPointSize( mPageAppearance->kisbHeadlineFontSize->value() );
00180     config->writeEntry( HeadlinesFont, headlines );
00181 
00182     // body:
00183     body.setFamily( mPageAppearance->kfcBodyFont->currentText() );
00184     body.setPointSize( mPageAppearance->kisbBodyFontSize->value() );
00185     config->writeEntry( BodyFont, body );
00186 
00187     // details:
00188     comment.setFamily( mPageAppearance->kfcDetailsFont->currentText() );
00189     comment.setPointSize( mPageAppearance->kisbDetailsFontSize->value() );
00190     config->writeEntry( DetailsFont, comment );
00191 
00192     // fixed:
00193     fixed.setFamily( mPageAppearance->kfcFixedFont->currentText() );
00194     fixed.setPointSize( mPageAppearance->kisbFixedFontSize->value() );
00195     config->writeEntry( FixedFont, fixed );
00196   }
00197 
00198   mPainter = new KABEntryPainter;
00199   mPainter->setForegroundColor( foreColor );
00200   mPainter->setHeaderColor( headerColor );
00201   mPainter->setBackgroundColor( backColor );
00202   mPainter->setUseHeaderColor( useHeaderColor );
00203   mPainter->setHeaderFont( header );
00204   mPainter->setHeadLineFont( headlines );
00205   mPainter->setBodyFont( body );
00206   mPainter->setFixedFont( fixed );
00207   mPainter->setCommentFont( comment );
00208 
00209   KPrinter *printer = wizard()->printer();
00210 
00211   QPainter painter;
00212   progress->addMessage( i18n( "Setting up margins and spacing" ) );
00213   int marginTop = 0,
00214       marginLeft = 64, // to allow stapling, need refinement with two-side prints
00215       marginRight = 0,
00216       marginBottom = 0;
00217 
00218   register int left, top, width, height;
00219 
00220   painter.begin( printer );
00221   printer->setFullPage( true ); // use whole page
00222 
00223   QPaintDeviceMetrics metrics( printer );
00224 
00225   left = QMAX( printer->margins().width(), marginLeft );
00226   top = QMAX( printer->margins().height(), marginTop );
00227   width = metrics.width() - left - QMAX( printer->margins().width(), marginRight );
00228   height = metrics.height() - top - QMAX( printer->margins().height(), marginBottom );
00229 
00230   painter.setViewport( left, top, width, height );
00231   progress->addMessage( i18n( "Printing" ) );
00232 
00233   printEntries( contacts, printer, &painter,
00234                 QRect( 0, 0, metrics.width(), metrics.height() ) );
00235 
00236   progress->addMessage( i18n( "Done" ) );
00237   painter.end();
00238 
00239   config->sync();
00240 }
00241 
00242 bool DetailledPrintStyle::printEntries( const KABC::Addressee::List &contacts,
00243                                         KPrinter *printer,
00244                                         QPainter *painter,
00245                                         const QRect &window)
00246 {
00247   QRect brect;
00248   int ypos = 0, count = 0;
00249 
00250   KABC::Addressee::List::ConstIterator it;
00251   for ( it = contacts.begin(); it != contacts.end(); ++it ) {
00252     if ( !(*it).isEmpty() ) {
00253       // do a faked print to get the bounding rect:
00254       if ( !mPainter->printAddressee( *it, window, painter, ypos, true, &brect) ) {
00255         // it does not fit on the page beginning at ypos:
00256         printer->newPage();
00257 
00258         // WORK_TO_DO: this assumes the entry fits on the whole page
00259         // (dunno how to fix this without being illogical)
00260         ypos = 0;
00261       }
00262 
00263       mPainter->printAddressee( *it, window, painter, ypos, false, &brect );
00264       ypos += brect.height();
00265     }
00266 
00267     mPrintProgress->setProgress( (count++ * 100) / contacts.count() );
00268   }
00269 
00270   mPrintProgress->setProgress( 100 );
00271 
00272   return true;
00273 }
00274 
00275 DetailledPrintStyleFactory::DetailledPrintStyleFactory( PrintingWizard *parent,
00276                                                         const char *name )
00277   : PrintStyleFactory( parent, name )
00278 {
00279 }
00280 
00281 PrintStyle *DetailledPrintStyleFactory::create() const
00282 {
00283   return new DetailledPrintStyle( mParent, mName );
00284 }
00285 
00286 QString DetailledPrintStyleFactory::description() const
00287 {
00288   return i18n( "Detailed Style" );
00289 }
00290 
00291 #include "detailledstyle.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys