nymea-app/libnymea-app/types/ruleactionparams.cpp

221 lines
6.5 KiB
C++

// 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 <https://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "ruleactionparams.h"
#include "ruleactionparam.h"
RuleActionParams::RuleActionParams(QObject *parent) : QAbstractListModel(parent)
{
}
int RuleActionParams::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return static_cast<int>(m_list.count());
}
QVariant RuleActionParams::data(const QModelIndex &index, int role) const
{
switch (role) {
case RoleParamTypeId:
return m_list.at(index.row())->paramTypeId();
case RoleValue:
return m_list.at(index.row())->value();
case RoleEventTypeId:
return m_list.at(index.row())->eventTypeId();
case RoleEventParamTypeId:
return m_list.at(index.row())->eventParamTypeId();
}
return QVariant();
}
QHash<int, QByteArray> RuleActionParams::roleNames() const
{
QHash<int, QByteArray> roles;
roles.insert(RoleParamTypeId, "paramTypeId");
roles.insert(RoleValue, "value");
roles.insert(RoleEventTypeId, "eventTypeId");
roles.insert(RoleEventParamTypeId, "eventParamTypeId");
return roles;
}
void RuleActionParams::addRuleActionParam(RuleActionParam *ruleActionParam)
{
ruleActionParam->setParent(this);
beginInsertRows(QModelIndex(), static_cast<int>(m_list.count()), static_cast<int>(m_list.count()));
m_list.append(ruleActionParam);
endInsertRows();
emit countChanged();
}
void RuleActionParams::setRuleActionParam(const QUuid &paramTypeId, const QVariant &value)
{
foreach (RuleActionParam *rap, m_list) {
if (rap->paramTypeId() == paramTypeId) {
rap->setValue(value);
return;
}
}
// Still here? Need to add it
RuleActionParam *rap = new RuleActionParam(this);
rap->setParamTypeId(paramTypeId);
rap->setValue(value);
addRuleActionParam(rap);
}
void RuleActionParams::setRuleActionParamByName(const QString &paramName, const QVariant &value)
{
foreach (RuleActionParam *rap, m_list) {
if (rap->paramName() == paramName) {
rap->setValue(value);
return;
}
}
// Still here? Need to add it
RuleActionParam *rap = new RuleActionParam(this);
rap->setParamName(paramName);
rap->setValue(value);
addRuleActionParam(rap);
}
void RuleActionParams::setRuleActionParamEvent(const QUuid &paramTypeId, const QString &eventTypeId, const QString &eventParamTypeId)
{
foreach (RuleActionParam *rap, m_list) {
if (rap->paramTypeId() == paramTypeId) {
rap->setEventTypeId(eventTypeId);
rap->setEventParamTypeId(eventParamTypeId);
return;
}
}
RuleActionParam *rap = new RuleActionParam(this);
rap->setParamTypeId(paramTypeId);
rap->setEventTypeId(eventTypeId);
rap->setEventParamTypeId(eventParamTypeId);
addRuleActionParam(rap);
}
void RuleActionParams::setRuleActionParamEventByName(const QString &paramName, const QString &eventTypeId, const QString &eventParamTypeId)
{
foreach (RuleActionParam *rap, m_list) {
if (rap->paramName() == paramName) {
rap->setEventTypeId(eventTypeId);
rap->setEventParamTypeId(eventParamTypeId);
return;
}
}
RuleActionParam *rap = new RuleActionParam(this);
rap->setParamName(paramName);
rap->setEventTypeId(eventTypeId);
rap->setEventParamTypeId(eventParamTypeId);
addRuleActionParam(rap);
}
void RuleActionParams::setRuleActionParamState(const QUuid &paramTypeId, const QString &stateThingId, const QString &stateTypeId)
{
foreach (RuleActionParam *rap, m_list) {
if (rap->paramTypeId() == paramTypeId) {
rap->setStateThingId(stateThingId);
rap->setStateTypeId(stateTypeId);
return;
}
}
RuleActionParam *rap = new RuleActionParam(this);
rap->setParamTypeId(paramTypeId);
rap->setStateThingId(stateThingId);
rap->setStateTypeId(stateTypeId);
addRuleActionParam(rap);
}
void RuleActionParams::setRuleActionParamStateByName(const QString &paramName, const QString &stateThingId, const QString &stateTypeId)
{
foreach (RuleActionParam *rap, m_list) {
if (rap->paramName() == paramName) {
rap->setStateThingId(stateThingId);
rap->setStateTypeId(stateTypeId);
return;
}
}
RuleActionParam *rap = new RuleActionParam(this);
rap->setParamName(paramName);
rap->setStateThingId(stateThingId);
rap->setStateTypeId(stateTypeId);
addRuleActionParam(rap);
}
RuleActionParam *RuleActionParams::get(int index) const
{
if (index < 0 || index >= m_list.count()) {
return nullptr;
}
return m_list.at(index);
}
RuleActionParam *RuleActionParams::getParam(const QUuid &paramTypeId)
{
for (int i = 0; i < m_list.count(); i++) {
if (m_list.at(i)->paramTypeId() == paramTypeId) {
return m_list.at(i);
}
}
return nullptr;
}
bool RuleActionParams::hasRuleActionParam(const QUuid &paramTypeId) const
{
for (int i = 0; i < m_list.count(); i++) {
if (m_list.at(i)->paramTypeId() == paramTypeId) {
return true;
}
}
return false;
}
void RuleActionParams::clear()
{
beginResetModel();
foreach (RuleActionParam *param, m_list)
param->deleteLater();
m_list.clear();
endResetModel();
emit countChanged();
}
bool RuleActionParams::operator==(RuleActionParams *other) const
{
if (rowCount() != other->rowCount()) {
return false;
}
for (int i = 0; i < rowCount(); i++) {
if (!get(i)->operator==(other->get(i))) {
return false;
}
}
return true;
}