File indexing completed on 2024-05-12 15:42:38

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