kaddressbook

detailledstyle.cpp
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 1996-2002 Mirko Boehm <mirko@kde.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of TQt, and distribute the resulting executable,
21 without including the source code for TQt in the source distribution.
22*/
23
24#include <tdeapplication.h>
25#include <tqcheckbox.h>
26#include <kcolorbutton.h>
27#include <tdeconfig.h>
28#include <kdebug.h>
29#include <kdialog.h>
30#include <tdefontcombo.h>
31#include <tdeglobalsettings.h>
32#include <tqlayout.h>
33#include <tdelocale.h>
34#include <knuminput.h>
35#include <tqpaintdevicemetrics.h>
36#include <tqpainter.h>
37#include <kprinter.h>
38#include <tdestandarddirs.h>
39
40#include "ds_appearance.h"
41#include "printingwizard.h"
42#include "printprogress.h"
43#include "printstyle.h"
44
45#include "detailledstyle.h"
46
47using namespace KABPrinting;
48
49const char *ConfigSectionName = "DetailedPrintStyle";
50const char *UseKDEFonts = "UseKDEFonts";
51const char *HeaderFont = "HeaderFont";
52const char *HeaderFontSize = "HeaderFontSize";
53const char *HeadlinesFont = "HeadlineFont";
54const char *HeadlinesFontSize = "HeadlineFontSize";
55const char *BodyFont = "BodyFont";
56const char *BodyFontSize = "BodyFontSize";
57const char *DetailsFont = "DetailsFont";
58const char *DetailsFontSize = "DetailsFontSize";
59const char *FixedFont = "FixedFont";
60const char *FixedFontSize = "FixedFontSize";
61const char *ColoredContactHeaders = "UseColoredContactHeaders";
62const char *ContactHeaderForeColor = "ContactHeaderForeColor";
63const char *ContactHeaderBGColor = "ContactHeaderBGColor";
64
65
66DetailledPrintStyle::DetailledPrintStyle( PrintingWizard *parent, const char *name )
67 : PrintStyle( parent, name ),
68 mPageAppearance( new AppearancePage( parent, "AppearancePage" ) ),
69 mPainter( 0 ),
70 mPrintProgress( 0 )
71{
72 TDEConfig *config;
73 TQFont font;
74 bool kdeFonts;
75 TQFont standard = TDEGlobalSettings::generalFont();
76 TQFont fixed = TDEGlobalSettings::fixedFont();
77
78 setPreview( "detailed-style.png" );
79
80 addPage( mPageAppearance, i18n( "Detailed Print Style - Appearance" ) );
81
82 config = tdeApp->config();
83 config->setGroup( ConfigSectionName );
84
85 kdeFonts = config->readBoolEntry( UseKDEFonts, true );
86 mPageAppearance->cbStandardFonts->setChecked( kdeFonts );
87
88 font = config->readFontEntry( HeaderFont, &standard );
89 mPageAppearance->kfcHeaderFont->setCurrentFont( font.family() );
90 mPageAppearance->kisbHeaderFontSize->setValue( font.pointSize() );
91
92 font = config->readFontEntry( HeadlinesFont, &standard );
93 mPageAppearance->kfcHeadlineFont->setCurrentFont( font.family() );
94 mPageAppearance->kisbHeadlineFontSize->setValue( font.pointSize() );
95
96 font = config->readFontEntry( BodyFont, &standard );
97 mPageAppearance->kfcBodyFont->setCurrentFont( font.family() );
98 mPageAppearance->kisbBodyFontSize->setValue( font.pointSize() );
99
100 font = config->readFontEntry( DetailsFont, &standard );
101 mPageAppearance->kfcDetailsFont->setCurrentFont( font.family() );
102 mPageAppearance->kisbDetailsFontSize->setValue( font.pointSize() );
103
104 font = config->readFontEntry( FixedFont, &fixed );
105 mPageAppearance->kfcFixedFont->setCurrentFont( font.family() );
106 mPageAppearance->kisbFixedFontSize->setValue( font.pointSize() );
107
108 mPageAppearance->cbBackgroundColor->setChecked(
109 config->readBoolEntry( ColoredContactHeaders, true ) );
110 mPageAppearance->kcbHeaderBGColor->setColor(
111 config->readColorEntry( ContactHeaderBGColor, &TQt::black ) );
112 mPageAppearance->kcbHeaderTextColor->setColor(
113 config->readColorEntry( ContactHeaderForeColor, &TQt::white ) );
114
115 mPageAppearance->layout()->setMargin( KDialog::marginHint() );
116 mPageAppearance->layout()->setSpacing( KDialog::spacingHint() );
117}
118
119DetailledPrintStyle::~DetailledPrintStyle()
120{
121 delete mPainter;
122 mPainter = 0;
123}
124
125void DetailledPrintStyle::print( const TDEABC::Addressee::List &contacts, PrintProgress *progress )
126{
127 mPrintProgress = progress;
128
129 progress->addMessage( i18n( "Setting up fonts and colors" ) );
130 progress->setProgress( 0 );
131
132 bool useKDEFonts;
133 TQFont font;
134 TQColor foreColor = TQt::black;
135 TQColor headerColor = TQt::white;
136 bool useHeaderColor = true;
137 TQColor backColor = TQt::black;
138 bool useBGColor;
139
140 // save, always available defaults:
141 TQFont header = TQFont("Helvetica", 12, TQFont::Normal);
142 TQFont headlines = TQFont("Helvetica", 12, TQFont::Normal, true);
143 TQFont body = TQFont("Helvetica", 12, TQFont::Normal);
144 TQFont fixed = TQFont("Courier", 12, TQFont::Normal);
145 TQFont comment = TQFont("Helvetica", 10, TQFont::Normal);
146
147 // store the configuration settings:
148 TDEConfig *config = tdeApp->config();
149 config->setGroup( ConfigSectionName );
150 useKDEFonts = mPageAppearance->cbStandardFonts->isChecked();
151 config->writeEntry( UseKDEFonts, useKDEFonts );
152
153 // read the font and color selections from the wizard pages:
154 useBGColor=mPageAppearance->cbBackgroundColor->isChecked();
155 config->writeEntry( ColoredContactHeaders, useBGColor );
156
157 // use colored contact headers, otherwise use plain black and white):
158 if ( useBGColor ) {
159 headerColor = mPageAppearance->kcbHeaderTextColor->color();
160 backColor = mPageAppearance->kcbHeaderBGColor->color();
161 config->writeEntry( ContactHeaderForeColor, headerColor );
162 config->writeEntry( ContactHeaderBGColor, backColor );
163 }
164
165 if ( mPageAppearance->cbStandardFonts->isChecked() ) {
166 TQFont standard = TDEGlobalSettings::generalFont();
167 header = standard;
168 headlines = standard;
169 body = standard;
170 fixed = TDEGlobalSettings::fixedFont();
171 comment = standard;
172 } else {
173 header.setFamily( mPageAppearance->kfcHeaderFont->currentText() );
174 header.setPointSize( mPageAppearance->kisbHeaderFontSize->value() );
175 config->writeEntry( HeaderFont, header );
176
177 // headlines:
178 headlines.setFamily( mPageAppearance->kfcHeadlineFont->currentText() );
179 headlines.setPointSize( mPageAppearance->kisbHeadlineFontSize->value() );
180 config->writeEntry( HeadlinesFont, headlines );
181
182 // body:
183 body.setFamily( mPageAppearance->kfcBodyFont->currentText() );
184 body.setPointSize( mPageAppearance->kisbBodyFontSize->value() );
185 config->writeEntry( BodyFont, body );
186
187 // details:
188 comment.setFamily( mPageAppearance->kfcDetailsFont->currentText() );
189 comment.setPointSize( mPageAppearance->kisbDetailsFontSize->value() );
190 config->writeEntry( DetailsFont, comment );
191
192 // fixed:
193 fixed.setFamily( mPageAppearance->kfcFixedFont->currentText() );
194 fixed.setPointSize( mPageAppearance->kisbFixedFontSize->value() );
195 config->writeEntry( FixedFont, fixed );
196 }
197
198 mPainter = new KABEntryPainter;
199 mPainter->setForegroundColor( foreColor );
200 mPainter->setHeaderColor( headerColor );
201 mPainter->setBackgroundColor( backColor );
202 mPainter->setUseHeaderColor( useHeaderColor );
203 mPainter->setHeaderFont( header );
204 mPainter->setHeadLineFont( headlines );
205 mPainter->setBodyFont( body );
206 mPainter->setFixedFont( fixed );
207 mPainter->setCommentFont( comment );
208
209 KPrinter *printer = wizard()->printer();
210
211 TQPainter painter;
212 progress->addMessage( i18n( "Setting up margins and spacing" ) );
213 int marginTop = 0,
214 marginLeft = 64, // to allow stapling, need refinement with two-side prints
215 marginRight = 0,
216 marginBottom = 0;
217
218 int left, top, width, height;
219
220 painter.begin( printer );
221 printer->setFullPage( true ); // use whole page
222
223 TQPaintDeviceMetrics metrics( printer );
224
225 left = TQMAX( printer->margins().width(), marginLeft );
226 top = TQMAX( printer->margins().height(), marginTop );
227 width = metrics.width() - left - TQMAX( printer->margins().width(), marginRight );
228 height = metrics.height() - top - TQMAX( printer->margins().height(), marginBottom );
229
230 painter.setViewport( left, top, width, height );
231 progress->addMessage( i18n( "Printing" ) );
232
233 printEntries( contacts, printer, &painter,
234 TQRect( 0, 0, metrics.width(), metrics.height() ) );
235
236 progress->addMessage( i18n( "Done" ) );
237 painter.end();
238
239 config->sync();
240}
241
242bool DetailledPrintStyle::printEntries( const TDEABC::Addressee::List &contacts,
243 KPrinter *printer,
244 TQPainter *painter,
245 const TQRect &window)
246{
247 TQRect brect;
248 int ypos = 0, count = 0;
249
250 TDEABC::Addressee::List::ConstIterator it;
251 for ( it = contacts.begin(); it != contacts.end(); ++it ) {
252 if ( !(*it).isEmpty() ) {
253 // do a faked print to get the bounding rect:
254 if ( !mPainter->printAddressee( *it, window, painter, ypos, true, &brect) ) {
255 // it does not fit on the page beginning at ypos:
256 printer->newPage();
257
258 // WORK_TO_DO: this assumes the entry fits on the whole page
259 // (dunno how to fix this without being illogical)
260 ypos = 0;
261 }
262
263 mPainter->printAddressee( *it, window, painter, ypos, false, &brect );
264 ypos += brect.height();
265 }
266
267 mPrintProgress->setProgress( (count++ * 100) / contacts.count() );
268 }
269
270 mPrintProgress->setProgress( 100 );
271
272 return true;
273}
274
275DetailledPrintStyleFactory::DetailledPrintStyleFactory( PrintingWizard *parent,
276 const char *name )
277 : PrintStyleFactory( parent, name )
278{
279}
280
281PrintStyle *DetailledPrintStyleFactory::create() const
282{
283 return new DetailledPrintStyle( mParent, mName );
284}
285
286TQString DetailledPrintStyleFactory::description() const
287{
288 return i18n( "Detailed Style" );
289}
290
291#include "detailledstyle.moc"
This defines a simple widget to display print progress information.
Definition: printprogress.h:41
void addMessage(const TQString &)
Add a message to the message log.
void setProgress(int)
Set the progress to a certain amount.
The factories are used to have all object of the respective print style created in one place.
Definition: printstyle.h:152
The class PrintStyle implements the abstract interface to the PrintingWizards style objects.
Definition: printstyle.h:61
The PrintingWizard combines pages common for all print styles and those provided by the respective st...