mrDarker
7 天以前 829fe6c6bc33d53fda9c31fd45a37e1df87befff
SourceCode/Bond/Servo/AlarmPopupDlg.cpp
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,321 @@
#include "stdafx.h"
#include "Servo.h"
#include "AlarmPopupDlg.h"
#include "afxdialogex.h"
#include "Log.h"
#include "Common.h"
#include "HorizontalLine.h"
#include "ServoDlg.h"
IMPLEMENT_DYNAMIC(CAlarmPopupDlg, CDialogEx)
CAlarmPopupDlg::CAlarmPopupDlg(CWnd* pParent /*=NULL*/)
   : CDialogEx(IDD_DIALOG_POPUP_ALARM, pParent)
   , m_hasActive(false)
{
   memset(&m_activeAlarm, 0, sizeof(m_activeAlarm));
   m_crBkgnd = RGB(112, 146, 190);
   m_hbrBkgnd = nullptr;
}
CAlarmPopupDlg::~CAlarmPopupDlg()
{
}
void CAlarmPopupDlg::DoDataExchange(CDataExchange* pDX)
{
   CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAlarmPopupDlg, CDialogEx)
   ON_BN_CLICKED(IDC_POPUP_BTN_CLOSE, &CAlarmPopupDlg::OnBnClickedClose)
   ON_BN_CLICKED(IDC_BUTTON_ALARM_OFF, &CAlarmPopupDlg::OnBnClickedAlarmOff)
   ON_BN_CLICKED(IDC_BUTTON_PREV, &CAlarmPopupDlg::OnBnClickedPrev)
   ON_BN_CLICKED(IDC_BUTTON_NEXT, &CAlarmPopupDlg::OnBnClickedNext)
   ON_WM_CTLCOLOR()
   ON_WM_DESTROY()
END_MESSAGE_MAP()
BOOL CAlarmPopupDlg::OnInitDialog()
{
   CDialogEx::OnInitDialog();
   // èƒŒæ™¯åˆ·
   if (m_hbrBkgnd != nullptr) {
      ::DeleteObject(m_hbrBkgnd);
   }
   m_hbrBkgnd = CreateSolidBrush(m_crBkgnd);
   // å­—体
   HFONT hFontDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
   LOGFONT lf;
   ::GetObject(hFontDefault, sizeof(LOGFONT), &lf);
   lf.lfHeight -= 8;
   lf.lfWeight = 700;
   m_fontTitle.CreateFontIndirect(&lf);
   ::GetObject(hFontDefault, sizeof(LOGFONT), &lf);
   lf.lfHeight -= 8;
   m_fontLevel.CreateFontIndirect(&lf);
   ::GetObject(hFontDefault, sizeof(LOGFONT), &lf);
   lf.lfHeight -= 16;
   m_fontName.CreateFontIndirect(&lf);
   ::GetObject(hFontDefault, sizeof(LOGFONT), &lf);
   lf.lfHeight -= 8;
   m_fontDescription.CreateFontIndirect(&lf);
   GetDlgItem(IDC_LABEL_TITLE)->SetFont(&m_fontTitle);
   GetDlgItem(IDC_LABEL_LEVEL)->SetFont(&m_fontLevel);
   GetDlgItem(IDC_LABEL_NAME)->SetFont(&m_fontName);
   GetDlgItem(IDC_LABEL_DESCRIPTION)->SetFont(&m_fontDescription);
   GetDlgItem(IDC_LABEL_NO_ALARM)->SetFont(&m_fontDescription);
   // å›¾æ ‡
   CString strIcon1;
   HICON hIcon;
   CStatic* pStatic;
   strIcon1.Format(_T("%s\\Res\\Alarm_o_24.ico"), theApp.m_strAppDir);
   pStatic = (CStatic*)GetDlgItem(IDC_ICON_TITLE);
   hIcon = (HICON)::LoadImage(AfxGetInstanceHandle(),
      strIcon1, IMAGE_ICON, 24, 24,
      LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
   pStatic->SetIcon(hIcon);
   strIcon1.Format(_T("%s\\Res\\Alarm_o_64.ico"), theApp.m_strAppDir);
   pStatic = (CStatic*)GetDlgItem(IDC_ICON_ALARM);
   hIcon = (HICON)::LoadImage(AfxGetInstanceHandle(),
      strIcon1, IMAGE_ICON, 64, 64,
      LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
   pStatic->SetIcon(hIcon);
   // å…³é—­æŒ‰é’®
   strIcon1.Format(_T("%s\\Res\\close_blcak_24.ico"), theApp.m_strAppDir);
   pStatic = (CStatic*)GetDlgItem(IDC_ICON_ALARM);
   hIcon = (HICON)::LoadImage(AfxGetInstanceHandle(),
      strIcon1, IMAGE_ICON, 128, 128,
      LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
   m_btnClose.SubclassDlgItem(IDC_POPUP_BTN_CLOSE, this);
   m_btnClose.SetIcon(hIcon, hIcon, 24);
   m_btnClose.SetFaceColor(m_crBkgnd);
   m_btnClose.SetBkgndColor(BS_NORMAL, BTN_ALARM_OFF_BKGND_NORMAL);
   m_btnClose.SetBkgndColor(BS_HOVER, BTN_ALARM_OFF_BKGND_NORMAL);
   m_btnClose.SetBkgndColor(BS_PRESS, BTN_ALARM_OFF_BKGND_PRESS);
   m_btnClose.SetFrameColor(m_crBkgnd);
   // è§£é™¤è­¦å‘ŠæŒ‰é’®
   m_btnAlarmOff.SubclassDlgItem(IDC_BUTTON_ALARM_OFF, this);
   m_btnAlarmOff.SetFrameColor(BS_NORMAL, BTN_ALARM_OFF_FRAME_NORMAL);
   m_btnAlarmOff.SetFrameColor(BS_HOVER, BTN_ALARM_OFF_FRAME_HOVER);
   m_btnAlarmOff.SetFrameColor(BS_PRESS, BTN_ALARM_OFF_FRAME_PRESS);
   m_btnAlarmOff.SetBkgndColor(BS_NORMAL, BTN_ALARM_OFF_BKGND_NORMAL);
   m_btnAlarmOff.SetBkgndColor(BS_HOVER, BTN_ALARM_OFF_BKGND_HOVER);
   m_btnAlarmOff.SetBkgndColor(BS_PRESS, BTN_ALARM_OFF_BKGND_PRESS);
   // é™éŸ³æŒ‰é’®
   bool bMute = false;
   m_btnSoundOff.SubclassDlgItem(IDC_BUTTON_SOUND_OFF, this);
   m_btnSoundOff.SetFrameColor(BS_NORMAL, BTN_SOUND_OFF_FRAME_NORMAL);
   m_btnSoundOff.SetFrameColor(BS_HOVER, BTN_SOUND_OFF_FRAME_HOVER);
   m_btnSoundOff.SetFrameColor(BS_PRESS, BTN_SOUND_OFF_FRAME_PRESS);
   SetButtonBackgroundColors(bMute);
   // æ¨ªçº¿1
   CHorizontalLine* pLine = CHorizontalLine::Hook(GetDlgItem(IDC_LINE1)->m_hWnd);
   pLine->SetBkgndColor(m_crBkgnd);
   pLine->SetLineColor(RGB(168, 168, 168));
   pLine = CHorizontalLine::Hook(GetDlgItem(IDC_LINE2)->m_hWnd);
   pLine->SetBkgndColor(m_crBkgnd);
   pLine->SetLineColor(RGB(168, 168, 168));
   RefreshContent();
   return TRUE;
}
void CAlarmPopupDlg::ShowNoAlarmControls(bool bShow)
{
   const int ids[] = { IDC_LABEL_NO_ALARM };
   for (int id : ids) {
      if (auto* p = GetDlgItem(id)) {
         p->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
      }
   }
}
void CAlarmPopupDlg::ShowAlarmControls(bool bShow)
{
   const int ids[] = {
      IDC_BUTTON_PREV, IDC_BUTTON_NEXT,
      IDC_LABEL_TITLE, IDC_ICON_ALARM, IDC_ICON_TITLE,
      IDC_LABEL_LEVEL, IDC_LABEL_NAME,
      IDC_LINE1, IDC_BUTTON_SOUND_OFF, IDC_BUTTON_ALARM_OFF,
      IDC_LINE2, IDC_LABEL_DESCRIPTION
   };
   for (int id : ids) {
      if (auto* p = GetDlgItem(id)) {
         p->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
      }
   }
}
void CAlarmPopupDlg::RefreshContent()
{
   m_activeAlarms = AlarmManager::getInstance().getActiveAlarms();
   m_activeIndex = 0;
   if (!m_activeAlarms.empty()) {
      m_hasActive = true;
      DisplayActiveAt(m_activeIndex);
      ShowAlarmControls(true);
      ShowNoAlarmControls(false);
   }
   else {
      m_hasActive = false;
      SetDlgItemText(IDC_LABEL_NO_ALARM, _T("当前无报警"));
      SetDlgItemText(IDC_LABEL_NAME, _T(""));
      SetDlgItemText(IDC_LABEL_LEVEL, _T(""));
      SetDlgItemText(IDC_LABEL_DESCRIPTION, _T(""));
      ShowAlarmControls(false);
      ShowNoAlarmControls(true);
   }
}
void CAlarmPopupDlg::OnBnClickedClose()
{
   ShowWindow(SW_HIDE);
}
void CAlarmPopupDlg::OnBnClickedAlarmOff()
{
   if (!m_hasActive) return;
   AlarmManager& alarmManager = AlarmManager::getInstance();
   alarmManager.clearAlarmByAttributes(
      m_activeAlarm.nId,
      m_activeAlarm.nDeviceId,
      m_activeAlarm.nUnitId,
      CToolUnits::getCurrentTimeString());
   RefreshContent();
}
void CAlarmPopupDlg::OnBnClickedPrev()
{
   if (m_activeIndex > 0) {
      --m_activeIndex;
      DisplayActiveAt(m_activeIndex);
   }
}
void CAlarmPopupDlg::OnBnClickedNext()
{
   if (m_activeIndex + 1 < static_cast<int>(m_activeAlarms.size())) {
      ++m_activeIndex;
      DisplayActiveAt(m_activeIndex);
   }
}
void CAlarmPopupDlg::DisplayActiveAt(int idx)
{
   if (idx < 0 || idx >= static_cast<int>(m_activeAlarms.size())) return;
   m_activeAlarm = m_activeAlarms[idx];
   // æ ‡è®°å·²è¯»
   if (auto* pParent = dynamic_cast<CServoDlg*>(GetParent())) {
      pParent->AckAlarm(m_activeAlarm.nId);
   }
   AlarmManager& alarmManager = AlarmManager::getInstance();
   const AlarmInfo* info = alarmManager.getAlarmInfoByID(m_activeAlarm.nId);
   CString title, level, name, desc;
   level.Format(_T("等级: %d"), m_activeAlarm.nSeverityLevel);
   if (info != nullptr && !info->strAlarmText.empty()) {
      name = CString(info->strAlarmText.c_str());
   }
   else {
      name.Format(_T("ID:%d (%s)"), m_activeAlarm.nId, CString(m_activeAlarm.strDeviceName.c_str()));
   }
   if (!m_activeAlarm.strDescription.empty()) {
      desc = CString(m_activeAlarm.strDescription.c_str());
   }
   else if (info != nullptr && !info->strDescription.empty()) {
      desc = CString(info->strDescription.c_str());
   }
   else {
      desc = _T("暂无描述");
   }
   title.Format(_T("设备:%s å•å…ƒ:%s"),
      CString(m_activeAlarm.strDeviceName.c_str()),
      CString(m_activeAlarm.strUnitName.c_str()));
   SetDlgItemText(IDC_LABEL_TITLE, title);
   SetDlgItemText(IDC_LABEL_NAME, name);
   SetDlgItemText(IDC_LABEL_LEVEL, level);
   SetDlgItemText(IDC_LABEL_DESCRIPTION, desc);
   UpdateNavButtons();
   ShowWindow(SW_SHOW);
}
void CAlarmPopupDlg::UpdateNavButtons()
{
   auto* pPrev = GetDlgItem(IDC_BUTTON_PREV);
   auto* pNext = GetDlgItem(IDC_BUTTON_NEXT);
   if (pPrev) pPrev->EnableWindow(m_activeIndex > 0);
   if (pNext) pNext->EnableWindow(m_activeIndex + 1 < static_cast<int>(m_activeAlarms.size()));
}
HBRUSH CAlarmPopupDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
   HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
   if (nCtlColor == CTLCOLOR_STATIC) {
      pDC->SetBkColor(m_crBkgnd);
      pDC->SetTextColor(RGB(30, 30, 30));
      hbr = m_hbrBkgnd;
   }
   else if (nCtlColor == CTLCOLOR_DLG) {
      hbr = m_hbrBkgnd;
   }
   return hbr;
}
void CAlarmPopupDlg::OnDestroy()
{
   CDialogEx::OnDestroy();
   if (m_hbrBkgnd != nullptr) {
      ::DeleteObject(m_hbrBkgnd);
      m_hbrBkgnd = nullptr;
   }
}
void CAlarmPopupDlg::SetButtonBackgroundColors(bool bMute)
{
   if (!bMute) {
      m_btnSoundOff.SetBkgndColor(BS_NORMAL, BTN_SOUND_OFF_BKGND_NORMAL);
      m_btnSoundOff.SetBkgndColor(BS_HOVER, BTN_SOUND_OFF_BKGND_HOVER);
      m_btnSoundOff.SetBkgndColor(BS_PRESS, BTN_SOUND_OFF_BKGND_PRESS);
      m_btnSoundOff.Invalidate();
   }
   else {
      m_btnSoundOff.SetBkgndColor(BS_NORMAL, BTN_SOUND_ON_BKGND_NORMAL);
      m_btnSoundOff.SetBkgndColor(BS_HOVER, BTN_SOUND_ON_BKGND_HOVER);
      m_btnSoundOff.SetBkgndColor(BS_PRESS, BTN_SOUND_ON_BKGND_PRESS);
      m_btnSoundOff.Invalidate();
   }
}