fonts.cpp 19.9 KB
Newer Older
1 2 3 4
/*
    Copyright 1997 Mark Donohoe
    Copyright 1999 Lars Knoll
    Copyright 2000 Rik Hemsley
Marco Martin's avatar
Marco Martin committed
5 6
    Copyright 2015 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org>
    Copyright 2017 Marco Martin <mart@kde.org>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

    Ported to kcontrol2 by Geert Jansen.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/
25 26 27 28


#include "fonts.h"

Marco Martin's avatar
Marco Martin committed
29 30 31 32 33 34
#include <QQuickItem>
#include <QWindow>
#include <QQmlEngine>
#include <QQuickView>
#include <QDebug>
#include <QFontDialog>
35
#include <QApplication>
Marco Martin's avatar
Marco Martin committed
36
#include <QFontDatabase>
37 38 39

#include <KAcceleratorManager>
#include <KGlobalSettings>
40
#include <KConfigGroup>
Marco Martin's avatar
Marco Martin committed
41 42
#include <KConfig>
#include <KAboutData>
43 44
#include <KLocalizedString>
#include <KPluginFactory>
Marco Martin's avatar
Marco Martin committed
45
#include <KFontDialog>
46
#include <KWindowSystem>
Marco Martin's avatar
Marco Martin committed
47
#include <KMessageBox>
48 49

#include "../krdb/krdb.h"
Julian Wolff's avatar
Julian Wolff committed
50
#include "previewimageprovider.h"
51 52

/**** DLL Interface ****/
Marco Martin's avatar
Marco Martin committed
53
K_PLUGIN_FACTORY_WITH_JSON(KFontsFactory, "kcm_fonts.json", registerPlugin<KFonts>();)
54

Marco Martin's avatar
Marco Martin committed
55 56 57 58
//from KFontRequester
// Determine if the font with given properties is available on the system,
// otherwise find and return the best fitting combination.
static QFont nearestExistingFont(const QFont &font)
59
{
Marco Martin's avatar
Marco Martin committed
60 61
    QFontDatabase dbase;

Yuri Chornoivan's avatar
Yuri Chornoivan committed
62
    // Initialize font data according to given font object.
Marco Martin's avatar
Marco Martin committed
63 64 65 66 67 68 69 70 71 72 73 74
    QString family = font.family();
    QString style = dbase.styleString(font);
    qreal size = font.pointSizeF();

    // Check if the family exists.
    const QStringList families = dbase.families();
    if (!families.contains(family)) {
        // Chose another family.
        family = QFontInfo(font).family(); // the nearest match
        if (!families.contains(family)) {
            family = families.count() ? families.at(0) : QStringLiteral("fixed");
        }
75 76
    }

Marco Martin's avatar
Marco Martin committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    // Check if the family has the requested style.
    // Easiest by piping it through font selection in the database.
    QString retStyle = dbase.styleString(dbase.font(family, style, 10));
    style = retStyle;

    // Check if the family has the requested size.
    // Only for bitmap fonts.
    if (!dbase.isSmoothlyScalable(family, style)) {
        QList<int> sizes = dbase.smoothSizes(family, style);
        if (!sizes.contains(size)) {
            // Find nearest available size.
            int mindiff = 1000;
            int refsize = size;
            Q_FOREACH (int lsize, sizes) {
                int diff = qAbs(refsize - lsize);
                if (mindiff > diff) {
                    mindiff = diff;
                    size = lsize;
                }
            }
97 98
        }
    }
99

Marco Martin's avatar
Marco Martin committed
100 101 102 103 104 105
    // Select the font with confirmed properties.
    QFont result = dbase.font(family, style, int(size));
    if (dbase.isSmoothlyScalable(family, style) && result.pointSize() == floor(size)) {
        result.setPointSizeF(size);
    }
    return result;
106 107 108 109
}

/**** FontAASettings ****/
#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
Marco Martin's avatar
Marco Martin committed
110 111 112 113 114
FontAASettings::FontAASettings(QObject *parent)
    : QObject(parent)
    , m_subPixelOptionsModel(new QStandardItemModel(this))
    , m_hintingOptionsModel(new QStandardItemModel(this))
{
115
    for (int t = KXftConfig::SubPixel::NotSet; t <= KXftConfig::SubPixel::Vbgr; ++t) {
Marco Martin's avatar
Marco Martin committed
116 117
        QStandardItem *item = new QStandardItem(KXftConfig::description((KXftConfig::SubPixel::Type)t));
        m_subPixelOptionsModel->appendRow(item);
118 119
    }

120
    for (int s = KXftConfig::Hint::NotSet; s <= KXftConfig::Hint::Full; ++s) {
Marco Martin's avatar
Marco Martin committed
121 122
        QStandardItem * item = new QStandardItem(KXftConfig::description((KXftConfig::Hint::Style)s));
        m_hintingOptionsModel->appendRow(item);
123
    }
124 125
}

Marco Martin's avatar
Marco Martin committed
126
void FontAASettings::load()
127
{
128 129 130 131
    double     from, to;
    KXftConfig xft;

    if (xft.getExcludeRange(from, to)) {
Marco Martin's avatar
Marco Martin committed
132 133 134
        m_excludeFrom = from;
        m_excludeTo = to;
        setExclude(true);
135
    } else {
Marco Martin's avatar
Marco Martin committed
136 137 138
        m_excludeFrom = 8;
        m_excludeTo = 15;
        setExclude(false);
139
    }
Marco Martin's avatar
Marco Martin committed
140 141 142 143
    m_excludeFromOriginal = m_excludeFrom;
    m_excludeToOriginal = m_excludeTo;
    excludeToChanged();
    excludeFromChanged();
144

145
    KXftConfig::SubPixel::Type spType;
146
    xft.getSubPixelType(spType);
147

Marco Martin's avatar
Marco Martin committed
148
    setSubPixelCurrentIndex(spType);
149
    m_subPixelCurrentIndexOriginal = spType;
150

151
    KXftConfig::Hint::Style hStyle;
152

153
    if (!xft.getHintStyle(hStyle) || KXftConfig::Hint::NotSet == hStyle) {
Marco Martin's avatar
Marco Martin committed
154 155
        KConfig kglobals("kdeglobals", KConfig::NoGlobals);

156
        hStyle = KXftConfig::Hint::NotSet;
157
        xft.setHintStyle(hStyle);
Marco Martin's avatar
Marco Martin committed
158 159
        KConfigGroup(&kglobals, "General").writeEntry("XftHintStyle", KXftConfig::toStr(hStyle));
        kglobals.sync();
160 161
        runRdb(KRdbExportXftSettings | KRdbExportGtkTheme);
    }
162

Marco Martin's avatar
Marco Martin committed
163
    setHintingCurrentIndex(hStyle);
164
    m_hintingCurrentIndexOriginal = hStyle;
165

Marco Martin's avatar
Marco Martin committed
166 167
    KConfig _cfgfonts("kcmfonts");
    KConfigGroup cfgfonts(&_cfgfonts, "General");
168

Marco Martin's avatar
Marco Martin committed
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
    int dpicfg;
    if (KWindowSystem::isPlatformWayland()) {
        dpicfg = cfgfonts.readEntry("forceFontDPIWayland", 0);
    } else {
        dpicfg = cfgfonts.readEntry("forceFontDPI", 0);
    }

    if (dpicfg <= 0) {
        m_dpiOriginal = 0;
    } else {
        m_dpiOriginal = dpicfg;
    };

    setDpi(dpicfg);

    KSharedConfig::Ptr config = KSharedConfig::openConfig("kdeglobals");
    KConfigGroup cg(config, "General");

    if (cfgfonts.readEntry("dontChangeAASettings", true)) {
        setAntiAliasing(1); //AASystem
    } else if (cg.readEntry("XftAntialias", true)) {
        setAntiAliasing(0); //AAEnabled
    } else {
        setAntiAliasing(2); //AADisabled
    }
    m_antiAliasingOriginal = m_antiAliasing;
195 196
}

197
bool FontAASettings::save(KXftConfig::AntiAliasing::State aaState)
198
{
199 200 201
    KXftConfig   xft;
    KConfig      kglobals("kdeglobals", KConfig::NoGlobals);
    KConfigGroup grp(&kglobals, "General");
202

203
    xft.setAntiAliasing(aaState);
Marco Martin's avatar
Marco Martin committed
204 205
    if (m_exclude) {
        xft.setExcludeRange(m_excludeFrom, m_excludeTo);
206 207 208
    } else {
        xft.setExcludeRange(0, 0);
    }
209

Marco Martin's avatar
Marco Martin committed
210
    KXftConfig::SubPixel::Type spType = (KXftConfig::SubPixel::Type)m_subPixelCurrentIndex;
211

212
    xft.setSubPixelType(spType);
Marco Martin's avatar
Marco Martin committed
213 214
    grp.writeEntry("XftSubPixel", KXftConfig::toStr(spType));
    if (aaState == KXftConfig::AntiAliasing::NotSet) {
215 216 217 218
        grp.revertToDefault("XftAntialias");
    } else {
        grp.writeEntry("XftAntialias", aaState == KXftConfig::AntiAliasing::Enabled);
    }
219

220
    bool mod = false;
Marco Martin's avatar
Marco Martin committed
221
    KXftConfig::Hint::Style hStyle = (KXftConfig::Hint::Style)m_hintingCurrentIndex;
222

223
    xft.setHintStyle(hStyle);
224

225
    QString hs(KXftConfig::toStr(hStyle));
Marco Martin's avatar
Marco Martin committed
226

227 228 229 230 231 232
    if (hs != grp.readEntry("XftHintStyle")) {
        if (KXftConfig::Hint::NotSet == hStyle) {
            grp.revertToDefault("XftHintStyle");
        } else {
            grp.writeEntry("XftHintStyle", hs);
        }
233
    }
234
    mod = true;
235
    kglobals.sync();
236

237 238 239
    if (!mod) {
        mod = xft.changed();
    }
240

241
    xft.apply();
242

Marco Martin's avatar
Marco Martin committed
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
    KConfig _cfgfonts("kcmfonts");
    KConfigGroup cfgfonts(&_cfgfonts, "General");

    if (KWindowSystem::isPlatformWayland()) {
        cfgfonts.writeEntry("forceFontDPIWayland", m_dpi);
    } else {
        cfgfonts.writeEntry("forceFontDPI", m_dpi);
    }

    cfgfonts.sync();

#if HAVE_X11
    // if the setting is reset in the module, remove the dpi value,
    // otherwise don't explicitly remove it and leave any possible system-wide value
    if (m_dpi == 0 && m_dpiOriginal != 0 && !KWindowSystem::isPlatformWayland()) {
        QProcess proc;
        proc.setProcessChannelMode(QProcess::ForwardedChannels);
        proc.start("xrdb", QStringList() << "-quiet" << "-remove" << "-nocpp");
        if (proc.waitForStarted()) {
            proc.write(QByteArray("Xft.dpi\n"));
            proc.closeWriteChannel();
            proc.waitForFinished();
        }
    }
#endif

    QApplication::processEvents();			// Process font change ourselves

    // Don't overwrite global settings unless explicitly asked for - e.g. the system
    // fontconfig setup may be much more complex than this module can provide.
    // TODO: With AASystem the changes already made by this module should be reverted somehow.
#if defined(HAVE_FONTCONFIG) && defined (HAVE_X11)
    if (mod || (m_antiAliasing != m_antiAliasingOriginal) || m_dpi != m_dpiOriginal) {
276
        KMessageBox::information(nullptr,
Marco Martin's avatar
Marco Martin committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
                                 i18n(
                                     "<p>Some changes such as anti-aliasing or DPI will only affect newly started applications.</p>"
                                 ), i18n("Font Settings Changed"), "FontSettingsChanged");
        m_antiAliasingOriginal = m_antiAliasing;
        m_dpiOriginal = m_dpi;
    }
#else
#if HAVE_X11
    if (m_dpi != m_dpiOriginal) {
        KMessageBox::information(0,
                                 i18n(
                                     "<p>Some changes such as DPI will only affect newly started applications.</p>"
                                 ), i18n("Font Settings Changed"), "FontSettingsChanged");
        m_dpiOriginal = m_dpi;
    }
#endif
#endif

    m_excludeToOriginal = m_excludeTo;
    m_excludeFromOriginal = m_excludeFrom;
297 298 299
    
    m_subPixelCurrentIndexOriginal = m_subPixelCurrentIndex;
    m_hintingCurrentIndexOriginal = m_hintingCurrentIndex;
Marco Martin's avatar
Marco Martin committed
300

301
    return mod;
302 303 304 305
}

void FontAASettings::defaults()
{
Marco Martin's avatar
Marco Martin committed
306 307 308 309
    setExcludeTo(15);
    setExcludeFrom(8);
    setAntiAliasing(1);
    m_antiAliasingOriginal = m_antiAliasing;
310
    setDpi(0);
Marco Martin's avatar
Marco Martin committed
311 312
    setSubPixelCurrentIndex(KXftConfig::SubPixel::NotSet);
    setHintingCurrentIndex(KXftConfig::Hint::NotSet);
313 314
}

Marco Martin's avatar
Marco Martin committed
315 316 317
#endif

void FontAASettings::setExclude(bool exclude)
318
{
Marco Martin's avatar
Marco Martin committed
319 320 321
    if (exclude == m_exclude) {
        return;
    }
322

Marco Martin's avatar
Marco Martin committed
323 324 325
    m_exclude = exclude;
    emit excludeChanged();
}
326

Marco Martin's avatar
Marco Martin committed
327 328 329
bool FontAASettings::exclude() const
{
    return m_exclude;
330 331
}

Marco Martin's avatar
Marco Martin committed
332
void FontAASettings::setExcludeTo(const int &excludeTo)
333
{
Marco Martin's avatar
Marco Martin committed
334 335 336
    if (m_excludeTo == excludeTo) {
        return;
    }
337

Marco Martin's avatar
Marco Martin committed
338 339 340
    m_excludeTo = excludeTo;
    emit excludeToChanged();
}
341

Marco Martin's avatar
Marco Martin committed
342 343 344
int FontAASettings::excludeTo() const
{
    return m_excludeTo;
345 346
}

Marco Martin's avatar
Marco Martin committed
347
void FontAASettings::setExcludeFrom(const int &excludeTo)
348
{
Marco Martin's avatar
Marco Martin committed
349 350 351
    if (m_excludeFrom == excludeTo) {
        return;
    }
352

Marco Martin's avatar
Marco Martin committed
353 354 355
    m_excludeFrom = excludeTo;
    emit excludeToChanged();
}
356

Marco Martin's avatar
Marco Martin committed
357 358 359
int FontAASettings::excludeFrom() const
{
    return m_excludeFrom;
360
}
361

Marco Martin's avatar
Marco Martin committed
362
void FontAASettings::setAntiAliasing(const int &antiAliasing)
363
{
Marco Martin's avatar
Marco Martin committed
364 365 366
    if (m_antiAliasing == antiAliasing) {
        return;
    }
367

Marco Martin's avatar
Marco Martin committed
368 369 370
    m_antiAliasing = antiAliasing;
    emit aliasingChanged();
}
371

Marco Martin's avatar
Marco Martin committed
372 373 374
int FontAASettings::antiAliasing() const
{
    return m_antiAliasing;
375 376
}

Marco Martin's avatar
Marco Martin committed
377
void FontAASettings::setDpi(const int &dpi)
378
{
Marco Martin's avatar
Marco Martin committed
379 380
    if (m_dpi == dpi) {
        return;
381
    }
Marco Martin's avatar
Marco Martin committed
382 383 384

    m_dpi = dpi;
    emit dpiChanged();
385 386
}

Marco Martin's avatar
Marco Martin committed
387
int FontAASettings::dpi() const
388
{
Marco Martin's avatar
Marco Martin committed
389
    return m_dpi;
390 391
}

Marco Martin's avatar
Marco Martin committed
392
void FontAASettings::setSubPixelCurrentIndex(int idx)
393
{
Marco Martin's avatar
Marco Martin committed
394 395 396 397 398 399 400
    if (m_subPixelCurrentIndex == idx) {
        return;
    }

    m_subPixelCurrentIndex = idx;
    emit subPixelCurrentIndexChanged();
}
401

Marco Martin's avatar
Marco Martin committed
402 403 404 405 406 407 408 409 410
int FontAASettings::subPixelCurrentIndex()
{
    return m_subPixelCurrentIndex;
}

void FontAASettings::setHintingCurrentIndex(int idx)
{
    if (m_hintingCurrentIndex == idx) {
        return;
411
    }
412

Marco Martin's avatar
Marco Martin committed
413 414
    m_hintingCurrentIndex = idx;
    emit hintingCurrentIndexChanged();
415 416
}

Marco Martin's avatar
Marco Martin committed
417 418 419 420
int FontAASettings::hintingCurrentIndex()
{
    return m_hintingCurrentIndex;
}
421

Marco Martin's avatar
Marco Martin committed
422
bool FontAASettings::needsSave() const
423
{
424 425 426 427 428 429
    return m_excludeTo != m_excludeToOriginal
        || m_excludeFrom != m_excludeFromOriginal
        || m_antiAliasing != m_antiAliasingOriginal
        || m_dpi != m_dpiOriginal
        || m_subPixelCurrentIndex != m_subPixelCurrentIndexOriginal
        || m_hintingCurrentIndex != m_hintingCurrentIndexOriginal;
Marco Martin's avatar
Marco Martin committed
430
}
431 432


Marco Martin's avatar
Marco Martin committed
433
/**** KFonts ****/
434

Marco Martin's avatar
Marco Martin committed
435 436 437 438 439
KFonts::KFonts(QObject *parent, const QVariantList &args)
    : KQuickAddons::ConfigModule(parent, args)
    , m_fontAASettings(new FontAASettings(this))
{
    qApp->setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
Yuri Chornoivan's avatar
Yuri Chornoivan committed
440
    KAboutData* about = new KAboutData("kcm_fonts", i18n("Configure Fonts"),
Marco Martin's avatar
Marco Martin committed
441 442 443 444 445 446 447 448 449
                                       "0.1", QString(), KAboutLicense::LGPL);
    about->addAuthor(i18n("Antonis Tsiapaliokas"), QString(), "antonis.tsiapaliokas@kde.org");
    setAboutData(about);
    qmlRegisterType<QStandardItemModel>();
    setButtons(Apply | Default);

    auto updateState = [this]() {
        setNeedsSave(m_fontAASettings->needsSave());
    };
450

Marco Martin's avatar
Marco Martin committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    connect(m_fontAASettings, &FontAASettings::subPixelCurrentIndexChanged, this, updateState);
    connect(m_fontAASettings, &FontAASettings::hintingCurrentIndexChanged, this, updateState);
    connect(m_fontAASettings, &FontAASettings::excludeToChanged, this, updateState);
    connect(m_fontAASettings, &FontAASettings::antiAliasingChanged, this, updateState);
    connect(m_fontAASettings, &FontAASettings::aliasingChanged, this, updateState);
    connect(m_fontAASettings, &FontAASettings::dpiChanged, this, updateState);
}

KFonts::~KFonts()
{
}

void KFonts::defaults()
{
#ifdef Q_OS_MAC
    setGeneralFont(QFont("Lucida Grande", 13));
    setMenuFont(QFont("Lucida Grande", 13));
    setFixedWidthFont(QFont("Monaco", 10));
    setToolbarFont(QFont("Lucida Grande", 11));
    setSmallFont(QFont("Lucida Grande", 9));
    setWindowTitleFont(QFont("Lucida Grande", 14));
472
#else
Marco Martin's avatar
Marco Martin committed
473 474 475 476 477 478
    setGeneralFont(QFont("Noto Sans", 10));
    setMenuFont(QFont("Noto Sans", 10));
    setFixedWidthFont(QFont("Hack", 9));
    setToolbarFont(QFont("Noto Sans", 10));
    setSmallFont(QFont("Noto Sans", 8));
    setWindowTitleFont(QFont("Noto Sans", 10));
479
#endif
480

Marco Martin's avatar
Marco Martin committed
481 482
    m_fontAASettings->defaults();
}
483

Marco Martin's avatar
Marco Martin committed
484 485 486 487 488 489 490 491
void KFonts::load()
{
    KSharedConfig::Ptr config = KSharedConfig::openConfig("kdeglobals");

    KConfigGroup cg(config, "General");
    m_generalFont = m_generalFontOriginal = nearestExistingFont(cg.readEntry("font", m_defaultFont));


492
    m_fixedWidthFont = m_fixedWidthFontOriginal = nearestExistingFont(cg.readEntry("fixed", QFont("Hack", 9)));
Marco Martin's avatar
Marco Martin committed
493 494 495 496 497 498

    m_smallFont = m_smallFontOriginal = nearestExistingFont(cg.readEntry("smallestReadableFont", m_defaultFont));

    m_toolbarFont = m_toolbarFontOriginal = nearestExistingFont(cg.readEntry("toolBarFont", m_defaultFont));

    m_menuFont = m_menuFontOriginal = nearestExistingFont(cg.readEntry("menuFont", m_defaultFont));
499

Marco Martin's avatar
Marco Martin committed
500 501 502
    cg = KConfigGroup(config, "WM");
    m_windowTitleFont = m_windowTitleFontOriginal = nearestExistingFont(cg.readEntry("activeFont", m_defaultFont));

Julian Wolff's avatar
Julian Wolff committed
503 504
    engine()->addImageProvider("preview", new PreviewImageProvider(generalFont()));
    
Marco Martin's avatar
Marco Martin committed
505 506 507 508 509 510 511 512 513
    emit generalFontChanged();
    emit fixedWidthFontChanged();
    emit smallFontChanged();
    emit toolbarFontChanged();
    emit menuFontChanged();
    emit windowTitleFontChanged();

    m_fontAASettings->load();
    setNeedsSave(false);
514 515
}

Marco Martin's avatar
Marco Martin committed
516
void KFonts::save()
517
{
Marco Martin's avatar
Marco Martin committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    KSharedConfig::Ptr config = KSharedConfig::openConfig("kdeglobals");

    KConfigGroup cg(config, "General");
    cg.writeEntry("font", m_generalFont.toString());
    cg.writeEntry("fixed", m_fixedWidthFont.toString());
    cg.writeEntry("smallestReadableFont", m_smallFont.toString());
    cg.writeEntry("toolBarFont", m_toolbarFont.toString());
    cg.writeEntry("menuFont", m_menuFont.toString());
    cg.sync();
    cg = KConfigGroup(config, "WM");
    cg.writeEntry("activeFont", m_windowTitleFont.toString());
    cg.sync();

    m_defaultFontOriginal = m_defaultFont;
    m_generalFontOriginal = m_generalFont;
    m_fixedWidthFontOriginal = m_fixedWidthFont;
    m_smallFontOriginal = m_smallFont;
    m_toolbarFontOriginal = m_toolbarFont;
    m_menuFontOriginal = m_menuFont;
    m_windowTitleFontOriginal = m_windowTitleFont;
538

Marco Martin's avatar
Marco Martin committed
539 540 541 542 543 544 545 546 547 548 549 550
    KConfig _cfgfonts("kcmfonts");
    KConfigGroup cfgfonts(&_cfgfonts, "General");

    FontAASettings::AASetting aaSetting = (FontAASettings::AASetting)m_fontAASettings->antiAliasing();
    cfgfonts.writeEntry("dontChangeAASettings", aaSetting == FontAASettings::AASystem);

    if (aaSetting == FontAASettings::AAEnabled) {
        m_fontAASettings->save(KXftConfig::AntiAliasing::Enabled);
    } else if (aaSetting == FontAASettings::AADisabled) {
        m_fontAASettings->save(KXftConfig::AntiAliasing::Disabled);
    } else {
        m_fontAASettings->save(KXftConfig::AntiAliasing::NotSet);
551
    }
Marco Martin's avatar
Marco Martin committed
552 553 554 555 556 557 558

    KGlobalSettings::self()->emitChange(KGlobalSettings::FontChanged);

    runRdb(KRdbExportXftSettings | KRdbExportGtkTheme);

    emit fontsHaveChanged();
    setNeedsSave(false);
559 560
}

Marco Martin's avatar
Marco Martin committed
561
void KFonts::updateNeedsSave()
562
{
Marco Martin's avatar
Marco Martin committed
563 564 565 566 567 568 569 570
    setNeedsSave(m_defaultFontOriginal != m_defaultFont ||
                 m_generalFontOriginal != m_generalFont ||
                 m_fixedWidthFontOriginal != m_fixedWidthFont ||
                 m_smallFontOriginal != m_smallFont ||
                 m_toolbarFontOriginal != m_toolbarFont ||
                 m_menuFontOriginal != m_menuFont ||
                 m_windowTitleFontOriginal != m_windowTitleFont ||
                m_fontAASettings->needsSave());
571 572
}

Marco Martin's avatar
Marco Martin committed
573
void KFonts::setGeneralFont(const QFont &font)
574
{
Marco Martin's avatar
Marco Martin committed
575 576
    if (m_generalFont == font) {
        return;
577
    }
578

Marco Martin's avatar
Marco Martin committed
579 580 581
    m_generalFont = font;
    emit generalFontChanged();
    updateNeedsSave();
582 583
}

Marco Martin's avatar
Marco Martin committed
584
QFont KFonts::generalFont() const
585
{
Marco Martin's avatar
Marco Martin committed
586 587
    return m_generalFont;
}
588

Marco Martin's avatar
Marco Martin committed
589 590 591 592
void KFonts::setFixedWidthFont(const QFont &font)
{
    if (m_fixedWidthFont == font) {
        return;
593
    }
594

Marco Martin's avatar
Marco Martin committed
595 596 597 598
    m_fixedWidthFont = font;
    emit fixedWidthFontChanged();
    updateNeedsSave();
}
599

Marco Martin's avatar
Marco Martin committed
600 601 602 603
QFont KFonts::fixedWidthFont() const
{
    return m_fixedWidthFont;
}
604

Marco Martin's avatar
Marco Martin committed
605 606 607 608
void KFonts::setSmallFont(const QFont &font)
{
    if (m_smallFont == font) {
        return;
609
    }
610

Marco Martin's avatar
Marco Martin committed
611 612 613
    m_smallFont = font;
    emit smallFontChanged();
    updateNeedsSave();
614 615
}

Marco Martin's avatar
Marco Martin committed
616
QFont KFonts::smallFont() const
617
{
Marco Martin's avatar
Marco Martin committed
618 619
    return m_smallFont;
}
620

Marco Martin's avatar
Marco Martin committed
621 622 623 624
void KFonts::setToolbarFont(const QFont &font)
{
    if (m_toolbarFont == font) {
        return;
625
    }
626

Marco Martin's avatar
Marco Martin committed
627 628 629 630
    m_toolbarFont = font;
    emit toolbarFontChanged();
    updateNeedsSave();
}
631

Marco Martin's avatar
Marco Martin committed
632 633 634 635 636 637 638 639 640
QFont KFonts::toolbarFont() const
{
    return m_toolbarFont;
}

void KFonts::setMenuFont(const QFont &font)
{
    if (m_menuFont == font) {
        return;
641
    }
642

Marco Martin's avatar
Marco Martin committed
643 644 645 646
    m_menuFont = font;
    emit menuFontChanged();
    updateNeedsSave();
}
647

Marco Martin's avatar
Marco Martin committed
648 649 650 651
QFont KFonts::menuFont() const
{
    return m_menuFont;
}
652

Marco Martin's avatar
Marco Martin committed
653 654 655 656
void KFonts::setWindowTitleFont(const QFont &font)
{
    if (m_windowTitleFont == font) {
        return;
657 658
    }

Marco Martin's avatar
Marco Martin committed
659 660 661 662
    m_windowTitleFont = font;
    emit windowTitleFontChanged();
    updateNeedsSave();
}
663

Marco Martin's avatar
Marco Martin committed
664 665 666
QFont KFonts::windowTitleFont() const
{
    return m_windowTitleFont;
667 668
}

Marco Martin's avatar
Marco Martin committed
669
void KFonts::adjustAllFonts()
670
{
Marco Martin's avatar
Marco Martin committed
671
    QFont font = m_generalFont;
672
    KFontChooser::FontDiffFlags fontDiffFlags;
Marco Martin's avatar
Marco Martin committed
673
    int ret = KFontDialog::getFontDiff(font, fontDiffFlags, KFontChooser::NoDisplayFlags);
674 675

    if (ret == KDialog::Accepted && fontDiffFlags) {
Marco Martin's avatar
Marco Martin committed
676 677 678 679 680 681 682
        setGeneralFont(applyFontDiff(m_generalFont, font, fontDiffFlags));
        setMenuFont(applyFontDiff(m_menuFont, font, fontDiffFlags));
        {
            const QFont adjustedFont = applyFontDiff(m_fixedWidthFont, font, fontDiffFlags);
            if (QFontInfo(adjustedFont).fixedPitch()) {
                setFixedWidthFont(adjustedFont);
            }
683
        }
Marco Martin's avatar
Marco Martin committed
684 685 686
        setToolbarFont(applyFontDiff(m_toolbarFont, font, fontDiffFlags));
        setSmallFont(applyFontDiff(m_smallFont, font, fontDiffFlags));
        setWindowTitleFont(applyFontDiff(m_windowTitleFont, font, fontDiffFlags));
687
    }
688 689
}

Marco Martin's avatar
Marco Martin committed
690
QFont KFonts::applyFontDiff(const QFont &fnt, const QFont &newFont, int fontDiffFlags)
691
{
Marco Martin's avatar
Marco Martin committed
692
    QFont font(fnt);
693

Marco Martin's avatar
Marco Martin committed
694 695
    if (fontDiffFlags & KFontChooser::FontDiffSize) {
        font.setPointSizeF(newFont.pointSizeF());
696
    }
Marco Martin's avatar
Marco Martin committed
697 698 699 700 701 702 703 704 705 706 707
    if ((fontDiffFlags & KFontChooser::FontDiffFamily)) {
        font.setFamily(newFont.family());
    }
    if (fontDiffFlags & KFontChooser::FontDiffStyle) {
        font.setWeight(newFont.weight());
        font.setStyle(newFont.style());
        font.setUnderline(newFont.underline());
        font.setStyleName(newFont.styleName());
    }

    return font;
708 709 710 711
}

#include "fonts.moc"