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