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 <QInputDevice>
14 #include <QMouseEvent>
15 #include <QSettings>
16 #include <QStandardPaths>
17 #include <QWindow>
18 
19 #include <QtGui/private/qguiapplication_p.h>
20 #include <QtGui/qpa/qplatformmenu.h>
21 #include <QtGui/qpa/qplatformtheme.h>
22 
23 #include "libkirigami/tabletmodewatcher.h"
24 
25 #ifndef KIRIGAMI_BUILD_TYPE_STATIC
26 #include "libkirigami/kirigami2_version.h"
27 #endif
28 
29 class SettingsSingleton
30 {
31 public:
32  Settings self;
33 };
34 
35 Q_GLOBAL_STATIC(SettingsSingleton, privateSettingsSelf)
36 
37 Settings::Settings(QObject *parent)
38  : QObject(parent)
39  , m_hasTouchScreen(false)
40  , m_hasTransientTouchInput(false)
41 {
42  m_tabletModeAvailable = Kirigami::TabletModeWatcher::self()->isTabletModeAvailable();
43  connect(Kirigami::TabletModeWatcher::self(), &Kirigami::TabletModeWatcher::tabletModeAvailableChanged, this, [this](bool tabletModeAvailable) {
44  setTabletModeAvailable(tabletModeAvailable);
45  });
46 
47  m_tabletMode = Kirigami::TabletModeWatcher::self()->isTabletMode();
48  connect(Kirigami::TabletModeWatcher::self(), &Kirigami::TabletModeWatcher::tabletModeChanged, this, [this](bool tabletMode) {
49  setTabletMode(tabletMode);
50  });
51 
52 #if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(UBUNTU_TOUCH)
53  m_mobile = true;
54  m_hasTouchScreen = true;
55 #else
56  // Mostly for debug purposes and for platforms which are always mobile,
57  // such as Plasma Mobile
58  if (qEnvironmentVariableIsSet("QT_QUICK_CONTROLS_MOBILE")) {
59  m_mobile = QByteArrayList{"1", "true"}.contains(qgetenv("QT_QUICK_CONTROLS_MOBILE"));
60  } else {
61  m_mobile = false;
62  }
63 
64  const auto touchDevices = QInputDevice::devices();
65  const auto touchDeviceType = QInputDevice::DeviceType::TouchScreen;
66  for (const auto &device : touchDevices) {
67  if (device->type() == touchDeviceType) {
68  m_hasTouchScreen = true;
69  break;
70  }
71  }
72  if (m_hasTouchScreen) {
73  connect(qApp, &QGuiApplication::focusWindowChanged, this, [this](QWindow *win) {
74  if (win) {
75  win->installEventFilter(this);
76  }
77  });
78  }
79 #endif
80 
81  auto bar = QGuiApplicationPrivate::platformTheme()->createPlatformMenuBar();
82  m_hasPlatformMenuBar = bar != nullptr;
83  if (bar != nullptr) {
84  bar->deleteLater();
85  }
86 
87  const QString configPath = QStandardPaths::locate(QStandardPaths::ConfigLocation, QStringLiteral("kdeglobals"));
88  if (QFile::exists(configPath)) {
89  QSettings globals(configPath, QSettings::IniFormat);
90  globals.beginGroup(QStringLiteral("KDE"));
91  m_scrollLines = qMax(1, globals.value(QStringLiteral("WheelScrollLines"), 3).toInt());
92  } else {
93  m_scrollLines = 3;
94  }
95 }
96 
97 Settings::~Settings()
98 {
99 }
100 
101 Settings *Settings::self()
102 {
103  return &privateSettingsSelf()->self;
104 }
105 
106 bool Settings::eventFilter(QObject *watched, QEvent *event)
107 {
108  Q_UNUSED(watched)
109  switch (event->type()) {
110  case QEvent::TouchBegin:
111  setTransientTouchInput(true);
112  break;
114  case QEvent::MouseMove: {
115  QMouseEvent *me = static_cast<QMouseEvent *>(event);
116  if (me->source() == Qt::MouseEventNotSynthesized) {
117  setTransientTouchInput(false);
118  }
119  break;
120  }
121  case QEvent::Wheel:
122  setTransientTouchInput(false);
123  default:
124  break;
125  }
126 
127  return false;
128 }
129 
130 void Settings::setTabletModeAvailable(bool mobileAvailable)
131 {
132  if (mobileAvailable == m_tabletModeAvailable) {
133  return;
134  }
135 
136  m_tabletModeAvailable = mobileAvailable;
137  Q_EMIT tabletModeAvailableChanged();
138 }
139 
140 bool Settings::isTabletModeAvailable() const
141 {
142  return m_tabletModeAvailable;
143 }
144 
145 void Settings::setIsMobile(bool mobile)
146 {
147  if (mobile == m_mobile) {
148  return;
149  }
150 
151  m_mobile = mobile;
152  Q_EMIT isMobileChanged();
153 }
154 
155 bool Settings::isMobile() const
156 {
157  return m_mobile;
158 }
159 
160 void Settings::setTabletMode(bool tablet)
161 {
162  if (tablet == m_tabletMode) {
163  return;
164  }
165 
166  m_tabletMode = tablet;
167  Q_EMIT tabletModeChanged();
168 }
169 
170 bool Settings::tabletMode() const
171 {
172  return m_tabletMode;
173 }
174 
175 void Settings::setTransientTouchInput(bool touch)
176 {
177  if (touch == m_hasTransientTouchInput) {
178  return;
179  }
180 
181  m_hasTransientTouchInput = touch;
182  if (!m_tabletMode) {
183  Q_EMIT hasTransientTouchInputChanged();
184  }
185 }
186 
188 {
189  return m_hasTransientTouchInput || m_tabletMode;
190 }
191 
192 QString Settings::style() const
193 {
194  return m_style;
195 }
196 
197 void Settings::setStyle(const QString &style)
198 {
199  m_style = style;
200 }
201 
203 {
204  return m_scrollLines;
205 }
206 
208 {
209  return {
210 #ifndef KIRIGAMI_BUILD_TYPE_STATIC
211  tr("KDE Frameworks %1").arg(QStringLiteral(KIRIGAMI2_VERSION_STRING)),
212 #endif
213  tr("The %1 windowing system").arg(QGuiApplication::platformName()),
214  tr("Qt %2 (built against %3)").arg(QString::fromLocal8Bit(qVersion()), QStringLiteral(QT_VERSION_STR))};
215 }
216 
218 {
219  const QIcon &windowIcon = qApp->windowIcon();
220  if (windowIcon.isNull()) {
221  return QVariant();
222  }
223  return windowIcon;
224 }
225 
226 bool Settings::hasPlatformMenuBar() const
227 {
228  return m_hasPlatformMenuBar;
229 }
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
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 Jun 4 2023 04:05:51 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.