File indexing completed on 2024-11-24 05:00:37

0001 /*
0002     SPDX-FileCopyrightText: 2019 Harald Sitter <sitter@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include "xkbobject.h"
0010 
0011 class Shape;
0012 
0013 // This is a fairly opinionated model of a key cap. We assume there won't
0014 // be any more than 4 levels.
0015 // This isn't necessarily the case since (e.g.) the NEO layout has 6 levels.
0016 // Since that is super niche and poses some complication as for how to
0017 // exactly render the levels we'll simply ignore this use case for now.
0018 // In the end the keyboard model display only needs to act as simple indicator
0019 // anyway.
0020 class KeyCap : public QObject
0021 {
0022     Q_OBJECT
0023     Q_PROPERTY(QString topLeft MEMBER topLeft CONSTANT)
0024     Q_PROPERTY(QString topRight MEMBER topRight CONSTANT)
0025     Q_PROPERTY(QString bottomLeft MEMBER bottomLeft CONSTANT)
0026     Q_PROPERTY(QString bottomRight MEMBER bottomRight CONSTANT)
0027 public:
0028     enum Level {
0029         TopLeft = 1,
0030         TopRight = 3,
0031         BottomLeft = 0,
0032         BottomRight = 2,
0033     };
0034     Q_ENUM(Level)
0035     constexpr static int levelCount = 4;
0036 
0037     KeyCap(const QString symbols[KeyCap::levelCount], QObject *parent);
0038 
0039     QString topLeft;
0040     QString topRight;
0041     QString bottomLeft;
0042     QString bottomRight;
0043 };
0044 
0045 class Key : public XkbObject
0046 {
0047     Q_OBJECT
0048 #define K_P(type, name)                                                                                                                                        \
0049 private:                                                                                                                                                       \
0050     Q_PROPERTY(type name READ auto_prop_##name CONSTANT)                                                                                                       \
0051 public:                                                                                                                                                        \
0052     type auto_prop_##name() const                                                                                                                              \
0053     {                                                                                                                                                          \
0054         return key->name;                                                                                                                                      \
0055     }
0056 
0057     K_P(short, gap)
0058 
0059     Q_PROPERTY(Shape *shape MEMBER shape CONSTANT)
0060     Q_PROPERTY(KeyCap *cap MEMBER cap CONSTANT)
0061     Q_PROPERTY(bool pressed MEMBER pressed NOTIFY pressedChanged)
0062 
0063     constexpr static uint INVALID_KEYCODE = static_cast<uint>(-1);
0064 
0065 public:
0066     Key(XkbKeyPtr key_, XkbDescPtr xkb_, QObject *parent = nullptr);
0067     uint nativeScanCodeFromName(const QByteArray &needle);
0068     KeyCap *resolveCap();
0069 
0070     XkbKeyPtr key = nullptr;
0071     Shape *shape = nullptr;
0072     QByteArray name; // Internal name in the geometry.
0073     quint32 nativeScanCode = INVALID_KEYCODE;
0074     KeyCap *cap = nullptr;
0075     bool pressed = false;
0076 
0077 Q_SIGNALS:
0078     void pressedChanged();
0079 };