// SPDX-License-Identifier: LGPL-3.0-or-later /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 2013 - 2024, nymea GmbH * Copyright (C) 2024 - 2025, chargebyte austria GmbH * * This file is part of libnymea-app. * * libnymea-app is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * libnymea-app is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with libnymea-app. If not, see . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "thingmodel.h" #include "types/statetype.h" ThingModel::ThingModel(QObject *parent) : QAbstractListModel(parent) { } int ThingModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent) return static_cast(m_list.count()); } QVariant ThingModel::data(const QModelIndex &index, int role) const { if (role == RoleId) { return m_list.at(index.row()); } if (role == RoleName) { StateType* stateType = m_device->thingClass()->stateTypes()->getStateType(m_list.at(index.row())); if (stateType) { return stateType->name(); } ActionType* actionType = m_device->thingClass()->actionTypes()->getActionType(m_list.at(index.row())); if (actionType) { return actionType->name(); } EventType* eventType = m_device->thingClass()->eventTypes()->getEventType(m_list.at(index.row())); if (eventType) { return eventType->name(); } } if (role == RoleType) { StateType* stateType = m_device->thingClass()->stateTypes()->getStateType(m_list.at(index.row())); if (stateType) { return TypeStateType; } ActionType* actionType = m_device->thingClass()->actionTypes()->getActionType(m_list.at(index.row())); if (actionType) { return TypeActionType; } EventType* eventType = m_device->thingClass()->eventTypes()->getEventType(m_list.at(index.row())); if (eventType) { return TypeEventType; } } if (role == RoleDisplayName) { StateType* stateType = m_device->thingClass()->stateTypes()->getStateType(m_list.at(index.row())); if (stateType) { return stateType->displayName(); } ActionType* actionType = m_device->thingClass()->actionTypes()->getActionType(m_list.at(index.row())); if (actionType) { return actionType->displayName(); } EventType* eventType = m_device->thingClass()->eventTypes()->getEventType(m_list.at(index.row())); if (eventType) { return eventType->displayName(); } } if (role == RoleWritable) { ActionType* actionType = m_device->thingClass()->actionTypes()->getActionType(m_list.at(index.row())); return actionType != nullptr; } return QVariant(); } QHash ThingModel::roleNames() const { QHash roles; roles.insert(RoleId, "id"); roles.insert(RoleName, "name"); roles.insert(RoleType, "type"); roles.insert(RoleDisplayName, "displayName"); roles.insert(RoleWritable, "writable"); return roles; } QVariant ThingModel::getData(int index, int role) const { if (index < 0 || index >= m_list.count()) { return QVariant(); } return data(this->index(index), role); } Thing *ThingModel::thing() const { return m_device; } void ThingModel::setThing(Thing *device) { if (m_device != device) { m_device = device; emit thingChanged(); updateList(); } } bool ThingModel::showStates() const { return m_showStates; } void ThingModel::setShowStates(bool showStates) { if (m_showStates != showStates) { m_showStates = showStates; emit showStatesChanged(); updateList(); } } bool ThingModel::showActions() const { return m_showActions; } void ThingModel::setShowActions(bool showActions) { if (m_showActions != showActions) { m_showActions = showActions; emit showActionsChanged(); updateList(); } } bool ThingModel::showEvents() const { return m_showEvents; } void ThingModel::setShowEvents(bool showEvents) { if (m_showEvents != showEvents) { m_showEvents = showEvents; emit showEventsChanged(); updateList(); } } void ThingModel::updateList() { if (!m_device) { beginResetModel(); m_list.clear(); endResetModel(); emit countChanged(); return; } beginResetModel(); m_list.clear(); if (m_showStates) { for (int i = 0; i < m_device->thingClass()->stateTypes()->rowCount(); i++) { m_list.append(m_device->thingClass()->stateTypes()->get(i)->id()); } } if (m_showActions) { for (int i = 0; i < m_device->thingClass()->actionTypes()->rowCount(); i++) { if (!m_list.contains(m_device->thingClass()->actionTypes()->get(i)->id())) { m_list.append(m_device->thingClass()->actionTypes()->get(i)->id()); } } } if (m_showEvents) { for (int i = 0; i < m_device->thingClass()->eventTypes()->rowCount(); i++) { if (!m_list.contains(m_device->thingClass()->eventTypes()->get(i)->id())) { m_list.append(m_device->thingClass()->eventTypes()->get(i)->id()); } } } endResetModel(); emit countChanged(); }