Files
qt5-legatus-legacy/Lts/Plugins/AreaEffectSimulation/Gui/Base/aes_base_model.cpp
T
2026-08-13 18:33:10 +02:00

357 lines
11 KiB
C++
Executable File

/**
* @RDE_HEADER@
* AreaEffectManangement - Aes Item Overview Dialog Model Base Class to be inherited by the Area Types
* $Id$
* $HeadURL$
* $Author$
*
* Created by Malte Diers, SLSE, 2016-09-26
*/
#include "aes_base_model.h"
#include <LtsSession/lts_session_data.h>
#include <LtsSession/lts_session.h>
#include <Plugins/ParticipantManagement/ppm_data_plugin_interface.h>
#include <Plugins/ParticipantManagement/ppm_gui_combobox_plugin_interface.h>
#include <Plugins/ProjectDataManagement/pdm_data_plugin_interface.h>
#include <ParticipantManagement/ppm_translation.h>
#include <PluginInterfaces/gui_plugin_interface.h>
#include <QLocale>
namespace AreaEffectSimulation
{
//
// AesGuiBasHelpers
//
AesGuiBasHelpers::AesGuiBasHelpers()
: m_aesDtc(nullptr)
, m_pdmDtc(nullptr)
, m_ppmDtc(nullptr)
, m_logger(getLogger("AesGuiBase"))
{}
bool AesGuiBasHelpers::initModel(LtsSession::LtsSession *session)
{
if (m_aesDtc)
{
ERROR_LOG(m_logger) << "double init()!";
return false;
}
if (!session->getDtc<AreaEffectSimulation::DataInterface>(m_aesDtc))
return false;
if (!session->getDtc<ProjectDataManagement::DataInterface>(m_pdmDtc))
return false;
if (!session->getDtc<ParticipantManagement::DataInterface>(m_ppmDtc))
return false;
session->getForeignGuiInterface<ParticipantManagement::ForeignGuiComboboxInterface>(m_ppmGuiCbxIfc);
return true;
}
QVariant AesGuiBasHelpers::stringFromPartyId(const dds::core::optional<int> &id) const
{
if (!id.is_set())
return QObject::tr("---");
auto it = m_partyNames.find(id.get());
if (it != m_partyNames.end())
return it.value();
const QString name = ParticipantManagement::trPartyName(m_ppmDtc->party(id.get()).name());
m_partyNames.insert(id.get(), name);
return name;
}
QIcon AesGuiBasHelpers::iconFromPartyId(const dds::core::optional<int> &id) const
{
if (!id.is_set() || !m_ppmGuiCbxIfc)
return QIcon();
auto it = m_partyIcons.find(id.get());
if (it != m_partyIcons.end())
return it.value();
const QRgb col = QRgb(m_ppmDtc->party(id.get()).color());
const QIcon icon = m_ppmGuiCbxIfc->pixmapFromParty(col);
m_partyIcons.insert(id.get(), icon);
return icon;
}
QVariant AesGuiBasHelpers::hierarchyFromParticipantId(const dds::core::optional<int> &id) const
{
if (!id.is_set())
return QObject::tr("---");
auto it = m_participantHierarchy.find(id.get());
if (it != m_participantHierarchy.end())
return it.value();
const QString name = m_ppmDtc->hierarchyFromParticipant(id.get());
m_participantHierarchy.insert(id.get(), name);
return name;
}
QVariant AesGuiBasHelpers::chargeTypeName(int chargeTypeId, bool shortName) const
{
if (shortName)
{
auto it = m_chargeTypeShortNames.find(chargeTypeId);
if (it != m_chargeTypeShortNames.end())
return it.value();
const QString name = m_pdmDtc->trChargeTypeShortName(chargeTypeId, true);
m_chargeTypeShortNames.insert(chargeTypeId, name);
return name;
}
auto it = m_chargeTypeNames.find(chargeTypeId);
if (it != m_chargeTypeNames.end())
return it.value();
const QString name = m_pdmDtc->trChargeTypeName(chargeTypeId, true);
m_chargeTypeNames.insert(chargeTypeId, name);
return name;
}
QString AesGuiBasHelpers::simulationTypeText(bool isCentral)
{
return isCentral ? QObject::tr("central") : QObject::tr("peripheral");
}
void AesGuiBasHelpers::updateEffectStateComboboxFilterData(bool bWithPause)
{
LtsWidgets::ComboboxFilterContent cfc;
LtsWidgets::ComboboxFilterData cfd;
const auto effectStates = {
AesEffectState::EsExpired,
AesEffectState::EsCreated,
AesEffectState::EsPrepared,
AesEffectState::EsActivated,
AesEffectState::EsPaused,
AesEffectState::EsFired,
AesEffectState::EsReceived
};
for (const auto es : effectStates) {
if (!bWithPause && es == AesEffectState::EsPaused)
continue;
cfd.data = es;
cfd.text = AreaEffectSimulation::toString(es);
cfc.data.push_back(cfd);
}
cfc.sortItems = false;
m_cbxFilterEffectState = cfc.toVariant();
}
void AesGuiBasHelpers::updateComboboxFilterData(const QSet<int> &availablePartyIds,
const QSet<int> &availabelChargeIds)
{
LtsWidgets::ComboboxFilterContent cfc;
LtsWidgets::ComboboxFilterData cfd;
for (const int i : qAsConst(availabelChargeIds))
{
cfd.data = i;
cfd.text = chargeTypeName(i).toString();
cfc.data.push_back(cfd);
}
cfc.sortItems = true;
m_cbxFilterChargeType = cfc.toVariant();
cfc.data.clear();
for (const int i : qAsConst(availablePartyIds))
{
cfd.data = i;
cfd.text = ParticipantManagement::trPartyName(m_ppmDtc->party(i).name());
cfd.icon = iconFromPartyId(i);
cfc.data.push_back(cfd);
}
cfc.sortItems = false;
m_cbxFilterParty = cfc.toVariant();
}
void AreaEffectSimulation::AesGuiBasHelpers::updateSimulationTypeComboboxFilterData()
{
LtsWidgets::ComboboxFilterContent cfc;
LtsWidgets::ComboboxFilterData cfd;
cfd.text = simulationTypeText(true);
cfd.data = cfd.text;
cfc.data.push_back(cfd);
cfd.text = simulationTypeText(false);
cfd.data = cfd.text;
cfc.data.push_back(cfd);
cfc.sortItems = false;
m_cbxFilterSimulationType = cfc.toVariant();
}
void AesGuiBasHelpers::fillAvailableChargeIds(ProjectDataManagement::GearClass gc)
{
m_availableChargeIds.clear();
const QVector<ProjectDataManagement::ChargeType> availableCharges = m_pdmDtc->chargesForGearClass(gc);
for (const auto &charge : availableCharges)
m_availableChargeIds += charge.id();
// enable filter although no charges for this type are available
if (m_availableChargeIds.isEmpty())
m_availableChargeIds += 0;
}
void AesGuiBasHelpers::fillAvailableChargeIds(const QVector<ProjectDataManagement::GearClass> &gcs)
{
m_availableChargeIds.clear();
for (auto gc : qAsConst(gcs))
{
const QVector<ProjectDataManagement::ChargeType> &availableCharges = m_pdmDtc->chargesForGearClass(gc);
for (const auto &charge : qAsConst(availableCharges))
m_availableChargeIds += charge.id();
}
// enable filter although no charges for this type are available
if (m_availableChargeIds.isEmpty())
m_availableChargeIds += 0;
}
//
// AesItemSortFilterProxyModel
//
AesItemSortFilterProxyModel::AesItemSortFilterProxyModel(LtsSession::LtsSession *session, QObject *parent)
: LtsWidgets::LtsColumnFilterProxyModel(parent)
, m_session(session)
, m_showExpired(false)
, m_offset(5 * 60 * 1000) // default: 5 minutes
{
std::shared_ptr<ParticipantManagement::Interface::Dtc> ppmDtc;
if (m_session->getDtc<ParticipantManagement::DataInterface>(ppmDtc))
{
auto cb = [this](const ParticipantManagement::Party &party)
{
m_partyColors[party.id()] = QColor::fromRgb(QRgb(party.color()));
};
ppmDtc->partys(cb);
}
}
void AesItemSortFilterProxyModel::setShowExpired(bool showExpired)
{
if (m_showExpired == showExpired)
return;
m_showExpired = showExpired;
invalidateFilter();
}
void AesItemSortFilterProxyModel::setOffset(int offsetMsecs)
{
if (m_offset == offsetMsecs)
return;
m_offset = offsetMsecs;
invalidateFilter();
}
bool AesItemSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
bool bRet = checkFilter(source_row);
if (!bRet)
return false;
return LtsWidgets::LtsColumnFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
bool AesItemSortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
{
if (source_left.column() != source_right.column())
return LtsWidgets::LtsColumnFilterProxyModel::lessThan(source_left, source_right);
switch (source_left.column())
{
case AesGuiBasHelpers::BcEffectStart:
{
const auto effectStartLeft = aesEffectStart(source_left.row());
const auto effectStartRight = aesEffectStart(source_right.row());
if (effectStartLeft.is_set() && effectStartRight.is_set())
return effectStartLeft.get() < effectStartRight.get();
if (effectStartRight.is_set())
return true;
if (effectStartLeft.is_set())
return false;
return aesId(source_left.row()) < aesId(source_right.row());
}
case AesGuiBasHelpers::BcEffectDuration:
{
const auto effectDurationLeft = aesEffectDuration(source_left.row());
const auto effectDurationRight = aesEffectDuration(source_right.row());
if (effectDurationLeft == effectDurationRight)
return aesId(source_left.row()) < aesId(source_right.row());
return effectDurationLeft < effectDurationRight;
}
case AesGuiBasHelpers::BcEffectState:
{
const auto effectStateLeft = aesEffectStateId(source_left.row());
const auto effectStateRight = aesEffectStateId(source_right.row());
if (effectStateLeft == effectStateRight)
return aesId(source_left.row()) < aesId(source_right.row());
return effectStateLeft < effectStateRight;
}
case AesGuiBasHelpers::BcEffectSource:
{
const auto effectSourceLeft = aesEffectSourceId(source_left.row());
const auto effectSourceRight = aesEffectSourceId(source_right.row());
if (effectSourceLeft == effectSourceRight)
return aesId(source_left.row()) < aesId(source_right.row());
return effectSourceLeft < effectSourceRight;
}
case AesGuiBasHelpers::BcParty:
{
const auto partyIdLeft = partyId(source_left.row());
const auto partyIdRight = partyId(source_right.row());
if (partyIdLeft == partyIdRight)
return aesId(source_left.row()) < aesId(source_right.row());
return partyIdLeft < partyIdRight;
}
default:
break;
}
return LtsWidgets::LtsColumnFilterProxyModel::lessThan(source_left, source_right);
}
bool AesItemSortFilterProxyModel::checkFilter(int source_row) const
{
int32_t party = partyId(source_row);
if (party != -1 && !LtsSessionData::LtsSessionData::getInstance()->isVisibleParty(m_session->exerciseId(), party))
return false;
const auto effectStateId = aesEffectStateId(source_row);
if (effectStateId == AesEffectState_def::EsRemoved)
return false;
// If showExpired: Show all
if (showExpired())
return true;
const int id = aesId(source_row);
if (id <= 0)
return false;
// If MA is not in state expired: Show
if (effectStateId != AreaEffectSimulation::AesEffectState::EsExpired && effectStateId != AreaEffectSimulation::AesEffectState::EsRemoved)
return true;
if (!m_session)
{
// Todo: Log
return false;
}
const auto effectStart = aesEffectStart(source_row);
qint64 effectEnd = 0;
if (effectStart.is_set())
effectEnd = effectStart.get() + aesEffectDuration(source_row);
// Expired. Show only if younger than m_offset
if (effectStart.is_set() &&
effectEnd + m_offset > m_session->partitionTime())
return true;
return false;
}
} // namespace AreaEffectSimulation