List gamepads

This commit is contained in:
Marc Riera 2022-10-12 01:15:56 +02:00
parent 139875808a
commit 5a8522d05a
5 changed files with 69 additions and 48 deletions

View file

@ -3,6 +3,7 @@ from PyQt5 import uic
from PyQt5.QtWidgets import QMainWindow, QTableWidgetItem from PyQt5.QtWidgets import QMainWindow, QTableWidgetItem
from gui.main_ui import Ui_MainWindow from gui.main_ui import Ui_MainWindow
from handlers.gamepad import Gamepad from handlers.gamepad import Gamepad
from models.gamepad import GamepadModel
class MainWindow(QMainWindow): class MainWindow(QMainWindow):
def __init__(self, gamepad_handler): def __init__(self, gamepad_handler):
@ -12,11 +13,12 @@ class MainWindow(QMainWindow):
self._gui = Ui_MainWindow() self._gui = Ui_MainWindow()
self._gui.setupUi(self) self._gui.setupUi(self)
model = GamepadModel(gamepad_handler.find_gamepads())
self._gui.tableView_physicalControllerList.setModel(model)
self._gui.tableView_physicalControllerList.resizeColumnsToContents()
self._gui.pushButton_physicalControllerRefresh.clicked.connect(self.controller_list_refresh) self._gui.pushButton_physicalControllerRefresh.clicked.connect(self.controller_list_refresh)
def controller_list_refresh(self): def controller_list_refresh(self):
self._gui.tableWidget_physicalControllerList.setRowCount(0) return
for gamepad in self._gamepad_handler.find_gamepads():
rowCount = self._gui.tableWidget_physicalControllerList.rowCount()
self._gui.tableWidget_physicalControllerList.insertRow(rowCount)
self._gui.tableWidget_physicalControllerList.setItem(rowCount , 1, QTableWidgetItem(gamepad.name))

View file

@ -14,14 +14,14 @@ from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object): class Ui_MainWindow(object):
def setupUi(self, MainWindow): def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow") MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600) MainWindow.resize(800, 400)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0) sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0) sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth()) sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy) MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(800, 600)) MainWindow.setMinimumSize(QtCore.QSize(800, 400))
MainWindow.setMaximumSize(QtCore.QSize(800, 600)) MainWindow.setMaximumSize(QtCore.QSize(800, 400))
self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget") self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget) self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
@ -33,20 +33,13 @@ class Ui_MainWindow(object):
self.groupBox_physicalControllers.setObjectName("groupBox_physicalControllers") self.groupBox_physicalControllers.setObjectName("groupBox_physicalControllers")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.groupBox_physicalControllers) self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.groupBox_physicalControllers)
self.verticalLayout_2.setObjectName("verticalLayout_2") self.verticalLayout_2.setObjectName("verticalLayout_2")
self.tableWidget_physicalControllerList = QtWidgets.QTableWidget(self.groupBox_physicalControllers) self.tableView_physicalControllerList = QtWidgets.QTableView(self.groupBox_physicalControllers)
self.tableWidget_physicalControllerList.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection) self.tableView_physicalControllerList.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
self.tableWidget_physicalControllerList.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) self.tableView_physicalControllerList.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.tableWidget_physicalControllerList.setObjectName("tableWidget_physicalControllerList") self.tableView_physicalControllerList.setObjectName("tableView_physicalControllerList")
self.tableWidget_physicalControllerList.setColumnCount(3) self.tableView_physicalControllerList.horizontalHeader().setStretchLastSection(True)
self.tableWidget_physicalControllerList.setRowCount(0) self.tableView_physicalControllerList.verticalHeader().setVisible(False)
item = QtWidgets.QTableWidgetItem() self.verticalLayout_2.addWidget(self.tableView_physicalControllerList)
self.tableWidget_physicalControllerList.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget_physicalControllerList.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget_physicalControllerList.setHorizontalHeaderItem(2, item)
self.tableWidget_physicalControllerList.horizontalHeader().setStretchLastSection(True)
self.verticalLayout_2.addWidget(self.tableWidget_physicalControllerList)
self.horizontalLayout_physicalControllerActions = QtWidgets.QHBoxLayout() self.horizontalLayout_physicalControllerActions = QtWidgets.QHBoxLayout()
self.horizontalLayout_physicalControllerActions.setObjectName("horizontalLayout_physicalControllerActions") self.horizontalLayout_physicalControllerActions.setObjectName("horizontalLayout_physicalControllerActions")
self.pushButton_physicalControllerRefresh = QtWidgets.QPushButton(self.groupBox_physicalControllers) self.pushButton_physicalControllerRefresh = QtWidgets.QPushButton(self.groupBox_physicalControllers)
@ -99,12 +92,6 @@ class Ui_MainWindow(object):
_translate = QtCore.QCoreApplication.translate _translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Densha de GO! Controller Converter")) MainWindow.setWindowTitle(_translate("MainWindow", "Densha de GO! Controller Converter"))
self.groupBox_physicalControllers.setTitle(_translate("MainWindow", "Physical controllers")) self.groupBox_physicalControllers.setTitle(_translate("MainWindow", "Physical controllers"))
item = self.tableWidget_physicalControllerList.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "ID"))
item = self.tableWidget_physicalControllerList.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Name"))
item = self.tableWidget_physicalControllerList.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Status"))
self.pushButton_physicalControllerRefresh.setText(_translate("MainWindow", "Refresh")) self.pushButton_physicalControllerRefresh.setText(_translate("MainWindow", "Refresh"))
self.pushButton_physicalControllerConfig.setText(_translate("MainWindow", "Configuration")) self.pushButton_physicalControllerConfig.setText(_translate("MainWindow", "Configuration"))
self.groupBox_emulatedController.setTitle(_translate("MainWindow", "Emulated controller")) self.groupBox_emulatedController.setTitle(_translate("MainWindow", "Emulated controller"))

View file

@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>800</width> <width>800</width>
<height>600</height> <height>400</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -19,13 +19,13 @@
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>800</width> <width>800</width>
<height>600</height> <height>400</height>
</size> </size>
</property> </property>
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>800</width> <width>800</width>
<height>600</height> <height>400</height>
</size> </size>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -54,7 +54,7 @@
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item> <item>
<widget class="QTableWidget" name="tableWidget_physicalControllerList"> <widget class="QTableView" name="tableView_physicalControllerList">
<property name="selectionMode"> <property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum> <enum>QAbstractItemView::SingleSelection</enum>
</property> </property>
@ -64,21 +64,9 @@
<attribute name="horizontalHeaderStretchLastSection"> <attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool> <bool>true</bool>
</attribute> </attribute>
<column> <attribute name="verticalHeaderVisible">
<property name="text"> <bool>false</bool>
<string>ID</string> </attribute>
</property>
</column>
<column>
<property name="text">
<string>Name</string>
</property>
</column>
<column>
<property name="text">
<string>Status</string>
</property>
</column>
</widget> </widget>
</item> </item>
<item> <item>

View file

@ -20,7 +20,6 @@ class Gamepad:
self.name = name self.name = name
""" cap = { """ cap = {
e.EV_KEY : [e.BTN_NORTH, e.BTN_SOUTH, e.BTN_EAST, e.BTN_WEST, e.BTN_SELECT, e.BTN_START], e.EV_KEY : [e.BTN_NORTH, e.BTN_SOUTH, e.BTN_EAST, e.BTN_WEST, e.BTN_SELECT, e.BTN_START],
e.EV_ABS : [(e.ABS_X, AbsInfo(0, 0, 255, 0, 0, 0)), (e.ABS_Y, AbsInfo(0, 0, 255, 0, 0, 0))] e.EV_ABS : [(e.ABS_X, AbsInfo(0, 0, 255, 0, 0, 0)), (e.ABS_Y, AbsInfo(0, 0, 255, 0, 0, 0))]

45
models/gamepad.py Normal file
View file

@ -0,0 +1,45 @@
from hashlib import sha1
from PyQt5.QtCore import Qt, QVariant, QAbstractTableModel
from handlers.gamepad import Gamepad
headers = ["ID", "Name", "Status"]
class GamepadModel(QAbstractTableModel):
def __init__(self, gamepads=None):
super(GamepadModel, self).__init__()
self.gamepads = gamepads or []
def data(self, index, role):
if role == Qt.DisplayRole:
match index.column():
case 0:
return self.get_gamepad_id(index)
case 1:
return self.gamepads[index.row()].name
case 2:
return self.get_gamepad_hash(index)
case _:
return None
def rowCount(self, index):
return len(self.gamepads)
def columnCount(self, index):
return len(headers)
def headerData(self, section, orientation, role):
if role != Qt.DisplayRole or orientation != Qt.Horizontal:
return QVariant()
return headers[section]
def get_gamepad_id(self, index):
vid = format(self.gamepads[index.row()].vid, "x").zfill(4)
pid = format(self.gamepads[index.row()].pid, "x").zfill(4)
id = str(vid + ":" + pid)
return id
def get_gamepad_hash(self, index):
id = self.get_gamepad_id(index)
name = self.gamepads[index.row()].name
hash = sha1(str(id + ":" + name).encode('utf-8')).hexdigest()
return hash