Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
globals.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "globals.h"
5
6#include <QApplication>
7#include <QColor>
8#include <QPalette>
9#include <QStyleHints>
10
11namespace {
12// Check for "Dark Mode", either system-wide or usage of a dark style
13static bool isLight(const QColor &textColor)
14{
15 constexpr int DarkThreshold = 200;
16 return textColor.red() > DarkThreshold && textColor.green() > DarkThreshold
17 && textColor.blue() > DarkThreshold;
18}
19} // namespace
20
21using namespace Qt::Literals::StringLiterals;
22
23QT_BEGIN_NAMESPACE
24
25const QString &settingsPrefix()
26{
27
28 static QString prefix =
29 QString::number(QT_VERSION_MAJOR) + u'.' + QString::number(QT_VERSION_MINOR) + u'/';
30 return prefix;
31}
32
33QString settingPath(const char *path)
34{
35 return settingsPrefix() + QLatin1String(path);
36}
37
39{
40 return QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark
41 || isLight(QGuiApplication::palette().color(QPalette::WindowText));
42}
43
44QPixmap createMarkIcon(TranslationMarks mark, bool darkMode)
45{
46 const QString prefix = darkMode ? ":/images/darkmarks"_L1 : ":/images/lightmarks"_L1;
47 switch (mark) {
48 case TranslationMarks::OnMark:
49 return QPixmap(prefix + "/on-mark"_L1)
50 .scaled(20, 20, Qt::KeepAspectRatio, Qt::SmoothTransformation);
51 case TranslationMarks::OffMark:
52 return QPixmap(prefix + "/off-mark"_L1)
53 .scaled(20, 20, Qt::KeepAspectRatio, Qt::SmoothTransformation);
54 case TranslationMarks::ObsoleteMark:
55 return QPixmap(prefix + "/obsolete-mark"_L1)
56 .scaled(20, 20, Qt::KeepAspectRatio, Qt::SmoothTransformation);
57 case TranslationMarks::DangerMark:
58 return QPixmap(prefix + "/danger-mark"_L1)
59 .scaled(24, 24, Qt::KeepAspectRatio, Qt::SmoothTransformation);
60 case TranslationMarks::WarningMark:
61 return QPixmap(prefix + "/warning-mark"_L1)
62 .scaled(20, 20, Qt::KeepAspectRatio, Qt::SmoothTransformation);
63 case TranslationMarks::EmptyMark:
64 return QPixmap(prefix + "/empty-mark"_L1)
65 .scaled(20, 20, Qt::KeepAspectRatio, Qt::SmoothTransformation);
66 };
67 Q_UNREACHABLE_RETURN({});
68}
69
70QT_END_NAMESPACE
QString settingPath(const char *path)
Definition globals.cpp:33
bool isDarkMode()
Definition globals.cpp:38
QPixmap createMarkIcon(TranslationMarks mark, bool darkMode)
Definition globals.cpp:44