add: save and load file

This commit is contained in:
liuyihui 2022-06-02 14:57:51 +08:00
parent 06b5c82686
commit 88415085c5
6 changed files with 116 additions and 9 deletions

View File

@ -21,11 +21,28 @@
### 6.1
* 光标显示
* 量程
* 对数量程
* 对数坐标轴
### 6.2
* 曲线平滑
## Features
- [x] 菜单栏、工具栏
- [x] 信息显示与结束判断
- [x] 全谱与局部谱
- [x] 光标与ROI选取
- [x] 量程
- [x] 对数坐标轴
- [x] 读取与保存
- [x] 模拟信号
- [ ] 高斯拟合寻峰
- [x] 曲线平滑
## Bugs
1. 点击其他视图时工具栏ToolBar和菜单栏Menu无法使用失去焦点
2. 最开始无法通过方向键左右移动或调整量程
- [ ] 点击其他视图时工具栏ToolBar和菜单栏Menu无法使用失去焦点
- [ ] 最开始无法通过方向键左右移动或调整量程
- [ ] 停止后ControlView无法更新
- [ ] 停止后平滑曲线无法更新
- [x] 保存文件乱码
- [x] 退出询问保存
- [ ] 读取文件后刷新视图无效

View File

@ -3,6 +3,9 @@
#include <math.h>
#include <chrono>
#include <fstream>
#include <sstream>
#include <iostream>
#include "pch.h"
@ -57,6 +60,8 @@ BEGIN_MESSAGE_MAP(CControlView, CFormView)
ON_WM_SIZE()
ON_WM_TIMER()
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)
@ -134,6 +139,12 @@ void CControlView::UpdateValue()
UpdateData(FALSE);
}
void CControlView::SaveFirst()
{
int res = MessageBox(TEXT("打开前是否保存?"), TEXT("保存文件"), MB_YESNO | MB_ICONQUESTION);
if (res == IDYES) OnFileSave();
}
// CControlView 消息处理程序
void CControlView::OnSize(UINT nType, int cx, int cy)
{
@ -173,6 +184,8 @@ void CControlView::OnTimer(UINT_PTR nIDEvent)
{
if (pDoc == NULL) pDoc = (CMCADoc*)GetDocument();
pDoc->SetModifiedFlag(TRUE);
// 生成随机信号
int n = rand() % 50 + 25;
pDoc->RandomPeak(m_sSource, n);
@ -261,6 +274,63 @@ void CControlView::OnUpdateStaStart(CCmdUI* pCmdUI)
pCmdUI->Enable(m_bStartFlag);
}
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()
{
// 设置状态
@ -401,6 +471,7 @@ void CControlView::OnDataOrigin()
m_bOriginFlag = FALSE;
m_bTSmoothFlag = TRUE;
m_bFSmoothFlag = TRUE;
pDoc->UpdateAllViews(NULL);
}
void CControlView::OnUpdateDataOrigin(CCmdUI* pCmdUI)
@ -414,6 +485,7 @@ void CControlView::OnData3()
m_bOriginFlag = TRUE;
m_bTSmoothFlag = FALSE;
m_bFSmoothFlag = TRUE;
pDoc->UpdateAllViews(NULL);
}
void CControlView::OnUpdateData3(CCmdUI* pCmdUI)
@ -427,9 +499,10 @@ void CControlView::OnData5()
m_bOriginFlag = TRUE;
m_bTSmoothFlag = TRUE;
m_bFSmoothFlag = FALSE;
pDoc->UpdateAllViews(NULL);
}
void CControlView::OnUpdateData5(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bFSmoothFlag);
}
}

View File

@ -1,6 +1,9 @@
#pragma once
#include <chrono>
#include <fstream>
#include <sstream>
#include <iostream>
#include "MCADoc.h"
@ -34,6 +37,7 @@ public:
int roundupPowerof2(int val);
double GetMilliTime();
void UpdateValue();
void SaveFirst();
// 消息处理
afx_msg void OnSize(UINT nType, int cx, int cy);
@ -71,6 +75,8 @@ public:
// 菜单与工具栏响应
CMenu m_Menu;
CToolBar m_wndToolBar;
afx_msg void OnFileOpen();
afx_msg void OnFileSave();
afx_msg void OnStaStart();
afx_msg void OnStaStop();
afx_msg void OnStaClear();

View File

@ -21,11 +21,6 @@
BEGIN_MESSAGE_MAP(CMCAApp, CWinApp)
ON_COMMAND(ID_APP_ABOUT, &CMCAApp::OnAppAbout)
// 基于文件的标准文档命令
ON_COMMAND(ID_FILE_NEW, &CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, &CWinApp::OnFileOpen)
// 标准打印设置命令
ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

View File

@ -21,6 +21,7 @@ IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_CLOSE()
END_MESSAGE_MAP()
static UINT indicators[] =
@ -73,6 +74,16 @@ int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
return 0;
}
void CMainFrame::OnClose()
{
pDoc = (CMCADoc*)GetActiveDocument();
if (pDoc->IsModified()) {
int res = MessageBox(TEXT("退出前是否保存?"), TEXT("保存文件"), MB_YESNO | MB_ICONQUESTION);
if (res == IDYES) ((CControlView*)pDoc->GetView(RUNTIME_CLASS(CControlView)))->OnFileSave();
}
CFrameWnd::OnClose();
}
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
CCreateContext* pContext)
{

View File

@ -4,6 +4,8 @@
#pragma once
#include "MCADoc.h"
class CMainFrame : public CFrameWnd
{
@ -43,6 +45,9 @@ protected:
DECLARE_MESSAGE_MAP()
public:
CMCADoc* pDoc = nullptr;
afx_msg void OnClose();
void OnLoadToolBarIcon();
CImageList m_imgList;
};