Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / konqueror / settings / konqhtml / appearance.cpp
blob5b694729bbda81e9cfc13d1238d61cbdb79e223b
2 #include <QtGui/QLabel>
3 #include <QtGui/QLayout>
4 #include <QtGui/QLineEdit>
5 #include <QtGui/QFontComboBox>
6 #include <QtDBus/QDBusMessage>
7 #include <QtDBus/QDBusConnection>
8 #include <QtGui/QGroupBox>
10 #include <kapplication.h>
11 #include <kcharsets.h>
12 #include <ksharedconfig.h>
13 #include <kdebug.h>
14 #include <kdialog.h>
15 #include <kglobal.h>
16 #include <khtmldefaults.h>
17 #include <klocale.h>
18 #include <knuminput.h>
19 #include <kglobalsettings.h>
20 #include <KPluginFactory>
21 #include <KPluginLoader>
23 #if defined Q_WS_X11 && !defined K_WS_QTONLY
24 #include <X11/Xlib.h>
25 #endif
28 #include "appearance.moc"
30 K_PLUGIN_FACTORY_DECLARATION(KcmKonqHtmlFactory)
32 KAppearanceOptions::KAppearanceOptions(QWidget *parent, const QVariantList&)
33 : KCModule( KcmKonqHtmlFactory::componentData(), parent ), m_groupname("HTML Settings"),
34 fSize( 10 ), fMinSize( HTML_DEFAULT_MIN_FONT_SIZE )
37 m_pConfig = KSharedConfig::openConfig("konquerorrc", KConfig::NoGlobals);
38 setQuickHelp( i18n("<h1>Konqueror Fonts</h1>On this page, you can configure "
39 "which fonts Konqueror should use to display the web "
40 "pages you view."));
42 QString wtstr;
43 //initialise fonts list otherwise it crashs
44 while (fonts.count() < 7)
45 fonts.append(QString());
47 QGridLayout *lay = new QGridLayout(this);
48 lay->setSpacing(KDialog::spacingHint());
49 int r = 0;
50 int E = 0, M = 1, W = 3; //CT 3 (instead 2) allows smaller color buttons
52 QGroupBox* gb = new QGroupBox( i18n("Font Si&ze"));
53 QVBoxLayout *laygroup1 = new QVBoxLayout;
55 gb->setLayout(laygroup1);
56 gb->setWhatsThis( i18n("This is the relative font size Konqueror uses to display web sites.") );
58 m_minSize = new KIntNumInput( fMinSize);
59 m_minSize->setLabel( i18n( "M&inimum font size:" ) );
60 m_minSize->setRange( 2, 30 );
61 laygroup1->addWidget(m_minSize);
62 connect( m_minSize, SIGNAL( valueChanged( int ) ), this, SLOT( slotMinimumFontSize( int ) ) );
63 connect( m_minSize, SIGNAL( valueChanged( int ) ), this, SLOT( changed() ) );
64 m_minSize->setWhatsThis( i18n( "Konqueror will never display text smaller than "
65 "this size,<br />overriding any other settings" ) );
67 m_MedSize = new KIntNumInput( fSize,m_minSize );
68 laygroup1->addWidget(m_MedSize);
69 m_MedSize->setLabel( i18n( "&Medium font size:" ) );
70 m_MedSize->setRange( 2, 30 );
71 connect( m_MedSize, SIGNAL( valueChanged( int ) ), this, SLOT( slotFontSize( int ) ) );
72 connect( m_MedSize, SIGNAL( valueChanged( int ) ), this, SLOT( changed() ) );
73 m_MedSize->setWhatsThis(
74 i18n("This is the relative font size Konqueror uses "
75 "to display web sites.") );
77 lay->addWidget(gb, r, E, 1, W- E+1);
78 QLabel* label = new QLabel( i18n("S&tandard font:"), this );
79 lay->addWidget( label , ++r, E);
81 m_pFonts[0] = new QFontComboBox( this );
83 label->setBuddy( m_pFonts[0] );
84 lay->addWidget(m_pFonts[0], r, M, 1, W- M+1);
86 wtstr = i18n("This is the font used to display normal text in a web page.");
87 label->setWhatsThis( wtstr );
88 m_pFonts[0]->setWhatsThis( wtstr );
90 connect( m_pFonts[0], SIGNAL( currentFontChanged(const QFont&) ),
91 SLOT( slotStandardFont(const QFont&) ) );
93 label = new QLabel( i18n( "&Fixed font:"), this );
94 lay->addWidget( label, ++r, E );
96 m_pFonts[1] = new QFontComboBox( this );
98 label->setBuddy( m_pFonts[1] );
99 lay->addWidget(m_pFonts[1], r, M, 1, W- M+1);
101 wtstr = i18n("This is the font used to display fixed-width (i.e. non-proportional) text.");
102 label->setWhatsThis( wtstr );
103 m_pFonts[1]->setWhatsThis( wtstr );
105 connect( m_pFonts[1], SIGNAL( currentFontChanged(const QFont&) ),
106 SLOT(slotFixedFont(const QFont&) ) );
108 label = new QLabel( i18n( "S&erif font:" ), this );
109 lay->addWidget( label, ++r, E );
111 m_pFonts[2] = new QFontComboBox( this );
113 label->setBuddy( m_pFonts[2] );
114 lay->addWidget( m_pFonts[2], r, M, 1, W - M+1);
116 wtstr= i18n( "This is the font used to display text that is marked up as serif." );
117 label->setWhatsThis( wtstr );
118 m_pFonts[2]->setWhatsThis( wtstr );
120 connect( m_pFonts[2], SIGNAL( currentFontChanged(const QFont&) ),
121 SLOT(slotSerifFont(const QFont&) ) );
123 label = new QLabel( i18n( "Sa&ns serif font:" ), this );
124 lay->addWidget( label, ++r, E );
126 m_pFonts[3] = new QFontComboBox( this );
128 label->setBuddy( m_pFonts[3] );
129 lay->addWidget( m_pFonts[3], r, M, 1, W - M+1);
131 wtstr= i18n( "This is the font used to display text that is marked up as sans-serif." );
132 label->setWhatsThis( wtstr );
133 m_pFonts[3]->setWhatsThis( wtstr );
135 connect( m_pFonts[3], SIGNAL( currentFontChanged(const QFont&) ),
136 SLOT(slotSansSerifFont(const QFont&) ) );
138 label = new QLabel( i18n( "C&ursive font:" ), this );
139 lay->addWidget( label, ++r, E );
141 m_pFonts[4] = new QFontComboBox( this );
143 label->setBuddy( m_pFonts[4] );
144 lay->addWidget( m_pFonts[4], r, M, 1, W - M+1);
146 wtstr= i18n( "This is the font used to display text that is marked up as italic." );
147 label->setWhatsThis( wtstr );
148 m_pFonts[4]->setWhatsThis( wtstr );
150 connect( m_pFonts[4], SIGNAL( currentFontChanged(const QFont&) ),
151 SLOT(slotCursiveFont(const QFont&) ) );
153 label = new QLabel( i18n( "Fantas&y font:" ), this );
154 lay->addWidget( label, ++r, E );
156 m_pFonts[5] = new QFontComboBox( this );
158 label->setBuddy( m_pFonts[5] );
159 lay->addWidget( m_pFonts[5], r, M, 1, W-M+1 );
161 wtstr= i18n( "This is the font used to display text that is marked up as a fantasy font." );
162 label->setWhatsThis( wtstr );
163 m_pFonts[5]->setWhatsThis( wtstr );
166 connect( m_pFonts[5], SIGNAL( currentFontChanged(const QFont&) ),
167 SLOT(slotFantasyFont(const QFont&) ) );
169 for(int i = 0; i < 6; ++i)
170 connect( m_pFonts[i], SIGNAL( currentFontChanged(const QFont&) ),
171 SLOT(changed() ) );
173 label = new QLabel( i18n( "Font &size adjustment for this encoding:" ), this );
174 lay->addWidget( label, ++r, M );
176 m_pFontSizeAdjust = new QSpinBox( this );
177 m_pFontSizeAdjust->setRange( -5, 5 );
178 m_pFontSizeAdjust->setSingleStep( 1 );
179 label->setBuddy( m_pFontSizeAdjust );
180 lay->addWidget( m_pFontSizeAdjust, r, M+1, 1, W-M );
182 connect( m_pFontSizeAdjust, SIGNAL( valueChanged( int ) ),
183 SLOT( slotFontSizeAdjust( int ) ) );
184 connect( m_pFontSizeAdjust, SIGNAL( valueChanged( int ) ),
185 SLOT( changed() ) );
187 label = new QLabel( i18n( "Default encoding:"), this );
188 //++r;
189 //lay->addWidget( label, r, E, 1, 2 );
190 lay->addWidget( label, ++r, E);
192 m_pEncoding = new QComboBox( this );
193 label->setBuddy( m_pEncoding );
194 m_pEncoding->setEditable( false );
195 encodings = KGlobal::charsets()->availableEncodingNames();
196 encodings.prepend(i18n("Use Language Encoding"));
197 m_pEncoding->addItems( encodings );
198 lay->addWidget(m_pEncoding,r, M, 1, W- M+1);
200 wtstr = i18n( "Select the default encoding to be used; normally, you will be fine with 'Use language encoding' "
201 "and should not have to change this.");
202 label->setWhatsThis( wtstr );
203 m_pEncoding->setWhatsThis( wtstr );
205 connect( m_pEncoding, SIGNAL( activated(const QString& ) ),
206 SLOT( slotEncoding(const QString&) ) );
207 connect( m_pEncoding, SIGNAL( activated(const QString& ) ),
208 SLOT( changed() ) );
210 ++r; lay->setRowStretch(r, 8);
212 for(int i = 0; i < 6; ++i)
213 m_pFonts[i]->setFontFilters( QFontComboBox::AllFonts );
215 load();
218 KAppearanceOptions::~KAppearanceOptions()
222 void KAppearanceOptions::slotFontSize( int i )
224 fSize = i;
225 if ( fSize < fMinSize ) {
226 m_minSize->setValue( fSize );
227 fMinSize = fSize;
232 void KAppearanceOptions::slotMinimumFontSize( int i )
234 fMinSize = i;
235 if ( fMinSize > fSize ) {
236 m_MedSize->setValue( fMinSize );
237 fSize = fMinSize;
242 void KAppearanceOptions::slotStandardFont(const QFont& n )
244 fonts[0] = n.family();
248 void KAppearanceOptions::slotFixedFont(const QFont& n )
250 fonts[1] = n.family();
254 void KAppearanceOptions::slotSerifFont( const QFont& n )
256 fonts[2] = n.family();
260 void KAppearanceOptions::slotSansSerifFont( const QFont& n )
262 fonts[3] = n.family();
266 void KAppearanceOptions::slotCursiveFont( const QFont& n )
268 fonts[4] = n.family();
272 void KAppearanceOptions::slotFantasyFont( const QFont& n )
274 fonts[5] = n.family();
277 void KAppearanceOptions::slotFontSizeAdjust( int value )
279 fonts[6] = QString::number( value );
282 void KAppearanceOptions::slotEncoding(const QString& n)
284 encodingName = n;
287 void KAppearanceOptions::load()
289 KConfigGroup khtmlrc(KSharedConfig::openConfig("khtmlrc", KConfig::NoGlobals), "");
290 KConfigGroup cg(m_pConfig, "");
291 #define SET_GROUP(x) cg = KConfigGroup(m_pConfig,x); khtmlrc = KConfigGroup(KSharedConfig::openConfig("khtmlrc", KConfig::NoGlobals),x)
292 #define READ_NUM(x,y) cg.readEntry(x, khtmlrc.readEntry(x, y))
293 #define READ_ENTRY(x,y) cg.readEntry(x, khtmlrc.readEntry(x, y))
294 #define READ_LIST(x) cg.readEntry(x, khtmlrc.readEntry(x, QStringList() ))
296 SET_GROUP(m_groupname);
297 fSize = READ_NUM( "MediumFontSize", 12 );
298 fMinSize = READ_NUM( "MinimumFontSize", HTML_DEFAULT_MIN_FONT_SIZE );
299 if (fSize < fMinSize)
300 fSize = fMinSize;
302 defaultFonts = QStringList();
303 defaultFonts.append( READ_ENTRY( "StandardFont", KGlobalSettings::generalFont().family() ) );
304 defaultFonts.append( READ_ENTRY( "FixedFont", KGlobalSettings::fixedFont().family() ) );
305 defaultFonts.append( READ_ENTRY( "SerifFont", HTML_DEFAULT_VIEW_SERIF_FONT ) );
306 defaultFonts.append( READ_ENTRY( "SansSerifFont", HTML_DEFAULT_VIEW_SANSSERIF_FONT ) );
307 defaultFonts.append( READ_ENTRY( "CursiveFont", HTML_DEFAULT_VIEW_CURSIVE_FONT ) );
308 defaultFonts.append( READ_ENTRY( "FantasyFont", HTML_DEFAULT_VIEW_FANTASY_FONT ) );
309 defaultFonts.append( QString("0") ); // default font size adjustment
311 if (cg.hasKey("Fonts"))
312 fonts = cg.readEntry( "Fonts" , QStringList() );
313 else
314 fonts = khtmlrc.readEntry( "Fonts" , QStringList() );
315 while (fonts.count() < 7)
316 fonts.append(QString());
318 encodingName = READ_ENTRY( "DefaultEncoding", QString() );
319 //kDebug(0) << "encoding = " << encodingName;
321 updateGUI();
322 emit changed(false);
325 void KAppearanceOptions::defaults()
327 bool old = m_pConfig->readDefaults();
328 m_pConfig->setReadDefaults(true);
329 load();
330 m_pConfig->setReadDefaults(old);
333 void KAppearanceOptions::updateGUI()
335 //kDebug() << "KAppearanceOptions::updateGUI " << charset;
336 for ( int f = 0; f < 6; f++ ) {
337 QString ff = fonts[f];
338 if (ff.isEmpty())
339 ff = defaultFonts[f];
340 m_pFonts[f]->setCurrentFont(ff);
343 int i = 0;
344 for ( QStringList::Iterator it = encodings.begin(); it != encodings.end(); ++it, ++i )
345 if ( encodingName == *it )
346 m_pEncoding->setCurrentIndex( i );
347 if(encodingName.isEmpty())
348 m_pEncoding->setCurrentIndex( 0 );
349 m_pFontSizeAdjust->setValue( fonts[6].toInt() );
350 m_MedSize->blockSignals(true);
351 m_MedSize->setValue( fSize );
352 m_MedSize->blockSignals(false);
353 m_minSize->blockSignals(true);
354 m_minSize->setValue( fMinSize );
355 m_minSize->blockSignals(false);
358 void KAppearanceOptions::save()
360 KConfigGroup cg(m_pConfig, m_groupname);
361 cg.writeEntry( "MediumFontSize", fSize );
362 cg.writeEntry( "MinimumFontSize", fMinSize );
363 cg.writeEntry( "Fonts", fonts );
365 // If the user chose "Use language encoding", write an empty string
366 if (encodingName == i18n("Use Language Encoding"))
367 encodingName = "";
368 cg.writeEntry( "DefaultEncoding", encodingName );
369 cg.sync();
370 // Send signal to all konqueror instances
371 QDBusMessage message =
372 QDBusMessage::createSignal("/KonqMain", "org.kde.Konqueror.Main", "reparseConfiguration");
373 QDBusConnection::sessionBus().send(message);
375 emit changed(false);