MauiMan

formfactormanager.cpp
1#include "formfactormanager.h"
2
3#include "settingsstore.h"
4#include "mauimanutils.h"
5
6#include <QDebug>
7#include <QSize>
8#include <QScreen>
9#include <QGuiApplication>
10
11#if !defined Q_OS_ANDROID
12#include <QDBusInterface>
13#endif
14
15#include <QInputDevice>
16
17using namespace MauiMan;
18
19#if !defined Q_OS_ANDROID
20static QString typeToString(QInputDevice::DeviceTypes type)
21{
22 qDebug() << type;
23 QStringList typeString;
24 if (type.testFlag(QInputDevice::DeviceType::Mouse))
25 typeString << QStringLiteral("Mouse");
26 if (type.testFlag(QInputDevice::DeviceType::TouchPad))
27 typeString << QStringLiteral("TouchPad");
28 if (type.testFlag(QInputDevice::DeviceType::TouchScreen))
29 typeString << QStringLiteral("TouchScreen");
30 if (type.testFlag(QInputDevice::DeviceType::Keyboard))
31 typeString << QStringLiteral("Keyboard");
32 if (typeString.isEmpty())
33 typeString << QStringLiteral("Unknown");
34 return typeString.join((QStringLiteral(", ")));
35}
36#endif
37
38void FormFactorManager::sync(const QString &key, const QVariant &value)
39{
40#if !defined Q_OS_ANDROID
41 if (m_interface && m_interface->isValid())
42 {
43 m_interface->call(key, value);
44 }
45#endif
46}
47
48void FormFactorManager::setConnections()
49{
50#if !defined Q_OS_ANDROID
51 if(m_interface)
52 {
53 m_interface->disconnect();
54 m_interface->deleteLater();
55 m_interface = nullptr;
56 }
57
58 m_interface = new QDBusInterface(QStringLiteral("org.mauiman.Manager"),
59 QStringLiteral("/FormFactor"),
60 QStringLiteral("org.mauiman.FormFactor"),
62
63 if (m_interface->isValid())
64 {
65 connect(m_interface, SIGNAL(preferredModeChanged(uint)), this, SLOT(onPreferredModeChanged(uint)));
66 connect(m_interface, SIGNAL(forceTouchScreenChanged(bool)), this, SLOT(onForceTouchScreenChanged(bool)));
67 }
68#endif
69}
70
71void FormFactorManager::loadSettings()
72{
73 m_settings->beginModule(QStringLiteral("FormFactor"));
74
75#if !defined Q_OS_ANDROID
76 if(m_interface && m_interface->isValid())
77 {
78 m_preferredMode = m_interface->property("preferredMode").toUInt();
79 m_forceTouchScreen = m_interface->property("forceTouchScreen").toBool();
80 return;
81 }
82#endif
83
84 m_preferredMode = m_settings->load(QStringLiteral("PreferredMode"), m_preferredMode).toUInt();
85 m_forceTouchScreen = m_settings->load(QStringLiteral("ForceTouchScreen"), m_forceTouchScreen).toBool();
86}
87
88FormFactorManager::FormFactorManager(QObject *parent) : MauiMan::FormFactorInfo(parent)
89 ,m_settings(new MauiMan::SettingsStore(this))
90 ,m_info(new MauiMan::FormFactorInfo(this))
91{
92 qDebug( " INIT FORMFACTOR MANAGER");
93
94#if !defined Q_OS_ANDROID
95 auto server = new MauiManUtils(this);
96 if(server->serverRunning())
97 {
98 this->setConnections();
99 }
100
101 connect(server, &MauiManUtils::serverRunningChanged, [this](bool state)
102 {
103 if(state)
104 {
105 this->setConnections();
106 }
107 });
108#endif
109 m_preferredMode = defaultMode();
110
111 loadSettings();
112}
113
115{
116 return m_preferredMode;
117}
118
119uint FormFactorInfo::bestMode() const
120{
121 return m_bestMode;
122}
123
125{
126 return m_defaultMode;
127}
128
130{
131 return m_hasKeyboard;
132}
133
135{
136 return m_hasTouchscreen;
137}
138
139bool FormFactorInfo::hasMouse() const
140{
141 return m_hasMouse;
142}
143
145{
146 return m_hasTouchpad;
147}
148
149void FormFactorManager::setPreferredMode(uint preferredMode)
150{
151 if (m_preferredMode == preferredMode)
152 return;
153
154 m_preferredMode = preferredMode;
155
156 sync(QStringLiteral("setPreferredMode"), m_preferredMode);
157 m_settings->save(QStringLiteral("PreferredMode"), m_preferredMode);
158
159 Q_EMIT preferredModeChanged(m_preferredMode);
160}
161
163{
164 return m_forceTouchScreen;
165}
166
167void FormFactorManager::setForceTouchScreen(bool newForceTouchScreen)
168{
169 if (m_forceTouchScreen == newForceTouchScreen)
170 return;
171
172 m_forceTouchScreen = newForceTouchScreen;
173
174 sync(QStringLiteral("forceTouchScreen"), m_forceTouchScreen);
175 m_settings->save(QStringLiteral("ForceTouchScreen"), m_forceTouchScreen);
176
177 Q_EMIT forceTouchScreenChanged(m_forceTouchScreen);
178}
179
180void FormFactorManager::onPreferredModeChanged(uint preferredMode)
181{
182 if (m_preferredMode == preferredMode)
183 return;
184
185 m_preferredMode = preferredMode;
186 Q_EMIT preferredModeChanged(m_preferredMode);
187}
188
189void FormFactorManager::onForceTouchScreenChanged(bool value)
190{
191 if (m_forceTouchScreen == value)
192 return;
193
194 m_forceTouchScreen = value;
195 Q_EMIT forceTouchScreenChanged(m_forceTouchScreen);
196}
197
198void FormFactorInfo::findBestMode()
199{
200 uint bestMode = m_defaultMode;
201 /*
202 * 0- desktop
203 * 1- tablet
204 * 2- phone
205 * */
206
207 if(m_hasTouchscreen)
208 {
209 if(m_screenSize.width() > 1500)
210 {
211 if(m_hasKeyboard || m_hasMouse || m_hasTouchpad)
212 {
213 bestMode = 0; //A big touch screen and with keyboard/mouse/trackpad
214 }else
215 {
216 bestMode = 1; //A big touch screen alone
217 }
218 }
219 else if(m_screenSize.width() > 500)
220 {
221 bestMode = 1; //A tablet size touch screen
222 }
223 else
224 {
225 bestMode = 2; //A mobile size touch screen
226 }
227
228 }else
229 {
230
231 if(m_screenSize.width() > 1500)
232 {
233 bestMode = 0; // A big screen
234
235 }
236 else if(m_screenSize.width() > 500)
237 {
238 if(m_hasTouchpad)
239 {
240 bestMode = 1; // A small screen with a trackpad
241 }else
242 {
243 bestMode = 0;
244 }
245 }
246 else
247 {
248 bestMode = 1; //A mobile size touch screen
249 }
250 }
251
252 m_bestMode = bestMode;
253 Q_EMIT bestModeChanged(m_bestMode);
254}
255
257{
258 QScreen *screen = qApp->primaryScreen();
259 return screen->geometry();
260}
261
263{
264 QScreen *screen = qApp->primaryScreen();
265 return screen->orientation();
266}
267
268void FormFactorInfo::checkInputs(const QList<const QInputDevice *> &devices)
269{
270 auto hasType = [devices](QInputDevice::DeviceType type) -> bool
271 {
272 auto res= std::find_if(devices.constBegin(), devices.constEnd(), [type](const QInputDevice *device)
273 {
274 return device->type() == type;
275 });
276
277 return res != std::end(devices);
278 };
279
280 m_hasKeyboard = hasType(QInputDevice::DeviceType::Keyboard);
281 m_hasMouse = hasType(QInputDevice::DeviceType::Mouse);
282 m_hasTouchscreen = hasType(QInputDevice::DeviceType::TouchScreen);
283 m_hasTouchpad = hasType(QInputDevice::DeviceType::TouchPad);
284
285 Q_EMIT hasKeyboardChanged(m_hasKeyboard);
286 Q_EMIT hasMouseChanged(m_hasMouse);
287 Q_EMIT hasTouchscreenChanged(m_hasTouchscreen);
288 Q_EMIT hasTouchpadChanged(m_hasTouchpad);
289}
290
291FormFactorInfo::FormFactorInfo(QObject *parent) : QObject(parent)
292{
293 qDebug( " INIT FORMFACTOR INFO");
294
295#if !defined Q_OS_ANDROID
296 checkInputs(QInputDevice::devices());
297
298 findBestMode();
299#endif
300
301}
The MauiManUtils class.
The FormFactoInfo class contains information about the input devices available in the current system.
bool hasKeyboard
Whether the device has a physical keyboard.
bool hasTouchpad
Whether the device has a trackpad or touchpad.
uint defaultMode
The system preferred mode.
bool hasTouchscreen
Whether the device has a touch screen.
QRect screenSize
The size of the main screen.
bool hasMouse
Whether the device has a physical mouse.
uint bestMode
The best fitted mode according to the available input devices and the screen size.
Qt::ScreenOrientation screenOrientation
The current orientation of the main screen.
bool forceTouchScreen
If a device is not detected to have a touch screen , and still it has it, this property can be used t...
uint preferredMode
The preferred mode to display information.
The SettingsStore class Allows to store and read settings for MauiMan from the local conf file.
void save(const QString &key, const QVariant &value)
Save a conf value entry to the local file.
void beginModule(const QString &module)
Set up the module section to write to.
QVariant load(const QString &key, const QVariant &defaultValue)
Load the value of a conf entry, with a possible default value.
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
The MauiMan name-space contains all of the available modules for configuring the Maui Applications an...
QDBusMessage call(QDBus::CallMode mode, const QString &method, Args &&... args)
bool isValid() const const
QDBusConnection sessionBus()
typedef DeviceTypes
QList< const QInputDevice * > devices()
const_iterator constBegin() const const
const_iterator constEnd() const const
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
bool disconnect(const QMetaObject::Connection &connection)
QVariant property(const char *name) const const
int width() const const
QString join(QChar separator) const const
ScreenOrientation
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool toBool() const const
uint toUInt(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:51:22 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.