Make combobox for device type register types work

This commit is contained in:
2023-02-18 18:14:42 +01:00
parent 4d61e066f4
commit 14af9d0093
6 changed files with 148 additions and 73 deletions

View File

@ -35,6 +35,7 @@ qt_add_qml_module(applightcontrol
DmxSlider.qml
StatusBar.qml
RegisterGroupsSettingsPage.qml
RegistersSettingsItem.qml
)
set_target_properties(applightcontrol PROPERTIES

View File

@ -14,7 +14,7 @@ ColumnLayout {
Layout.fillHeight: true
EditableListView {
id: deviceTypesListView
id: listView
Layout.preferredWidth: 300
Layout.maximumWidth: 300
@ -27,7 +27,7 @@ ColumnLayout {
}
ColumnLayout {
enabled: deviceTypesListView.currentIndex !== -1
enabled: listView.currentIndex !== -1
GridLayout {
Layout.preferredWidth: 600
@ -39,74 +39,21 @@ ColumnLayout {
SpinBox {
enabled: false
Layout.fillWidth: true
value: deviceTypesListView.currentData.id
onValueModified: deviceTypesListView.currentData.id = value
value: listView.currentData.id
onValueModified: listView.currentData.id = value
}
Label { text: qsTr("Name:") }
TextField {
Layout.fillWidth: true
text: deviceTypesListView.currentData.name
onTextEdited: deviceTypesListView.currentData.name = text
text: listView.currentData.name
onTextEdited: listView.currentData.name = text
}
Label { text: qsTr("Registers:") }
Pane {
RegistersSettingsItem {
Layout.fillWidth: true
Layout.fillHeight: true
Material.elevation: 6
RowLayout {
anchors.fill: parent
Pane {
Layout.preferredWidth: 300
Layout.fillHeight: true
Material.elevation: 6
EditableListView {
id: deviceTypesRegistersListView
anchors.fill: parent
textRole: 'registerTypeName'
model: DeviceTypeRegistersModel {
controller: __controller
deviceTypeId: deviceTypesListView.currentData.id
}
}
}
ColumnLayout {
Layout.fillWidth: true
Layout.fillHeight: true
enabled: deviceTypesRegistersListView.currentIndex >= 0
GridLayout {
Layout.fillWidth: true
columns: 2
Label {
text: qsTr('Type:')
}
ComboBox {
id: comboBox
model: deviceTypeRegisterTypesModel
textRole: "text"
valueRole: "value"
currentIndex: deviceTypesRegistersListView.currentData ? comboBox.indexOfValue(deviceTypesRegistersListView.currentData.registerType) : -1
}
}
Item {
Layout.fillWidth: true
Layout.fillHeight: true
}
}
}
deviceTypeId: listView.currentData.id
}
}
Item {

View File

@ -56,7 +56,7 @@ ColumnLayout {
textRole: "name"
valueRole: "id"
currentIndex: listView.currentData ? deviceTypeCombobox.indexOfValue(listView.currentData.deviceTypeId) : -1
onCurrentValueChanged: if (listView.currentData) listView.currentData.deviceTypeId = currentValue; else console.warn('discarded');
onActivated: if (listView.currentData) listView.currentData.deviceTypeId = currentValue; else console.warn('discarded');
}
Label { text: qsTr("Address:") }
SpinBox {

67
RegistersSettingsItem.qml Normal file
View File

@ -0,0 +1,67 @@
import QtQuick
import QtQuick.Controls.Material
import QtQuick.Layouts
import com.büro 1.0
Pane {
property alias deviceTypeId: deviceTypeRegistersModel.deviceTypeId
Material.elevation: 6
DeviceTypeRegistersModel {
id: deviceTypeRegistersModel
controller: __controller
}
RowLayout {
anchors.fill: parent
Pane {
Layout.preferredWidth: 300
Layout.fillHeight: true
Material.elevation: 6
EditableListView {
id: listView
anchors.fill: parent
textRole: 'registerTypeName'
model: deviceTypeRegistersModel
}
}
ColumnLayout {
Layout.fillWidth: true
Layout.fillHeight: true
enabled: listView.currentIndex >= 0
GridLayout {
Layout.fillWidth: true
columns: 2
Label {
text: qsTr('Type:')
}
ComboBox {
id: comboBox
model: deviceTypeRegisterTypesModel
textRole: "text"
valueRole: "value"
currentIndex: listView.currentData ? comboBox.indexOfValue(listView.currentData.registerType) : -1
onActivated: if (listView.currentData) listView.currentData.registerType = currentValue; else console.warn('discarded');
}
}
Item {
Layout.fillWidth: true
Layout.fillHeight: true
}
}
}
}

View File

@ -42,14 +42,16 @@ int DeviceTypeRegistersModel::rowCount(const QModelIndex &parent) const
if (m_deviceTypeId == -1)
return 0;
auto deviceType = m_controller->lightProject().deviceTypes.findById(m_deviceTypeId);
if (!deviceType)
auto deviceTypePtr = m_controller->lightProject().deviceTypes.findById(m_deviceTypeId);
if (!deviceTypePtr)
{
qWarning() << "hilfe" << __LINE__;
return 0;
}
return deviceType->registers.size();
const auto &deviceType = *deviceTypePtr;
return deviceType.registers.size();
}
QVariant DeviceTypeRegistersModel::data(const QModelIndex &index, int role) const
@ -72,14 +74,16 @@ QVariant DeviceTypeRegistersModel::data(const QModelIndex &index, int role) cons
return {};
}
auto deviceType = m_controller->lightProject().deviceTypes.findById(m_deviceTypeId);
if (!deviceType)
auto deviceTypePtr = m_controller->lightProject().deviceTypes.findById(m_deviceTypeId);
if (!deviceTypePtr)
{
qWarning() << "hilfe" << __LINE__;
return {};
}
if (index.row() < 0 || index.row() >= deviceType->registers.size())
const auto &deviceType = *deviceTypePtr;
if (index.row() < 0 || index.row() >= deviceType.registers.size())
{
qWarning() << "hilfe" << __LINE__;
return {};
@ -91,7 +95,7 @@ QVariant DeviceTypeRegistersModel::data(const QModelIndex &index, int role) cons
return {};
}
const auto &deviceTypeRegister = deviceType->registers.at(index.row());
const auto &deviceTypeRegister = deviceType.registers.at(index.row());
switch (role)
{
@ -126,14 +130,16 @@ QMap<int, QVariant> DeviceTypeRegistersModel::itemData(const QModelIndex &index)
return {};
}
auto deviceType = m_controller->lightProject().deviceTypes.findById(m_deviceTypeId);
if (!deviceType)
auto deviceTypePtr = m_controller->lightProject().deviceTypes.findById(m_deviceTypeId);
if (!deviceTypePtr)
{
qWarning() << "hilfe" << __LINE__;
return {};
}
if (index.row() < 0 || index.row() >= deviceType->registers.size())
auto &deviceType = *deviceTypePtr;
if (index.row() < 0 || index.row() >= deviceType.registers.size())
{
qWarning() << "hilfe" << __LINE__;
return {};
@ -145,7 +151,7 @@ QMap<int, QVariant> DeviceTypeRegistersModel::itemData(const QModelIndex &index)
return {};
}
const auto &deviceTypeRegister = deviceType->registers.at(index.row());
const auto &deviceTypeRegister = deviceType.registers.at(index.row());
return {
{ Qt::DisplayRole, QMetaEnum::fromType<DeviceTypeRegisterType>().valueToKey(std::to_underlying(deviceTypeRegister.type)) },
@ -161,6 +167,58 @@ QHash<int, QByteArray> DeviceTypeRegistersModel::roleNames() const
};
}
bool DeviceTypeRegistersModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid())
{
qWarning() << "hilfe" << __LINE__;
return {};
}
if (!m_controller)
{
qWarning() << "hilfe" << __LINE__;
return {};
}
if (m_deviceTypeId == -1)
{
qWarning() << "hilfe" << __LINE__;
return {};
}
auto deviceTypePtr = m_controller->lightProject().deviceTypes.findById(m_deviceTypeId);
if (!deviceTypePtr)
{
qWarning() << "hilfe" << __LINE__;
return {};
}
auto &deviceType = *deviceTypePtr;
if (index.row() < 0 || index.row() >= deviceType.registers.size())
{
qWarning() << "hilfe" << __LINE__;
return {};
}
if (index.column() != 0)
{
qWarning() << "hilfe" << __LINE__;
return {};
}
qDebug() << value.value<DeviceTypeRegisterType>();
auto &deviceTypeRegister = deviceType.registers.at(index.row());
deviceTypeRegister.type = value.value<DeviceTypeRegisterType>();
emit dataChanged(index, index, { Qt::DisplayRole, Qt::EditRole });
return true;
return false;
}
namespace {
void registerDenShit()
{

View File

@ -25,6 +25,8 @@ public:
QMap<int, QVariant> itemData(const QModelIndex &index) const override;
QHash<int, QByteArray> roleNames() const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
signals:
void controllerChanged(DmxController *controller);
void deviceTypeIdChanged(int deviceTypeId);