// 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 "paramtypes.h"
ParamTypes::ParamTypes(QObject *parent) :
QAbstractListModel(parent)
{
}
QList ParamTypes::paramTypes()
{
return m_paramTypes;
}
ParamType *ParamTypes::get(int index) const
{
if (index >= 0 && index < m_paramTypes.count()) {
return m_paramTypes.at(index);
}
return nullptr;
}
ParamType *ParamTypes::getParamType(const QUuid &id) const
{
foreach (ParamType *paramType, m_paramTypes) {
if (paramType->id() == id) {
return paramType;
}
}
return nullptr;
}
ParamType *ParamTypes::findByName(const QString &name) const
{
foreach (ParamType *paramType, m_paramTypes) {
if (paramType->name() == name) {
return paramType;
}
}
return nullptr;
}
int ParamTypes::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return static_cast(m_paramTypes.count());
}
QVariant ParamTypes::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_paramTypes.count())
return QVariant();
ParamType *paramType = m_paramTypes.at(index.row());
if (role == NameRole) {
return paramType->name();
} else if (role == DisplayNameRole) {
return paramType->displayName();
} else if (role == IdRole) {
return paramType->id();
} else if (role == TypeRole) {
return paramType->type();
} else if (role == DefaultValueRole) {
return paramType->defaultValue();
} else if (role == MinValueRole) {
return paramType->minValue();
} else if (role == MaxValueRole) {
return paramType->maxValue();
} else if (role == InputTypeRole) {
return paramType->inputType();
} else if (role == AllowedValuesRole) {
return paramType->allowedValues();
} else if (role == ReadOnlyRole) {
return paramType->readOnly();
}
return QVariant();
}
void ParamTypes::addParamType(ParamType *paramType)
{
paramType->setParent(this);
beginInsertRows(QModelIndex(), static_cast(m_paramTypes.count()), static_cast(m_paramTypes.count()));
//qDebug() << "ParamTypes: loaded paramType" << paramType->name();
m_paramTypes.append(paramType);
endInsertRows();
emit countChanged();
}
void ParamTypes::clearModel()
{
beginResetModel();
m_paramTypes.clear();
endResetModel();
emit countChanged();
}
QHash ParamTypes::roleNames() const
{
QHash roles;
roles[IdRole] = "id";
roles[NameRole] = "name";
roles[DisplayNameRole] = "displayName";
roles[TypeRole] = "type";
roles[MinValueRole] = "minValue";
roles[MaxValueRole] = "maxValue";
roles[InputTypeRole] = "inputType";
roles[AllowedValuesRole] = "allowedValues";
roles[ReadOnlyRole] = "readOnly";
return roles;
}