@@ -109,6 +109,10 @@ PTZSettings::PTZSettings() : QWidget(nullptr), ui(new Ui_PTZSettings)
109109 SLOT (setChecked (bool )));
110110 connect (ui->livemoveCheckBox , SIGNAL (clicked (bool )), PTZControls::getInstance (),
111111 SLOT (setDisableLiveMoves (bool )));
112+ connect (PTZControls::getInstance (), SIGNAL (joystickAxisActionChanged (size_t , ptz_joy_action_id)), this ,
113+ SLOT (joystickAxisMappingChanged (size_t , ptz_joy_action_id)));
114+ connect (PTZControls::getInstance (), SIGNAL (joystickButtonActionChanged (size_t , ptz_joy_action_id)), this ,
115+ SLOT (joystickButtonMappingChanged (size_t , ptz_joy_action_id)));
112116
113117 ui->speedRampCheckBox ->setChecked (PTZControls::getInstance ()->speedRampEnabled ());
114118 connect (PTZControls::getInstance (), SIGNAL (speedRampEnabledChanged (bool )), ui->speedRampCheckBox ,
@@ -157,6 +161,8 @@ void PTZSettings::joystickSetup()
157161 connect (joysticks, SIGNAL (countChanged ()), this , SLOT (joystickUpdate ()));
158162 connect (joysticks, SIGNAL (axisEvent (const QJoystickAxisEvent)), this ,
159163 SLOT (joystickAxisEvent (const QJoystickAxisEvent)));
164+ connect (joysticks, SIGNAL (buttonEvent (const QJoystickButtonEvent)), this ,
165+ SLOT (joystickButtonEvent (const QJoystickButtonEvent)));
160166
161167 auto selectionModel = ui->joystickNamesListView ->selectionModel ();
162168 connect (selectionModel, SIGNAL (currentChanged (QModelIndex, QModelIndex)), this ,
@@ -180,21 +186,126 @@ void PTZSettings::on_joystickGroupBox_toggled(bool checked)
180186 ui->joystickNamesListView ->setEnabled (checked);
181187}
182188
189+ void PTZSettings::on_joystickAxisActionChanged (int idx)
190+ {
191+ auto controls = PTZControls::getInstance ();
192+ auto cb = qobject_cast<QComboBox *>(sender ());
193+ if (cb == nullptr )
194+ return ;
195+ auto axis = cb->property (" axis-id" );
196+ ptz_joy_action_id action = cb->itemData (idx).toInt ();
197+ if (axis.isValid ())
198+ controls->setJoystickAxisAction (axis.toInt (), action);
199+ }
200+
201+ void PTZSettings::on_joystickButtonActionChanged (int idx)
202+ {
203+ auto controls = PTZControls::getInstance ();
204+ auto cb = qobject_cast<QComboBox *>(sender ());
205+ if (cb == nullptr )
206+ return ;
207+ auto axis = cb->property (" button-id" );
208+ ptz_joy_action_id action = cb->itemData (idx).toInt ();
209+ if (axis.isValid ())
210+ controls->setJoystickButtonAction (axis.toInt (), action);
211+ }
212+
213+ #define BUTTON_ROW_OFFSET 100
183214void PTZSettings::joystickUpdate ()
184215{
216+ auto cbAddJoyAction = [](QComboBox *cb, ptz_joy_action_id action) {
217+ cb->addItem (ptz_joy_action_names[action], (int )action);
218+ };
219+
185220 auto joysticks = QJoysticks::getInstance ();
186221 auto controls = PTZControls::getInstance ();
187222 m_joystickNamesModel.setStringList (joysticks->deviceNames ());
188- auto idx = m_joystickNamesModel.index (controls->joystickId (), 0 );
189- if (idx.isValid ())
223+ auto jid = controls->joystickId ();
224+ auto idx = m_joystickNamesModel.index (jid, 0 );
225+ if (idx.isValid ()) {
190226 ui->joystickNamesListView ->setCurrentIndex (idx);
227+ for (int i = joystickAxisLabels.count (); i < joysticks->getNumAxes (jid); i++) {
228+ auto label = new QLabel (this );
229+ auto cb = new QComboBox (this );
230+ label->setText (QString (" Axis %1" ).arg (i));
231+ cbAddJoyAction (cb, PTZ_JOY_ACTION_NONE);
232+ cbAddJoyAction (cb, PTZ_JOY_ACTION_PAN);
233+ cbAddJoyAction (cb, PTZ_JOY_ACTION_TILT);
234+ cbAddJoyAction (cb, PTZ_JOY_ACTION_ZOOM);
235+ cb->setProperty (" axis-id" , i);
236+ joystickAxisLabels.append (label);
237+ joystickAxisCBs.append (cb);
238+ ui->joystickMapGridLayout ->addWidget (label, i, 0 );
239+ ui->joystickMapGridLayout ->addWidget (cb, i, 1 );
240+ connect (cb, SIGNAL (currentIndexChanged (int )), this , SLOT (on_joystickAxisActionChanged (int )));
241+ }
242+ for (int i = joystickButtonLabels.count (); i < joysticks->getNumButtons (jid); i++) {
243+ auto label = new QLabel (this );
244+ auto cb = new QComboBox (this );
245+ label->setText (QString (" Button %1" ).arg (i));
246+ cbAddJoyAction (cb, PTZ_JOY_ACTION_NONE);
247+ cbAddJoyAction (cb, PTZ_JOY_ACTION_CAMERA_PREV);
248+ cbAddJoyAction (cb, PTZ_JOY_ACTION_CAMERA_NEXT);
249+ cbAddJoyAction (cb, PTZ_JOY_ACTION_PRESET_PREV);
250+ cbAddJoyAction (cb, PTZ_JOY_ACTION_PRESET_NEXT);
251+ cbAddJoyAction (cb, PTZ_JOY_ACTION_PRESET_RECALL);
252+ cb->setProperty (" button-id" , i);
253+ joystickButtonLabels.append (label);
254+ joystickButtonCBs.append (cb);
255+ ui->joystickMapGridLayout ->addWidget (label, i + BUTTON_ROW_OFFSET, 0 );
256+ ui->joystickMapGridLayout ->addWidget (cb, i + BUTTON_ROW_OFFSET, 1 );
257+ connect (cb, SIGNAL (currentIndexChanged (int )), this , SLOT (on_joystickButtonActionChanged (int )));
258+ }
259+
260+ for (int i = 0 ; i < joystickAxisCBs.size (); i++)
261+ joystickAxisMappingChanged (i, controls->joystickAxisAction (i));
262+ for (int i = 0 ; i < joystickButtonCBs.size (); i++)
263+ joystickButtonMappingChanged (i, controls->joystickButtonAction (i));
264+ }
265+ }
266+
267+ void PTZSettings::joystickAxisMappingChanged (size_t axis, ptz_joy_action_id action)
268+ {
269+ if (axis >= (size_t )joystickAxisCBs.size ())
270+ return ;
271+ auto cb = joystickAxisCBs.at (axis);
272+ auto idx = cb->findData (QVariant ((int )action));
273+ cb->setCurrentIndex (idx >= 0 ? idx : 0 );
274+ }
275+
276+ void PTZSettings::joystickButtonMappingChanged (size_t button, ptz_joy_action_id action)
277+ {
278+ if (button >= (size_t )joystickButtonCBs.size ())
279+ return ;
280+ auto cb = joystickButtonCBs.at (button);
281+ auto idx = cb->findData (QVariant ((int )action));
282+ cb->setCurrentIndex (idx >= 0 ? idx : 0 );
191283}
192284
193285void PTZSettings::joystickCurrentChanged (QModelIndex current, QModelIndex previous)
194286{
195287 Q_UNUSED (previous);
196288 PTZControls::getInstance ()->setJoystickId (current.row ());
197289}
290+
291+ void PTZSettings::joystickAxisEvent (const QJoystickAxisEvent evt)
292+ {
293+ auto jid = PTZControls::getInstance ()->joystickId ();
294+ if (evt.joystick ->id != jid || evt.axis >= joystickAxisLabels.size ())
295+ return ;
296+ auto label = joystickAxisLabels.at (evt.axis );
297+ label->setText (QString (" Axis %1 [%2]" ).arg (evt.axis ).arg (evt.value ));
298+ }
299+
300+ void PTZSettings::joystickButtonEvent (const QJoystickButtonEvent evt)
301+ {
302+ auto jid = PTZControls::getInstance ()->joystickId ();
303+ if (evt.joystick ->id != jid || evt.button >= joystickButtonLabels.size ())
304+ return ;
305+ auto label = joystickButtonLabels.at (evt.button );
306+ label->setText (QString (" Button %1 [%2]" ).arg (evt.button ).arg (evt.pressed ));
307+ }
308+
198309#else
199310void PTZSettings::joystickSetup ()
200311{
0 commit comments