summaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/themetuner.cpp
blob: e48865f31c1d337091cd8fe2808e6b4fcd72b6ed (plain) (blame)
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
#include "themetuner.h"
#include "uiconfig.h"
#include "mainwindow.h"
#include "uiconfig.h"
#include <QApplication>
#include <QFile>
#include <QStyle>
#include <QDebug>

ThemeTuner* ThemeTuner::instance = nullptr;

ThemeTuner::ThemeTuner(QObject* parent) :
    QObject(parent)
{
    init();
}

void ThemeTuner::tuneTheme(const QString& themeName)
{
    tuneCss(themeName);
}

void ThemeTuner::tuneCurrentTheme()
{
    tuneTheme(QApplication::style()->objectName());
}

void ThemeTuner::manageCompactLayout(QWidget* w)
{
    manageCompactLayout(QList<QWidget*>({w}));
}

void ThemeTuner::manageCompactLayout(QList<QWidget*> wList)
{
    widgetsForCompactLayout += wList;
    for (QWidget* w : wList)
        connect(w, SIGNAL(destroyed()), this, SLOT(handleWidgetDestroyed()));

    handleCompactLayoutChange(CFG_UI.General.CompactLayout.get());
}

QString ThemeTuner::getDefaultCss(const QString& themeName) const
{
    QString css = defaultGeneralCss;
    QString lowerTheme = themeName.toLower();
    if (!themeName.isNull() && defaultPerStyleCss.contains(lowerTheme))
        css += "\n" + defaultPerStyleCss[lowerTheme];

    return css;
}

ThemeTuner* ThemeTuner::getInstance()
{
    if (!instance)
        instance = new ThemeTuner();

    return instance;
}

void ThemeTuner::cleanUp()
{
    if (instance)
        safe_delete(instance);
}

void ThemeTuner::init()
{
    QFile f(":/css/general.css");
    if (!f.open(QIODevice::ReadOnly))
    {
        qCritical() << "Could not open general.css";
        return;
    }

    defaultGeneralCss = QString::fromLatin1(f.readAll());
    f.close();

    connect(CFG_UI.General.CompactLayout, SIGNAL(changed(QVariant)), this, SLOT(handleCompactLayoutChange(QVariant)));
}

void ThemeTuner::tuneCss(const QString& themeName)
{
    if (!CFG_UI.General.CustomCss.get().isNull())
    {
        applyCss(CFG_UI.General.CustomCss.get());
        return;
    }

    applyCss(getDefaultCss(themeName));
}

void ThemeTuner::applyCss(const QString& css)
{
    MAINWINDOW->setStyleSheet(css);
}

void ThemeTuner::handleWidgetDestroyed()
{
    QWidget* w = dynamic_cast<QWidget*>(sender());
    if (!w)
        return;

    widgetsForCompactLayout.removeOne(w);
}

void ThemeTuner::handleCompactLayoutChange(const QVariant& newValue)
{
    if (newValue.toBool())
    {
        for (QWidget* w : widgetsForCompactLayout)
        {
            w->layout()->setContentsMargins(0, 0, 0, 0);
            w->layout()->setSpacing(0);
        }
    }
    else
    {
        for (QWidget* w : widgetsForCompactLayout)
        {
            w->layout()->setContentsMargins(-1, -1, -1, -1);
            w->layout()->setSpacing(-1);
        }
    }
}