This repository has been archived on 2022-07-04. You can view files and clone it, but cannot push or open issues or pull requests.
Multichannel-Analyzer/MCA/ControlView.cpp

509 lines
14 KiB
C++
Raw Normal View History

2022-03-20 15:14:44 +08:00
// ControlView.cpp: 实现文件
//
#include <math.h>
#include <chrono>
2022-06-02 14:57:51 +08:00
#include <fstream>
#include <sstream>
#include <iostream>
2022-03-20 15:14:44 +08:00
#include "pch.h"
#include "MCA.h"
#include "MCADoc.h"
#include "MainFrm.h"
#include "ControlView.h"
// CControlView
IMPLEMENT_DYNCREATE(CControlView, CFormView)
CControlView::CControlView()
: CFormView(IDD_DIALOG_CONTROL)
, m_nCond(100)
, m_nT0(0)
, m_nTime(0)
, m_nTimeStr((CString)'0')
, m_nTotalCount(0)
, m_nPartCount(0)
, m_nMaxCount(0)
, m_nLC(4)
, m_nCursor1(0)
, m_nCursor2(1023)
, m_nCursorROI(0)
, m_nCursorROICount(0)
2022-03-20 15:14:44 +08:00
{
}
CControlView::~CControlView()
{
}
void CControlView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Text(pDX, IDC_COND, m_nCond);
DDX_Text(pDX, IDC_TIME, m_nTimeStr);
DDX_Text(pDX, IDC_TCOUNT, m_nTotalCount);
DDX_Text(pDX, IDC_PCOUNT, m_nPartCount);
DDX_Text(pDX, IDC_MCOUNT, m_nMaxCount);
DDX_Control(pDX, IDC_MODE, m_ComboMode);
DDX_Control(pDX, IDC_AXIS, m_ComboAxis);
DDX_Text(pDX, IDC_RANGE, m_nLC);
DDX_Text(pDX, IDC_CURSOR1, m_nCursor1);
DDX_Text(pDX, IDC_CURSOR2, m_nCursor2);
DDX_Text(pDX, IDC_CURSORROI, m_nCursorROI);
DDX_Text(pDX, IDC_CURSORROI_COUNT, m_nCursorROICount);
2022-03-20 15:14:44 +08:00
}
BEGIN_MESSAGE_MAP(CControlView, CFormView)
ON_WM_SIZE()
ON_WM_TIMER()
2022-06-02 14:57:51 +08:00
ON_COMMAND(ID_FILE_OPEN, &CControlView::OnFileOpen)
ON_COMMAND(ID_FILE_SAVE, &CControlView::OnFileSave)
ON_COMMAND(ID_STA_START, &CControlView::OnStaStart)
ON_COMMAND(ID_STA_STOP, &CControlView::OnStaStop)
ON_COMMAND(ID_STA_CLEAR, &CControlView::OnStaClear)
ON_COMMAND(ID_SIMU_CO, &CControlView::OnSimuCo)
ON_COMMAND(ID_SIMU_CS, &CControlView::OnSimuCs)
ON_COMMAND(ID_SIMU_NA, &CControlView::OnSimuNa)
ON_COMMAND(ID_AXIS_LINEAR, &CControlView::OnAxisLinear)
ON_COMMAND(ID_AXIS_LOG, &CControlView::OnAxisLog)
ON_COMMAND(ID_RANGE_AUTO, &CControlView::OnRangeAuto)
ON_COMMAND(ID_RANGE_D4, &CControlView::OnRangeD4)
ON_COMMAND(ID_RANGE_D2, &CControlView::OnRangeD2)
ON_COMMAND(ID_RANGE_M2, &CControlView::OnRangeM2)
ON_COMMAND(ID_RANGE_M4, &CControlView::OnRangeM4)
ON_COMMAND(ID_DATA_ORIGIN, &CControlView::OnDataOrigin)
ON_COMMAND(ID_DATA_3, &CControlView::OnData3)
ON_COMMAND(ID_DATA_5, &CControlView::OnData5)
2022-03-20 15:14:44 +08:00
ON_UPDATE_COMMAND_UI(ID_STA_START, &CControlView::OnUpdateStaStart)
ON_UPDATE_COMMAND_UI(ID_STA_STOP, &CControlView::OnUpdateStaStop)
ON_UPDATE_COMMAND_UI(ID_STA_CLEAR, &CControlView::OnUpdateStaClear)
ON_UPDATE_COMMAND_UI(ID_SIMU_CO, &CControlView::OnUpdateSimuCo)
ON_UPDATE_COMMAND_UI(ID_SIMU_CS, &CControlView::OnUpdateSimuCs)
ON_UPDATE_COMMAND_UI(ID_SIMU_NA, &CControlView::OnUpdateSimuNa)
ON_UPDATE_COMMAND_UI(ID_RANGE_AUTO, &CControlView::OnUpdateRangeAuto)
ON_UPDATE_COMMAND_UI(ID_DATA_ORIGIN, &CControlView::OnUpdateDataOrigin)
ON_UPDATE_COMMAND_UI(ID_DATA_3, &CControlView::OnUpdateData3)
ON_UPDATE_COMMAND_UI(ID_DATA_5, &CControlView::OnUpdateData5)
END_MESSAGE_MAP()
2022-03-20 15:14:44 +08:00
// CControlView 诊断
#ifdef _DEBUG
void CControlView::AssertValid() const
{
CFormView::AssertValid();
}
#ifndef _WIN32_WCE
void CControlView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif
#endif //_DEBUG
int CControlView::roundupPowerof2(int val)
{
if ((val & (val - 1)) == 0) return val;
int andv = 1 << 30;
while ((andv & val) == 0) andv = andv >> 1;
return andv << 1;
}
double CControlView::GetMilliTime()
{
std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
);
return ms.count() / 1000.;
}
void CControlView::UpdateValue()
{
// 更新信息
m_nTotalCount = pDoc->GetTotal(0, 1023);
m_nMaxCount = pDoc->GetMax(0, 1023);
m_nPartCount = pDoc->GetTotal(m_nCursor1, m_nCursor2);
m_nCursorROICount = pDoc->m_nChannelCount[m_nCursorROI];
m_nTime += GetMilliTime() - m_nT0;
m_nTimeStr.Format(_T("%.1f"), m_nTime);
m_nT0 = GetMilliTime();
// 更新屏幕信息
UpdateData(FALSE);
}
2022-03-20 15:14:44 +08:00
2022-06-02 14:57:51 +08:00
void CControlView::SaveFirst()
{
int res = MessageBox(TEXT("打开前是否保存?"), TEXT("保存文件"), MB_YESNO | MB_ICONQUESTION);
if (res == IDYES) OnFileSave();
}
2022-03-20 15:14:44 +08:00
// CControlView 消息处理程序
void CControlView::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
CRect rect;
GetClientRect(&rect); // 获取当前客户区view大小
CSize size;
size.cx = rect.right - rect.left;
size.cy = rect.bottom - rect.top;
SetScrollSizes(MM_HIMETRIC, size); // 将CScrollView的大小设置为当前客户区大小
}
void CControlView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
// 添加运行模式
m_ComboMode.AddString((CString)"时间");
m_ComboMode.AddString((CString)"计数");
m_ComboMode.SetCurSel(0);
// 添加坐标轴模式
m_ComboAxis.AddString((CString)"线性");
m_ComboAxis.AddString((CString)"对数");
m_ComboAxis.SetCurSel(0);
m_Menu.LoadMenuW(IDR_MAINFRAME);
m_wndToolBar.LoadToolBar(IDR_MAINFRAME);
SetMenu(&m_Menu);
}
void CControlView::OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/)
{
}
void CControlView::OnTimer(UINT_PTR nIDEvent)
{
if (pDoc == NULL) pDoc = (CMCADoc*)GetDocument();
2022-06-02 14:57:51 +08:00
pDoc->SetModifiedFlag(TRUE);
// 生成随机信号
int n = rand() % 50 + 25;
pDoc->RandomPeak(m_sSource, n);
// 平滑数据
for (int i = 0; i < 1024; i++) pDoc->m_nChannelSmooth[i] = pDoc->m_nChannelCount[i];
OutputDebugString(m_sSmoothType);
if (m_sSmoothType == "S3") {
pDoc->m_nChannelSmooth[0] = (5.0 * pDoc->m_nChannelCount[0] + 2.0 * pDoc->m_nChannelCount[1] - pDoc->m_nChannelCount[2]) / 6.0;
for (int i = 1; i < 1023; i++)
pDoc->m_nChannelSmooth[i] = (pDoc->m_nChannelCount[i - 1] + pDoc->m_nChannelCount[i] + pDoc->m_nChannelCount[i + 1]) / 3.0;
pDoc->m_nChannelSmooth[1023] = (5.0 * pDoc->m_nChannelCount[1023] + 2.0 * pDoc->m_nChannelCount[1022] - pDoc->m_nChannelCount[1021]) / 6.0;
}
else if (m_sSmoothType == "S5") {
pDoc->m_nChannelSmooth[0] = (3.0 * pDoc->m_nChannelCount[0] + 2.0 * pDoc->m_nChannelCount[1] + pDoc->m_nChannelCount[2] - pDoc->m_nChannelCount[4]) / 5.0;
pDoc->m_nChannelSmooth[1] = (4.0 * pDoc->m_nChannelCount[0] + 3.0 * pDoc->m_nChannelCount[1] + 2 * pDoc->m_nChannelCount[2] + pDoc->m_nChannelCount[3]) / 10.0;
for (int i = 2; i < 1022; i++)
pDoc->m_nChannelSmooth[i] = (pDoc->m_nChannelCount[i - 2] + pDoc->m_nChannelCount[i - 1] + pDoc->m_nChannelCount[i] + pDoc->m_nChannelCount[i + 1] + pDoc->m_nChannelCount[i + 2]) / 5.0;
pDoc->m_nChannelSmooth[1022] = (4.0 * pDoc->m_nChannelCount[1023] + 3.0 * pDoc->m_nChannelCount[1022] + 2 * pDoc->m_nChannelCount[1021] + pDoc->m_nChannelCount[1020]) / 10.0;
pDoc->m_nChannelSmooth[1023] = (3.0 * pDoc->m_nChannelCount[1023] + 2.0 * pDoc->m_nChannelCount[1022] + pDoc->m_nChannelCount[1021] - pDoc->m_nChannelCount[1019]) / 5.0;
}
for (int i = 0; i < 1024; i++) pDoc->m_nChannelSmooth[i] = max(0, pDoc->m_nChannelSmooth[i]);
// 更新信息
UpdateValue();
// 判断终止
CString tmp;
m_ComboMode.GetWindowText(tmp);
if (tmp == "时间" && m_nTime >= m_nCond) OnStaStop();
if (tmp == "计数" && m_nTotalCount >= m_nCond) OnStaStop();
// 修改坐标轴
m_ComboAxis.GetWindowText(tmp);
if (tmp == "对数") {
m_bAutoFlag = FALSE;
m_sAxisMode = "Log";
m_sRangeMode = "Auto";
}
else m_sAxisMode = "Linear";
// 判断量程
if (m_sRangeMode == "Auto" && m_nMaxCount >= m_nLC) m_nLC *= 2;
pDoc->UpdateAllViews(NULL);
CFormView::OnTimer(nIDEvent);
}
BOOL CControlView::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN && pMsg->wParam >= 0x25 && pMsg->wParam <= 0x28) {
if (pMsg->wParam == VK_LEFT) m_nCursorROI = max(m_nCursor1, m_nCursorROI - 1);
if (pMsg->wParam == VK_RIGHT) m_nCursorROI = min(m_nCursor2, m_nCursorROI + 1);
if (pMsg->wParam == VK_UP) {
m_nLC *= 2;
m_bAutoFlag = TRUE;
m_sRangeMode = (CString)"Manual";
}
if (pMsg->wParam == VK_DOWN) {
m_nLC = m_nLC > 1 ? m_nLC / 2 : 1;
m_bAutoFlag = TRUE;
m_sRangeMode = (CString)"Manual";
}
return TRUE;
}
return CFormView::PreTranslateMessage(pMsg);
}
void CControlView::OnStaStart()
{
// 重置结束条件
m_nT0 = GetMilliTime();
// 设置状态
m_bStartFlag = FALSE;
m_bStopFlag = TRUE;
m_bClearFlag = FALSE;
((CEdit*)GetDlgItem(IDC_COND))->EnableWindow(FALSE);
// 获取屏幕值至变量
UpdateData(TRUE);
// 设置更新
SetTimer(1, 100, NULL);
}
void CControlView::OnUpdateStaStart(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bStartFlag);
}
2022-06-02 14:57:51 +08:00
void CControlView::OnFileOpen()
{
if (pDoc == NULL) pDoc = (CMCADoc*)GetDocument();
if (pDoc->IsModified()) SaveFirst();
CString szFilter = (CString)"文本文件(*.txt)|*.txt|";
CFileDialog fileDlg(TRUE, (CString)"*.txt", NULL, OFN_OVERWRITEPROMPT, szFilter);
if (IDCANCEL == fileDlg.DoModal())
return;
CString szfile = fileDlg.GetPathName();
int k, cnt = 0;
int count[1024] = {0};
std::string line, tmp;
std::ifstream ifs(szfile);
try
{
std::getline(ifs, line);
m_nTime = 0;
m_nT0 = GetMilliTime() - std::stoi(line);
while (std::getline(ifs, line))
{
std::stringstream ss(line);
std::getline(ss, tmp, ',');
k = std::stoi(tmp);
std::getline(ss, tmp, ',');
count[k] = std::stoi(tmp);
cnt += 1;
}
for (int i = 1; i < 1024; i++) pDoc->m_nChannelCount[i] = count[i];
UpdateValue();
pDoc->UpdateAllViews(NULL);
}
catch (const std::exception&)
{
AfxMessageBox((CString)"文件格式错误");
}
}
void CControlView::OnFileSave()
{
CString szFilter = (CString)"文本文件(*.txt)|*.txt|";
CFileDialog fileDlg(FALSE, (CString)"*.txt", NULL, OFN_OVERWRITEPROMPT, szFilter);
if (IDCANCEL == fileDlg.DoModal())
return;
CString szfile = fileDlg.GetPathName();
std::ofstream ofs(szfile);
ofs << m_nTime << std::endl;
for (int i = 0; i < 1024; i++)
ofs << i << ", " << pDoc->m_nChannelCount[i] << std::endl;
AfxMessageBox((CString)"保存成功");
pDoc->SetModifiedFlag(FALSE);
}
void CControlView::OnStaStop()
{
// 设置状态
m_bStartFlag = TRUE;
m_bStopFlag = FALSE;
m_bClearFlag = TRUE;
((CEdit*)GetDlgItem(IDC_COND))->EnableWindow(TRUE);
// 停止更新
KillTimer(1);
}
void CControlView::OnUpdateStaStop(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bStopFlag);
}
void CControlView::OnStaClear()
{
if (pDoc == NULL) pDoc = (CMCADoc*)GetDocument();
// 重置
m_nLC = 4;
m_nTime = 0;
m_nMaxCount = 0;
m_nPartCount = 0;
m_nTotalCount = 0;
m_nTimeStr = (CString)"0.0";
// 更新屏幕信息
UpdateData(FALSE);
// 设置曲线
for (int i = 0; i < 1024; i++) pDoc->m_nChannelCount[i] = 0;
for (int i = 0; i < 1024; i++) pDoc->m_nChannelSmooth[i] = 0;
pDoc->UpdateAllViews(NULL);
}
void CControlView::OnUpdateStaClear(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bClearFlag);
}
void CControlView::OnSimuCo()
{
m_sSource = (CString)"Co60";
m_bCoFlag = FALSE;
m_bCsFlag = TRUE;
m_bNaFlag = TRUE;
OnStaClear();
OnStaStart();
}
void CControlView::OnUpdateSimuCo(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bCoFlag);
}
void CControlView::OnSimuCs()
{
m_sSource = (CString)"Cs137";
m_bCoFlag = TRUE;
m_bCsFlag = FALSE;
m_bNaFlag = TRUE;
OnStaClear();
OnStaStart();
}
void CControlView::OnUpdateSimuCs(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bCsFlag);
}
void CControlView::OnSimuNa()
{
m_sSource = (CString)"Na22";
m_bCoFlag = TRUE;
m_bCsFlag = TRUE;
m_bNaFlag = FALSE;
OnStaClear();
OnStaStart();
}
void CControlView::OnUpdateSimuNa(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bNaFlag);
}
void CControlView::OnAxisLinear()
{
m_sAxisMode = (CString)"Linear";
}
void CControlView::OnAxisLog()
{
m_sAxisMode = (CString)"Log";
}
void CControlView::OnRangeAuto()
{
m_sRangeMode = (CString)"Auto";
m_bAutoFlag = FALSE;
m_nLC = roundupPowerof2(m_nMaxCount);
}
void CControlView::OnUpdateRangeAuto(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bAutoFlag);
}
void CControlView::OnRangeD4()
{
m_sRangeMode = (CString)"Manual";
m_nLC /= 4;
m_bAutoFlag = TRUE;
}
void CControlView::OnRangeD2()
{
m_sRangeMode = (CString)"Manual";
m_nLC /= 2;
m_bAutoFlag = TRUE;
}
void CControlView::OnRangeM2()
{
m_sRangeMode = (CString)"Manual";
m_nLC *= 2;
m_bAutoFlag = TRUE;
}
void CControlView::OnRangeM4()
{
m_sRangeMode = (CString)"Manual";
m_nLC *= 4;
m_bAutoFlag = TRUE;
}
void CControlView::OnDataOrigin()
{
m_sSmoothType = (CString)"Origin";
m_bOriginFlag = FALSE;
m_bTSmoothFlag = TRUE;
m_bFSmoothFlag = TRUE;
2022-06-02 14:57:51 +08:00
pDoc->UpdateAllViews(NULL);
}
void CControlView::OnUpdateDataOrigin(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bOriginFlag);
}
void CControlView::OnData3()
{
m_sSmoothType = (CString)"S3";
m_bOriginFlag = TRUE;
m_bTSmoothFlag = FALSE;
m_bFSmoothFlag = TRUE;
2022-06-02 14:57:51 +08:00
pDoc->UpdateAllViews(NULL);
}
void CControlView::OnUpdateData3(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bTSmoothFlag);
}
void CControlView::OnData5()
{
m_sSmoothType = (CString)"S5";
m_bOriginFlag = TRUE;
m_bTSmoothFlag = TRUE;
m_bFSmoothFlag = FALSE;
2022-06-02 14:57:51 +08:00
pDoc->UpdateAllViews(NULL);
}
void CControlView::OnUpdateData5(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bFSmoothFlag);
2022-06-02 14:57:51 +08:00
}