theme.cpp 12.5 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 Thorben Kroeger <thorbenkroeger@gmail.com>.
** Contact: https://www.qt.io/licensing/
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 24 25 26 27
**
****************************************************************************/

#include "theme.h"
#include "theme_p.h"
28
#include "../algorithm.h"
Alessandro Portale's avatar
Alessandro Portale committed
29
#include "../qtcassert.h"
30 31 32 33

#include <QApplication>
#include <QFileInfo>
#include <QMetaEnum>
34
#include <QPalette>
35 36 37 38 39 40
#include <QSettings>

namespace Utils {

static Theme *m_creatorTheme = 0;

Orgad Shaneh's avatar
Orgad Shaneh committed
41 42 43
ThemePrivate::ThemePrivate()
{
    const QMetaObject &m = Theme::staticMetaObject;
Alessandro Portale's avatar
Alessandro Portale committed
44
    colors.resize        (m.enumerator(m.indexOfEnumerator("Color")).keyCount());
Orgad Shaneh's avatar
Orgad Shaneh committed
45
    imageFiles.resize    (m.enumerator(m.indexOfEnumerator("ImageFile")).keyCount());
Alessandro Portale's avatar
Alessandro Portale committed
46
    gradients.resize     (m.enumerator(m.indexOfEnumerator("Gradient")).keyCount());
Orgad Shaneh's avatar
Orgad Shaneh committed
47 48 49
    flags.resize         (m.enumerator(m.indexOfEnumerator("Flag")).keyCount());
}

50 51 52 53 54
Theme *creatorTheme()
{
    return m_creatorTheme;
}

55 56 57 58 59
Theme *proxyTheme()
{
    return new Theme(*(new ThemePrivate(*(m_creatorTheme->d))));
}

60 61
void setCreatorTheme(Theme *theme)
{
Orgad Shaneh's avatar
Orgad Shaneh committed
62 63 64
    if (m_creatorTheme == theme)
        return;
    delete m_creatorTheme;
65
    m_creatorTheme = theme;
Eike Ziller's avatar
Eike Ziller committed
66 67
    if (theme && theme->flag(Theme::ApplyThemePaletteGlobally))
        QApplication::setPalette(theme->palette());
68 69
}

70
Theme::Theme(const QString &id, QObject *parent)
71 72 73
  : QObject(parent)
  , d(new ThemePrivate)
{
74
    d->id = id;
75 76
}

77 78 79 80
Theme::Theme(ThemePrivate &dd, QObject *parent) : QObject(parent), d(&dd)
{
}

81 82 83 84 85
Theme::~Theme()
{
    delete d;
}

86 87 88 89 90
QStringList Theme::preferredStyles() const
{
    return d->preferredStyles;
}

91 92 93 94 95
QString Theme::defaultTextEditorColorScheme() const
{
    return d->defaultTextEditorColorScheme;
}

96 97 98 99 100
QString Theme::id() const
{
    return d->id;
}

101 102 103 104 105
bool Theme::flag(Theme::Flag f) const
{
    return d->flags[f];
}

Alessandro Portale's avatar
Alessandro Portale committed
106
QColor Theme::color(Theme::Color role) const
107 108 109 110
{
    return d->colors[role].first;
}

Alessandro Portale's avatar
Alessandro Portale committed
111
QString Theme::imageFile(Theme::ImageFile imageFile, const QString &fallBack) const
112
{
Alessandro Portale's avatar
Alessandro Portale committed
113 114
    const QString &file = d->imageFiles.at(imageFile);
    return file.isEmpty() ? fallBack : file;
115 116
}

Alessandro Portale's avatar
Alessandro Portale committed
117
QGradientStops Theme::gradient(Theme::Gradient role) const
118
{
Alessandro Portale's avatar
Alessandro Portale committed
119
    return d->gradients[role];
120 121 122 123 124 125
}

QPair<QColor, QString> Theme::readNamedColor(const QString &color) const
{
    if (d->palette.contains(color))
        return qMakePair(d->palette[color], color);
126 127
    if (color == QLatin1String("style"))
        return qMakePair(QColor(), QString());
128 129 130 131

    bool ok = true;
    const QRgb rgba = color.toLongLong(&ok, 16);
    if (!ok) {
jkobus's avatar
jkobus committed
132
        qWarning("Color \"%s\" is neither a named color nor a valid color", qPrintable(color));
133 134 135 136 137
        return qMakePair(Qt::black, QString());
    }
    return qMakePair(QColor::fromRgba(rgba), QString());
}

138
QString Theme::filePath() const
139 140 141 142
{
    return d->fileName;
}

143
QString Theme::displayName() const
144
{
145
    return d->displayName;
146 147
}

148
void Theme::setDisplayName(const QString &name)
149
{
150
    d->displayName = name;
151 152
}

153 154 155 156 157 158 159 160 161 162 163
const QVariantMap &Theme::values() const
{
    if (d->values.isEmpty()) {
        const QMetaObject &m = *metaObject();
        {
            const QMetaEnum e = m.enumerator(m.indexOfEnumerator("Color"));
            for (int i = 0, total = e.keyCount(); i < total; ++i) {
                const QString key = QLatin1String(e.key(i));
                const QPair<QColor, QString> &var = d->colors.at(i);
                d->values.insert(key, var.first);
            }
164
        }
165 166 167 168 169 170
        {
            const QMetaEnum e = m.enumerator(m.indexOfEnumerator("Flag"));
            for (int i = 0, total = e.keyCount(); i < total; ++i) {
                const QString key = QLatin1String(e.key(i));
                d->values.insert(key, flag(static_cast<Theme::Flag>(i)));
            }
171 172
        }
    }
173
    return d->values;
174 175
}

176 177 178 179 180 181 182 183 184 185 186 187 188
static QColor readColor(const QString &color)
{
    bool ok = true;
    const QRgb rgba = color.toLongLong(&ok, 16);
    return QColor::fromRgba(rgba);
}

void Theme::readSettings(QSettings &settings)
{
    d->fileName = settings.fileName();
    const QMetaObject &m = *metaObject();

    {
189
        d->displayName = settings.value(QLatin1String("ThemeName"), QLatin1String("unnamed")).toString();
190
        d->preferredStyles = settings.value(QLatin1String("PreferredStyles")).toStringList();
191
        d->preferredStyles.removeAll(QLatin1String(""));
192 193
        d->defaultTextEditorColorScheme =
                settings.value(QLatin1String("DefaultTextEditorColorScheme")).toString();
194 195 196
    }
    {
        settings.beginGroup(QLatin1String("Palette"));
197 198
        foreach (const QString &key, settings.allKeys())
            d->palette[key] = readNamedColor(settings.value(key).toString()).first;
199 200 201 202
        settings.endGroup();
    }
    {
        settings.beginGroup(QLatin1String("Colors"));
Alessandro Portale's avatar
Alessandro Portale committed
203
        QMetaEnum e = m.enumerator(m.indexOfEnumerator("Color"));
204 205
        for (int i = 0, total = e.keyCount(); i < total; ++i) {
            const QString key = QLatin1String(e.key(i));
206
            if (!settings.contains(key)) {
207 208 209
                if (i < PaletteWindow || i > PaletteShadowDisabled)
                    qWarning("Theme \"%s\" misses color setting for key \"%s\".",
                             qPrintable(d->fileName), qPrintable(key));
210 211
                continue;
            }
212 213 214 215
            d->colors[i] = readNamedColor(settings.value(key).toString());
        }
        settings.endGroup();
    }
Alessandro Portale's avatar
Alessandro Portale committed
216 217 218 219 220 221 222 223 224
    {
        settings.beginGroup(QLatin1String("ImageFiles"));
        QMetaEnum e = m.enumerator(m.indexOfEnumerator("ImageFile"));
        for (int i = 0, total = e.keyCount(); i < total; ++i) {
            const QString key = QLatin1String(e.key(i));
            d->imageFiles[i] = settings.value(key).toString();
        }
        settings.endGroup();
    }
225 226
    {
        settings.beginGroup(QLatin1String("Gradients"));
Alessandro Portale's avatar
Alessandro Portale committed
227
        QMetaEnum e = m.enumerator(m.indexOfEnumerator("Gradient"));
228 229 230 231 232 233 234 235 236 237 238 239 240
        for (int i = 0, total = e.keyCount(); i < total; ++i) {
            const QString key = QLatin1String(e.key(i));
            QGradientStops stops;
            int size = settings.beginReadArray(key);
            for (int j = 0; j < size; ++j) {
                settings.setArrayIndex(j);
                QTC_ASSERT(settings.contains(QLatin1String("pos")), return);;
                double pos = settings.value(QLatin1String("pos")).toDouble();
                QTC_ASSERT(settings.contains(QLatin1String("color")), return);;
                QColor c = readColor(settings.value(QLatin1String("color")).toString());
                stops.append(qMakePair(pos, c));
            }
            settings.endArray();
Alessandro Portale's avatar
Alessandro Portale committed
241
            d->gradients[i] = stops;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
        }
        settings.endGroup();
    }
    {
        settings.beginGroup(QLatin1String("Flags"));
        QMetaEnum e = m.enumerator(m.indexOfEnumerator("Flag"));
        for (int i = 0, total = e.keyCount(); i < total; ++i) {
            const QString key = QLatin1String(e.key(i));
            QTC_ASSERT(settings.contains(key), return);;
            d->flags[i] = settings.value(key).toBool();
        }
        settings.endGroup();
    }
}

Orgad Shaneh's avatar
Orgad Shaneh committed
257
QPalette Theme::initialPalette()
258
{
Orgad Shaneh's avatar
Orgad Shaneh committed
259 260 261 262 263
    static QPalette palette = QApplication::palette();
    return palette;
}

QPalette Theme::palette() const
264
{
Orgad Shaneh's avatar
Orgad Shaneh committed
265
    QPalette pal = initialPalette();
266
    if (!flag(DerivePaletteFromTheme))
Orgad Shaneh's avatar
Orgad Shaneh committed
267
        return pal;
268

269 270 271 272 273 274
    const static struct {
        Color themeColor;
        QPalette::ColorRole paletteColorRole;
        QPalette::ColorGroup paletteColorGroup;
        bool setColorRoleAsBrush;
    } mapping[] = {
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
        {PaletteWindow,                    QPalette::Window,           QPalette::All,      false},
        {PaletteWindowDisabled,            QPalette::Window,           QPalette::Disabled, false},
        {PaletteWindowText,                QPalette::WindowText,       QPalette::All,      true},
        {PaletteWindowTextDisabled,        QPalette::WindowText,       QPalette::Disabled, true},
        {PaletteBase,                      QPalette::Base,             QPalette::All,      false},
        {PaletteBaseDisabled,              QPalette::Base,             QPalette::Disabled, false},
        {PaletteAlternateBase,             QPalette::AlternateBase,    QPalette::All,      false},
        {PaletteAlternateBaseDisabled,     QPalette::AlternateBase,    QPalette::Disabled, false},
        {PaletteToolTipBase,               QPalette::ToolTipBase,      QPalette::All,      true},
        {PaletteToolTipBaseDisabled,       QPalette::ToolTipBase,      QPalette::Disabled, true},
        {PaletteToolTipText,               QPalette::ToolTipText,      QPalette::All,      false},
        {PaletteToolTipTextDisabled,       QPalette::ToolTipText,      QPalette::Disabled, false},
        {PaletteText,                      QPalette::Text,             QPalette::All,      true},
        {PaletteTextDisabled,              QPalette::Text,             QPalette::Disabled, true},
        {PaletteButton,                    QPalette::Button,           QPalette::All,      false},
        {PaletteButtonDisabled,            QPalette::Button,           QPalette::Disabled, false},
        {PaletteButtonText,                QPalette::ButtonText,       QPalette::All,      true},
        {PaletteButtonTextDisabled,        QPalette::ButtonText,       QPalette::Disabled, true},
        {PaletteBrightText,                QPalette::BrightText,       QPalette::All,      false},
        {PaletteBrightTextDisabled,        QPalette::BrightText,       QPalette::Disabled, false},
        {PaletteHighlight,                 QPalette::Highlight,        QPalette::All,      true},
        {PaletteHighlightDisabled,         QPalette::Highlight,        QPalette::Disabled, true},
        {PaletteHighlightedText,           QPalette::HighlightedText,  QPalette::All,      true},
        {PaletteHighlightedTextDisabled,   QPalette::HighlightedText,  QPalette::Disabled, true},
        {PaletteLink,                      QPalette::Link,             QPalette::All,      false},
        {PaletteLinkDisabled,              QPalette::Link,             QPalette::Disabled, false},
        {PaletteLinkVisited,               QPalette::LinkVisited,      QPalette::All,      false},
        {PaletteLinkVisitedDisabled,       QPalette::LinkVisited,      QPalette::Disabled, false},
        {PaletteLight,                     QPalette::Light,            QPalette::All,      false},
        {PaletteLightDisabled,             QPalette::Light,            QPalette::Disabled, false},
        {PaletteMidlight,                  QPalette::Midlight,         QPalette::All,      false},
        {PaletteMidlightDisabled,          QPalette::Midlight,         QPalette::Disabled, false},
        {PaletteDark,                      QPalette::Dark,             QPalette::All,      false},
        {PaletteDarkDisabled,              QPalette::Dark,             QPalette::Disabled, false},
        {PaletteMid,                       QPalette::Mid,              QPalette::All,      false},
        {PaletteMidDisabled,               QPalette::Mid,              QPalette::Disabled, false},
        {PaletteShadow,                    QPalette::Shadow,           QPalette::All,      false},
        {PaletteShadowDisabled,            QPalette::Shadow,           QPalette::Disabled, false}
313 314 315 316 317 318 319 320 321 322 323 324 325
    };

    for (auto entry: mapping) {
        const QColor themeColor = color(entry.themeColor);
        // Use original color if color is not defined in theme.
        if (themeColor.isValid()) {
            if (entry.setColorRoleAsBrush)
                // TODO: Find out why sometimes setBrush is used
                pal.setBrush(entry.paletteColorGroup, entry.paletteColorRole, themeColor);
            else
                pal.setColor(entry.paletteColorGroup, entry.paletteColorRole, themeColor);
        }
    }
326 327 328 329 330

    return pal;
}

} // namespace Utils