Kirigami2

settings.cpp
1 /*
2  * SPDX-FileCopyrightText: 2016 Marco Martin <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include "settings.h"
8 
9 #include <QDebug>
10 #include <QFile>
11 #include <QGuiApplication>
12 #include <QIcon>
13 #include <QMouseEvent>
14 #include <QSettings>
15 #include <QStandardPaths>
16 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
17 #include <QTouchDevice>
18 #else
19 #include <QInputDevice>
20 #endif
21 #include <QWindow>
22 
23 #include <QtGui/private/qguiapplication_p.h>
24 #include <QtGui/qpa/qplatformmenu.h>
25 #include <QtGui/qpa/qplatformtheme.h>
26 
27 #include "libkirigami/tabletmodewatcher.h"
28 
29 #ifndef KIRIGAMI_BUILD_TYPE_STATIC
30 #include "../kirigami_version.h"
31 #endif
32 
33 class SettingsSingleton
34 {
35 public:
36  Settings self;
37 };
38 
39 Q_GLOBAL_STATIC(SettingsSingleton, privateSettingsSelf)
40 
41 Settings::Settings(QObject *parent)
42  : QObject(parent)
43  , m_hasTouchScreen(false)
44  , m_hasTransientTouchInput(false)
45 {
46  m_tabletModeAvailable = Kirigami::TabletModeWatcher::self()->isTabletModeAvailable();
47  connect(Kirigami::TabletModeWatcher::self(), &Kirigami::TabletModeWatcher::tabletModeAvailableChanged, this, [this](bool tabletModeAvailable) {
48  setTabletModeAvailable(tabletModeAvailable);
49  });
50 
51  m_tabletMode = Kirigami::TabletModeWatcher::self()->isTabletMode();
52  connect(Kirigami::TabletModeWatcher::self(), &Kirigami::TabletModeWatcher::tabletModeChanged, this, [this](bool tabletMode) {
53  setTabletMode(tabletMode);
54  });
55 
56 #if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(UBUNTU_TOUCH)
57  m_mobile = true;
58  m_hasTouchScreen = true;
59 #else
60  // Mostly for debug purposes and for platforms which are always mobile,
61  // such as Plasma Mobile
62  if (qEnvironmentVariableIsSet("QT_QUICK_CONTROLS_MOBILE")) {
63  m_mobile = QByteArrayList{"1", "true"}.contains(qgetenv("QT_QUICK_CONTROLS_MOBILE"));
64  } else {
65  m_mobile = false;
66  }
67 
68 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
69  const auto touchDevices = QTouchDevice::devices();
70  const auto touchDeviceType = QTouchDevice::TouchScreen;
71 #else
72  const auto touchDevices = QInputDevice::devices();
73  const auto touchDeviceType = QInputDevice::DeviceType::TouchScreen;
74 #endif
75  for (const auto &device : touchDevices) {
76  if (device->type() == touchDeviceType) {
77  m_hasTouchScreen = true;
78  break;
79  }
80  }
81  if (m_hasTouchScreen) {
82  connect(qApp, &QGuiApplication::focusWindowChanged, this, [this](QWindow *win) {
83  if (win) {
84  win->installEventFilter(this);
85  }
86  });
87  }
88 #endif
89 
90  auto bar = QGuiApplicationPrivate::platformTheme()->createPlatformMenuBar();
91  m_hasPlatformMenuBar = bar != nullptr;
92  if (bar != nullptr) {
93  bar->deleteLater();
94  }
95 
96  const QString configPath = QStandardPaths::locate(QStandardPaths::ConfigLocation, QStringLiteral("kdeglobals"));
97  if (QFile::exists(configPath)) {
98  QSettings globals(configPath, QSettings::IniFormat);
99  globals.beginGroup(QStringLiteral("KDE"));
100  m_scrollLines = qMax(1, globals.value(QStringLiteral("WheelScrollLines"), 3).toInt());
101  } else {
102  m_scrollLines = 3;
103  }
104 }
105 
106 Settings::~Settings()
107 {
108 }
109 
110 Settings *Settings::self()
111 {
112  return &privateSettingsSelf()->self;
113 }
114 
115 bool Settings::eventFilter(QObject *watched, QEvent *event)
116 {
117  Q_UNUSED(watched)
118  switch (event->type()) {
119  case QEvent::TouchBegin:
120  setTransientTouchInput(true);
121  break;
123  case QEvent::MouseMove: {
124  QMouseEvent *me = static_cast<QMouseEvent *>(event);
125  if (me->source() == Qt::MouseEventNotSynthesized) {
126  setTransientTouchInput(false);
127  }
128  break;
129  }
130  case QEvent::Wheel:
131  setTransientTouchInput(false);
132  default:
133  break;
134  }
135 
136  return false;
137 }
138 
139 void Settings::setTabletModeAvailable(bool mobileAvailable)
140 {
141  if (mobileAvailable == m_tabletModeAvailable) {
142  return;
143  }
144 
145  m_tabletModeAvailable = mobileAvailable;
146  Q_EMIT tabletModeAvailableChanged();
147 }
148 
149 bool Settings::isTabletModeAvailable() const
150 {
151  return m_tabletModeAvailable;
152 }
153 
154 void Settings::setIsMobile(bool mobile)
155 {
156  if (mobile == m_mobile) {
157  return;
158  }
159 
160  m_mobile = mobile;
161  Q_EMIT isMobileChanged();
162 }
163 
164 bool Settings::isMobile() const
165 {
166  return m_mobile;
167 }
168 
169 void Settings::setTabletMode(bool tablet)
170 {
171  if (tablet == m_tabletMode) {
172  return;
173  }
174 
175  m_tabletMode = tablet;
176  Q_EMIT tabletModeChanged();
177 }
178 
179 bool Settings::tabletMode() const
180 {
181  return m_tabletMode;
182 }
183 
184 void Settings::setTransientTouchInput(bool touch)
185 {
186  if (touch == m_hasTransientTouchInput) {
187  return;
188  }
189 
190  m_hasTransientTouchInput = touch;
191  if (!m_tabletMode) {
192  Q_EMIT hasTransientTouchInputChanged();
193  }
194 }
195 
197 {
198  return m_hasTransientTouchInput || m_tabletMode;
199 }
200 
201 QString Settings::style() const
202 {
203  return m_style;
204 }
205 
206 void Settings::setStyle(const QString &style)
207 {
208  m_style = style;
209 }
210 
212 {
213  return m_scrollLines;
214 }
215 
217 {
218  return {
219 #ifndef KIRIGAMI_BUILD_TYPE_STATIC
220  tr("KDE Frameworks %1").arg(QStringLiteral(KIRIGAMI2_VERSION_STRING)),
221 #endif
222  tr("The %1 windowing system").arg(QGuiApplication::platformName()),
223  tr("Qt %2 (built against %3)").arg(QString::fromLocal8Bit(qVersion()), QStringLiteral(QT_VERSION_STR))};
224 }
225 
227 {
228  const QIcon &windowIcon = qApp->windowIcon();
229  if (windowIcon.isNull()) {
230  return QVariant();
231  }
232  return windowIcon;
233 }
234 
235 bool Settings::hasPlatformMenuBar() const
236 {
237  return m_hasPlatformMenuBar;
238 }
This class contains global kirigami settings about the current device setup It is exposed to QML as t...
Definition: settings.h:16
QVariant applicationWindowIcon
This property holds the name of the application window icon.
Definition: settings.h:86
bool isMobile
This property holds whether the application is running on a small mobile device such as a mobile phon...
Definition: settings.h:33
bool contains(const T &value) const const
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
bool exists() const const
QList< const QTouchDevice * > devices()
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
QString fromLocal8Bit(const char *str, int size)
bool isNull() const const
void installEventFilter(QObject *filterObj)
QStringList information
This property holds the runtime information about the libraries in use.
Definition: settings.h:77
void focusWindowChanged(QWindow *focusWindow)
Qt::MouseEventSource source() const const
bool tabletMode
This property holds whether the application is running on a device that is behaving like a tablet.
Definition: settings.h:43
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
int mouseWheelScrollLines
This property holds the number of lines of text the mouse wheel should scroll.
Definition: settings.h:69
bool hasTransientTouchInput
This property holds whether the user in this moment is interacting with the app with the touch screen...
Definition: settings.h:57
bool hasPlatformMenuBar
This property holds whether the system has a platform menu bar; e.g.
Definition: settings.h:51
MouseEventNotSynthesized
QString style
This property holds the name of the QtQuickControls2 style the application is using,...
Definition: settings.h:63
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jan 29 2023 04:11:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.