Qt Utilities: misc/desktoputils.cpp Source File

Qt Utilities 6.21.3 Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models

Loading...
Searching...
No Matches
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
22namespace QtUtilities {
23
37 bool openLocalFileOrDir(const QString &path)
38{
39#ifdef QT_UTILITIES_OPEN_VIA_ACTIVITY
40 if (const auto context = QNativeInterface::QAndroidApplication::context(); context.isValid()) {
41 auto env = QJniEnvironment();
42 if (auto openPathMethod = env.findMethod(context.objectClass(), "openPath", "(Ljava/lang/String;)Z")) {
43 return env->CallBooleanMethod(context.object(), openPathMethod, QJniObject::fromString(path).object()) == JNI_TRUE;
44 }
45 }
46#endif
47
48 QUrl url(QStringLiteral("file://"));
49
50#ifdef Q_OS_WIN32
51 // replace backslashes with regular slashes
52 QString tmp(path);
53 tmp.replace(QChar('\\'), QChar('/'));
54
55 // add a slash before the drive letter of an absolute path
56 if (QFileInfo(path).isAbsolute()) {
57 tmp = QStringLiteral("/") + tmp;
58 }
59 url.setPath(tmp, QUrl::DecodedMode);
60
61#else
62 url.setPath(path, QUrl::DecodedMode);
63#endif
64 return QDesktopServices::openUrl(url);
65}
66
70 bool isPaletteDark(const QPalette &palette)
71{
72 return palette.color(QPalette::WindowText).lightness() > palette.color(QPalette::Window).lightness();
73}
74
80std::optional<bool> isDarkModeEnabled()
81{
82#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
83 if (const auto *const styleHints = QGuiApplication::styleHints()) {
84 const auto colorScheme = styleHints->colorScheme();
85 if (colorScheme != Qt::ColorScheme::Unknown) {
86 return colorScheme == Qt::ColorScheme::Dark;
87 }
88 }
89#endif
90 return std::nullopt;
91}
92
102QMetaObject::Connection onDarkModeChanged(std::function<void(bool)> &&darkModeChangedCallback, QObject *context, bool invokeImmediately)
103{
104#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
105 if (const auto *const styleHints = QGuiApplication::styleHints()) {
106 if (invokeImmediately) {
107 darkModeChangedCallback(styleHints->colorScheme() == Qt::ColorScheme::Dark);
108 }
109 return QObject::connect(styleHints, &QStyleHints::colorSchemeChanged, context,
110 [handler = std::move(darkModeChangedCallback)](Qt::ColorScheme colorScheme) { return handler(colorScheme == Qt::ColorScheme::Dark); });
111 }
112#else
113 Q_UNUSED(darkModeChangedCallback)
114 Q_UNUSED(context)
115 Q_UNUSED(invokeImmediately)
116#endif
117 return QMetaObject::Connection();
118}
119
120} // namespace QtUtilities
desktoputils.h
QtUtilities
!
Definition trylocker.h:8
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:102
QtUtilities::isDarkModeEnabled
QT_UTILITIES_EXPORT std::optional< bool > isDarkModeEnabled()
Returns whether dark mode is enabled.
Definition desktoputils.cpp:80
QtUtilities::isPaletteDark
QT_UTILITIES_EXPORT bool isPaletteDark(const QPalette &palette=QPalette())
Returns whether palette is dark.
Definition desktoputils.cpp:70
QtUtilities::openLocalFileOrDir
QT_UTILITIES_EXPORT bool openLocalFileOrDir(const QString &path)
Shows the specified file or directory using the default file browser.
Definition desktoputils.cpp:37