QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
17#include "moc_qgsinputcontrollermanager.cpp"
18#include "qgs2dmapcontroller.h"
19#include "qgs3dmapcontroller.h"
20#include "qgsconfig.h"
21
22#include <QRegularExpression>
23#include <QRegularExpressionMatch>
24
25#ifdef HAVE_QTGAMEPAD
28#include <QtGamepad/QGamepadManager>
29#endif
30
31//
32// QgsInputControllerManager
33//
34
36 : QObject( parent )
37{
38
39}
40
42{
43 qDeleteAll( m2DMapControllers );
44 qDeleteAll( m3DMapControllers );
45}
46
48{
49 if ( !controller )
50 return false;
51
52 if ( m2DMapControllers.contains( controller->deviceId() ) )
53 {
54 delete controller;
55 return false;
56 }
57
58 m2DMapControllers.insert( controller->deviceId(), controller );
59 return true;
60}
61
63{
64 if ( !controller )
65 return false;
66
67 if ( m3DMapControllers.contains( controller->deviceId() ) )
68 {
69 delete controller;
70 return false;
71 }
72
73 m3DMapControllers.insert( controller->deviceId(), controller );
74 return true;
75}
76
78{
79 QStringList devices = m2DMapControllers.keys();
80
81#ifdef HAVE_QTGAMEPAD
82 const QList< int > gamepadIds = QGamepadManager::instance()->connectedGamepads();
83 for ( int id : gamepadIds )
84 {
85 devices.append( QStringLiteral( "gamepad2d:%1" ).arg( id ) );
86 }
87#endif
88
89 return devices;
90}
91
93{
94#ifdef HAVE_QTGAMEPAD
95 const thread_local QRegularExpression gamepadRx( QStringLiteral( "^gamepad2d:(\\d+)$" ) );
96 const QRegularExpressionMatch gamepadMatch = gamepadRx.match( deviceId );
97 if ( gamepadMatch.hasMatch() )
98 {
99 const int gamepadId = gamepadMatch.captured( 1 ).toInt();
100 return new QgsGamepad2DMapController( gamepadId );
101 }
102#endif
103
104 auto it = m2DMapControllers.constFind( deviceId );
105 if ( it == m2DMapControllers.constEnd() )
106 return nullptr;
107
108 return qgis::down_cast< QgsAbstract2DMapController *>( it.value()->clone() );
109}
110
112{
113 QStringList devices = m3DMapControllers.keys();
114
115#ifdef HAVE_QTGAMEPAD
116 const QList< int > gamepadIds = QGamepadManager::instance()->connectedGamepads();
117 for ( int id : gamepadIds )
118 {
119 devices.append( QStringLiteral( "gamepad3d:%1" ).arg( id ) );
120 }
121#endif
122
123 return devices;
124}
125
127{
128#ifdef HAVE_QTGAMEPAD
129 const thread_local QRegularExpression gamepadRx( QStringLiteral( "^gamepad3d:(\\d+)$" ) );
130 const QRegularExpressionMatch gamepadMatch = gamepadRx.match( deviceId );
131 if ( gamepadMatch.hasMatch() )
132 {
133 const int gamepadId = gamepadMatch.captured( 1 ).toInt();
134 return new QgsGamepad3DMapController( gamepadId );
135 }
136#endif
137
138 auto it = m3DMapControllers.constFind( deviceId );
139 if ( it == m3DMapControllers.constEnd() )
140 return nullptr;
141
142 return qgis::down_cast< QgsAbstract3DMapController *>( it.value()->clone() );
143}
144
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.