/** * @RDE_HEADER@ * NotificationManagement - NomNotification Overview Dialog Model * * Created by Mike Lennart Wille, ESZS, 2026-06-17 */ #include "nom_dialog_model.h" #include #include #include #include #include #include #include // #include namespace NotificationManagement { NotificationManagementDialogModel::NotificationManagementDialogModel(QObject *parent) : LtsWidgets::LtsAbstractTableModel( { tr("Id"), tr("Title"), tr("Timestamp"), tr("ProcessedBy"), tr("ProcessTime"), tr("Priority"), tr("Sender"), // Address }, parent) , m_dtc(nullptr) , m_ftc(nullptr) , m_logger(getLogger("NotificationManagementDialogModel")) { } bool NotificationManagementDialogModel::initModel(const LtsSession::LtsSessionDataDds *session) { session->getDtc(m_dtc); session->getFtc(m_ftc); session->getDtc(m_dtcOpm); session->getDtc(m_dtcAsm); m_exerciseId = session->exerciseId(); m_workstationId = LtsSessionData::LtsSessionData::getInstance()->getWorkstationId(); m_operatorId = LtsSessionData::LtsSessionData::getInstance()->getOperatorId(); connect(m_dtc.get(), &NotificationManagement::Interface::Dtc::nomNotificationChanged, this, &NotificationManagementDialogModel::onDataChanged); m_dtc->nomNotifications( [this](const NotificationManagement::NomNotification ¬i) { if (!isFiltered(noti, DataModification::eAdded)) insertRow(noti); }); INFO_LOG(m_logger) << "initModel - #notis: " << m_vecData.size(); return true; } int NotificationManagementDialogModel::getId(const NotificationManagement::NomNotification &data) const { return data.id(); } QVariant NotificationManagementDialogModel::data(const QModelIndex &index, int role) const { if ((role != Qt::DisplayRole && role != Qt::UserRole && role != Qt::TextColorRole && role != Qt::FontRole) || !indexIsValid(index)) return QVariant(); const NotificationManagement::NomNotification ¬i = m_vecData.at(index.row()); if (role == Qt::FontRole) { bool setBold = false; for (const auto &addr : noti.recipientIds()) { if (addr.addressType() == AddressType_def::Operator && addr.addressId() != m_operatorId) continue; if (addr.addressType() == AddressType_def::Workstation && addr.addressId() != m_workstationId) continue; if (!noti.processed()) setBold = true; break; } if (setBold) { QFont font; font.setBold(true); return font; } } if (role == Qt::DisplayRole) { switch (index.column()) { case NotificationManagementDialogModel::Columns::Id: return noti.id(); case NotificationManagementDialogModel::Columns::Title: return QCoreApplication::translate("Notification", noti.title().c_str()); case NotificationManagementDialogModel::Columns::Content: { QString text = QCoreApplication::translate("Notification", noti.content().c_str()); for (const std::string& arg : noti.contentArgs()) { // .arg() ersetzt immer den kleinsten freien Platzhalter (z. B. zuerst %1, dann %2) text = text.arg(QString::fromStdString(arg)); } return text; } case NotificationManagementDialogModel::Columns::Timestamp: return QDateTime::fromMSecsSinceEpoch(noti.timeStamp()); case NotificationManagementDialogModel::Columns::Priority: return NotificationManagement::toString(noti.priority().underlying()); case NotificationManagementDialogModel::Columns::ProcessedBy: return getNameByAddressId(noti.processed(), noti.recipientIds()); case NotificationManagementDialogModel::Columns::ProcessTime: return QDateTime::fromMSecsSinceEpoch(noti.processTime()); case NotificationManagementDialogModel::Columns::Sender: return QString::fromStdString(noti.sender().name()); } } else if (role == Qt::TextColorRole) { if (index.column() == NotificationManagementDialogModel::Columns::Priority) { switch (noti.priority().underlying()) { case NotificationManagement::Priority_def::Low: return QColor(Qt::black); case NotificationManagement::Priority_def::Normal: return QColor(Qt::cyan); case NotificationManagement::Priority_def::High: return QColor(Qt::magenta); case NotificationManagement::Priority::Highest: return QColor(Qt::red); } } } return QVariant(); } bool NotificationManagementDialogModel::isFiltered(const NotificationManagement::NomNotification &data, DataModification modType) const { if (modType == DataModification::eRemoved) return false; if (data.exerciseId() != m_exerciseId && data.exerciseId() != 0) return true; // Check Operator & workstation bool isFiltered = true; for (const auto &addr : data.recipientIds()) { if (addr.addressType() == AddressType_def::Workstation) if (addr.addressId() != m_workstationId) continue; if (addr.addressType() == AddressType_def::Operator) if (addr.addressId() != m_operatorId) continue; isFiltered = false; break; } return isFiltered; } int NotificationManagementDialogModel::isDeleted(const NomNotification &data) const { for (const auto &addr : data.recipientIds()) { if (addr.addressType() == AddressType_def::Operator) { if (addr.addressId() != m_operatorId) continue; if (addr.isDeleted()) return true; } } return false; } QString NotificationManagementDialogModel::getNameByAddressId(int addressId, const std::vector &recipients) const { for (const auto &addr : recipients) { if (addr.id() == addressId) { if (addr.addressType() == AddressType_def::Operator) { DEBUG_LOG(m_logger) << "addressId=" << addressId << ", addr.addressId()).name()=" << QString::fromStdString(m_dtcOpm->user(addr.addressId()).name()); return QString::fromStdString(m_dtcOpm->user(addr.addressId()).name()); } if (addr.addressType() == AddressType_def::Workstation) return QString::fromStdString(m_dtcAsm->asset(addr.addressId()).name()); } } return {}; } // // NotificationDialogProxyModel // void NotificationDialogProxyModel::setSourceModel(QAbstractItemModel *sourceModel) { m_sourceModel = qobject_cast(sourceModel); LtsWidgets::LtsColumnFilterProxyModel::setSourceModel(sourceModel); } bool NotificationDialogProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const { if (!m_sourceModel || source_left.column() != source_right.column() || !source_left.isValid() || !source_right.isValid()) return LtsWidgets::LtsColumnFilterProxyModel::lessThan(source_left, source_right); const auto &dataLeft = m_sourceModel->m_vecData.at(source_left.row()); const auto &dataRight = m_sourceModel->m_vecData.at(source_right.row()); switch (source_left.column()) { case NotificationManagementDialogModel::Timestamp: return dataLeft.timeStamp() < dataRight.timeStamp(); case NotificationManagementDialogModel::Content: case NotificationManagementDialogModel::isRead: case NotificationManagementDialogModel::Priority: case NotificationManagementDialogModel::ProcessTime: case NotificationManagementDialogModel::ProcessedBy: case NotificationManagementDialogModel::Sender: case NotificationManagementDialogModel::Title: break; } return LtsWidgets::LtsColumnFilterProxyModel::lessThan(source_left, source_right); } } // namespace NotificationManagement