QGIS API Documentation 3.41.0-Master (af5edcb665c)
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
41{
42 qDeleteAll( m2DMapControllers );
43 qDeleteAll( m3DMapControllers );
44}
45
47{
48 if ( !controller )
49 return false;
50
51 if ( m2DMapControllers.contains( controller->deviceId() ) )
52 {
53 delete controller;
54 return false;
55 }
56
57 m2DMapControllers.insert( controller->deviceId(), controller );
58 return true;
59}
60
62{
63 if ( !controller )
64 return false;
65
66 if ( m3DMapControllers.contains( controller->deviceId() ) )
67 {
68 delete controller;
69 return false;
70 }
71
72 m3DMapControllers.insert( controller->deviceId(), controller );
73 return true;
74}
75
77{
78 QStringList devices = m2DMapControllers.keys();
79
80#ifdef HAVE_QTGAMEPAD
81 const QList<int> gamepadIds = QGamepadManager::instance()->connectedGamepads();
82 for ( int id : gamepadIds )
83 {
84 devices.append( QStringLiteral( "gamepad2d:%1" ).arg( id ) );
85 }
86#endif
87
88 return devices;
89}
90
92{
93#ifdef HAVE_QTGAMEPAD
94 const thread_local QRegularExpression gamepadRx( QStringLiteral( "^gamepad2d:(\\d+)$" ) );
95 const QRegularExpressionMatch gamepadMatch = gamepadRx.match( deviceId );
96 if ( gamepadMatch.hasMatch() )
97 {
98 const int gamepadId = gamepadMatch.captured( 1 ).toInt();
99 return new QgsGamepad2DMapController( gamepadId );
100 }
101#endif
102
103 auto it = m2DMapControllers.constFind( deviceId );
104 if ( it == m2DMapControllers.constEnd() )
105 return nullptr;
106
107 return qgis::down_cast<QgsAbstract2DMapController *>( it.value()->clone() );
108}
109
111{
112 QStringList devices = m3DMapControllers.keys();
113
114#ifdef HAVE_QTGAMEPAD
115 const QList<int> gamepadIds = QGamepadManager::instance()->connectedGamepads();
116 for ( int id : gamepadIds )
117 {
118 devices.append( QStringLiteral( "gamepad3d:%1" ).arg( id ) );
119 }
120#endif
121
122 return devices;
123}
124
126{
127#ifdef HAVE_QTGAMEPAD
128 const thread_local QRegularExpression gamepadRx( QStringLiteral( "^gamepad3d:(\\d+)$" ) );
129 const QRegularExpressionMatch gamepadMatch = gamepadRx.match( deviceId );
130 if ( gamepadMatch.hasMatch() )
131 {
132 const int gamepadId = gamepadMatch.captured( 1 ).toInt();
133 return new QgsGamepad3DMapController( gamepadId );
134 }
135#endif
136
137 auto it = m3DMapControllers.constFind( deviceId );
138 if ( it == m3DMapControllers.constEnd() )
139 return nullptr;
140
141 return qgis::down_cast<QgsAbstract3DMapController *>( it.value()->clone() );
142}
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.