summaryrefslogtreecommitdiffstats
path: root/kcontrol/css/kcmcss.cpp
blob: bf06c3bc58f5b5f00bf6473eb07ae8ddaf95f9d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385

#include <tqcheckbox.h>
#include <tqcombobox.h>
#include <tqlayout.h>
#include <tqradiobutton.h>
#include <tqtextbrowser.h>

#include <kapplication.h>
#include <kcolorbutton.h>
#include <tdeconfig.h>
#include <kdialogbase.h>
#include <tdefontdialog.h>
#include <kgenericfactory.h>
#include <kglobalsettings.h>
#include <kstandarddirs.h>
#include <kurlrequester.h>

#include "cssconfig.h"
#include "csscustom.h"
#include "template.h"
#include "preview.h"

#include "kcmcss.h"

typedef KGenericFactory<CSSConfig, TQWidget> CSSFactory;
K_EXPORT_COMPONENT_FACTORY( kcm_css, CSSFactory("kcmcss") )

CSSConfig::CSSConfig(TQWidget *parent, const char *name, const TQStringList &)
  : TDECModule(CSSFactory::instance(), parent, name)
{
  customDialogBase = new KDialogBase(this, "customCSSDialog", true, TQString::null, 
        KDialogBase::Close, KDialogBase::Close, true );
  customDialog = new CSSCustomDialog(customDialogBase);
  customDialogBase->setMainWidget(customDialog);
  configDialog = new CSSConfigDialog(this);

  setQuickHelp( i18n("<h1>Konqueror Stylesheets</h1> This module allows you to apply your own color"
              " and font settings to Konqueror by using"
              " stylesheets (CSS). You can either specify"
              " options or apply your own self-written"
              " stylesheet by pointing to its location.<br>"
              " Note that these settings will always have"
              " precedence before all other settings made"
              " by the site author. This can be useful to"
              " visually impaired people or for web pages"
              " that are unreadable due to bad design."));


  TQStringList fonts;
  TDEFontChooser::getFontList(fonts, 0);
  customDialog->fontFamily->insertStringList(fonts);

  connect(configDialog->useDefault, TQT_SIGNAL(clicked()),
	  TQT_SLOT(changed()));
  connect(configDialog->useAccess, TQT_SIGNAL(clicked()),
	  TQT_SLOT(changed()));
  connect(configDialog->useUser, TQT_SIGNAL(clicked()),
	  TQT_SLOT(changed()));
  connect(configDialog->urlRequester, TQT_SIGNAL(textChanged(const TQString&)),
	  TQT_SLOT(changed()));
  connect(configDialog->customize, TQT_SIGNAL(clicked()),
          TQT_SLOT(slotCustomize()));
  connect(customDialog->basefontsize, TQT_SIGNAL(highlighted(int)),
	  TQT_SLOT(changed()));
  connect(customDialog->basefontsize, TQT_SIGNAL(textChanged(const TQString&)),
	  TQT_SLOT(changed()));
  connect(customDialog->dontScale, TQT_SIGNAL(clicked()),
	  TQT_SLOT(changed()));
  connect(customDialog->blackOnWhite, TQT_SIGNAL(clicked()),
	  TQT_SLOT(changed()));
  connect(customDialog->whiteOnBlack, TQT_SIGNAL(clicked()),
	  TQT_SLOT(changed()));
  connect(customDialog->customColor, TQT_SIGNAL(clicked()),
	  TQT_SLOT(changed()));
  connect(customDialog->foregroundColor, TQT_SIGNAL(changed(const TQColor &)),
	  TQT_SLOT(changed()));
  connect(customDialog->backgroundColor, TQT_SIGNAL(changed(const TQColor &)),
	  TQT_SLOT(changed()));
  connect(customDialog->fontFamily, TQT_SIGNAL(highlighted(int)),
	  TQT_SLOT(changed()));
  connect(customDialog->fontFamily, TQT_SIGNAL(textChanged(const TQString&)),
	  TQT_SLOT(changed()));
  connect(customDialog->sameFamily, TQT_SIGNAL(clicked()),
	  TQT_SLOT(changed()));
  connect(customDialog->preview, TQT_SIGNAL(clicked()),
          TQT_SLOT(slotPreview()));
  connect(customDialog->sameColor, TQT_SIGNAL(clicked()),
	  TQT_SLOT(changed()));
  connect(customDialog->hideImages, TQT_SIGNAL(clicked()),
	  TQT_SLOT(changed()));
  connect(customDialog->hideBackground, TQT_SIGNAL(clicked()),
	  TQT_SLOT(changed()));

  TQVBoxLayout *vbox = new TQVBoxLayout(this, 0, 0);
  vbox->addWidget(configDialog);

  load();
}

void CSSConfig::load()
{
   load( false );
}

void CSSConfig::load( bool useDefaults )
{
  TDEConfig *c = new TDEConfig("kcmcssrc", false, false);
  c->setReadDefaults( useDefaults );

  c->setGroup("Stylesheet");
  TQString u = c->readEntry("Use", "default");
  configDialog->useDefault->setChecked(u == "default");
  configDialog->useUser->setChecked(u == "user");
  configDialog->useAccess->setChecked(u == "access");
  configDialog->urlRequester->setURL(c->readEntry("SheetName"));

  c->setGroup("Font");
  customDialog->basefontsize->setEditText(TQString::number(c->readNumEntry("BaseSize", 12)));
  customDialog->dontScale->setChecked(c->readBoolEntry("DontScale", false));

  TQString fname = c->readEntry("Family", "Arial");
  for (int i=0; i < customDialog->fontFamily->count(); ++i)
    if (customDialog->fontFamily->text(i) == fname)
      {
	customDialog->fontFamily->setCurrentItem(i);
	break;
      }

  customDialog->sameFamily->setChecked(c->readBoolEntry("SameFamily", false));

  c->setGroup("Colors");
  TQString m = c->readEntry("Mode", "black-on-white");
  customDialog->blackOnWhite->setChecked(m == "black-on-white");
  customDialog->whiteOnBlack->setChecked(m == "white-on-black");
  customDialog->customColor->setChecked(m == "custom");
  customDialog->backgroundColor->setColor(c->readColorEntry("BackColor", &TQt::white));
  customDialog->foregroundColor->setColor(c->readColorEntry("ForeColor", &TQt::black));
  customDialog->sameColor->setChecked(c->readBoolEntry("SameColor", false));

  // Images
  c->setGroup("Images");
  customDialog->hideImages->setChecked(c->readBoolEntry("Hide", false));
  customDialog->hideBackground->setChecked(c->readBoolEntry("HideBackground", true));

  delete c;

  emit changed( useDefaults );
}


void CSSConfig::save()
{
  // write to config file
  TDEConfig *c = new TDEConfig("kcmcssrc", false, false);

  c->setGroup("Stylesheet");
  if (configDialog->useDefault->isChecked())
    c->writeEntry("Use", "default");
  if (configDialog->useUser->isChecked())
    c->writeEntry("Use", "user");
  if (configDialog->useAccess->isChecked())
    c->writeEntry("Use", "access");
  c->writeEntry("SheetName", configDialog->urlRequester->url());

  c->setGroup("Font");
  c->writeEntry("BaseSize", customDialog->basefontsize->currentText());
  c->writeEntry("DontScale", customDialog->dontScale->isChecked());
  c->writeEntry("SameFamily", customDialog->sameFamily->isChecked());
  c->writeEntry("Family", customDialog->fontFamily->currentText());

  c->setGroup("Colors");
  if (customDialog->blackOnWhite->isChecked())
    c->writeEntry("Mode", "black-on-white");
  if (customDialog->whiteOnBlack->isChecked())
    c->writeEntry("Mode", "white-on-black");
  if (customDialog->customColor->isChecked())
    c->writeEntry("Mode", "custom");
  c->writeEntry("BackColor", customDialog->backgroundColor->color());
  c->writeEntry("ForeColor", customDialog->foregroundColor->color());
  c->writeEntry("SameColor", customDialog->sameColor->isChecked());

  c->setGroup("Images");
  c->writeEntry("Hide", customDialog->hideImages->isChecked());
  c->writeEntry("HideBackground", customDialog->hideBackground->isChecked());

  c->sync();
  delete c;

  // generate CSS template
  TQString templ = locate("data", "kcmcss/template.css");
  TQString dest;
  if (!templ.isEmpty())
    {
      CSSTemplate css(templ);

      dest = kapp->dirs()->saveLocation("data", "kcmcss");
      dest += "/override.css";

      css.expand(dest, cssDict());
    }

  // make konqueror use the right stylesheet
  c = new TDEConfig("konquerorrc", false, false);

  c->setGroup("HTML Settings");
  c->writeEntry("UserStyleSheetEnabled", !configDialog->useDefault->isChecked());

  if (configDialog->useUser->isChecked())
    c->writeEntry("UserStyleSheet", configDialog->urlRequester->url());
  if (configDialog->useAccess->isChecked())
    c->writeEntry("UserStyleSheet", dest);

  c->sync();
  delete c;
  emit changed(false);
}


void CSSConfig::defaults()
{
   load( true );
}


TQString px(int i, double scale)
{
  TQString px;
  px.setNum(static_cast<int>(i * scale));
  px += "px";
  return px;
}


TQMap<TQString,TQString> CSSConfig::cssDict()
{
  TQMap<TQString,TQString> dict;

  // Fontsizes ------------------------------------------------------

  int bfs = customDialog->basefontsize->currentText().toInt();
  dict.insert("fontsize-base", px(bfs, 1.0));

  if (customDialog->dontScale->isChecked())
    {
      dict.insert("fontsize-small-1", px(bfs, 1.0));
      dict.insert("fontsize-large-1", px(bfs, 1.0));
      dict.insert("fontsize-large-2", px(bfs, 1.0));
      dict.insert("fontsize-large-3", px(bfs, 1.0));
      dict.insert("fontsize-large-4", px(bfs, 1.0));
      dict.insert("fontsize-large-5", px(bfs, 1.0));
    }
  else
    {
      // TODO: use something harmonic here
      dict.insert("fontsize-small-1", px(bfs, 0.8));
      dict.insert("fontsize-large-1", px(bfs, 1.2));
      dict.insert("fontsize-large-2", px(bfs, 1.4));
      dict.insert("fontsize-large-3", px(bfs, 1.5));
      dict.insert("fontsize-large-4", px(bfs, 1.6));
      dict.insert("fontsize-large-5", px(bfs, 1.8));
    }

  // Colors --------------------------------------------------------

  if (customDialog->blackOnWhite->isChecked())
    {
      dict.insert("background-color", "White");
      dict.insert("foreground-color", "Black");
    }
  else if (customDialog->whiteOnBlack->isChecked())
    {
      dict.insert("background-color", "Black");
      dict.insert("foreground-color", "White");
    }
  else
    {
      dict.insert("background-color", customDialog->backgroundColor->color().name());
      dict.insert("foreground-color", customDialog->foregroundColor->color().name());
    }

  if (customDialog->sameColor->isChecked())
    dict.insert("force-color", "! important");
  else
    dict.insert("force-color", "");

  // Fonts -------------------------------------------------------------
  dict.insert("font-family", customDialog->fontFamily->currentText());
  if (customDialog->sameFamily->isChecked())
    dict.insert("force-font", "! important");
  else
    dict.insert("force-font", "");

  // Images

  if (customDialog->hideImages->isChecked())
    dict.insert("display-images", "background-image : none ! important");
  else
    dict.insert("display-images", "");
  if (customDialog->hideBackground->isChecked())
    dict.insert("display-background", "background-image : none ! important");
  else
    dict.insert("display-background", "");

  return dict;
}


void CSSConfig::slotCustomize()
{
  customDialogBase->exec();
}


void CSSConfig::slotPreview()
{

  TQStyleSheetItem *h1 = new TQStyleSheetItem(TQStyleSheet::defaultSheet(), "h1");
  TQStyleSheetItem *h2 = new TQStyleSheetItem(TQStyleSheet::defaultSheet(), "h2");
  TQStyleSheetItem *h3 = new TQStyleSheetItem(TQStyleSheet::defaultSheet(), "h3");
  TQStyleSheetItem *text = new TQStyleSheetItem(TQStyleSheet::defaultSheet(), "p");

  // Fontsize

  int bfs = customDialog->basefontsize->currentText().toInt();
  text->setFontSize(bfs);
  if (customDialog->dontScale->isChecked())
    {
      h1->setFontSize(bfs);
      h2->setFontSize(bfs);
      h3->setFontSize(bfs);
    }
  else
    {
      h1->setFontSize(static_cast<int>(bfs * 1.8));
      h2->setFontSize(static_cast<int>(bfs * 1.6));
      h3->setFontSize(static_cast<int>(bfs * 1.4));
    }

  // Colors

  TQColor back, fore;

  if (customDialog->blackOnWhite->isChecked())
    {
      back = Qt::white;
      fore = Qt::black;
    }
  else if (customDialog->whiteOnBlack->isChecked())
    {
      back = Qt::black;
      fore = Qt::white;
    }
  else
    {
      back = customDialog->backgroundColor->color();
      fore = customDialog->foregroundColor->color();
    }

  h1->setColor(fore);
  h2->setColor(fore);
  h3->setColor(fore);
  text->setColor(fore);

  // Fonts

  h1->setFontFamily(customDialog->fontFamily->currentText());
  h2->setFontFamily(customDialog->fontFamily->currentText());
  h3->setFontFamily(customDialog->fontFamily->currentText());
  text->setFontFamily(customDialog->fontFamily->currentText());

  // Show the preview
  PreviewDialog *dlg = new PreviewDialog(this, 0, true);
  dlg->preview->setPaper(back);
  dlg->preview->viewport()->setFont(TQFont(TDEGlobalSettings::generalFont().family(), bfs));

  dlg->exec();

  delete dlg;
}




#include "kcmcss.moc"