6 #include <QtGui/QCheckBox>
7 #include <QtGui/QComboBox>
8 #include <QtGui/QLayout>
9 #include <QtGui/QRadioButton>
10 #include <Qt3Support/Q3TextBrowser>
11 #include <QtGui/QBoxLayout>
14 #include <kapplication.h>
15 #include <kcolorbutton.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>
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."));
56 KFontChooser::getFontList(fonts
, 0);
57 customDialog
->fontFamily
->addItems(fonts
);
59 connect(configDialog
->useDefault
, SIGNAL(clicked()),
61 connect(configDialog
->useAccess
, SIGNAL(clicked()),
63 connect(configDialog
->useUser
, SIGNAL(clicked()),
65 connect(configDialog
->urlRequester
, SIGNAL(textChanged(const QString
&)),
67 connect(configDialog
->customize
, SIGNAL(clicked()),
68 SLOT(slotCustomize()));
69 connect(customDialog
->basefontsize
, SIGNAL(highlighted(int)),
71 connect(customDialog
->basefontsize
, SIGNAL(textChanged(const QString
&)),
73 connect(customDialog
->dontScale
, SIGNAL(clicked()),
75 connect(customDialog
->blackOnWhite
, SIGNAL(clicked()),
77 connect(customDialog
->whiteOnBlack
, SIGNAL(clicked()),
79 connect(customDialog
->customColor
, SIGNAL(clicked()),
81 connect(customDialog
->foregroundColorButton
, SIGNAL(changed(const QColor
&)),
83 connect(customDialog
->backgroundColorButton
, SIGNAL(changed(const QColor
&)),
85 connect(customDialog
->fontFamily
, SIGNAL(highlighted(int)),
87 connect(customDialog
->fontFamily
, SIGNAL(textChanged(const QString
&)),
89 connect(customDialog
->sameFamily
, SIGNAL(clicked()),
91 connect(customDialog
->preview
, SIGNAL(clicked()),
93 connect(customDialog
->sameColor
, SIGNAL(clicked()),
95 connect(customDialog
->hideImages
, SIGNAL(clicked()),
97 connect(customDialog
->hideBackground
, SIGNAL(clicked()),
100 QVBoxLayout
*vbox
= new QVBoxLayout(this);
103 vbox
->addWidget(configDialog
);
104 configDialog
->layout()->setMargin(0);
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
);
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));
147 group
= c
->group("Images");
148 customDialog
->hideImages
->setChecked(group
.readEntry("Hide", false));
149 customDialog
->hideBackground
->setChecked(group
.readEntry("HideBackground", true));
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());
192 // generate CSS template
193 QString templ
= KStandardDirs::locate("data", "kcmcss/template.css");
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
);
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
);
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);
253 QString
px(int i
, double scale
)
256 px
.setNum(static_cast<int>(i
* scale
));
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));
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");
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");
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");
319 dict
.insert("force-font", "");
323 if (customDialog
->hideImages
->isChecked())
324 dict
.insert("display-images", "background-image : none ! important");
326 dict
.insert("display-images", "");
327 if (customDialog
->hideBackground
->isChecked())
328 dict
.insert("display-background", "background-image : none ! important");
330 dict
.insert("display-background", "");
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");
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
);
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));
371 if (customDialog
->blackOnWhite
->isChecked())
376 else if (customDialog
->whiteOnBlack
->isChecked())
383 back
= customDialog
->backgroundColorButton
->color();
384 fore
= customDialog
->foregroundColorButton
->color();
390 text
->setColor(fore
);
394 h1
->setFontFamily(customDialog
->fontFamily
->currentText());
395 h2
->setFontFamily(customDialog
->fontFamily
->currentText());
396 h3
->setFontFamily(customDialog
->fontFamily
->currentText());
397 text
->setFontFamily(customDialog
->fontFamily
->currentText());
400 PreviewDialog
*dlg
= new PreviewDialog(customDialogBase
);
401 dlg
->preview
->setPaper(back
);
402 dlg
->preview
->viewport()->setFont(QFont(KGlobalSettings::generalFont().family(), bfs
));
412 #include "kcmcss.moc"