File indexing completed on 2024-05-05 03:56:27

0001 /*
0002  *  SPDX-FileCopyrightText: 2018 Marco Martin <mart@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef KIRIGAMI_TABLETMODEWATCHER_H
0008 #define KIRIGAMI_TABLETMODEWATCHER_H
0009 
0010 #include <QEvent>
0011 #include <QObject>
0012 
0013 #include "kirigamiplatform_export.h"
0014 
0015 namespace Kirigami
0016 {
0017 namespace Platform
0018 {
0019 class TabletModeWatcherPrivate;
0020 
0021 class KIRIGAMIPLATFORM_EXPORT TabletModeChangedEvent : public QEvent
0022 {
0023 public:
0024     TabletModeChangedEvent(bool tablet)
0025         : QEvent(TabletModeChangedEvent::type)
0026         , tabletMode(tablet)
0027     {
0028     }
0029 
0030     bool tabletMode = false;
0031 
0032     inline static QEvent::Type type = QEvent::None;
0033 };
0034 
0035 /**
0036  * @class TabletModeWatcher tabletmodewatcher.h <Kirigami/TabletModeWatcher>
0037  *
0038  * This class reports on the status of certain transformable
0039  * devices which can be both tablets and laptops at the same time,
0040  * with a detachable keyboard.
0041  * It reports whether the device supports a tablet mode and if
0042  * the device is currently in such mode or not, emitting a signal
0043  * when the user switches.
0044  */
0045 class KIRIGAMIPLATFORM_EXPORT TabletModeWatcher : public QObject
0046 {
0047     Q_OBJECT
0048 
0049     Q_PROPERTY(bool tabletModeAvailable READ isTabletModeAvailable NOTIFY tabletModeAvailableChanged FINAL)
0050     Q_PROPERTY(bool tabletMode READ isTabletMode NOTIFY tabletModeChanged FINAL)
0051 
0052 public:
0053     ~TabletModeWatcher() override;
0054     static TabletModeWatcher *self();
0055 
0056     /**
0057      * @returns true if the device supports a tablet mode and has a switch
0058      * to report when the device has been transformed.
0059      * For debug purposes, if either the environment variable QT_QUICK_CONTROLS_MOBILE
0060      * or KDE_KIRIGAMI_TABLET_MODE are set to true, isTabletModeAvailable will be true
0061      */
0062     bool isTabletModeAvailable() const;
0063 
0064     /**
0065      * @returns true if the machine is now in tablet mode, such as the
0066      * laptop keyboard flipped away or detached.
0067      * Note that this doesn't mean exactly a tablet form factor, but
0068      * that the preferred input mode for the device is the touch screen
0069      * and that pointer and keyboard are either secondary or not available.
0070      *
0071      * For debug purposes, if either the environment variable QT_QUICK_CONTROLS_MOBILE
0072      * or KDE_KIRIGAMI_TABLET_MODE are set to true, isTabletMode will be true
0073      */
0074     bool isTabletMode() const;
0075 
0076     /**
0077      * Register an arbitrary QObject to send events from this.
0078      * At the moment only one event will be sent: TabletModeChangedEvent
0079      */
0080     void addWatcher(QObject *watcher);
0081 
0082     /*
0083      * Unsubscribe watcher from receiving events from TabletModeWatcher.
0084      */
0085     void removeWatcher(QObject *watcher);
0086 
0087 Q_SIGNALS:
0088     void tabletModeAvailableChanged(bool tabletModeAvailable);
0089     void tabletModeChanged(bool tabletMode);
0090 
0091 private:
0092     KIRIGAMIPLATFORM_NO_EXPORT explicit TabletModeWatcher(QObject *parent = nullptr);
0093     TabletModeWatcherPrivate *d;
0094     friend class TabletModeWatcherSingleton;
0095 };
0096 
0097 }
0098 }
0099 
0100 #endif // KIRIGAMI_TABLETMODEWATCHER