Qt Utilities
6.22.1
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
Toggle main menu visibility
Loading...
Searching...
No Matches
misc
desktoputils.cpp
Go to the documentation of this file.
1
#include "
./desktoputils.h
"
2
3
#include <QDesktopServices>
4
#include <QUrl>
5
6
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
7
#include <QGuiApplication>
8
#include <QStyleHints>
9
#endif
10
11
#ifdef Q_OS_WIN32
12
#include <QFileInfo>
13
#endif
14
15
#if defined(Q_OS_ANDROID) && (QT_VERSION >= QT_VERSION_CHECK(6, 4, 0))
16
#define QT_UTILITIES_OPEN_VIA_ACTIVITY
17
#include <QCoreApplication>
18
#include <QJniEnvironment>
19
#include <QJniObject>
20
#endif
21
22
namespace
QtUtilities
{
23
47
53
67
bool
openLocalFileOrDir
(
const
QString &path)
68
{
69
#ifdef QT_UTILITIES_OPEN_VIA_ACTIVITY
70
if
(
const
auto
context = QNativeInterface::QAndroidApplication::context(); context.isValid()) {
71
auto
env = QJniEnvironment();
72
if
(
auto
openPathMethod = env.findMethod(context.objectClass(),
"openPath"
,
"(Ljava/lang/String;)Z"
)) {
73
return
env->CallBooleanMethod(context.object(), openPathMethod, QJniObject::fromString(path).object()) == JNI_TRUE;
74
}
75
}
76
#endif
77
78
QUrl url(QStringLiteral(
"file://"
));
79
80
#ifdef Q_OS_WIN32
81
// replace backslashes with regular slashes
82
QString tmp(path);
83
tmp.replace(QChar(
'\\'
), QChar(
'/'
));
84
85
// add a slash before the drive letter of an absolute path
86
if
(QFileInfo(path).isAbsolute()) {
87
tmp = QStringLiteral(
"/"
) + tmp;
88
}
89
url.setPath(tmp, QUrl::DecodedMode);
90
91
#else
92
url.setPath(path, QUrl::DecodedMode);
93
#endif
94
return
QDesktopServices::openUrl(url);
95
}
96
97
bool
showToast
(
const
QString &message,
ToastDuration
duration)
98
{
99
#ifdef Q_OS_ANDROID
100
if
(
const
auto
context = QNativeInterface::QAndroidApplication::context(); context.isValid()) {
101
auto
env = QJniEnvironment();
102
if
(
auto
showToastMethod = env.findMethod(context.objectClass(),
"showToast"
,
"(Ljava/lang/String;I)Z"
)) {
103
return
env->CallBooleanMethod(context.object(), showToastMethod, QJniObject::fromString(message).object(), duration) == JNI_TRUE;
104
}
105
}
106
#else
107
Q_UNUSED(message)
108
Q_UNUSED(duration)
109
#endif
110
return
false
;
111
}
112
113
bool
performHapticFeedback
()
114
{
115
#ifdef Q_OS_ANDROID
116
if
(
const
auto
context = QNativeInterface::QAndroidApplication::context(); context.isValid()) {
117
auto
env = QJniEnvironment();
118
if
(
auto
hapticFeedbackMethod = env.findMethod(context.objectClass(),
"performHapticFeedback"
,
"()Z"
)) {
119
return
env->CallBooleanMethod(context.object(), hapticFeedbackMethod) == JNI_TRUE;
120
}
121
}
122
#endif
123
return
false
;
124
}
125
129
bool
isPaletteDark
(
const
QPalette &palette)
130
{
131
return
palette.color(QPalette::WindowText).lightness() > palette.color(QPalette::Window).lightness();
132
}
133
139
std::optional<bool>
isDarkModeEnabled
()
140
{
141
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
142
if
(
const
auto
*
const
styleHints = QGuiApplication::styleHints()) {
143
const
auto
colorScheme = styleHints->colorScheme();
144
if
(colorScheme != Qt::ColorScheme::Unknown) {
145
return
colorScheme == Qt::ColorScheme::Dark;
146
}
147
}
148
#endif
149
return
std::nullopt;
150
}
151
161
QMetaObject::Connection
onDarkModeChanged
(std::function<
void
(
bool
)> &&darkModeChangedCallback, QObject *context,
bool
invokeImmediately)
162
{
163
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
164
if
(
const
auto
*
const
styleHints = QGuiApplication::styleHints()) {
165
if
(invokeImmediately) {
166
darkModeChangedCallback(styleHints->colorScheme() == Qt::ColorScheme::Dark);
167
}
168
return
QObject::connect(styleHints, &QStyleHints::colorSchemeChanged, context,
169
[handler = std::move(darkModeChangedCallback)](Qt::ColorScheme colorScheme) {
return
handler(colorScheme == Qt::ColorScheme::Dark); });
170
}
171
#else
172
Q_UNUSED(darkModeChangedCallback)
173
Q_UNUSED(context)
174
Q_UNUSED(invokeImmediately)
175
#endif
176
return
QMetaObject::Connection();
177
}
178
179
}
// namespace QtUtilities
desktoputils.h
QtUtilities
!
Definition
trylocker.h:8
QtUtilities::ToastDuration
ToastDuration
Definition
desktoputils.h:26
QtUtilities::performHapticFeedback
QT_UTILITIES_EXPORT bool performHapticFeedback()
Definition
desktoputils.cpp:113
QtUtilities::onDarkModeChanged
QT_UTILITIES_EXPORT QMetaObject::Connection onDarkModeChanged(std::function< void(bool)> &&darkModeChangedCallback, QObject *context=nullptr, bool invokeImmediately=true)
Invokes the specified callback when the color scheme changed.
Definition
desktoputils.cpp:161
QtUtilities::isDarkModeEnabled
QT_UTILITIES_EXPORT std::optional< bool > isDarkModeEnabled()
Returns whether dark mode is enabled.
Definition
desktoputils.cpp:139
QtUtilities::isPaletteDark
QT_UTILITIES_EXPORT bool isPaletteDark(const QPalette &palette=QPalette())
Returns whether palette is dark.
Definition
desktoputils.cpp:129
QtUtilities::openLocalFileOrDir
QT_UTILITIES_EXPORT bool openLocalFileOrDir(const QString &path)
\macro QT_UTILITIES_DARK_MODE_FROM_COLOR_SCHEME
Definition
desktoputils.cpp:67
QtUtilities::showToast
QT_UTILITIES_EXPORT bool showToast(const QString &message, ToastDuration duration=ToastDuration::Long)
Definition
desktoputils.cpp:97
Generated on
for Qt Utilities by
1.18.0