Files
qt5-legatus-legacy/Lts/Plugins/InfrastructureManagement/Shared/ism_mounting_point.cpp
T
2026-08-13 18:33:10 +02:00

489 lines
15 KiB
C++
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @RDE_HEADER@
* The MountingPoint class wraps all information from InfrastructureManagement which defines a mounting point
* $Id$
* $HeadURL$
* $Author$
* Created by Hannes Baeuerle, SLSE, 2017-01-16
*/
#include "ism_mounting_point.h"
#include <QIcon>
#include <QPainter>
#include <QMap>
#include <QDir>
#include <QElapsedTimer>
#include <QDebug>
namespace InfrastructureManagement
{
MountingPoint::MountingPoint()
: MountingPoint(StructureElement(), -10)
{
m_data.id(-1);
}
MountingPoint::MountingPoint(const StructureElement &data, int floorId)
: m_data(data)
, m_associatedAssetId(-1)
, m_aimState(MountingPointState::Idle)
, m_hasAssociatedAssetDefect(false)
, m_isValid(true)
, m_floor(floorId)
{
}
QString MountingPoint::name() const
{
if (m_data.displayName().size() > 0)
return QString::fromStdString(m_data.displayName());
if (m_data.name().size() > 0)
return QString::fromStdString(m_data.name());
return QStringLiteral("MP_%1").arg(m_data.id());
}
void MountingPoint::setHasAssociatedAssetDefect(bool hasAssociatedAssetDefect)
{
m_hasAssociatedAssetDefect = hasAssociatedAssetDefect;
}
MountingPointState MountingPoint::getNextMountingPointState(MountingPointState currState)
{
if (currState == MountingPointState::Empty)
return MountingPointState::Idle;
else if( currState == MountingPointState::Idle)
return MountingPointState::Equipped;
else if (currState == MountingPointState::Equipped)
return MountingPointState::Empty;
else
return MountingPointState::Idle;
}
QImage MountingPoint::toImage(int32_t edgeLength) const
{
QImage result = getImage(*this);
if (result.isNull())
return result;
if (edgeLength == 100)
return result;
return result.scaled(edgeLength, edgeLength);
}
QString MountingPoint::fileName() const
{
return QStringLiteral("%1/mp_%2.png").arg(QDir::tempPath()).arg(m_data.id());
}
bool MountingPoint::writeFile(int32_t edgeLength) const
{
const QImage img(toImage(edgeLength));
if (img.isNull())
return false;
return img.save(fileName(), "PNG");
}
void MountingPoint::setAssociatedAssetId(int32_t id)
{
m_associatedAssetId = id;
}
void MountingPoint::insertHelper(MountingPointType_def::type type,
const QString &name,
const QString &colorCodeGray,
const QString &colorCodeGreen,
const QString &colorCodeRed,
const QString &colorCodeOrange,
const QString &defName)
{
QMap<MountingPointType_def::type, MountingPoint::MpNameImageData> &imageCont = MountingPoint::getImageTableInternal();
MountingPoint::MpNameImageData mnid;
static QString path(QStringLiteral(":/style/")); // :icons/svg/XY.svg
// used blue direct from file
{
// prepare:
QString curFilename = path + name + QStringLiteral(".svg");
if (!defName.isEmpty())
curFilename = path + defName + QStringLiteral(".svg");
QFile file(curFilename);
if (!file.open(QIODevice::ReadOnly))
{
qCritical() << " insertHelper: file could not be opened: filename= '" << curFilename << "'";
return;
}
QByteArray fileData;
fileData = file.readAll(); // read all the data into the byte array
const QString &text = QString::fromUtf8(fileData); // add to text string for easy string replace
fileData = text.toUtf8();
QImage currentImg;
if (!currentImg.loadFromData(fileData))
{
qCritical() << " insertHelper: loadFromData failed: filename= '" << curFilename << "'";
return;
}
mnid.nipBlue = MountingPoint::NameImagePair(name + MountingPoint::mpColorToPostfix(MountingPoint::MpColor::BLUE), currentImg);
}
// Gray, Red, Green, Orange:
{
// prepare:
QString curFilename = path + name + QStringLiteral("_fillcolor.svg.template");
QFile file(curFilename);
if (!file.open(QIODevice::ReadOnly))
{
qCritical() << " insertHelper: file could not be opened: filename= '" << curFilename << "'";
return;
}
QByteArray fileData;
fileData = file.readAll(); // read all the data into the byte array
const QString &textTemplate = QString::fromUtf8(fileData); // add to text string for easy string replace
QString temp;
{
temp = textTemplate;
temp.replace(QString(QStringLiteral("@@FILLCOLOR@@")), colorCodeGray);
fileData = temp.toUtf8();
QImage currentImg;
if (!currentImg.loadFromData(fileData))
{
qCritical() << " insertHelper: loadFromData failed: filename= '" << curFilename << "'";
return;
}
mnid.nipGray = MountingPoint::NameImagePair(name + MountingPoint::mpColorToPostfix(MountingPoint::MpColor::GRAY), currentImg);
}
{
temp = textTemplate;
temp.replace(QString(QStringLiteral("@@FILLCOLOR@@")), colorCodeRed);
fileData = temp.toUtf8();
QImage currentImg;
if (!currentImg.loadFromData(fileData))
{
qCritical() << " insertHelper: loadFromData failed: filename= '" << curFilename << "'";
return;
}
currentImg.save(QStringLiteral("/tmp/") + name + QStringLiteral("_red.png"));
mnid.nipRed = MountingPoint::NameImagePair(name + MountingPoint::mpColorToPostfix(MountingPoint::MpColor::RED), currentImg);
}
{
temp = textTemplate;
temp.replace(QString(QStringLiteral("@@FILLCOLOR@@")), colorCodeGreen);
fileData = temp.toUtf8();
QImage currentImg;
if (!currentImg.loadFromData(fileData))
{
qCritical() << " insertHelper: loadFromData failed: filename= '" << curFilename << "'";
return;
}
mnid.nipGreen = MountingPoint::NameImagePair(name + MountingPoint::mpColorToPostfix(MountingPoint::MpColor::GREEN), currentImg);
}
{
temp = textTemplate;
temp.replace(QString(QStringLiteral("@@FILLCOLOR@@")), colorCodeOrange);
fileData = temp.toUtf8();
QImage currentImg;
if (!currentImg.loadFromData(fileData))
{
qCritical() << " insertHelper: loadFromData failed: filename= '" << curFilename << "'";
return;
}
mnid.nipOrange = MountingPoint::NameImagePair(name + MountingPoint::mpColorToPostfix(MountingPoint::MpColor::ORANGE), currentImg);
}
}
imageCont.insert(type, mnid);
}
void MountingPoint::init()
{
// #define BENCHMARK
// fill ImageTable
if (MountingPoint::getImageTableInternal().isEmpty())
{
#ifdef BENCHMARK
QElapsedTimer elapsedTimer1;
elapsedTimer1.start();
#endif // BENCHMARK
// static QString colorCodeBlue (QStringLiteral("#0000FF")); // default from svgFile
static QString colorCodeGray (QStringLiteral("808080")); // #808080 // lead leading '#'
static QString colorCodeGreen (QStringLiteral("00FF00")); // #00FF00
static QString colorCodeRed (QStringLiteral("FF0000")); // #FF0000
static QString colorCodeOrange(QStringLiteral("FF5F1F")); // #FF5F1F
insertHelper(MountingPointType_def::BuildingSensorModule, QStringLiteral("reference_module"), colorCodeGray, colorCodeGreen, colorCodeRed, colorCodeOrange);
insertHelper(MountingPointType_def::OutdoorCamera, QStringLiteral("mobile_video"), colorCodeGray, colorCodeGreen, colorCodeRed, colorCodeOrange);
insertHelper(MountingPointType_def::IndoorCamera, QStringLiteral("fixed_video_announcement"), colorCodeGray, colorCodeGreen, colorCodeRed, colorCodeOrange);
// Attention: OutdoorIPS has same keyword/icons as IndoorIPS
// UniqueValueRenderer can handle that. So accept that identical entries
// see also MountingPointToMap::initRenderer();
insertHelper(MountingPointType_def::OutdoorIPS, QStringLiteral("ips"), colorCodeGray, colorCodeGreen, colorCodeRed, colorCodeOrange);
insertHelper(MountingPointType_def::IndoorIPS, QStringLiteral("ips"), colorCodeGray, colorCodeGreen, colorCodeRed, colorCodeOrange);
insertHelper(MountingPointType_def::MeteoDataUnit, QStringLiteral("meteorological_data"), colorCodeGray, colorCodeGreen, colorCodeRed, colorCodeOrange);
// AnnouncementUnit, BuildingNetworkSwitch are not relevant in GUEZ_MODE, but avoid crash with test-db
insertHelper(MountingPointType_def::AnnouncementUnit, QStringLiteral("announcement"), colorCodeGray, colorCodeGreen, colorCodeRed, colorCodeOrange);
insertHelper(MountingPointType_def::BuildingNetworkSwitch, QStringLiteral("presets"), colorCodeGray, colorCodeGreen, colorCodeRed, colorCodeOrange);
// egcu breaks name convention. instead of egcu.svg there is egcu_enabled.svg provided:
insertHelper(MountingPointType_def::EffectGenerationControlUnit, QStringLiteral("egcu"), colorCodeGray, colorCodeGreen, colorCodeRed, colorCodeOrange, QStringLiteral("egcu_enabled"));
// unknown_disabled.svg will generate a few msg like "QFont::setPointSizeF: Point size <= 0 (0.000000), must be greater than 0"
// Don´t know the reason
insertHelper(MountingPointType_def::Unknown, QStringLiteral("unknown"), colorCodeGray, colorCodeGreen, colorCodeRed, colorCodeOrange);
#ifdef BENCHMARK
// ~150ms at CTCAP15
qCritical() << " MountingPoint::init-BENCHMARK: took " << elapsedTimer1.elapsed() << " [ms]";
#endif // BENCHMARK
}
}
QString MountingPoint::mpColorToString(MpColor mpColor)
{
switch (mpColor) {
case MpColor::BLUE:
return QStringLiteral("BLUE");
break;
case MpColor::GRAY:
return QStringLiteral("GRAY");
break;
case MpColor::GREEN:
return QStringLiteral("GREEN");
break;
case MpColor::RED:
return QStringLiteral("RED");
break;
case MpColor::ORANGE:
return QStringLiteral("ORANGE");
break;
default:
// empty
break;
}
return QStringLiteral("MpColorUndefined");
}
QString MountingPoint::mpColorToPostfix(MpColor mpColor)
{
return QStringLiteral("_") + MountingPoint::mpColorToString(mpColor);
}
QMap<MountingPointType_def::type, MountingPoint::MpNameImageData> &MountingPoint::getImageTableInternal()
{
static QMap<MountingPointType_def::type, MpNameImageData> s_imageTable;
return s_imageTable;
}
QMap<MountingPointType_def::type, MountingPoint::MpNameImageData> &MountingPoint::getImageTable()
{
MountingPoint::init();
return getImageTableInternal();
}
bool MountingPoint::getImageAttributePlanActivity(const MountingPoint &mp,
QImage &imgOut, QString &attribNameOut)
{
const MpNameImageData *mnip = getNameImageData(mp);
// should never happen
#ifdef LTS_DEBUG
Q_ASSERT(mnip);
#endif // LTS_DEBUG
if (!mnip)
return false;
const NameImagePair *nip = nullptr;
const bool hasAsset = mp.hasAssociatedAsset();
const InfrastructureManagement::MountingPointState_def::type aimState = mp.aimState().underlying();
if ( ((aimState==InfrastructureManagement::MountingPointState_def::Empty) && !hasAsset ) ||
((aimState==InfrastructureManagement::MountingPointState_def::Equipped) && hasAsset )
)
nip = &mnip->nipGreen;
else if ((aimState==InfrastructureManagement::MountingPointState_def::Empty) && hasAsset )
nip = &mnip->nipRed;
else if ((aimState==InfrastructureManagement::MountingPointState_def::Equipped) && !hasAsset )
nip = &mnip->nipBlue;
// maybe idle...
if (!nip)
nip = &mnip->nipGray;
imgOut = nip->m_img;
attribNameOut = nip->m_name;
return true;
}
bool MountingPoint::getImageAttributePlanDefinition(const MountingPoint &mp, QImage &imgOut, QString &attribNameOut)
{
const MpNameImageData *mnip = getNameImageData(mp);
// should never happen
#ifdef LTS_DEBUG
Q_ASSERT(mnip);
#endif // LTS_DEBUG
if (!mnip)
return false;
const NameImagePair *nip = nullptr;
const InfrastructureManagement::MountingPointState_def::type aimState = mp.aimState().underlying();
switch (aimState) {
case InfrastructureManagement::MountingPointState_def::Empty:
nip = &mnip->nipRed;
break;
case InfrastructureManagement::MountingPointState_def::Idle:
nip = &mnip->nipGray;
break;
case InfrastructureManagement::MountingPointState_def::Equipped:
nip = &mnip->nipBlue;
break;
default:
// empty
break;
} // switch
imgOut = nip->m_img;
attribNameOut = nip->m_name;
return true;
}
bool MountingPoint::getImageAttribute(const MountingPoint &mp, MpColor mpColor, QImage &img, QString &attribName)
{
const MpNameImageData *mnip = getNameImageData(mp);
// should never happen
#ifdef LTS_DEBUG
Q_ASSERT(mnip);
#endif // LTS_DEBUG
if (!mnip)
return false;
const NameImagePair *nip = nullptr;
switch (mpColor) {
case MpColor::BLUE:
nip = &mnip->nipBlue;
break;
case MpColor::GRAY:
nip = &mnip->nipGray;
break;
case MpColor::GREEN:
nip = &mnip->nipGreen;
break;
case MpColor::RED:
nip = &mnip->nipRed;
break;
case MpColor::ORANGE:
nip = &mnip->nipOrange;
break;
default:
// empty
break;
} // switch
img = nip->m_img;
attribName = nip->m_name;
return true;
}
QImage MountingPoint::getImage(const MountingPoint &mp)
{
const NameImagePair *nip = getNameImagePair(mp);
// should never happen
#ifdef LTS_DEBUG
Q_ASSERT(nip);
#endif // LTS_DEBUG
if (!nip)
return {};
return nip->m_img;
}
const MountingPoint::MpNameImageData *MountingPoint::getNameImageData(const MountingPoint &mp)
{
QMap<MountingPointType_def::type, MountingPoint::MpNameImageData> &imageCont = MountingPoint::getImageTable();
const MountingPointType_def::type type = mp.type().underlying();
auto it = imageCont.find(type);
if (it == imageCont.end())
{
// should never happen
#ifdef LTS_DEBUG
qCritical() << " MountingPointType not found: type= " << type;
Q_ASSERT(nullptr);
#endif // LTS_DEBUG
return {};
}
const MpNameImageData *mnip = nullptr;
mnip = &(*it);
return mnip;
}
const MountingPoint::NameImagePair* MountingPoint::getNameImagePair(const MountingPoint &mp)
{
QMap<MountingPointType_def::type, MountingPoint::MpNameImageData> &imageCont = MountingPoint::getImageTable();
const MountingPointType_def::type type = mp.type().underlying();
auto it = imageCont.find(type);
if (it == imageCont.end())
return {};
const MountingPoint::MpNameImageData &mnid = *it;
const NameImagePair *nip = nullptr;
switch (mp.aimState().underlying())
{
case MountingPointState_def::Equipped:
nip = &mnid.nipBlue;
break;
case MountingPointState_def::Empty:
nip = &mnid.nipRed;
break;
case MountingPointState_def::Idle:
nip = &mnid.nipGray;
break;
}
// should never happen
#ifdef LTS_DEBUG
Q_ASSERT(nip);
#endif // LTS_DEBUG
return nip;
}
} // namespace InfrasturctureManagement