2025-03-17 16:41:49 +08:00

211 lines
7.0 KiB
C++

#include <QFile>
#include <QPainter>
#include <QDomDocument>
#include "CAlarmDelegate.h"
#include "CAlarmMsgInfo.h"
#include "CAlarmItemModel.h"
#include "pub_utility_api/FileUtil.h"
#include <QApplication>
#include <QMouseEvent>
#include "pub_logger_api/logger.h"
CAlarmDelegate::CAlarmDelegate(CAlarmItemModel * model, QObject *parent)
: QStyledItemDelegate(parent),
m_pModel(model),
m_isFlash(false)
{
loadColorConfig();
}
void CAlarmDelegate::loadColorConfig()
{
QString filePath = QString::fromStdString(iot_public::CFileUtil::getCurModuleDir()) + QString("../../data/model/alarm_color_define.xml");
QDomDocument document;
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly))
{
LOGERROR("loadColorConfig Failed!");
return;
}
if (!document.setContent(&file))
{
file.close();
return;
}
file.close();
bool isFlash = false;
QDomElement element = document.documentElement();
QDomNode node = element.firstChild();
while(!node.isNull())
{
QDomElement attr = node.toElement();
if(!attr.isNull())
{
if(QString("Flash") == attr.tagName())
{
isFlash = (bool)attr.attribute("alternate").toInt();
}
else if(QString("Level") == attr.tagName())
{
int priority = attr.attribute("priority").toInt();
m_backgroundMap[priority] = QColor(attr.attribute("background_color"));
m_alternatingMap[priority] = QColor(attr.attribute("alternating_color"));
m_confirmMap[priority] = QColor(attr.attribute("confirm_color"));
m_activeTextMap[priority] = QColor(attr.attribute("active_text_color"));
m_confirmTextMap[priority] = QColor(attr.attribute("confirm_text_color"));
}
else if(QString("Select") == attr.tagName())
{
m_selectBackgroundColor = QColor(attr.attribute("background_color"));
m_selectTextColor = QColor(attr.attribute("text_color"));
}
else if(QString("NoItem") == attr.tagName())
{
m_emptyBackgroundColor = QColor(attr.attribute("background_color"));
m_emptyTipColor = QColor(attr.attribute("tips_color"));
m_emptyTip = tr("当前无告警!");
}
}
node = node.nextSibling();
}
if(E_Alarm_Pop == m_pModel->getAlarmMode())
{
m_isFlash = false;
}
else if(E_Alarm_Dock == m_pModel->getAlarmMode())
{
m_isFlash = true;
}
else if(E_Alarm_Window == m_pModel->getAlarmMode())
{
m_isFlash = isFlash;
}
}
void CAlarmDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(!index.isValid())
{
return;
}
QColor fillColor;
QColor penColor;
bool select = option.state & QStyle::State_Selected;
//< Dock Empty
if(E_Alarm_Dock == m_pModel->getAlarmMode() && 0 == m_pModel->getShowAlarmCount())
{
painter->fillRect(option.rect, m_emptyBackgroundColor);
if(CAlarmItemModel::CONTENT == index.column() && parent())
{
QRect rt = option.rect;
QWidget * parentWidget = dynamic_cast<QWidget*>(parent());
if(parentWidget)
{
rt.setLeft(0);
rt.setWidth(parentWidget->width());
painter->save();
painter->setPen(m_emptyTipColor);
painter->drawText(rt, Qt::AlignHCenter | Qt::AlignVCenter, m_emptyTip);
painter->restore();
}
}
return;
}
//< Common
AlarmMsgPtr info = m_pModel->getAlarmInfo(index);
if(info != Q_NULLPTR)
{
painter->save();
if(E_ALS_ALARM == info->logic_state || E_ALS_RETURN == info->logic_state) //< 未确认
{
if(select)
{
fillColor = m_selectBackgroundColor;
penColor = m_selectTextColor;
}
else
{
if(m_isFlash && m_pModel->alternate())
{
penColor = m_alternatingMap.value(info->priorityOrder, Qt::white);
}
else
{
penColor = m_activeTextMap[info->priorityOrder];
}
fillColor = m_backgroundMap[info->priorityOrder];
}
}
else
{
if(select)
{
fillColor = m_selectBackgroundColor;
}
else
{
fillColor = m_confirmMap.value(info->priorityOrder, Qt::white);
}
penColor = m_confirmTextMap.value(info->priorityOrder, Qt::red);
}
painter->setPen(penColor);
painter->fillRect(option.rect, fillColor);
if( VIDEO_COLUMN == index.column() && info->m_needVideoAlm)
{
painter->save();
QStyleOptionButton button;
button.state |= QStyle::State_Enabled;
button.rect = option.rect;
button.rect.adjust(option.rect.width() - 40, option.rect.height()/2-10, -12, -(option.rect.height()/2-10));
button.iconSize = QSize(button.rect.width()+2,button.rect.height()+2);
button.icon = QIcon(iot_public::CFileUtil::getPathOfResFile("gui/icon/alarm/video.png").c_str());
QApplication::style()->drawControl(QStyle::CE_PushButton,&button,painter);
painter->restore();
}
painter->drawText(option.rect, m_pModel->data(index, Qt::TextAlignmentRole).toInt(), m_pModel->data(index).toString());
painter->restore();
}
}
bool CAlarmDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
if(!index.isValid())
{
return false;
}
if(index.column() != VIDEO_COLUMN)
{
return false;
}
AlarmMsgPtr info = m_pModel->getAlarmInfo(index);
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if(info &&info->m_needVideoAlm)
{
if(mouseEvent != NULL && mouseEvent->button() == Qt::LeftButton && mouseEvent->type() == QMouseEvent::MouseButtonPress)
{
QStyleOptionButton button;
button.rect = option.rect;
button.rect.adjust(option.rect.width() - 42, option.rect.height()/2-10, -10, -(option.rect.height()/2-10));
QRect rect = button.rect;
QString pointTag = info->key_id_tag;
pointTag.remove(pointTag.length()-6,6);
if(rect.contains(mouseEvent->pos()))
{
QString pointTag = info->key_id_tag;
pointTag.remove(pointTag.length()-6,6);
emit openVideo(info->domain_id,info->app_id,pointTag,info->time_stamp-VIDEO_TIME,info->time_stamp+VIDEO_TIME);
}
}
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}