~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/fonts/fonts.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// KDE Display fonts setup tab
 
2
//
 
3
// Copyright (c)  Mark Donohoe 1997
 
4
//                lars Knoll 1999
 
5
//                Rik Hemsley 2000
 
6
//
 
7
// Ported to kcontrol2 by Geert Jansen.
 
8
 
 
9
#include <config-workspace.h>
 
10
 
 
11
#include <QCheckBox>
 
12
#include <QComboBox>
 
13
#include <QDir>
 
14
#include <QLabel>
 
15
#include <QPushButton>
 
16
#include <QtCore/QSettings>
 
17
 
 
18
 
 
19
//Added by qt3to4:
 
20
#include <QPixmap>
 
21
#include <QByteArray>
 
22
#include <QGridLayout>
 
23
#include <QHBoxLayout>
 
24
#include <QVBoxLayout>
 
25
#include <QFormLayout>
 
26
 
 
27
 
 
28
#include <kacceleratormanager.h>
 
29
#include <kapplication.h>
 
30
#include <kglobalsettings.h>
 
31
#include <kmessagebox.h>
 
32
#include <knuminput.h>
 
33
#include <kprocess.h>
 
34
#include <kconfig.h>
 
35
#include <kstandarddirs.h>
 
36
#include <stdlib.h>
 
37
 
 
38
#include "../krdb/krdb.h"
 
39
#include "fonts.h"
 
40
#include "fonts.moc"
 
41
 
 
42
#include <kdebug.h>
 
43
#include <klocale.h>
 
44
 
 
45
#ifdef HAVE_FREETYPE
 
46
#include <ft2build.h>
 
47
#ifdef FT_LCD_FILTER_H
 
48
#include FT_FREETYPE_H
 
49
#include FT_LCD_FILTER_H
 
50
#endif
 
51
#endif
 
52
 
 
53
#ifdef Q_WS_X11
 
54
#include <X11/Xlib.h>
 
55
#endif
 
56
 
 
57
#include <KPluginFactory>
 
58
 
 
59
#ifdef Q_WS_X11
 
60
// X11 headers
 
61
#undef Bool
 
62
#undef Unsorted
 
63
#undef None
 
64
#endif
 
65
 
 
66
static const char * const aa_rgb_xpm[]={
 
67
"12 12 3 1",
 
68
"a c #0000ff",
 
69
"# c #00ff00",
 
70
". c #ff0000",
 
71
"....####aaaa",
 
72
"....####aaaa",
 
73
"....####aaaa",
 
74
"....####aaaa",
 
75
"....####aaaa",
 
76
"....####aaaa",
 
77
"....####aaaa",
 
78
"....####aaaa",
 
79
"....####aaaa",
 
80
"....####aaaa",
 
81
"....####aaaa",
 
82
"....####aaaa"};
 
83
static const char * const aa_bgr_xpm[]={
 
84
"12 12 3 1",
 
85
". c #0000ff",
 
86
"# c #00ff00",
 
87
"a c #ff0000",
 
88
"....####aaaa",
 
89
"....####aaaa",
 
90
"....####aaaa",
 
91
"....####aaaa",
 
92
"....####aaaa",
 
93
"....####aaaa",
 
94
"....####aaaa",
 
95
"....####aaaa",
 
96
"....####aaaa",
 
97
"....####aaaa",
 
98
"....####aaaa",
 
99
"....####aaaa"};
 
100
static const char * const aa_vrgb_xpm[]={
 
101
"12 12 3 1",
 
102
"a c #0000ff",
 
103
"# c #00ff00",
 
104
". c #ff0000",
 
105
"............",
 
106
"............",
 
107
"............",
 
108
"............",
 
109
"############",
 
110
"############",
 
111
"############",
 
112
"############",
 
113
"aaaaaaaaaaaa",
 
114
"aaaaaaaaaaaa",
 
115
"aaaaaaaaaaaa",
 
116
"aaaaaaaaaaaa"};
 
117
static const char * const aa_vbgr_xpm[]={
 
118
"12 12 3 1",
 
119
". c #0000ff",
 
120
"# c #00ff00",
 
121
"a c #ff0000",
 
122
"............",
 
123
"............",
 
124
"............",
 
125
"............",
 
126
"############",
 
127
"############",
 
128
"############",
 
129
"############",
 
130
"aaaaaaaaaaaa",
 
131
"aaaaaaaaaaaa",
 
132
"aaaaaaaaaaaa",
 
133
"aaaaaaaaaaaa"};
 
134
 
 
135
static const char* const * const aaPixmaps[]={ aa_rgb_xpm, aa_bgr_xpm, aa_vrgb_xpm, aa_vbgr_xpm };
 
136
 
 
137
/**** DLL Interface ****/
 
138
K_PLUGIN_FACTORY(FontFactory, registerPlugin<KFonts>(); )
 
139
K_EXPORT_PLUGIN(FontFactory("kcmfonts"))
 
140
 
 
141
/**** FontUseItem ****/
 
142
 
 
143
FontUseItem::FontUseItem(
 
144
  QWidget * parent,
 
145
  const QString &name,
 
146
  const QString &grp,
 
147
  const QString &key,
 
148
  const QString &rc,
 
149
  const QFont &default_fnt,
 
150
  bool f
 
151
)
 
152
  : KFontRequester(parent, f),
 
153
    _rcfile(rc),
 
154
    _rcgroup(grp),
 
155
    _rckey(key),
 
156
    _default(default_fnt)
 
157
{
 
158
  KAcceleratorManager::setNoAccel( this );
 
159
  setTitle( name );
 
160
  readFont();
 
161
}
 
162
 
 
163
void FontUseItem::setDefault()
 
164
{
 
165
  setFont( _default, isFixedOnly() );
 
166
}
 
167
 
 
168
void FontUseItem::readFont()
 
169
{
 
170
  KConfig *config;
 
171
 
 
172
  bool deleteme = false;
 
173
  if (_rcfile.isEmpty())
 
174
    config = KGlobal::config().data();
 
175
  else
 
176
  {
 
177
    config = new KConfig(_rcfile);
 
178
    deleteme = true;
 
179
  }
 
180
 
 
181
  KConfigGroup group(config, _rcgroup);
 
182
  QFont tmpFnt(_default);
 
183
  setFont( group.readEntry(_rckey, tmpFnt), isFixedOnly() );
 
184
  if (deleteme) delete config;
 
185
}
 
186
 
 
187
void FontUseItem::writeFont()
 
188
{
 
189
  KConfig *config;
 
190
 
 
191
  if (_rcfile.isEmpty()) {
 
192
    config = KGlobal::config().data();
 
193
    KConfigGroup(config, _rcgroup).writeEntry(_rckey, font(), KConfig::Normal|KConfig::Global);
 
194
  } else {
 
195
    config = new KConfig(KStandardDirs::locateLocal("config", _rcfile));
 
196
    KConfigGroup(config, _rcgroup).writeEntry(_rckey, font());
 
197
    config->sync();
 
198
    delete config;
 
199
  }
 
200
}
 
201
 
 
202
void FontUseItem::applyFontDiff( const QFont &fnt, int fontDiffFlags )
 
203
{
 
204
  QFont _font( font() );
 
205
 
 
206
  if (fontDiffFlags & KFontChooser::FontDiffSize) {
 
207
    _font.setPointSizeF( fnt.pointSizeF() );
 
208
  }
 
209
  if (fontDiffFlags & KFontChooser::FontDiffFamily) {
 
210
    if (!isFixedOnly() || QFontInfo(fnt).fixedPitch()) _font.setFamily( fnt.family() );
 
211
  }
 
212
  if (fontDiffFlags & KFontChooser::FontDiffStyle) {
 
213
    _font.setWeight( fnt.weight() );
 
214
    _font.setStyle( fnt.style() );
 
215
    _font.setUnderline( fnt.underline() );
 
216
  }
 
217
 
 
218
  setFont( _font, isFixedOnly() );
 
219
}
 
220
 
 
221
/**** FontAASettings ****/
 
222
#ifdef HAVE_FONTCONFIG
 
223
FontAASettings::FontAASettings(QWidget *parent)
 
224
              : KDialog( parent ),
 
225
                changesMade(false)
 
226
{
 
227
  setObjectName( "FontAASettings" );
 
228
  setModal( true );
 
229
  setCaption( i18n("Configure Anti-Alias Settings") );
 
230
  setButtons( Ok|Cancel );
 
231
 
 
232
  QWidget     *mw=new QWidget(this);
 
233
  QFormLayout *layout=new QFormLayout(mw);
 
234
  layout->setMargin(0);
 
235
 
 
236
  excludeRange=new QCheckBox(i18n("E&xclude range:"), mw);
 
237
  QHBoxLayout *rangeLayout = new QHBoxLayout();
 
238
  excludeFrom=new KDoubleNumInput(0, 72, 8.0, mw, 1, 1);
 
239
  excludeFrom->setSuffix(i18n(" pt"));
 
240
  rangeLayout->addWidget(excludeFrom);
 
241
  excludeToLabel=new QLabel(i18n(" to "), mw);
 
242
  rangeLayout->addWidget(excludeToLabel);
 
243
  excludeTo=new KDoubleNumInput(0, 72, 15.0, mw, 1, 1);
 
244
  excludeTo->setSuffix(i18n(" pt"));
 
245
  rangeLayout->addWidget(excludeTo);
 
246
  layout->addRow(excludeRange, rangeLayout);
 
247
 
 
248
  QString subPixelWhatsThis = i18n("<p>If you have a TFT or LCD screen you"
 
249
       " can further improve the quality of displayed fonts by selecting"
 
250
       " this option.<br />Sub-pixel rendering is also known as ClearType(tm).<br />"
 
251
       " In order for sub-pixel rendering to"
 
252
       " work correctly you need to know how the sub-pixels of your display"
 
253
       " are aligned.</p>"
 
254
       " <p>On TFT or LCD displays a single pixel is actually composed of"
 
255
       " three sub-pixels, red, green and blue. Most displays"
 
256
       " have a linear ordering of RGB sub-pixel, some have BGR.<br />"
 
257
       " This feature does not work with CRT monitors.</p>" );
 
258
 
 
259
  useSubPixel=new QCheckBox(i18n("&Use sub-pixel rendering:"), mw);
 
260
  useSubPixel->setWhatsThis( subPixelWhatsThis );
 
261
 
 
262
  subPixelType=new QComboBox(mw);
 
263
  layout->addRow(useSubPixel, subPixelType);
 
264
 
 
265
  subPixelType->setEditable(false);
 
266
  subPixelType->setWhatsThis( subPixelWhatsThis );
 
267
 
 
268
  for(int t=KXftConfig::SubPixel::None+1; t<=KXftConfig::SubPixel::Vbgr; ++t)
 
269
    subPixelType->addItem(QPixmap(aaPixmaps[t-1]), i18n(KXftConfig::description((KXftConfig::SubPixel::Type)t).toUtf8()));
 
270
 
 
271
  QLabel *hintingLabel=new QLabel(i18n("Hinting style: "), mw);
 
272
  hintingStyle=new QComboBox(mw);
 
273
  hintingStyle->setEditable(false);
 
274
  layout->addRow(hintingLabel, hintingStyle);
 
275
  for(int s=KXftConfig::Hint::NotSet+1; s<=KXftConfig::Hint::Full; ++s)
 
276
    hintingStyle->addItem(i18n(KXftConfig::description((KXftConfig::Hint::Style)s).toUtf8()));
 
277
 
 
278
  QString hintingText(i18n("Hinting is a process used to enhance the quality of fonts at small sizes."));
 
279
  hintingStyle->setWhatsThis( hintingText);
 
280
  hintingLabel->setWhatsThis( hintingText);
 
281
  load();
 
282
  enableWidgets();
 
283
  setMainWidget(mw);
 
284
 
 
285
  connect(excludeRange, SIGNAL(toggled(bool)), SLOT(changed()));
 
286
  connect(useSubPixel, SIGNAL(toggled(bool)), SLOT(changed()));
 
287
  connect(excludeFrom, SIGNAL(valueChanged(double)), SLOT(changed()));
 
288
  connect(excludeTo, SIGNAL(valueChanged(double)), SLOT(changed()));
 
289
  connect(subPixelType, SIGNAL(activated(const QString &)), SLOT(changed()));
 
290
  connect(hintingStyle, SIGNAL(activated(const QString &)), SLOT(changed()));
 
291
}
 
292
 
 
293
bool FontAASettings::load()
 
294
{
 
295
  double     from, to;
 
296
  KXftConfig xft;
 
297
 
 
298
  if(xft.getExcludeRange(from, to))
 
299
     excludeRange->setChecked(true);
 
300
  else
 
301
  {
 
302
    excludeRange->setChecked(false);
 
303
    from=8.0;
 
304
    to=15.0;
 
305
  }
 
306
 
 
307
  excludeFrom->setValue(from);
 
308
  excludeTo->setValue(to);
 
309
 
 
310
  KXftConfig::SubPixel::Type spType;
 
311
 
 
312
  if(!xft.getSubPixelType(spType) || KXftConfig::SubPixel::None==spType)
 
313
    useSubPixel->setChecked(false);
 
314
  else
 
315
  {
 
316
    int idx=getIndex(spType);
 
317
 
 
318
    if(idx>-1)
 
319
    {
 
320
      useSubPixel->setChecked(true);
 
321
      subPixelType->setCurrentIndex(idx);
 
322
    }
 
323
    else
 
324
      useSubPixel->setChecked(false);
 
325
  }
 
326
 
 
327
  KXftConfig::Hint::Style hStyle;
 
328
 
 
329
  if(!xft.getHintStyle(hStyle) || KXftConfig::Hint::NotSet==hStyle)
 
330
  {
 
331
    KConfig kglobals("kdeglobals", KConfig::NoGlobals);
 
332
 
 
333
    hStyle=KXftConfig::Hint::Medium;
 
334
    xft.setHintStyle(hStyle);
 
335
    xft.apply();  // Save this setting
 
336
    KConfigGroup(&kglobals, "General").writeEntry("XftHintStyle", KXftConfig::toStr(hStyle));
 
337
    kglobals.sync();
 
338
    runRdb(KRdbExportXftSettings | KRdbExportGtkTheme);
 
339
  }
 
340
 
 
341
  hintingStyle->setCurrentIndex(getIndex(hStyle));
 
342
 
 
343
  enableWidgets();
 
344
 
 
345
  return xft.getAntiAliasing();
 
346
}
 
347
 
 
348
bool FontAASettings::save( bool useAA )
 
349
{
 
350
  KXftConfig   xft;
 
351
  KConfig      kglobals("kdeglobals", KConfig::NoGlobals);
 
352
  KConfigGroup grp(&kglobals, "General");
 
353
 
 
354
  xft.setAntiAliasing( useAA );
 
355
 
 
356
  if(excludeRange->isChecked())
 
357
    xft.setExcludeRange(excludeFrom->value(), excludeTo->value());
 
358
  else
 
359
    xft.setExcludeRange(0, 0);
 
360
 
 
361
  KXftConfig::SubPixel::Type spType(useSubPixel->isChecked()
 
362
                                        ? getSubPixelType()
 
363
                                        : KXftConfig::SubPixel::None);
 
364
 
 
365
  xft.setSubPixelType(spType);
 
366
  grp.writeEntry("XftSubPixel", KXftConfig::toStr(spType));
 
367
  grp.writeEntry("XftAntialias", useAA);
 
368
 
 
369
  bool mod=false;
 
370
  KXftConfig::Hint::Style hStyle(getHintStyle());
 
371
 
 
372
  xft.setHintStyle(hStyle);
 
373
 
 
374
  QString hs(KXftConfig::toStr(hStyle));
 
375
 
 
376
  if(!hs.isEmpty() && hs!=grp.readEntry("XftHintStyle"))
 
377
  {
 
378
    grp.writeEntry("XftHintStyle", hs);
 
379
    mod=true;
 
380
  }
 
381
  kglobals.sync();
 
382
 
 
383
  if(!mod)
 
384
    mod=xft.changed();
 
385
 
 
386
  xft.apply();
 
387
 
 
388
  return mod;
 
389
}
 
390
 
 
391
void FontAASettings::defaults()
 
392
{
 
393
  excludeRange->setChecked(true);
 
394
  excludeFrom->setValue(8.0);
 
395
  excludeTo->setValue(15.0);
 
396
  useSubPixel->setChecked(false);
 
397
  hintingStyle->setCurrentIndex(getIndex(KXftConfig::Hint::Medium));
 
398
  enableWidgets();
 
399
}
 
400
 
 
401
int FontAASettings::getIndex(KXftConfig::SubPixel::Type spType)
 
402
{
 
403
  int pos=-1;
 
404
  int index;
 
405
 
 
406
  for(index=0; index<subPixelType->count(); ++index)
 
407
    if(subPixelType->itemText(index)==i18n(KXftConfig::description(spType).toUtf8()))
 
408
    {
 
409
      pos=index;
 
410
      break;
 
411
    }
 
412
 
 
413
  return pos;
 
414
}
 
415
 
 
416
KXftConfig::SubPixel::Type FontAASettings::getSubPixelType()
 
417
{
 
418
  int t;
 
419
 
 
420
  for(t=KXftConfig::SubPixel::None; t<=KXftConfig::SubPixel::Vbgr; ++t)
 
421
    if(subPixelType->currentText()==i18n(KXftConfig::description((KXftConfig::SubPixel::Type)t).toUtf8()))
 
422
      return (KXftConfig::SubPixel::Type)t;
 
423
 
 
424
  return KXftConfig::SubPixel::None;
 
425
}
 
426
 
 
427
int FontAASettings::getIndex(KXftConfig::Hint::Style hStyle)
 
428
{
 
429
  int pos=-1;
 
430
  int index;
 
431
 
 
432
  for(index=0; index<hintingStyle->count(); ++index)
 
433
    if(hintingStyle->itemText(index)==i18n(KXftConfig::description(hStyle).toUtf8()))
 
434
    {
 
435
      pos=index;
 
436
      break;
 
437
    }
 
438
 
 
439
  return pos;
 
440
}
 
441
 
 
442
 
 
443
KXftConfig::Hint::Style FontAASettings::getHintStyle()
 
444
{
 
445
  int s;
 
446
 
 
447
  for(s=KXftConfig::Hint::NotSet; s<=KXftConfig::Hint::Full; ++s)
 
448
    if(hintingStyle->currentText()==i18n(KXftConfig::description((KXftConfig::Hint::Style)s).toUtf8()))
 
449
      return (KXftConfig::Hint::Style)s;
 
450
 
 
451
  return KXftConfig::Hint::Medium;
 
452
}
 
453
 
 
454
void FontAASettings::enableWidgets()
 
455
{
 
456
  excludeFrom->setEnabled(excludeRange->isChecked());
 
457
  excludeTo->setEnabled(excludeRange->isChecked());
 
458
  excludeToLabel->setEnabled(excludeRange->isChecked());
 
459
  subPixelType->setEnabled(useSubPixel->isChecked());
 
460
#ifdef FT_LCD_FILTER_H
 
461
  static int ft_has_subpixel = -1;
 
462
  if( ft_has_subpixel == -1 ) {
 
463
    FT_Library            ftLibrary;
 
464
    if(FT_Init_FreeType(&ftLibrary) == 0) {
 
465
      ft_has_subpixel = ( FT_Library_SetLcdFilter(ftLibrary, FT_LCD_FILTER_DEFAULT )
 
466
        == FT_Err_Unimplemented_Feature ) ? 0 : 1;
 
467
      FT_Done_FreeType(ftLibrary);
 
468
    }
 
469
  }
 
470
  useSubPixel->setEnabled(ft_has_subpixel);
 
471
  subPixelType->setEnabled(ft_has_subpixel);
 
472
#endif
 
473
}
 
474
#endif
 
475
 
 
476
void FontAASettings::changed()
 
477
{
 
478
#ifdef HAVE_FONTCONFIG
 
479
    changesMade=true;
 
480
    enableWidgets();
 
481
#endif
 
482
}
 
483
 
 
484
#ifdef HAVE_FONTCONFIG
 
485
int FontAASettings::exec()
 
486
{
 
487
    int i=KDialog::exec();
 
488
 
 
489
    if(!i)
 
490
        load(); // Reset settings...
 
491
 
 
492
    return i && changesMade;
 
493
}
 
494
#endif
 
495
 
 
496
/**** KFonts ****/
 
497
 
 
498
KFonts::KFonts(QWidget *parent, const QVariantList &args)
 
499
    :   KCModule(FontFactory::componentData(), parent, args)
 
500
{
 
501
  QStringList nameGroupKeyRc;
 
502
 
 
503
  nameGroupKeyRc
 
504
    << i18nc("font usage", "General")       << "General"    << "font"         << ""
 
505
    << i18nc("font usage", "Fixed width")   << "General"    << "fixed"        << ""
 
506
    << i18nc("font usage", "Small")         << "General"    << "smallestReadableFont" << ""
 
507
    << i18nc("font usage", "Toolbar")       << "General"    << "toolBarFont"  << ""
 
508
    << i18nc("font usage", "Menu")          << "General"    << "menuFont"     << ""
 
509
    << i18nc("font usage", "Window title")  << "WM"         << "activeFont"   << ""
 
510
    << i18nc("font usage", "Taskbar")       << "General"    << "taskbarFont"  << ""
 
511
    << i18nc("font usage", "Desktop")       << "General"    << "desktopFont" << "";
 
512
 
 
513
  QList<QFont> defaultFontList;
 
514
 
 
515
  // NOTE: keep in sync with kdelibs/kdeui/kernel/kglobalsettings.cpp
 
516
 
 
517
  QFont f0("Sans Serif", 9); // general/menu/desktop
 
518
  QFont f1("Monospace", 9);
 
519
  QFont f2("Sans Serif", 8); // toolbar
 
520
  QFont f3("Sans Serif", 8); // window title
 
521
  QFont f4("Sans Serif", 9); // taskbar
 
522
  QFont f5("Sans Serif", 8); // smallestReadableFont
 
523
 
 
524
  defaultFontList << f0 << f1 << f5 << f2 << f0 << f3 << f4 << f0;
 
525
 
 
526
  QList<bool> fixedList;
 
527
 
 
528
  fixedList
 
529
    <<  false
 
530
    <<  true
 
531
    <<  false
 
532
    <<  false
 
533
    <<  false
 
534
    <<  false
 
535
    <<  false
 
536
    <<  false;
 
537
 
 
538
  QStringList quickHelpList;
 
539
 
 
540
  quickHelpList
 
541
    << i18n("Used for normal text (e.g. button labels, list items).")
 
542
    << i18n("A non-proportional font (i.e. typewriter font).")
 
543
    << i18n("Smallest font that is still readable well.")
 
544
    << i18n("Used to display text beside toolbar icons.")
 
545
    << i18n("Used by menu bars and popup menus.")
 
546
    << i18n("Used by the window titlebar.")
 
547
    << i18n("Used by the taskbar.")
 
548
    << i18n("Used for desktop icons.");
 
549
 
 
550
  QVBoxLayout * layout = new QVBoxLayout(this );
 
551
  layout->setMargin(0);
 
552
 
 
553
  QGridLayout * fontUseLayout = new QGridLayout( );
 
554
  layout->addLayout( fontUseLayout );
 
555
  fontUseLayout->setColumnStretch(0, 0);
 
556
  fontUseLayout->setColumnStretch(1, 1);
 
557
  fontUseLayout->setColumnStretch(2, 0);
 
558
 
 
559
  QList<QFont>::ConstIterator defaultFontIt(defaultFontList.begin());
 
560
  QList<bool>::ConstIterator fixedListIt(fixedList.begin());
 
561
  QStringList::ConstIterator quickHelpIt(quickHelpList.begin());
 
562
  QStringList::ConstIterator it(nameGroupKeyRc.begin());
 
563
 
 
564
  unsigned int count = 0;
 
565
 
 
566
  while (it != nameGroupKeyRc.constEnd()) {
 
567
 
 
568
    QString name = *it; it++;
 
569
    QString group = *it; it++;
 
570
    QString key = *it; it++;
 
571
    QString file = *it; it++;
 
572
 
 
573
    FontUseItem * i =
 
574
      new FontUseItem(
 
575
        this,
 
576
        name,
 
577
        group,
 
578
        key,
 
579
        file,
 
580
        *defaultFontIt++,
 
581
        *fixedListIt++
 
582
      );
 
583
 
 
584
    fontUseList.append(i);
 
585
    connect(i, SIGNAL(fontSelected(const QFont &)), SLOT(fontSelected()));
 
586
 
 
587
    QLabel * fontUse = new QLabel(i18nc("Font role", "%1: ", name), this);
 
588
    fontUse->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
 
589
    fontUse->setWhatsThis( *quickHelpIt++);
 
590
 
 
591
    fontUseLayout->addWidget(fontUse, count, 0);
 
592
    fontUseLayout->addWidget(i, count, 1);
 
593
 
 
594
    ++count;
 
595
  }
 
596
 
 
597
   QHBoxLayout *hblay = new QHBoxLayout();
 
598
   layout->addLayout(hblay);
 
599
   hblay->addStretch();
 
600
   QPushButton * fontAdjustButton = new QPushButton(i18n("Ad&just All Fonts..."), this);
 
601
   fontAdjustButton->setWhatsThis( i18n("Click to change all fonts"));
 
602
   hblay->addWidget( fontAdjustButton );
 
603
   connect(fontAdjustButton, SIGNAL(clicked()), SLOT(slotApplyFontDiff()));
 
604
 
 
605
   layout->addSpacing(KDialog::spacingHint());
 
606
 
 
607
   QGridLayout* lay = new QGridLayout();
 
608
   layout->addLayout(lay);
 
609
   lay->setColumnStretch( 3, 10 );
 
610
   QLabel* label=0L;
 
611
#ifdef HAVE_FONTCONFIG
 
612
   label = new QLabel( i18n( "Use a&nti-aliasing:" ), this );
 
613
   label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
 
614
   lay->addWidget( label, 0, 0 );
 
615
   cbAA = new QComboBox( this );
 
616
   cbAA->insertItem( AAEnabled, i18nc( "Use anti-aliasing", "Enabled" )); // change AASetting type if order changes
 
617
   cbAA->insertItem( AASystem, i18nc( "Use anti-aliasing", "System Settings" ));
 
618
   cbAA->insertItem( AADisabled, i18nc( "Use anti-aliasing", "Disabled" ));
 
619
   cbAA->setWhatsThis( i18n("If this option is selected, KDE will smooth the edges of curves in "
 
620
                              "fonts."));
 
621
   aaSettingsButton = new QPushButton( i18n( "Configure..." ), this);
 
622
   connect(aaSettingsButton, SIGNAL(clicked()), SLOT(slotCfgAa()));
 
623
   label->setBuddy( cbAA );
 
624
   lay->addWidget( cbAA, 0, 1 );
 
625
   lay->addWidget( aaSettingsButton, 0, 2 );
 
626
   connect(cbAA, SIGNAL(activated(int)), SLOT(slotUseAntiAliasing()));
 
627
#endif
 
628
   label = new QLabel( i18n( "Force fonts DPI:" ), this );
 
629
   label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
 
630
   lay->addWidget( label, 1, 0 );
 
631
   comboForceDpi = new QComboBox( this );
 
632
   label->setBuddy( comboForceDpi );
 
633
   comboForceDpi->insertItem( DPINone, i18nc("Force fonts DPI", "Disabled" )); // change DPISetti ng type if order changes
 
634
   comboForceDpi->insertItem( DPI96, i18n( "96 DPI" ));
 
635
   comboForceDpi->insertItem( DPI120, i18n( "120 DPI" ));
 
636
   QString whatsthis = i18n(
 
637
       "<p>This option forces a specific DPI value for fonts. It may be useful"
 
638
       " when the real DPI of the hardware is not detected properly and it"
 
639
       " is also often misused when poor quality fonts are used that do not"
 
640
       " look well with DPI values other than 96 or 120 DPI.</p>"
 
641
       "<p>The use of this option is generally discouraged. For selecting proper DPI"
 
642
       " value a better option is explicitly configuring it for the whole X server if"
 
643
       " possible (e.g. DisplaySize in xorg.conf or adding <i>-dpi value</i> to"
 
644
       " ServerLocalArgs= in $KDEDIR/share/config/kdm/kdmrc). When fonts do not render"
 
645
       " properly with real DPI value better fonts should be used or configuration"
 
646
       " of font hinting should be checked.</p>" );
 
647
   comboForceDpi->setWhatsThis(whatsthis);
 
648
   connect( comboForceDpi, SIGNAL( activated( int )), SLOT( changed()));
 
649
   lay->addWidget( comboForceDpi, 1, 1 );
 
650
 
 
651
   layout->addStretch(1);
 
652
 
 
653
#ifdef HAVE_FONTCONFIG
 
654
   aaSettings=new FontAASettings(this);
 
655
#endif
 
656
 
 
657
}
 
658
 
 
659
KFonts::~KFonts()
 
660
{
 
661
  QList<FontUseItem *>::Iterator it(fontUseList.begin()),
 
662
                                 end(fontUseList.end());
 
663
 
 
664
  for(; it!=end; ++it)
 
665
    delete (*it);
 
666
  fontUseList.clear();
 
667
}
 
668
 
 
669
void KFonts::fontSelected()
 
670
{
 
671
  emit changed(true);
 
672
}
 
673
 
 
674
void KFonts::defaults()
 
675
{
 
676
  for ( int i = 0; i < (int) fontUseList.count(); i++ )
 
677
    fontUseList.at( i )->setDefault();
 
678
 
 
679
#ifdef HAVE_FONTCONFIG
 
680
  useAA = AASystem;
 
681
  cbAA->setCurrentIndex( useAA );
 
682
  aaSettings->defaults();
 
683
#endif
 
684
  comboForceDpi->setCurrentIndex( DPINone );
 
685
  emit changed(true);
 
686
}
 
687
 
 
688
void KFonts::load()
 
689
{
 
690
  QList<FontUseItem *>::Iterator it(fontUseList.begin()),
 
691
                                 end(fontUseList.end());
 
692
 
 
693
  for(; it!=end; ++it)
 
694
    (*it)->readFont();
 
695
 
 
696
#ifdef HAVE_FONTCONFIG
 
697
  useAA_original = useAA = aaSettings->load() ? AAEnabled : AADisabled;
 
698
  cbAA->setCurrentIndex( useAA );
 
699
#endif
 
700
 
 
701
  KConfig _cfgfonts( "kcmfonts" );
 
702
  KConfigGroup cfgfonts(&_cfgfonts, "General");
 
703
  int dpicfg = cfgfonts.readEntry( "forceFontDPI", 0 );
 
704
  DPISetting dpi = dpicfg == 120 ? DPI120 : dpicfg == 96 ? DPI96 : DPINone;
 
705
  comboForceDpi->setCurrentIndex( dpi );
 
706
  dpi_original = dpi;
 
707
#ifdef HAVE_FONTCONFIG
 
708
  if( cfgfonts.readEntry( "dontChangeAASettings", true )) {
 
709
      useAA_original = useAA = AASystem;
 
710
      cbAA->setCurrentIndex( useAA );
 
711
  }
 
712
  aaSettingsButton->setEnabled( cbAA->currentIndex() == AAEnabled );
 
713
#endif
 
714
 
 
715
  emit changed(false);
 
716
}
 
717
 
 
718
void KFonts::save()
 
719
{
 
720
  QList<FontUseItem *>::Iterator it(fontUseList.begin()),
 
721
                                 end(fontUseList.end());
 
722
 
 
723
  for(; it!=end; ++it)
 
724
      (*it)->writeFont();
 
725
 
 
726
  KGlobal::config()->sync();
 
727
 
 
728
  KConfig _cfgfonts( "kcmfonts" );
 
729
  KConfigGroup cfgfonts(&_cfgfonts, "General");
 
730
  DPISetting dpi = static_cast< DPISetting >( comboForceDpi->currentIndex());
 
731
  const int dpi2value[] = { 0, 96, 120 };
 
732
  cfgfonts.writeEntry( "forceFontDPI", dpi2value[ dpi ] );
 
733
#ifdef HAVE_FONTCONFIG
 
734
  cfgfonts.writeEntry( "dontChangeAASettings", cbAA->currentIndex() == AASystem );
 
735
#endif
 
736
  cfgfonts.sync();
 
737
  // if the setting is reset in the module, remove the dpi value,
 
738
  // otherwise don't explicitly remove it and leave any possible system-wide value
 
739
  if( dpi == DPINone && dpi_original != DPINone ) {
 
740
      KProcess proc;
 
741
      proc << "xrdb" << "-quiet" << "-remove" << "-nocpp";
 
742
      proc.start();
 
743
      if (proc.waitForStarted()) {
 
744
        proc.write( QByteArray( "Xft.dpi\n" ) );
 
745
        proc.closeWriteChannel();
 
746
        proc.waitForFinished();
 
747
      }
 
748
  }
 
749
 
 
750
  // KDE-1.x support
 
751
  {
 
752
  KConfig config( QDir::homePath() + "/.kderc", KConfig::SimpleConfig);
 
753
  KConfigGroup grp(&config, "General");
 
754
 
 
755
  for(it=fontUseList.begin(); it!=end; ++it) {
 
756
      if("font"==(*it)->rcKey())
 
757
          QSettings().setValue("/qt/font", (*it)->font().toString());
 
758
      kDebug(1208) << "write entry " <<  (*it)->rcKey();
 
759
      grp.writeEntry( (*it)->rcKey(), (*it)->font() );
 
760
  }
 
761
  config.sync();
 
762
  }
 
763
 
 
764
  KGlobalSettings::self()->emitChange(KGlobalSettings::FontChanged);
 
765
 
 
766
  kapp->processEvents(); // Process font change ourselves
 
767
 
 
768
 
 
769
  // Don't overwrite global settings unless explicitly asked for - e.g. the system
 
770
  // fontconfig setup may be much more complex than this module can provide.
 
771
  // TODO: With AASystem the changes already made by this module should be reverted somehow.
 
772
#ifdef HAVE_FONTCONFIG
 
773
  bool aaSave = false;
 
774
  if( cbAA->currentIndex() != AASystem )
 
775
      aaSave = aaSettings->save( useAA == AAEnabled );
 
776
 
 
777
  if( aaSave || (useAA != useAA_original) || dpi != dpi_original) {
 
778
    KMessageBox::information(this,
 
779
      i18n(
 
780
        "<p>Some changes such as anti-aliasing will only affect newly started applications.</p>"
 
781
      ), i18n("Font Settings Changed"), "FontSettingsChanged");
 
782
    useAA_original = useAA;
 
783
    dpi_original = dpi;
 
784
  }
 
785
#else
 
786
  if(dpi != dpi_original) {
 
787
    KMessageBox::information(this,
 
788
      i18n(
 
789
        "<p>Some changes such as DPI will only affect newly started applications.</p>"
 
790
      ), i18n("Font Settings Changed"), "FontSettingsChanged");
 
791
    dpi_original = dpi;
 
792
  }
 
793
#endif
 
794
  runRdb(KRdbExportXftSettings | KRdbExportGtkTheme);
 
795
 
 
796
  emit changed(false);
 
797
}
 
798
 
 
799
 
 
800
void KFonts::slotApplyFontDiff()
 
801
{
 
802
  QFont font = QFont(fontUseList.first()->font());
 
803
        KFontChooser::FontDiffFlags fontDiffFlags = 0;
 
804
  int ret = KFontDialog::getFontDiff(font,fontDiffFlags,KFontChooser::NoDisplayFlags,this);
 
805
 
 
806
  if (ret == KDialog::Accepted && fontDiffFlags)
 
807
  {
 
808
    for ( int i = 0; i < (int) fontUseList.count(); i++ )
 
809
      fontUseList.at( i )->applyFontDiff( font,fontDiffFlags );
 
810
    emit changed(true);
 
811
  }
 
812
}
 
813
 
 
814
void KFonts::slotUseAntiAliasing()
 
815
{
 
816
#ifdef HAVE_FONTCONFIG
 
817
    useAA = static_cast< AASetting >( cbAA->currentIndex());
 
818
    aaSettingsButton->setEnabled( cbAA->currentIndex() == AAEnabled );
 
819
    emit changed(true);
 
820
#endif
 
821
}
 
822
 
 
823
void KFonts::slotCfgAa()
 
824
{
 
825
#ifdef HAVE_FONTCONFIG
 
826
  if(aaSettings->exec())
 
827
  {
 
828
    emit changed(true);
 
829
  }
 
830
#endif
 
831
}
 
832
 
 
833
// vim:ts=2:sw=2:tw=78