File: application.cpp

package info (click to toggle)
openmw 0.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,076 kB
  • sloc: cpp: 380,958; xml: 2,192; sh: 1,449; python: 911; makefile: 26; javascript: 5
file content (118 lines) | stat: -rw-r--r-- 3,373 bytes parent folder | download
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
#include "application.hpp"

#include <QFile>
#include <QOperatingSystemVersion>
#include <QStyle>
#include <QStyleHints>

#include <components/debug/debuglog.hpp>
#include <components/misc/scalableicon.hpp>

namespace Platform
{
    Application::Application(int& argc, char* argv[])
        : QApplication(argc, argv)
    {
#if defined(WIN32) && QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
        init();
    }

    static QString getStyleSheetPath()
    {
        QString qssPath(":/dark/dark_win10.qss");
        if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows11)
            qssPath = ":/dark/dark_win11.qss";

        return qssPath;
    }

    void Application::init()
    {
        connect(this, &Application::darkModeChanged, this, &Application::updateStyle);

        const auto* hints = QGuiApplication::styleHints();
        const auto currentStyle = QApplication::style()->objectName();
        mInitialStyle = currentStyle.toStdString();
        mCurrentStyle = currentStyle.toStdString();
        if (hints->colorScheme() == Qt::ColorScheme::Dark)
        {
            mDarkMode = true;
            if (currentStyle == "windowsvista")
            {
                mCurrentStyle = "windows";
                setStyle("windows");

                QFile file(getStyleSheetPath());
                if (!file.open(QIODevice::ReadOnly))
                {
                    qDebug() << "Failed to open style sheet file:" << getStyleSheetPath();
                    return;
                }

                setStyleSheet(file.readAll());
            }
        }
    }

    void Application::updateStyle(bool isDark)
    {
        if (mInitialStyle != "windowsvista")
            return;

        if (isDark)
        {
            mCurrentStyle = "windows";
            setStyle("windows");

            QFile file(getStyleSheetPath());
            if (!file.open(QIODevice::ReadOnly))
            {
                qDebug() << "Failed to open style sheet file:" << getStyleSheetPath();
                return;
            }

            setStyleSheet(file.readAll());
        }
        else
        {
            mCurrentStyle = mInitialStyle;
            setStyleSheet("");
            setStyle(mInitialStyle.c_str());
        }
#endif
    }

    bool Application::notify(QObject* receiver, QEvent* event)
    {
        try
        {
            if (event->type() == QEvent::ThemeChange || event->type() == QEvent::PaletteChange)
            {
#if defined(WIN32) && QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
                const auto* hints = QGuiApplication::styleHints();
                const auto currentStyle = QApplication::style()->objectName();
                bool isDark = hints->colorScheme() == Qt::ColorScheme::Dark;
                if (isDark != mDarkMode)
                {
                    mDarkMode = isDark;

                    bool result = QApplication::notify(receiver, event);

                    emit darkModeChanged(isDark);

                    return result;
                }
#endif
                Misc::ScalableIcon::updateAllIcons();
            }

            return QApplication::notify(receiver, event);
        }
        catch (const std::exception& exception)
        {
            Log(Debug::Error) << "An exception has been caught: " << exception.what();
        }

        return false;
    }
}