QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsinputcontrollermanager.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsinputcontroller.cpp
3 ---------------------
4 begin : March 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsconfig.h"
18
19#include "qgs2dmapcontroller.h"
20#include "qgs3dmapcontroller.h"
21
22#include <QRegularExpression>
23#include <QRegularExpressionMatch>
24#include <QString>
25
26#include "moc_qgsinputcontrollermanager.cpp"
27
28using namespace Qt::StringLiterals;
29
30#ifdef HAVE_QTGAMEPAD
33#include <QtGamepad/QGamepadManager>
34#endif
35
36//
37// QgsInputControllerManager
38//
39
41 : QObject( parent )
42{
43}
44
46{
47 qDeleteAll( m2DMapControllers );
48 qDeleteAll( m3DMapControllers );
49}
50
52{
53 if ( !controller )
54 return false;
55
56 if ( m2DMapControllers.contains( controller->deviceId() ) )
57 {
58 delete controller;
59 return false;
60 }
61
62 m2DMapControllers.insert( controller->deviceId(), controller );
63 return true;
64}
65
67{
68 if ( !controller )
69 return false;
70
71 if ( m3DMapControllers.contains( controller->deviceId() ) )
72 {
73 delete controller;
74 return false;
75 }
76
77 m3DMapControllers.insert( controller->deviceId(), controller );
78 return true;
79}
80
82{
83 QStringList devices = m2DMapControllers.keys();
84
85#ifdef HAVE_QTGAMEPAD
86 const QList<int> gamepadIds = QGamepadManager::instance()->connectedGamepads();
87 for ( int id : gamepadIds )
88 {
89 devices.append( u"gamepad2d:%1"_s.arg( id ) );
90 }
91#endif
92
93 return devices;
94}
95
97{
98#ifdef HAVE_QTGAMEPAD
99 const thread_local QRegularExpression gamepadRx( u"^gamepad2d:(\\d+)$"_s );
100 const QRegularExpressionMatch gamepadMatch = gamepadRx.match( deviceId );
101 if ( gamepadMatch.hasMatch() )
102 {
103 const int gamepadId = gamepadMatch.captured( 1 ).toInt();
104 return new QgsGamepad2DMapController( gamepadId );
105 }
106#endif
107
108 auto it = m2DMapControllers.constFind( deviceId );
109 if ( it == m2DMapControllers.constEnd() )
110 return nullptr;
111
112 return qgis::down_cast<QgsAbstract2DMapController *>( it.value()->clone() );
113}
114
116{
117 QStringList devices = m3DMapControllers.keys();
118
119#ifdef HAVE_QTGAMEPAD
120 const QList<int> gamepadIds = QGamepadManager::instance()->connectedGamepads();
121 for ( int id : gamepadIds )
122 {
123 devices.append( u"gamepad3d:%1"_s.arg( id ) );
124 }
125#endif
126
127 return devices;
128}
129
131{
132#ifdef HAVE_QTGAMEPAD
133 const thread_local QRegularExpression gamepadRx( u"^gamepad3d:(\\d+)$"_s );
134 const QRegularExpressionMatch gamepadMatch = gamepadRx.match( deviceId );
135 if ( gamepadMatch.hasMatch() )
136 {
137 const int gamepadId = gamepadMatch.captured( 1 ).toInt();
138 return new QgsGamepad3DMapController( gamepadId );
139 }
140#endif
141
142 auto it = m3DMapControllers.constFind( deviceId );
143 if ( it == m3DMapControllers.constEnd() )
144 return nullptr;
145
146 return qgis::down_cast<QgsAbstract3DMapController *>( it.value()->clone() );
147}
Abstract base class for all 2D map controllers.
Abstract base class for all 3D map controllers.
virtual QString deviceId() const =0
Returns a string uniquely identifying the device.
QStringList available2DMapControllers() const
Returns a list of the device IDs of available 2D map controllers.
QStringList available3DMapControllers() const
Returns a list of the device IDs of available 3D map controllers.
bool register2DMapController(QgsAbstract2DMapController *controller)
Registers a new 2D map controller.
QgsAbstract3DMapController * create3DMapController(const QString &deviceId) const
Returns a new instance of the 3D map controller with the specified deviceId.
QgsInputControllerManager(QObject *parent=nullptr)
Constructor for QgsInputControllerManager, with the specified parent object.
QgsAbstract2DMapController * create2DMapController(const QString &deviceId) const
Returns a new instance of the 2D map controller with the specified deviceId.
bool register3DMapController(QgsAbstract3DMapController *controller)
Registers a new 3D map controller.