Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / konqueror / settings / css / kcmcss.cpp
blob177ac34eba98bab837c2e8ec4f2b176a386a66e4
2 // Own
3 #include "kcmcss.h"
5 // Qt
6 #include <QtGui/QCheckBox>
7 #include <QtGui/QComboBox>
8 #include <QtGui/QLayout>
9 #include <QtGui/QRadioButton>
10 #include <Qt3Support/Q3TextBrowser>
11 #include <QtGui/QBoxLayout>
13 // KDE
14 #include <kapplication.h>
15 #include <kcolorbutton.h>
16 #include <kconfig.h>
17 #include <kdialog.h>
18 #include <kfontdialog.h>
19 #include <kglobalsettings.h>
20 #include <kstandarddirs.h>
21 #include <kurlrequester.h>
22 #include <kpluginfactory.h>
23 #include <kpluginloader.h>
25 // Local
26 #include "template.h"
28 K_PLUGIN_FACTORY(CSSFactory, registerPlugin<CSSConfig>();)
29 K_EXPORT_PLUGIN(CSSFactory("kcmcss"))
32 CSSConfig::CSSConfig(QWidget *parent, const QVariantList &)
33 : KCModule(CSSFactory::componentData(), parent)
35 customDialogBase = new KDialog(this);
36 customDialogBase->setObjectName( "customCSSDialog" );
37 customDialogBase->setModal( true );
38 customDialogBase->setButtons( KDialog::Close );
39 customDialogBase->setDefaultButton( KDialog::Close );
40 customDialog = new CSSCustomDialog(customDialogBase);
41 customDialogBase->setMainWidget(customDialog);
42 configDialog = new CSSConfigDialog(this);
44 setQuickHelp( i18n("<h1>Konqueror Stylesheets</h1> This module allows you to apply your own color"
45 " and font settings to Konqueror by using"
46 " stylesheets (CSS). You can either specify"
47 " options or apply your own self-written"
48 " stylesheet by pointing to its location.<br />"
49 " Note that these settings will always have"
50 " precedence before all other settings made"
51 " by the site author. This can be useful to"
52 " visually impaired people or for web pages"
53 " that are unreadable due to bad design."));
55 QStringList fonts;
56 KFontChooser::getFontList(fonts, 0);
57 customDialog->fontFamily->addItems(fonts);
59 connect(configDialog->useDefault, SIGNAL(clicked()),
60 SLOT(changed()));
61 connect(configDialog->useAccess, SIGNAL(clicked()),
62 SLOT(changed()));
63 connect(configDialog->useUser, SIGNAL(clicked()),
64 SLOT(changed()));
65 connect(configDialog->urlRequester, SIGNAL(textChanged(const QString&)),
66 SLOT(changed()));
67 connect(configDialog->customize, SIGNAL(clicked()),
68 SLOT(slotCustomize()));
69 connect(customDialog->basefontsize, SIGNAL(highlighted(int)),
70 SLOT(changed()));
71 connect(customDialog->basefontsize, SIGNAL(textChanged(const QString&)),
72 SLOT(changed()));
73 connect(customDialog->dontScale, SIGNAL(clicked()),
74 SLOT(changed()));
75 connect(customDialog->blackOnWhite, SIGNAL(clicked()),
76 SLOT(changed()));
77 connect(customDialog->whiteOnBlack, SIGNAL(clicked()),
78 SLOT(changed()));
79 connect(customDialog->customColor, SIGNAL(clicked()),
80 SLOT(changed()));
81 connect(customDialog->foregroundColorButton, SIGNAL(changed(const QColor &)),
82 SLOT(changed()));
83 connect(customDialog->backgroundColorButton, SIGNAL(changed(const QColor &)),
84 SLOT(changed()));
85 connect(customDialog->fontFamily, SIGNAL(highlighted(int)),
86 SLOT(changed()));
87 connect(customDialog->fontFamily, SIGNAL(textChanged(const QString&)),
88 SLOT(changed()));
89 connect(customDialog->sameFamily, SIGNAL(clicked()),
90 SLOT(changed()));
91 connect(customDialog->preview, SIGNAL(clicked()),
92 SLOT(slotPreview()));
93 connect(customDialog->sameColor, SIGNAL(clicked()),
94 SLOT(changed()));
95 connect(customDialog->hideImages, SIGNAL(clicked()),
96 SLOT(changed()));
97 connect(customDialog->hideBackground, SIGNAL(clicked()),
98 SLOT(changed()));
100 QVBoxLayout *vbox = new QVBoxLayout(this);
101 vbox->setMargin(0);
102 vbox->setSpacing(0);
103 vbox->addWidget(configDialog);
104 configDialog->layout()->setMargin(0);
106 load();
110 void CSSConfig::load()
112 KConfig *c = new KConfig("kcmcssrc", KConfig::NoGlobals);
113 KConfigGroup group = c->group("Stylesheet");
114 QString u = group.readEntry("Use", "default");
115 configDialog->useDefault->setChecked(u == "default");
116 configDialog->useUser->setChecked(u == "user");
117 configDialog->useAccess->setChecked(u == "access");
118 configDialog->urlRequester->setUrl(group.readEntry("SheetName"));
120 group = c->group("Font");
121 customDialog->basefontsize->setEditText(QString::number(group.readEntry("BaseSize", 12)));
122 customDialog->dontScale->setChecked(group.readEntry("DontScale", false));
124 QString fname = group.readEntry("Family", "Arial");
125 for (int i=0; i < customDialog->fontFamily->count(); ++i)
126 if (customDialog->fontFamily->itemText(i) == fname)
128 customDialog->fontFamily->setCurrentIndex(i);
129 break;
132 customDialog->sameFamily->setChecked(group.readEntry("SameFamily", false));
134 group = c->group("Colors");
135 QString m = group.readEntry("Mode", "black-on-white");
136 customDialog->blackOnWhite->setChecked(m == "black-on-white");
137 customDialog->whiteOnBlack->setChecked(m == "white-on-black");
138 customDialog->customColor->setChecked(m == "custom");
140 QColor white (Qt::white);
141 QColor black (Qt::black);
142 customDialog->backgroundColorButton->setColor(group.readEntry("BackColor", white));
143 customDialog->foregroundColorButton->setColor(group.readEntry("ForeColor", black));
144 customDialog->sameColor->setChecked(group.readEntry("SameColor", false));
146 // Images
147 group = c->group("Images");
148 customDialog->hideImages->setChecked(group.readEntry("Hide", false));
149 customDialog->hideBackground->setChecked(group.readEntry("HideBackground", true));
151 delete c;
155 void CSSConfig::save()
157 // write to config file
158 KConfig *c = new KConfig("kcmcssrc", KConfig::NoGlobals);
159 KConfigGroup group = c->group("Stylesheet");
160 if (configDialog->useDefault->isChecked())
161 group.writeEntry("Use", "default");
162 if (configDialog->useUser->isChecked())
163 group.writeEntry("Use", "user");
164 if (configDialog->useAccess->isChecked())
165 group.writeEntry("Use", "access");
166 group.writeEntry("SheetName", configDialog->urlRequester->url().url());
168 group = c->group("Font");
169 group.writeEntry("BaseSize", customDialog->basefontsize->currentText());
170 group.writeEntry("DontScale", customDialog->dontScale->isChecked());
171 group.writeEntry("SameFamily", customDialog->sameFamily->isChecked());
172 group.writeEntry("Family", customDialog->fontFamily->currentText());
174 group = c->group("Colors");
175 if (customDialog->blackOnWhite->isChecked())
176 group.writeEntry("Mode", "black-on-white");
177 if (customDialog->whiteOnBlack->isChecked())
178 group.writeEntry("Mode", "white-on-black");
179 if (customDialog->customColor->isChecked())
180 group.writeEntry("Mode", "custom");
181 group.writeEntry("BackColor", customDialog->backgroundColorButton->color());
182 group.writeEntry("ForeColor", customDialog->foregroundColorButton->color());
183 group.writeEntry("SameColor", customDialog->sameColor->isChecked());
185 group = c->group("Images");
186 group.writeEntry("Hide", customDialog->hideImages->isChecked());
187 group.writeEntry("HideBackground", customDialog->hideBackground->isChecked());
189 c->sync();
190 delete c;
192 // generate CSS template
193 QString templ = KStandardDirs::locate("data", "kcmcss/template.css");
194 QString dest;
195 if (!templ.isEmpty())
197 CSSTemplate css(templ);
199 dest = KGlobal::mainComponent().dirs()->saveLocation("data", "kcmcss");
200 dest += "/override.css";
202 css.expand(dest, cssDict());
205 // make konqueror use the right stylesheet
206 c = new KConfig("konquerorrc", KConfig::NoGlobals);
207 group = c->group("HTML Settings");
208 group.writeEntry("UserStyleSheetEnabled", !configDialog->useDefault->isChecked());
210 if (configDialog->useUser->isChecked())
211 group.writeEntry("UserStyleSheet", configDialog->urlRequester->url().url());
212 if (configDialog->useAccess->isChecked())
213 group.writeEntry("UserStyleSheet", dest);
215 c->sync();
216 delete c;
217 emit changed(false);
221 void CSSConfig::defaults()
223 configDialog->useDefault->setChecked(true);
224 configDialog->useUser->setChecked(false);
225 configDialog->useAccess->setChecked(false);
226 configDialog->urlRequester->setUrl(KUrl());
228 customDialog->basefontsize->setEditText(QString::number(12));
229 customDialog->dontScale->setChecked(false);
231 QString fname = "Arial";
232 for (int i=0; i < customDialog->fontFamily->count(); ++i)
233 if (customDialog->fontFamily->itemText(i) == fname)
235 customDialog->fontFamily->setCurrentIndex(i);
236 break;
239 customDialog->sameFamily->setChecked(false);
240 customDialog->blackOnWhite->setChecked(true);
241 customDialog->whiteOnBlack->setChecked(false);
242 customDialog->customColor->setChecked(false);
243 customDialog->backgroundColorButton->setColor(Qt::white);
244 customDialog->foregroundColorButton->setColor(Qt::black);
245 customDialog->sameColor->setChecked(false);
247 customDialog->hideImages->setChecked(false);
248 customDialog->hideBackground->setChecked( true);
249 emit changed(true);
253 QString px(int i, double scale)
255 QString px;
256 px.setNum(static_cast<int>(i * scale));
257 px += "px";
258 return px;
262 QMap<QString,QString> CSSConfig::cssDict()
264 QMap<QString,QString> dict;
266 // Fontsizes ------------------------------------------------------
268 int bfs = customDialog->basefontsize->currentText().toInt();
269 dict.insert("fontsize-base", px(bfs, 1.0));
271 if (customDialog->dontScale->isChecked())
273 dict.insert("fontsize-small-1", px(bfs, 1.0));
274 dict.insert("fontsize-large-1", px(bfs, 1.0));
275 dict.insert("fontsize-large-2", px(bfs, 1.0));
276 dict.insert("fontsize-large-3", px(bfs, 1.0));
277 dict.insert("fontsize-large-4", px(bfs, 1.0));
278 dict.insert("fontsize-large-5", px(bfs, 1.0));
280 else
282 // TODO: use something harmonic here
283 dict.insert("fontsize-small-1", px(bfs, 0.8));
284 dict.insert("fontsize-large-1", px(bfs, 1.2));
285 dict.insert("fontsize-large-2", px(bfs, 1.4));
286 dict.insert("fontsize-large-3", px(bfs, 1.5));
287 dict.insert("fontsize-large-4", px(bfs, 1.6));
288 dict.insert("fontsize-large-5", px(bfs, 1.8));
291 // Colors --------------------------------------------------------
293 if (customDialog->blackOnWhite->isChecked())
295 dict.insert("background-color", "White");
296 dict.insert("foreground-color", "Black");
298 else if (customDialog->whiteOnBlack->isChecked())
300 dict.insert("background-color", "Black");
301 dict.insert("foreground-color", "White");
303 else
305 dict.insert("background-color", customDialog->backgroundColorButton->color().name());
306 dict.insert("foreground-color", customDialog->foregroundColorButton->color().name());
309 if (customDialog->sameColor->isChecked())
310 dict.insert("force-color", "! important");
311 else
312 dict.insert("force-color", "");
314 // Fonts -------------------------------------------------------------
315 dict.insert("font-family", customDialog->fontFamily->currentText());
316 if (customDialog->sameFamily->isChecked())
317 dict.insert("force-font", "! important");
318 else
319 dict.insert("force-font", "");
321 // Images
323 if (customDialog->hideImages->isChecked())
324 dict.insert("display-images", "background-image : none ! important");
325 else
326 dict.insert("display-images", "");
327 if (customDialog->hideBackground->isChecked())
328 dict.insert("display-background", "background-image : none ! important");
329 else
330 dict.insert("display-background", "");
332 return dict;
336 void CSSConfig::slotCustomize()
338 customDialogBase->exec();
342 void CSSConfig::slotPreview()
345 Q3StyleSheetItem *h1 = new Q3StyleSheetItem(Q3StyleSheet::defaultSheet(), "h1");
346 Q3StyleSheetItem *h2 = new Q3StyleSheetItem(Q3StyleSheet::defaultSheet(), "h2");
347 Q3StyleSheetItem *h3 = new Q3StyleSheetItem(Q3StyleSheet::defaultSheet(), "h3");
348 Q3StyleSheetItem *text = new Q3StyleSheetItem(Q3StyleSheet::defaultSheet(), "p");
350 // Fontsize
352 int bfs = customDialog->basefontsize->currentText().toInt();
353 text->setFontSize(bfs);
354 if (customDialog->dontScale->isChecked())
356 h1->setFontSize(bfs);
357 h2->setFontSize(bfs);
358 h3->setFontSize(bfs);
360 else
362 h1->setFontSize(static_cast<int>(bfs * 1.8));
363 h2->setFontSize(static_cast<int>(bfs * 1.6));
364 h3->setFontSize(static_cast<int>(bfs * 1.4));
367 // Colors
369 QColor back, fore;
371 if (customDialog->blackOnWhite->isChecked())
373 back = Qt::white;
374 fore = Qt::black;
376 else if (customDialog->whiteOnBlack->isChecked())
378 back = Qt::black;
379 fore = Qt::white;
381 else
383 back = customDialog->backgroundColorButton->color();
384 fore = customDialog->foregroundColorButton->color();
387 h1->setColor(fore);
388 h2->setColor(fore);
389 h3->setColor(fore);
390 text->setColor(fore);
392 // Fonts
394 h1->setFontFamily(customDialog->fontFamily->currentText());
395 h2->setFontFamily(customDialog->fontFamily->currentText());
396 h3->setFontFamily(customDialog->fontFamily->currentText());
397 text->setFontFamily(customDialog->fontFamily->currentText());
399 // Show the preview
400 PreviewDialog *dlg = new PreviewDialog(customDialogBase);
401 dlg->preview->setPaper(back);
402 dlg->preview->viewport()->setFont(QFont(KGlobalSettings::generalFont().family(), bfs));
404 dlg->exec();
406 delete dlg;
412 #include "kcmcss.moc"