274 lines
8.6 KiB
C++
Executable File
274 lines
8.6 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* HMI Plugin for OperatorPartyAssignment
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by Steve Kemayou, ESZS, 2025-07-22
|
|
*/
|
|
|
|
#include "operator_party_assignment.h"
|
|
#include "ui_operator_party_assignment.h"
|
|
#include <Plugins/ParticipantManagement/ppm_data_plugin_interface.h>
|
|
#include <Plugins/OperatorManagement/opm_data_plugin_interface.h>
|
|
#include <LtsSession/lts_session_data.h>
|
|
#include <QHeaderView>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonArray>
|
|
|
|
QString s_partyOpAssignStr = QStringLiteral("PartyIdsAssignedOperator");
|
|
namespace ParticipantManagement
|
|
{
|
|
OperatorPartyAssignment::OperatorPartyAssignment(const QString &menuGroup, QWidget *parent)
|
|
: LtsWidgets::LtsDialog(parent, LtsWidgets::ButtonCombination::OkApplyCancel)
|
|
, m_menuGroup(menuGroup)
|
|
, m_ppmDtc(nullptr)
|
|
, m_ppmFtc(nullptr)
|
|
, m_opmDtc(nullptr)
|
|
, m_requestHelper(new LtsUtils::RequestHelper(this))
|
|
, m_operatorId(-1)
|
|
, m_model(nullptr)
|
|
, m_logger(getLogger("OperatorPartyAssignment"))
|
|
, m_ui(new Ui::OperatorPartyAssignment)
|
|
{
|
|
m_ui->setupUi(this);
|
|
|
|
connect(m_ui->dataView, &LtsDataView::LtsDataView::clicked,
|
|
this, &OperatorPartyAssignment::enaDisa);
|
|
}
|
|
|
|
OperatorPartyAssignment::~OperatorPartyAssignment()
|
|
{
|
|
delete m_ui;
|
|
}
|
|
|
|
void OperatorPartyAssignment::retranslate()
|
|
{
|
|
translate(m_ui, tr("Operator Party Assignment"));
|
|
}
|
|
|
|
bool OperatorPartyAssignment::init(LtsSession::LtsSession *session)
|
|
{
|
|
m_session = session;
|
|
|
|
m_buttonBox->getButton(LtsWidgets::DialogButton::Ok)->setVisible(false);
|
|
if (!m_session->getDtc<ParticipantManagement::DataInterface>(m_ppmDtc))
|
|
{
|
|
ERROR_LOG(m_logger)<<" -- Couldn't init ppmDtc";
|
|
return false;
|
|
}
|
|
if (!m_session->getDtc<OperatorManagement::DataInterface>(m_opmDtc))
|
|
{
|
|
ERROR_LOG(m_logger)<<" -- Couldn't init opmDtc";
|
|
return false;
|
|
}
|
|
if (!m_session->getFtc<ParticipantManagement::DataInterface>(m_ppmFtc))
|
|
{
|
|
ERROR_LOG(m_logger)<<" -- Couldn't init ppmFtc";
|
|
return false;
|
|
}
|
|
|
|
setUnchangeable(true);
|
|
|
|
m_operatorId= LtsSessionData::LtsSessionData::getInstance()->getOperatorId();
|
|
m_model = new OPeratorPartyAssignmentModel(this);
|
|
m_model->setPartyCheckStatesData(getPartyStatesForOpertator());
|
|
if (!m_model->initModel(m_ppmDtc))
|
|
return false;
|
|
|
|
m_ui->dataView->setModel(m_model);
|
|
m_ui->dataView->hideColumn(OPeratorPartyAssignmentModel::Columns::Id);
|
|
|
|
|
|
connect(m_opmDtc.get(), &OperatorManagement::Interface::Dtc::userChanged, this, &OperatorPartyAssignment::onOperatorChanged);
|
|
auto cbOperator = [this](const OperatorManagement::User & u){
|
|
onOperatorChanged(u, DataModification::eAdded);
|
|
};
|
|
|
|
m_opmDtc->users(cbOperator);
|
|
connect(m_ui->operatorComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
|
this, &OperatorPartyAssignment::onSelectionOperatorChanged);
|
|
|
|
if (m_ui->operatorComboBox->currentIndex() != m_ui->operatorComboBox->findData(m_operatorId))
|
|
m_ui->operatorComboBox->setCurrentIndex(m_ui->operatorComboBox->findData(m_operatorId));
|
|
else
|
|
onSelectionOperatorChanged(0); //param is unused
|
|
|
|
setUnchangeable(false);
|
|
|
|
return true;
|
|
}
|
|
|
|
QMap<int, bool> OperatorPartyAssignment::getPartyStatesForOpertator() const
|
|
{
|
|
// get Party List and properties
|
|
// iterarate over partyId in the properties, find the operatorId
|
|
// if no operatorId found checked=false else true;
|
|
DEBUG_LOG(m_logger)<<" -- [getPartyStatesForOpertator] -> ";
|
|
const auto &partys = m_ppmDtc->partys();
|
|
QMap<int,bool> mapPartyCheckStates;
|
|
for (auto p : partys)
|
|
{
|
|
const QString partyId = QString::number(p.id());
|
|
QJsonObject mainObj = QJsonDocument::fromJson(QByteArray::fromStdString(p.properties())).object();
|
|
QJsonObject subObj = mainObj.value(s_partyOpAssignStr).toObject();
|
|
QJsonArray opIdJsArry = subObj.value(QString::number(p.id())).toArray();
|
|
|
|
if (opIdJsArry.contains(QJsonValue::fromVariant(m_operatorId)))
|
|
mapPartyCheckStates.insert(p.id(), true);
|
|
else
|
|
mapPartyCheckStates.insert(p.id(), false);
|
|
|
|
}
|
|
|
|
return mapPartyCheckStates;
|
|
}
|
|
|
|
void OperatorPartyAssignment::onOperatorChanged(const OperatorManagement::User &data, DataModification modType)
|
|
{
|
|
switch (modType)
|
|
{
|
|
case DataModification::eRemoved:
|
|
{
|
|
Q_ASSERT(m_operatorMap.contains(data.id()));
|
|
|
|
// Check, if Operator is currently open, if yes, reset any changes
|
|
if (m_ui->operatorComboBox->currentData().toInt() == data.id())
|
|
{
|
|
__unchange__();
|
|
}
|
|
|
|
m_operatorMap.remove(data.id());
|
|
m_ui->operatorComboBox->removeItem(m_ui->operatorComboBox->findData(data.id()));
|
|
break;
|
|
}
|
|
|
|
case DataModification::eAdded:
|
|
{
|
|
Q_ASSERT(!m_operatorMap.contains(data.id()));
|
|
const QString operatorName = QString::fromStdString(data.name());
|
|
m_operatorMap.insert(data.id(), operatorName);
|
|
m_ui->operatorComboBox->addItem(operatorName, data.id());
|
|
m_ui->operatorComboBox->model()->sort(0);
|
|
break;
|
|
}
|
|
|
|
case DataModification::eModified:
|
|
{
|
|
Q_ASSERT(m_operatorMap.contains(data.id()));
|
|
const QString operatorName = QString::fromStdString(data.name());
|
|
m_operatorMap.insert(data.id(), operatorName);
|
|
int i = m_ui->operatorComboBox->findData(data.id());
|
|
Q_ASSERT(i != -1);
|
|
m_ui->operatorComboBox->setItemText(i, operatorName);
|
|
m_ui->operatorComboBox->model()->sort(0);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void OperatorPartyAssignment::onSelectionOperatorChanged(const int index)
|
|
{
|
|
Q_UNUSED(index);
|
|
if (hasUnsavedChanges() /*|| m_model->getChangeState() */)
|
|
{
|
|
switch (askApplyDiscardCancel())
|
|
{
|
|
case LtsWidgets::Button::Apply:
|
|
if (!apply())
|
|
return;
|
|
break;
|
|
case LtsWidgets::Button::Cancel:
|
|
{
|
|
// cfroebel 2019-06-26:
|
|
// Setting back the operator would cause this slot to be called again.
|
|
// Thus we avoid this by blocking signals.
|
|
{
|
|
QSignalBlocker blockSignals(m_ui->operatorComboBox);
|
|
m_ui->operatorComboBox->setCurrentIndex(m_ui->operatorComboBox->findData(m_operatorId)); // reset position
|
|
}
|
|
return;
|
|
}
|
|
case LtsWidgets::Button::Discard:
|
|
__unchange__(); // can't call resetChanges() by myself :(
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
m_operatorId = m_ui->operatorComboBox->currentData().toInt();
|
|
m_model->updateView(getPartyStatesForOpertator());
|
|
}
|
|
|
|
|
|
bool OperatorPartyAssignment::apply()
|
|
{
|
|
const auto statesData = m_model->modelData();
|
|
|
|
const auto &partys = m_ppmDtc->partys();
|
|
for (auto party : partys)
|
|
{
|
|
const QString partyId = QString::number(party.id());
|
|
QJsonObject propertiesObj = QJsonDocument::fromJson(QByteArray::fromStdString(party.properties())).object();
|
|
QJsonObject subObj = propertiesObj.value(s_partyOpAssignStr).toObject();
|
|
QJsonArray jsArry = subObj.value(QString::number(party.id())).toArray();
|
|
|
|
const bool containsOpId = jsArry.contains(QJsonValue::fromVariant(m_operatorId));
|
|
const bool isPartyChecked = statesData.value(party.id(), false);
|
|
if (!containsOpId && !isPartyChecked)
|
|
continue;
|
|
if (containsOpId && isPartyChecked)
|
|
continue;
|
|
|
|
if (containsOpId) // this condition is for: OpId available but party is unchecked
|
|
{
|
|
//remove opId from jsObject because party is not longed checked -> remove opId from propertiesObject
|
|
for (int idx = 0; idx < jsArry.count(); ++idx)
|
|
{
|
|
if (jsArry.at(idx).toInt() == m_operatorId)
|
|
{
|
|
jsArry.removeAt(idx);
|
|
subObj.insert(QString::number(party.id()), jsArry); // if already exist item will be replaced
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
else if (isPartyChecked)
|
|
{
|
|
jsArry.append(QJsonValue::fromVariant(m_operatorId));
|
|
subObj.insert(QString::number(party.id()), jsArry); // if already exist item will be replaced
|
|
}
|
|
|
|
// update properties jsObj
|
|
propertiesObj.insert(s_partyOpAssignStr, subObj); // if already exist item will be replaced
|
|
// set request;
|
|
|
|
// request
|
|
Party req = party;
|
|
req.id(party.id());
|
|
req.name(party.name());
|
|
req.color(party.color());
|
|
req.fadingColor(party.fadingColor());
|
|
req.hlaForceIdentifier(party.hlaForceIdentifier());
|
|
req.isUsedBy(party.isUsedBy());
|
|
req.role(party.role());
|
|
QString props = QString::fromUtf8(QJsonDocument(propertiesObj).toJson());
|
|
req.properties(props.toStdString());
|
|
|
|
m_requestHelper->addRequest([this, req]
|
|
{
|
|
return m_ppmFtc->modifyPartyDataRequest(req, this);
|
|
});
|
|
}
|
|
return m_requestHelper->startRequests();
|
|
}
|
|
|
|
void OperatorPartyAssignment::enaDisa()
|
|
{
|
|
m_buttonBox->getButton(LtsWidgets::DialogButton::Apply)->setEnabled(true);
|
|
}
|
|
|
|
|
|
}// namespace ParticipantManagement
|