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