krop's commit fixes my problem in a better way, reverting
[kdepim.git] / libkdepim / csshelper.cpp
blob62e59a37ef51d857521956db471a9718a5450cbc
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 csshelper.cpp
4 This file is part of KMail, the KDE mail client.
5 Copyright (c) 2003 Marc Mutz <mutz@kde.org>
7 KMail is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License, version 2, as
9 published by the Free Software Foundation.
11 KMail is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 In addition, as a special exception, the copyright holders give
21 permission to link the code of this program with any edition of
22 the Qt library by Trolltech AS, Norway (or with modified versions
23 of Qt that use the same license as Qt), and distribute linked
24 combinations including the two. You must obey the GNU General
25 Public License in all respects for all of the code used other than
26 Qt. If you modify this file, you may extend this exception to
27 your version of the file, but you are not obligated to do so. If
28 you do not wish to do so, delete this exception statement from
29 your version.
32 #include "csshelper.h"
34 #include <KColorScheme>
35 #include <KConfig>
36 #include <KDebug>
37 #include <KGlobal>
38 #include <KGlobalSettings>
40 #include <QString>
41 #include <QApplication>
42 #include <QPaintDevice>
44 namespace KPIM {
46 namespace {
47 // some QColor manipulators that hide the ugly QColor API w.r.t. HSV:
48 inline QColor darker( const QColor & c ) {
49 int h, s, v;
50 c.getHsv( &h, &s, &v );
51 return QColor::fromHsv( h, s, v*4/5 );
54 inline QColor desaturate( const QColor & c ) {
55 int h, s, v;
56 c.getHsv( &h, &s, &v );
57 return QColor::fromHsv( h, s/8, v );
60 inline QColor fixValue( const QColor & c, int newV ) {
61 int h, s, v;
62 c.getHsv( &h, &s, &v );
63 return QColor::fromHsv( h, s, newV );
66 inline int getValueOf( const QColor & c ) {
67 int h, s, v;
68 c.getHsv( &h, &s, &v );
69 return v;
73 CSSHelper::CSSHelper( const QPaintDevice *pd ) :
74 mShrinkQuotes( false ),
75 mPaintDevice( pd )
77 // initialize with defaults - should match the corresponding application defaults
78 mForegroundColor = QApplication::palette().color( QPalette::Text );
79 mLinkColor = KColorScheme( QPalette::Active, KColorScheme::View ).foreground( KColorScheme::LinkText ).color();
80 mVisitedLinkColor = KColorScheme( QPalette::Active, KColorScheme::View ).foreground( KColorScheme::VisitedText ).color();
81 mBackgroundColor = QApplication::palette().color( QPalette::Base );
82 cHtmlWarning = QColor( 0xFF, 0x40, 0x40 ); // warning frame color: light red
84 cPgpEncrH = QColor( 0x00, 0x80, 0xFF ); // light blue
85 cPgpOk1H = QColor( 0x40, 0xFF, 0x40 ); // light green
86 cPgpOk0H = QColor( 0xFF, 0xFF, 0x40 ); // light yellow
87 cPgpWarnH = QColor( 0xFF, 0xFF, 0x40 ); // light yellow
88 cPgpErrH = Qt::red;
90 for ( int i = 0 ; i < 3 ; ++i )
91 mQuoteColor[i] = QColor( 0x00, 0x80 - i * 0x10, 0x00 ); // shades of green
92 mRecycleQuoteColors = false;
94 QFont defaultFont = KGlobalSettings::generalFont();
95 QFont defaultFixedFont = KGlobalSettings::fixedFont();
96 mBodyFont = mPrintFont = defaultFont;
97 mFixedFont = mFixedPrintFont = defaultFixedFont;
98 defaultFont.setItalic( true );
99 for ( int i = 0 ; i < 3 ; ++i )
100 mQuoteFont[i] = defaultFont;
102 mBackingPixmapOn = false;
104 recalculatePGPColors();
107 void CSSHelper::recalculatePGPColors() {
108 // determine the frame and body color for PGP messages from the header color
109 // if the header color equals the background color then the other colors are
110 // also set to the background color (-> old style PGP message viewing)
111 // else
112 // the brightness of the frame is set to 4/5 of the brightness of the header
113 // and in case of a light background color
114 // the saturation of the body is set to 1/8 of the saturation of the header
115 // while in case of a dark background color
116 // the value of the body is set to the value of the background color
118 // Check whether the user uses a light color scheme
119 const int vBG = getValueOf( mBackgroundColor );
120 const bool lightBG = vBG >= 128;
121 if ( cPgpOk1H == mBackgroundColor ) {
122 cPgpOk1F = mBackgroundColor;
123 cPgpOk1B = mBackgroundColor;
124 } else {
125 cPgpOk1F= darker( cPgpOk1H );
126 cPgpOk1B = lightBG ? desaturate( cPgpOk1H ) : fixValue( cPgpOk1H, vBG );
128 if ( cPgpOk0H == mBackgroundColor ) {
129 cPgpOk0F = mBackgroundColor;
130 cPgpOk0B = mBackgroundColor;
131 } else {
132 cPgpOk0F = darker( cPgpOk0H );
133 cPgpOk0B = lightBG ? desaturate( cPgpOk0H ) : fixValue( cPgpOk0H, vBG );
135 if ( cPgpWarnH == mBackgroundColor ) {
136 cPgpWarnF = mBackgroundColor;
137 cPgpWarnB = mBackgroundColor;
138 } else {
139 cPgpWarnF = darker( cPgpWarnH );
140 cPgpWarnB = lightBG ? desaturate( cPgpWarnH ) : fixValue( cPgpWarnH, vBG );
142 if ( cPgpErrH == mBackgroundColor ) {
143 cPgpErrF = mBackgroundColor;
144 cPgpErrB = mBackgroundColor;
145 } else {
146 cPgpErrF = darker( cPgpErrH );
147 cPgpErrB = lightBG ? desaturate( cPgpErrH ) : fixValue( cPgpErrH, vBG );
149 if ( cPgpEncrH == mBackgroundColor ) {
150 cPgpEncrF = mBackgroundColor;
151 cPgpEncrB = mBackgroundColor;
152 } else {
153 cPgpEncrF = darker( cPgpEncrH );
154 cPgpEncrB = lightBG ? desaturate( cPgpEncrH ) : fixValue( cPgpEncrH, vBG );
158 QString CSSHelper::cssDefinitions( bool fixed ) const {
159 return
160 commonCssDefinitions()
162 "@media screen {\n\n"
164 screenCssDefinitions( this, fixed )
166 "}\n"
167 "@media print {\n\n"
169 printCssDefinitions( fixed )
171 "}\n";
174 QString CSSHelper::htmlHead( bool /*fixed*/ ) const {
175 return
176 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"
177 "<html><head><title></title></head>\n"
178 "<body>\n";
181 QString CSSHelper::quoteFontTag( int level ) const {
182 if ( level < 0 )
183 level = 0;
184 static const int numQuoteLevels = sizeof mQuoteFont / sizeof *mQuoteFont;
185 const int effectiveLevel = mRecycleQuoteColors
186 ? level % numQuoteLevels + 1
187 : qMin( level + 1, numQuoteLevels ) ;
188 if ( level >= numQuoteLevels )
189 return QString( "<div class=\"deepquotelevel%1\">" ).arg( effectiveLevel );
190 else
191 return QString( "<div class=\"quotelevel%1\">" ).arg( effectiveLevel );
194 QString CSSHelper::nonQuotedFontTag() const {
195 return "<div class=\"noquote\">";
198 QFont CSSHelper::bodyFont( bool fixed, bool print ) const {
199 return fixed ? ( print ? mFixedPrintFont : mFixedFont )
200 : ( print ? mPrintFont : mBodyFont );
203 int CSSHelper::fontSize( bool fixed, bool print ) const {
204 return bodyFont( fixed, print ).pointSize();
208 namespace {
209 int pointsToPixel( const QPaintDevice *pd, int pointSize ) {
210 return ( pointSize * pd->logicalDpiY() + 36 ) / 72 ;
214 static const char * const quoteFontSizes[] = { "85", "80", "75" };
216 QString CSSHelper::printCssDefinitions( bool fixed ) const {
217 const QString headerFont = QString( " font-family: \"%1\" ! important;\n"
218 " font-size: %2pt ! important;\n" )
219 .arg( mPrintFont.family() )
220 .arg( mPrintFont.pointSize() );
221 const QPalette &pal = QApplication::palette();
223 const QFont printFont = bodyFont( fixed, true /* print */ );
224 QString quoteCSS;
225 if ( printFont.italic() )
226 quoteCSS += " font-style: italic ! important;\n";
227 if ( printFont.bold() )
228 quoteCSS += " font-weight: bold ! important;\n";
229 if ( !quoteCSS.isEmpty() )
230 quoteCSS = "div.noquote {\n" + quoteCSS + "}\n\n";
232 return
233 QString( "body {\n"
234 " font-family: \"%1\" ! important;\n"
235 " font-size: %2pt ! important;\n"
236 " color: #000000 ! important;\n"
237 " background-color: #ffffff ! important\n"
238 "}\n\n" )
239 .arg( printFont.family(),
240 QString::number( printFont.pointSize() ) )
242 QString( "tr.textAtmH,\n"
243 "tr.signInProgressH,\n"
244 "tr.rfc822H,\n"
245 "tr.encrH,\n"
246 "tr.signOkKeyOkH,\n"
247 "tr.signOkKeyBadH,\n"
248 "tr.signWarnH,\n"
249 "tr.signErrH,\n"
250 "div.header {\n"
251 "%1"
252 "}\n\n"
254 "div.fancy.header > div {\n"
255 " background-color: %2 ! important;\n"
256 " color: %3 ! important;\n"
257 " padding: 4px ! important;\n"
258 " border: solid %3 1px ! important;\n"
259 "}\n\n"
261 "div.fancy.header > div a[href] { color: %3 ! important; }\n\n"
263 "div.fancy.header > table.outer{\n"
264 " background-color: %2 ! important;\n"
265 " color: %3 ! important;\n"
266 " border-bottom: solid %3 1px ! important;\n"
267 " border-left: solid %3 1px ! important;\n"
268 " border-right: solid %3 1px ! important;\n"
269 "}\n\n"
271 "div.spamheader {\n"
272 " display:none ! important;\n"
273 "}\n\n"
275 "div.htmlWarn {\n"
276 " border: 2px solid #ffffff ! important;\n"
277 "}\n\n"
279 "div.senderpic{\n"
280 " font-size:0.8em ! important;\n"
281 " border:1px solid black ! important;\n"
282 " background-color:%2 ! important;\n"
283 "}\n\n"
285 "div.senderstatus{\n"
286 " text-align:center ! important;\n"
287 "}\n\n"
289 "div.noprint {\n"
290 " display:none ! important;\n"
291 "}\n\n"
293 .arg( headerFont,
294 pal.color( QPalette::Background ).name(),
295 pal.color( QPalette::Foreground ).name() )
296 + quoteCSS;
299 QString CSSHelper::screenCssDefinitions( const CSSHelper * helper, bool fixed ) const {
300 const QString fgColor = mForegroundColor.name();
301 const QString bgColor = mBackgroundColor.name();
302 const QString linkColor = mLinkColor.name();
303 const QString headerFont = QString(" font-family: \"%1\" ! important;\n"
304 " font-size: %2px ! important;\n")
305 .arg( mBodyFont.family() )
306 .arg( pointsToPixel( helper->mPaintDevice, mBodyFont.pointSize() ) );
307 const QString background = ( mBackingPixmapOn
308 ? QString( " background-image:url(file://%1) ! important;\n" )
309 .arg( mBackingPixmapStr )
310 : QString( " background-color: %1 ! important;\n" )
311 .arg( bgColor ) );
312 const QString bodyFontSize = QString::number( pointsToPixel( helper->mPaintDevice, fontSize( fixed ) ) ) + "px" ;
313 const QPalette & pal = QApplication::palette();
315 QString quoteCSS;
316 if ( bodyFont( fixed ).italic() )
317 quoteCSS += " font-style: italic ! important;\n";
318 if ( bodyFont( fixed ).bold() )
319 quoteCSS += " font-weight: bold ! important;\n";
320 if ( !quoteCSS.isEmpty() )
321 quoteCSS = "div.noquote {\n" + quoteCSS + "}\n\n";
323 // CSS definitions for quote levels 1-3
324 for ( int i = 0 ; i < 3 ; ++i ) {
325 quoteCSS += QString( "div.quotelevel%1 {\n"
326 " color: %2 ! important;\n" )
327 .arg( QString::number(i+1), mQuoteColor[i].name() );
328 if ( mQuoteFont[i].italic() )
329 quoteCSS += " font-style: italic ! important;\n";
330 if ( mQuoteFont[i].bold() )
331 quoteCSS += " font-weight: bold ! important;\n";
332 if ( mShrinkQuotes )
333 quoteCSS += " font-size: " + QString::fromLatin1( quoteFontSizes[i] )
334 + "% ! important;\n";
335 quoteCSS += "}\n\n";
338 // CSS definitions for quote levels 4+
339 for ( int i = 0 ; i < 3 ; ++i ) {
340 quoteCSS += QString( "div.deepquotelevel%1 {\n"
341 " color: %2 ! important;\n" )
342 .arg( QString::number(i+1), mQuoteColor[i].name() );
343 if ( mQuoteFont[i].italic() )
344 quoteCSS += " font-style: italic ! important;\n";
345 if ( mQuoteFont[i].bold() )
346 quoteCSS += " font-weight: bold ! important;\n";
347 if ( mShrinkQuotes )
348 quoteCSS += " font-size: 70% ! important;\n";
349 quoteCSS += "}\n\n";
352 return
353 QString( "body {\n"
354 " font-family: \"%1\" ! important;\n"
355 " font-size: %2 ! important;\n"
356 " color: %3 ! important;\n"
357 "%4"
358 "}\n\n" )
359 .arg( bodyFont( fixed ).family(),
360 bodyFontSize,
361 fgColor,
362 background )
364 QString( "a {\n"
365 " color: %1 ! important;\n"
366 " text-decoration: none ! important;\n"
367 "}\n\n"
369 "a.white {\n"
370 " color: white ! important;\n"
371 "}\n\n"
373 "a.black {\n"
374 " color: black ! important;\n"
375 "}\n\n"
377 "table.textAtm { background-color: %2 ! important; }\n\n"
379 "tr.textAtmH {\n"
380 " background-color: %3 ! important;\n"
381 "%4"
382 "}\n\n"
384 "tr.textAtmB {\n"
385 " background-color: %3 ! important;\n"
386 "}\n\n"
388 "table.signInProgress,\n"
389 "table.rfc822 {\n"
390 " background-color: %3 ! important;\n"
391 "}\n\n"
393 "tr.signInProgressH,\n"
394 "tr.rfc822H {\n"
395 "%4"
396 "}\n\n" )
397 .arg( linkColor, fgColor, bgColor, headerFont )
399 QString( "table.encr {\n"
400 " background-color: %1 ! important;\n"
401 "}\n\n"
403 "tr.encrH {\n"
404 " background-color: %2 ! important;\n"
405 "%3"
406 "}\n\n"
408 "tr.encrB { background-color: %4 ! important; }\n\n" )
409 .arg( cPgpEncrF.name(),
410 cPgpEncrH.name(),
411 headerFont,
412 cPgpEncrB.name() )
414 QString( "table.signOkKeyOk {\n"
415 " background-color: %1 ! important;\n"
416 "}\n\n"
418 "tr.signOkKeyOkH {\n"
419 " background-color: %2 ! important;\n"
420 "%3"
421 "}\n\n"
423 "tr.signOkKeyOkB { background-color: %4 ! important; }\n\n" )
424 .arg( cPgpOk1F.name(),
425 cPgpOk1H.name(),
426 headerFont,
427 cPgpOk1B.name() )
429 QString( "table.signOkKeyBad {\n"
430 " background-color: %1 ! important;\n"
431 "}\n\n"
433 "tr.signOkKeyBadH {\n"
434 " background-color: %2 ! important;\n"
435 "%3"
436 "}\n\n"
438 "tr.signOkKeyBadB { background-color: %4 ! important; }\n\n" )
439 .arg( cPgpOk0F.name(),
440 cPgpOk0H.name(),
441 headerFont,
442 cPgpOk0B.name() )
444 QString( "table.signWarn {\n"
445 " background-color: %1 ! important;\n"
446 "}\n\n"
448 "tr.signWarnH {\n"
449 " background-color: %2 ! important;\n"
450 "%3"
451 "}\n\n"
453 "tr.signWarnB { background-color: %4 ! important; }\n\n" )
454 .arg( cPgpWarnF.name(),
455 cPgpWarnH.name(),
456 headerFont,
457 cPgpWarnB.name() )
459 QString( "table.signErr {\n"
460 " background-color: %1 ! important;\n"
461 "}\n\n"
463 "tr.signErrH {\n"
464 " background-color: %2 ! important;\n"
465 "%3"
466 "}\n\n"
468 "tr.signErrB { background-color: %4 ! important; }\n\n" )
469 .arg( cPgpErrF.name(),
470 cPgpErrH.name(),
471 headerFont,
472 cPgpErrB.name() )
474 QString( "div.htmlWarn {\n"
475 " border: 2px solid %1 ! important;\n"
476 "}\n\n" )
477 .arg( cHtmlWarning.name() )
479 QString( "div.header {\n"
480 "%1"
481 "}\n\n"
483 "div.fancy.header > div {\n"
484 " background-color: %2 ! important;\n"
485 " color: %3 ! important;\n"
486 " border: solid %4 1px ! important;\n"
487 "}\n\n"
489 "div.fancy.header > div a[href] { color: %3 ! important; }\n\n"
491 "div.fancy.header > div a[href]:hover { text-decoration: underline ! important; }\n\n"
493 "div.fancy.header > div.spamheader {\n"
494 " background-color: #cdcdcd ! important;\n"
495 " border-top: 0px ! important;\n"
496 " padding: 3px ! important;\n"
497 " color: black ! important;\n"
498 " font-weight: bold ! important;\n"
499 " font-size: smaller ! important;\n"
500 "}\n\n"
502 "div.fancy.header > table.outer {\n"
503 " background-color: %5 ! important;\n"
504 " color: %4 ! important;\n"
505 " border-bottom: solid %4 1px ! important;\n"
506 " border-left: solid %4 1px ! important;\n"
507 " border-right: solid %4 1px ! important;\n"
508 "}\n\n"
510 "div.senderpic{\n"
511 " padding: 0px ! important;\n"
512 " font-size:0.8em ! important;\n"
513 " border:1px solid %6 ! important;\n"
514 // FIXME: InfoBackground crashes KHTML
515 //" background-color:InfoBackground ! important;\n"
516 " background-color:%5 ! important;\n"
517 "}\n\n"
519 "div.senderstatus{\n"
520 " text-align:center ! important;\n"
521 "}\n\n"
524 .arg( headerFont )
525 .arg( pal.color( QPalette::Highlight ).name(),
526 pal.color( QPalette::HighlightedText ).name(),
527 pal.color( QPalette::Foreground ).name(),
528 pal.color( QPalette::Background ).name() )
529 .arg( pal.color( QPalette::Mid ).name() )
530 + quoteCSS;
533 QString CSSHelper::commonCssDefinitions() const {
534 return
535 "div.header {\n"
536 " margin-bottom: 10pt ! important;\n"
537 "}\n\n"
539 "table.textAtm {\n"
540 " margin-top: 10pt ! important;\n"
541 " margin-bottom: 10pt ! important;\n"
542 "}\n\n"
544 "tr.textAtmH,\n"
545 "tr.textAtmB,\n"
546 "tr.rfc822B {\n"
547 " font-weight: normal ! important;\n"
548 "}\n\n"
550 "tr.signInProgressH,\n"
551 "tr.rfc822H,\n"
552 "tr.encrH,\n"
553 "tr.signOkKeyOkH,\n"
554 "tr.signOkKeyBadH,\n"
555 "tr.signWarnH,\n"
556 "tr.signErrH {\n"
557 " font-weight: bold ! important;\n"
558 "}\n\n"
560 "tr.textAtmH td,\n"
561 "tr.textAtmB td {\n"
562 " padding: 3px ! important;\n"
563 "}\n\n"
565 "table.rfc822 {\n"
566 " width: 100% ! important;\n"
567 " border: solid 1px black ! important;\n"
568 " margin-top: 10pt ! important;\n"
569 " margin-bottom: 10pt ! important;\n"
570 "}\n\n"
572 "table.textAtm,\n"
573 "table.encr,\n"
574 "table.signWarn,\n"
575 "table.signErr,\n"
576 "table.signOkKeyBad,\n"
577 "table.signOkKeyOk,\n"
578 "table.signInProgress,\n"
579 "div.fancy.header table {\n"
580 " width: 100% ! important;\n"
581 " border-width: 0px ! important;\n"
582 "}\n\n"
584 "div.htmlWarn {\n"
585 " margin: 0px 5% ! important;\n"
586 " padding: 10px ! important;\n"
587 " text-align: left ! important;\n"
588 "}\n\n"
590 "div.fancy.header > div {\n"
591 " font-weight: bold ! important;\n"
592 " padding: 4px ! important;\n"
593 "}\n\n"
595 "div.fancy.header table {\n"
596 " padding: 2px ! important;\n" // ### khtml bug: this is ignored
597 " text-align: left ! important\n"
598 "}\n\n"
600 "div.fancy.header table th {\n"
601 " padding: 0px ! important;\n"
602 " white-space: nowrap ! important;\n"
603 " border-spacing: 0px ! important;\n"
604 " text-align: left ! important;\n"
605 " vertical-align: top ! important;\n"
606 "}\n\n"
608 "div.fancy.header table td {\n"
609 " padding: 0px ! important;\n"
610 " border-spacing: 0px ! important;\n"
611 " text-align: left ! important;\n"
612 " vertical-align: top ! important;\n"
613 " width: 100% ! important;\n"
614 "}\n\n"
616 "span.pimsmileytext {\n"
617 " position: absolute;\n"
618 " top: 0px;\n"
619 " left: 0px;\n"
620 " visibility: hidden;\n"
621 "}\n\n"
623 "img.pimsmileyimg {\n"
624 "}\n\n"
626 "div.quotelevelmark {\n"
627 " position: absolute;\n"
628 " margin-left:-10px;\n"
629 "}\n\n"
634 void CSSHelper::setBodyFont( const QFont& font )
636 mBodyFont = font;
639 void CSSHelper::setPrintFont( const QFont& font )
641 mPrintFont = font;
644 QColor CSSHelper::quoteColor( int level )
646 const int actualLevel = qMin( qMax( level, 0 ), 2 );
647 return mQuoteColor[actualLevel];
650 QColor CSSHelper::pgpWarnColor() const
652 return cPgpWarnH;
655 } // namespace KPIM