MauiKit Controls

handy.cpp
1/*
2 * Copyright 2018 Camilo Higuita <milo.h@aol.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "handy.h"
21#include "fmh.h"
22
23#include <QDateTime>
24#include <QClipboard>
25#include <QDebug>
26#include <QIcon>
27#include <QMimeData>
28#include <QOperatingSystemVersion>
29#include <QStandardPaths>
30#include <QWindow>
31#include <QMouseEvent>
32
33#include "platforms/platform.h"
34
35#include <QInputDevice>
36
37#ifdef Q_OS_ANDROID
38#include <QGuiApplication>
39#else
40#include <QApplication>
41#endif
42
43#include <MauiMan4/formfactormanager.h>
44#include <MauiMan4/accessibilitymanager.h>
45
46Q_GLOBAL_STATIC(Handy, handyInstance)
47
48Handy::Handy(QObject *parent)
49 : QObject(parent)
50 ,m_formFactor(new MauiMan::FormFactorManager(this))
51 ,m_accessibility(new MauiMan::AccessibilityManager(this))
52 ,m_hasTransientTouchInput(false)
53{
54 qDebug() << "CREATING INSTANCE OF MAUI HANDY";
55
56 connect(m_accessibility, &MauiMan::AccessibilityManager::singleClickChanged, [&](bool value)
57 {
58 m_singleClick = value;
59 Q_EMIT singleClickChanged();
60 });
61
62 m_singleClick = m_accessibility->singleClick();
63
64// #ifdef FORMFACTOR_FOUND //TODO check here for Cask desktop enviroment
65
66connect(m_formFactor, &MauiMan::FormFactorManager::preferredModeChanged, [this](uint value)
67{
68 m_ffactor = static_cast<FFactor>(value);
69 m_mobile = m_ffactor == FFactor::Phone || m_ffactor == FFactor::Tablet;
70 Q_EMIT formFactorChanged();
71 Q_EMIT isMobileChanged();
72});
73
74connect(m_formFactor, &MauiMan::FormFactorManager::hasTouchscreenChanged, [this](bool value)
75{
76 m_isTouch = value || m_formFactor->forceTouchScreen();
77 Q_EMIT isTouchChanged();
78});
79
80m_ffactor = static_cast<FFactor>(m_formFactor->preferredMode());
81m_mobile = m_ffactor == FFactor::Phone || m_ffactor == FFactor::Tablet;
82m_isTouch = m_formFactor->hasTouchscreen() || m_formFactor->forceTouchScreen();
83
84 connect(qApp, &QGuiApplication::focusWindowChanged, this, [this](QWindow *win)
85 {
86 if (win)
87 {
88 win->installEventFilter(this);
89 }
90 });
91}
92
93Handy *Handy::instance()
94{
95 return handyInstance();
96}
97
98bool Handy::isTouch()
99{
100 return m_isTouch;
101}
102
104{
105 return m_ffactor;
106}
107
109{
110 return m_hasTransientTouchInput;
111}
112
113void Handy::setTransientTouchInput(bool touch)
114{
115 if (touch == m_hasTransientTouchInput) {
116 return;
117 }
118
119 m_hasTransientTouchInput = touch;
120 Q_EMIT hasTransientTouchInputChanged();
121}
122
123bool Handy::eventFilter(QObject *watched, QEvent *event)
124{
125 Q_UNUSED(watched)
126 switch (event->type())
127 {
129 setTransientTouchInput(true);
130 break;
132 case QEvent::MouseMove: {
133 QMouseEvent *me = static_cast<QMouseEvent *>(event);
135 {
136 setTransientTouchInput(false);
137 }
138 break;
139 }
140 case QEvent::Wheel:
141 setTransientTouchInput(false);
142 default:
143 break;
144 }
145
146 return false;
147}
148
149#ifdef Q_OS_ANDROID
150static inline struct {
151 QList<QUrl> urls;
152 QString text;
153 bool cut = false;
154
155 bool hasUrls()
156 {
157 return !urls.isEmpty();
158 }
159 bool hasText()
160 {
161 return !text.isEmpty();
162 }
163
164} _clipboard;
165#endif
166
167QVariantMap Handy::userInfo()
168{
169 QString name = qgetenv("USER");
170 if (name.isEmpty())
171 name = qgetenv("USERNAME");
172
173 return QVariantMap({{FMH::MODEL_NAME[FMH::MODEL_KEY::NAME], name}});
174}
175
177{
178#ifdef Q_OS_ANDROID
179 auto clipboard = QGuiApplication::clipboard();
180#else
181 auto clipboard = QApplication::clipboard();
182#endif
183
184 auto mime = clipboard->mimeData();
185 if (mime->hasText())
186 return clipboard->text();
187
188 return QString();
189}
190
192{
193 QVariantMap res;
194#ifdef Q_OS_ANDROID
195 if (_clipboard.hasUrls())
196 res.insert("urls", QUrl::toStringList(_clipboard.urls));
197
198 if (_clipboard.hasText())
199 res.insert("text", _clipboard.text);
200
201 res.insert("cut", _clipboard.cut);
202#else
203 auto clipboard = QApplication::clipboard();
204
205 auto mime = clipboard->mimeData();
206
207 if(!mime)
208 return res;
209
210 if (mime->hasUrls())
211 res.insert("urls", QUrl::toStringList(mime->urls()));
212
213 if (mime->hasText())
214 res.insert("text", mime->text());
215
216 if(mime->hasImage())
217 res.insert("image", mime->imageData());
218
219 const QByteArray a = mime->data(QStringLiteral("application/x-kde-cutselection"));
220
221 res.insert("cut", !a.isEmpty() && a.at(0) == '1');
222#endif
223 return res;
224}
225
226bool Handy::copyToClipboard(const QVariantMap &value, const bool &cut)
227{
228#ifdef Q_OS_ANDROID
229 if (value.contains("urls"))
230 _clipboard.urls = QUrl::fromStringList(value["urls"].toStringList());
231
232 if (value.contains("text"))
233 _clipboard.text = value["text"].toString();
234
235 _clipboard.cut = cut;
236
237 return true;
238#else
239 auto clipboard = QApplication::clipboard();
240 QMimeData *mimeData = new QMimeData();
241
242 if (value.contains("urls"))
243 mimeData->setUrls(QUrl::fromStringList(value["urls"].toStringList()));
244
245 if (value.contains("text"))
246 mimeData->setText(value["text"].toString());
247
248 mimeData->setData(QStringLiteral("application/x-kde-cutselection"), cut ? "1" : "0");
249 clipboard->setMimeData(mimeData);
250
251 return true;
252#endif
253
254 return false;
255}
256
258{
259#ifdef Q_OS_ANDROID
260 Handy::copyToClipboard({{"text", text}});
261#else
263#endif
264 return true;
265}
266
271
272bool Handy::isAndroid()
273{
274 return FMH::isAndroid();
275}
276
277bool Handy::isLinux()
278{
279 return FMH::isLinux();
280}
281
282bool Handy::isIOS()
283{
284 return FMH::isIOS();
285}
286
288{
289 return m_formFactor->hasKeyboard();
290}
291
292bool Handy::hasMouse()
293{
294 return m_formFactor->hasMouse();
295}
296
297bool Handy::isWindows()
298{
299 return FMH::isWindows();
300}
301
302bool Handy::isMac()
303{
304 return FMH::isMac();
305}
306
307
309{
310 const QLocale locale;
311 return locale.formattedDataSize(size);
312}
313
314QString Handy::formatDate(const QString &dateStr, const QString &format, const QString &initFormat)
315{
316 if (initFormat.isEmpty())
317 return QDateTime::fromString(dateStr, Qt::TextDate).toString(format);
318 else
319 return QDateTime::fromString(dateStr, initFormat).toString(format);
320}
321
322QString Handy::formatTime(const qint64 &value)
323{
324 QString tStr;
325 if (value) {
326 QTime time((value / 3600) % 60, (value / 60) % 60, value % 60, (value * 1000) % 1000);
327 QString format = "mm:ss";
328 if (value > 3600)
329 {
330 format = "hh:mm:ss";
331 }
332 tStr = time.toString(format);
333 }
334
335 return tStr.isEmpty() ? "00:00" : tStr;
336}
337
338bool Handy::isMobile() const
339{
340 return m_mobile;
341}
The Handy class.
Definition handy.h:41
FFactor formFactor
The current preferred from factor the user has selected.
Definition handy.h:109
bool isTouch
Whether the target device has a touch screen.
Definition handy.h:55
static int version()
Returns the major version of the current OS.
Definition handy.cpp:267
QML_SINGLETONbool isMobile
Whether the host platform is set as mobile.
Definition handy.h:50
static QVariantMap getClipboard()
Retrieves the data in the clipboard into a key-value map.
Definition handy.cpp:191
static QString formatTime(const qint64 &value)
Format a milliseconds value to a readable format.
Definition handy.cpp:322
FFactor
The different form factor options.
Definition handy.h:118
bool isLinux
Whether the host platform is a GNU/Linux distribution.
Definition handy.h:80
bool isMac
Whether the host platform is running MacOS.
Definition handy.h:90
bool hasMouse
Whether the target device has a physical mouse attached.
Definition handy.h:60
bool isWindows
Whether the host platform is running Windows OS.
Definition handy.h:85
static QString getClipboardText()
Returns the text contained in the clipboard.
Definition handy.cpp:176
bool hasKeyboard
Whether the target device has a physical keyboard attached.
Definition handy.h:65
static bool copyToClipboard(const QVariantMap &value, const bool &cut=false)
Adds a key-value map to the clipboard.
Definition handy.cpp:226
bool isIOS
Whether the host platform is running IOS.
Definition handy.h:95
static QString formatDate(const QString &dateStr, const QString &format=QString("dd/MM/yyyy"), const QString &initFormat=QString())
Given a date string, its original format and an intended format, return a readable string.
Definition handy.cpp:314
static QVariantMap userInfo()
Returns a key-value map containing basic information about the current user.
Definition handy.cpp:167
bool hasTransientTouchInput
Whether the current press input has been received from a touch screen.
Definition handy.h:70
bool isAndroid
Whether the host platform is an Android device.
Definition handy.h:75
static bool copyTextToClipboard(const QString &text)
Copies a text string to the clipboard.
Definition handy.cpp:257
static QString formatSize(quint64 size)
Format a size value to the a readable locale size format.
Definition handy.cpp:308
bool isLinux()
Whether the platform running is GNU/Linux.
Definition fmh.cpp:104
bool isIOS()
Whether the platform running is IOS.
Definition fmh.cpp:124
static const QHash< MODEL_KEY, QString > MODEL_NAME
The mapping of the FMH::MODEL_KEY enum values to its string representation.
Definition fmh.h:219
bool isWindows()
Whether the platform running is Window.
Definition fmh.cpp:93
bool isMac()
Whether the platform running is Mac.
Definition fmh.cpp:113
bool isAndroid()
Whether the platform running is Android.
Definition fmh.cpp:84
QAction * cut(const QObject *recvr, const char *slot, QObject *parent)
char at(qsizetype i) const const
char * data()
bool isEmpty() const const
void setText(const QString &text, Mode mode)
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
QString toString(QStringView format, QCalendar cal) const const
QClipboard * clipboard()
void focusWindowChanged(QWindow *focusWindow)
bool isEmpty() const const
QString formattedDataSize(qint64 bytes, int precision, DataSizeFormats format) const const
void setData(const QString &mimeType, const QByteArray &data)
void setText(const QString &text)
void setUrls(const QList< QUrl > &urls)
Qt::MouseEventSource source() const const
Q_EMITQ_EMIT
virtual bool event(QEvent *e)
void installEventFilter(QObject *filterObj)
QOperatingSystemVersion current()
int majorVersion() const const
bool isEmpty() const const
TextDate
MouseEventNotSynthesized
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString(QStringView format) const const
QList< QUrl > fromStringList(const QStringList &urls, ParsingMode mode)
QStringList toStringList(const QList< QUrl > &urls, FormattingOptions options)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 19 2024 12:00:22 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.