File indexing completed on 2024-05-12 05:35:39
0001 /* 0002 SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com> 0003 SPDX-FileCopyrightText: 2023 Jeremy Whiting <jpwhiting@kde.org> 0004 SPDX-FileCopyrightText: 2023 Niccolò Venerandi <niccolo@venerandi.com> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "axesmodel.h" 0010 0011 #include <SDL2/SDL_joystick.h> 0012 0013 AxesModel::AxesModel(QObject *parent) 0014 : QAbstractTableModel(parent) 0015 { 0016 connect(this, &AxesModel::deviceChanged, this, [this] { 0017 beginResetModel(); 0018 endResetModel(); 0019 0020 if (m_device != nullptr) { 0021 connect(m_device, &Gamepad::axisStateChanged, this, [this](int index) { 0022 const QModelIndex changedIndex = this->index(index, 0); 0023 Q_EMIT dataChanged(changedIndex, changedIndex, {Qt::DisplayRole}); 0024 }); 0025 } 0026 }); 0027 } 0028 0029 int AxesModel::rowCount(const QModelIndex &parent) const 0030 { 0031 Q_UNUSED(parent); 0032 0033 if (m_device == nullptr) { 0034 return 0; 0035 } 0036 0037 return SDL_JoystickNumAxes(m_device->joystick()); 0038 } 0039 0040 int AxesModel::columnCount(const QModelIndex &parent) const 0041 { 0042 Q_UNUSED(parent); 0043 return 1; 0044 } 0045 0046 QVariant AxesModel::data(const QModelIndex &index, int role) const 0047 { 0048 if (!checkIndex(index) || m_device == nullptr) { 0049 return {}; 0050 } 0051 0052 if (index.column() == 0 && role == Qt::DisplayRole) { 0053 return SDL_JoystickGetAxis(m_device->joystick(), index.row()); 0054 } 0055 0056 return {}; 0057 } 0058 0059 QVariant AxesModel::headerData(int section, Qt::Orientation orientation, int role) const 0060 { 0061 if (role == Qt::DisplayRole) { 0062 if (orientation == Qt::Horizontal && section == 0) { 0063 return i18nc("@label Axis value", "Value"); 0064 } else if (orientation == Qt::Vertical) { 0065 return QString::number(section + 1); 0066 } 0067 } 0068 0069 return {}; 0070 } 0071 0072 #include "moc_axesmodel.cpp"