File: thememanager.cpp

package info (click to toggle)
ostinato 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,840 kB
  • sloc: cpp: 46,226; makefile: 8
file content (130 lines) | stat: -rw-r--r-- 3,866 bytes parent folder | download | duplicates (2)
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
/*
Copyright (C) 2021 Srivats P.

This file is part of "Ostinato"

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

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

#include "thememanager.h"

#include "settings.h"

#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QDirIterator>
#include <QRegularExpression>
#include <QResource>

ThemeManager *ThemeManager::instance_{nullptr};

ThemeManager::ThemeManager()
{
    themeDir_ = QCoreApplication::applicationDirPath() + "/themes/";
#if defined(Q_OS_MAC)
    /*
     * Executable and Theme directory location inside app bundle -
     * Ostinato.app/Contents/MacOS/
     * Ostinato.app/Contents/SharedSupport/themes/
     */
    themeDir_.replace("/MacOS/", "/SharedSupport/");
#elif defined(Q_OS_UNIX)
    /*
     * Possible (but not comprehensive) locations for Ostinato executable
     * and theme directory locations
     *
     * non-install-dir/
     * non-install-dir/themes/
     *
     * /usr/[local]/bin/
     * /usr/[local]/share/ostinato-controller/themes/
     *
     * /opt/ostinato/bin/
     * /opt/ostinato/share/themes/
     */
    if (themeDir_.contains(QRegularExpression("^/usr/.*/bin/")))
        themeDir_.replace("/bin/", "/share/ostinato-controller/");
    else if (themeDir_.contains(QRegularExpression("^/opt/.*/bin/")))
        themeDir_.replace("/bin/", "/share/");
#endif
    qDebug("Themes directory: %s", qPrintable(themeDir_));
}

QStringList ThemeManager::themeList()
{
    QDir themeDir(themeDir_);

    themeDir.setFilter(QDir::Files);
    themeDir.setNameFilters(QStringList() << "*.qss");
    themeDir.setSorting(QDir::Name);

    QStringList themes = themeDir.entryList();
    for (QString& theme : themes)
        theme.remove(".qss");
    themes.prepend("default");

    return themes;
}

void ThemeManager::setTheme(QString theme)
{
    // Remove current theme, if we have one
    QString oldTheme = appSettings->value(kThemeKey).toString();
    if (!oldTheme.isEmpty()) {
        // Remove stylesheet first so that there are
        // no references to resources when unregistering 'em
        qApp->setStyleSheet("");
        QString rccFile = themeDir_ + oldTheme + ".rcc";
        if (QResource::unregisterResource(rccFile)) {
            qDebug("Unable to unregister theme rccFile %s",
                   qPrintable(rccFile));
        }
        appSettings->setValue(kThemeKey, QVariant());
    }

    if (theme.isEmpty() || (theme == "default"))
        return;

    // Apply new theme
    QFile qssFile(themeDir_ + theme + ".qss");
    if (!qssFile.open(QFile::ReadOnly)) {
        qDebug("Unable to open theme qssFile %s",
               qPrintable(qssFile.fileName()));
        return;
    }

    // Register theme resource before applying theme style sheet
    QString rccFile = themeDir_ + theme + ".rcc";
    if (!QResource::registerResource(rccFile))
        qDebug("Unable to register theme rccFile %s", qPrintable(rccFile));

#if 0 // FIXME: debug only
    QDirIterator it(":", QDirIterator::Subdirectories);
    while (it.hasNext()) {
        qDebug() << it.next();
    }
#endif

    QString styleSheet { qssFile.readAll() };
    qApp->setStyleSheet(styleSheet);
    appSettings->setValue(kThemeKey, theme);
}

ThemeManager* ThemeManager::instance()
{
    if (!instance_)
        instance_ = new ThemeManager();
    return instance_;
}