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#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
36#include <QTouchDevice>
37#else
38#include <QInputDevice>
39#endif
40
41#ifdef Q_OS_ANDROID
42#include <QGuiApplication>
43#else
44#include <QApplication>
45#endif
46
47#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
48#include <MauiMan3/formfactormanager.h>
49#include <MauiMan3/accessibilitymanager.h>
50#else
51#include <MauiMan4/formfactormanager.h>
52#include <MauiMan4/accessibilitymanager.h>
53#endif
54
55Q_GLOBAL_STATIC(Handy, handyInstance)
56
57Handy::Handy(QObject *parent)
58 : QObject(parent)
59 ,m_formFactor(new MauiMan::FormFactorManager(this))
60 ,m_accessibility(new MauiMan::AccessibilityManager(this))
61 ,m_hasTransientTouchInput(false)
62{
63 qDebug() << "CREATING INSTANCE OF MAUI HANDY";
64
65 connect(m_accessibility, &MauiMan::AccessibilityManager::singleClickChanged, [&](bool value)
66 {
67 m_singleClick = value;
68 Q_EMIT singleClickChanged();
69 });
70
71 m_singleClick = m_accessibility->singleClick();
72
73// #ifdef FORMFACTOR_FOUND //TODO check here for Cask desktop enviroment
74
75connect(m_formFactor, &MauiMan::FormFactorManager::preferredModeChanged, [this](uint value)
76{
77 m_ffactor = static_cast<FFactor>(value);
78 m_mobile = m_ffactor == FFactor::Phone || m_ffactor == FFactor::Tablet;
79 Q_EMIT formFactorChanged();
80 Q_EMIT isMobileChanged();
81});
82
83connect(m_formFactor, &MauiMan::FormFactorManager::hasTouchscreenChanged, [this](bool value)
84{
85 m_isTouch = value || m_formFactor->forceTouchScreen();
86 Q_EMIT isTouchChanged();
87});
88
89m_ffactor = static_cast<FFactor>(m_formFactor->preferredMode());
90m_mobile = m_ffactor == FFactor::Phone || m_ffactor == FFactor::Tablet;
91m_isTouch = m_formFactor->hasTouchscreen() || m_formFactor->forceTouchScreen();
92
93 connect(qApp, &QGuiApplication::focusWindowChanged, this, [this](QWindow *win)
94 {
95 if (win)
96 {
97 win->installEventFilter(this);
98 }
99 });
100}
101
102Handy *Handy::instance()
103{
104 return handyInstance();
105}
106
107bool Handy::isTouch()
108{
109 return m_isTouch;
110}
111
113{
114 return m_ffactor;
115}
116
118{
119 return m_hasTransientTouchInput;
120}
121
122void Handy::setTransientTouchInput(bool touch)
123{
124 if (touch == m_hasTransientTouchInput) {
125 return;
126 }
127
128 m_hasTransientTouchInput = touch;
129 Q_EMIT hasTransientTouchInputChanged();
130}
131
132bool Handy::eventFilter(QObject *watched, QEvent *event)
133{
134 Q_UNUSED(watched)
135 switch (event->type())
136 {
138 setTransientTouchInput(true);
139 break;
141 case QEvent::MouseMove: {
142 QMouseEvent *me = static_cast<QMouseEvent *>(event);
144 {
145 setTransientTouchInput(false);
146 }
147 break;
148 }
149 case QEvent::Wheel:
150 setTransientTouchInput(false);
151 default:
152 break;
153 }
154
155 return false;
156}
157
158#ifdef Q_OS_ANDROID
159static inline struct {
160 QList<QUrl> urls;
161 QString text;
162 bool cut = false;
163
164 bool hasUrls()
165 {
166 return !urls.isEmpty();
167 }
168 bool hasText()
169 {
170 return !text.isEmpty();
171 }
172
173} _clipboard;
174#endif
175
176QVariantMap Handy::userInfo()
177{
178 QString name = qgetenv("USER");
179 if (name.isEmpty())
180 name = qgetenv("USERNAME");
181
182 return QVariantMap({{FMH::MODEL_NAME[FMH::MODEL_KEY::NAME], name}});
183}
184
186{
187#ifdef Q_OS_ANDROID
188 auto clipboard = QGuiApplication::clipboard();
189#else
190 auto clipboard = QApplication::clipboard();
191#endif
192
193 auto mime = clipboard->mimeData();
194 if (mime->hasText())
195 return clipboard->text();
196
197 return QString();
198}
199
201{
202 QVariantMap res;
203#ifdef Q_OS_ANDROID
204 if (_clipboard.hasUrls())
205 res.insert("urls", QUrl::toStringList(_clipboard.urls));
206
207 if (_clipboard.hasText())
208 res.insert("text", _clipboard.text);
209
210 res.insert("cut", _clipboard.cut);
211#else
212 auto clipboard = QApplication::clipboard();
213
214 auto mime = clipboard->mimeData();
215
216 if(!mime)
217 return res;
218
219 if (mime->hasUrls())
220 res.insert("urls", QUrl::toStringList(mime->urls()));
221
222 if (mime->hasText())
223 res.insert("text", mime->text());
224
225 if(mime->hasImage())
226 res.insert("image", mime->imageData());
227
228 const QByteArray a = mime->data(QStringLiteral("application/x-kde-cutselection"));
229
230 res.insert("cut", !a.isEmpty() && a.at(0) == '1');
231#endif
232 return res;
233}
234
235bool Handy::copyToClipboard(const QVariantMap &value, const bool &cut)
236{
237#ifdef Q_OS_ANDROID
238 if (value.contains("urls"))
239 _clipboard.urls = QUrl::fromStringList(value["urls"].toStringList());
240
241 if (value.contains("text"))
242 _clipboard.text = value["text"].toString();
243
244 _clipboard.cut = cut;
245
246 return true;
247#else
248 auto clipboard = QApplication::clipboard();
249 QMimeData *mimeData = new QMimeData();
250
251 if (value.contains("urls"))
252 mimeData->setUrls(QUrl::fromStringList(value["urls"].toStringList()));
253
254 if (value.contains("text"))
255 mimeData->setText(value["text"].toString());
256
257 mimeData->setData(QStringLiteral("application/x-kde-cutselection"), cut ? "1" : "0");
258 clipboard->setMimeData(mimeData);
259
260 return true;
261#endif
262
263 return false;
264}
265
267{
268#ifdef Q_OS_ANDROID
269 Handy::copyToClipboard({{"text", text}});
270#else
272#endif
273 return true;
274}
275
280
281bool Handy::isAndroid()
282{
283 return FMH::isAndroid();
284}
285
286bool Handy::isLinux()
287{
288 return FMH::isLinux();
289}
290
291bool Handy::isIOS()
292{
293 return FMH::isIOS();
294}
295
297{
298 return m_formFactor->hasKeyboard();
299}
300
301bool Handy::hasMouse()
302{
303 return m_formFactor->hasMouse();
304}
305
306bool Handy::isWindows()
307{
308 return FMH::isWindows();
309}
310
311bool Handy::isMac()
312{
313 return FMH::isMac();
314}
315
316
318{
319 const QLocale locale;
320 return locale.formattedDataSize(size);
321}
322
323QString Handy::formatDate(const QString &dateStr, const QString &format, const QString &initFormat)
324{
325 if (initFormat.isEmpty())
326 return QDateTime::fromString(dateStr, Qt::TextDate).toString(format);
327 else
328 return QDateTime::fromString(dateStr, initFormat).toString(format);
329}
330
331QString Handy::formatTime(const qint64 &value)
332{
333 QString tStr;
334 if (value) {
335 QTime time((value / 3600) % 60, (value / 60) % 60, value % 60, (value * 1000) % 1000);
336 QString format = "mm:ss";
337 if (value > 3600)
338 {
339 format = "hh:mm:ss";
340 }
341 tStr = time.toString(format);
342 }
343
344 return tStr.isEmpty() ? "00:00" : tStr;
345}
346
347bool Handy::isMobile() const
348{
349 return m_mobile;
350}
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:276
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:200
static QString formatTime(const qint64 &value)
Format a milliseconds value to a readable format.
Definition handy.cpp:331
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:185
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:235
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:323
static QVariantMap userInfo()
Returns a key-value map containing basic information about the current user.
Definition handy.cpp:176
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:266
static QString formatSize(quint64 size)
Format a size value to the a readable locale size format.
Definition handy.cpp:317
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
const QList< QKeySequence > & cut()
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 May 10 2024 11:50:48 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.