From a3bc080f196a1b4a9caadad2529cd7f712896342 Mon Sep 17 00:00:00 2001 From: Xun Li Date: Wed, 4 May 2016 14:33:45 -0700 Subject: [PATCH] #409 save DID regression results to file --- DialogTools/RegressionReportDlg.cpp | 66 +++++++++++++++++++++++++---- DialogTools/RegressionReportDlg.h | 1 + 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/DialogTools/RegressionReportDlg.cpp b/DialogTools/RegressionReportDlg.cpp index 45b21e2e0..ac251769d 100644 --- a/DialogTools/RegressionReportDlg.cpp +++ b/DialogTools/RegressionReportDlg.cpp @@ -19,6 +19,8 @@ // For compilers that support precompilation, includes . #include +#include +#include #ifndef WX_PRECOMP #include @@ -90,14 +92,13 @@ void RegressionReportDlg::CreateControls() panel->SetSizer(vbox); - //wxBitmap edit = wxArtProvider::GetBitmap(wxART_PRINT); - //wxBitmap save = wxArtProvider::GetBitmap(wxART_FILE_SAVE); - //wxToolBar *toolbar = CreateToolBar(); - //toolbar->AddTool(wxID_EDIT, "Change Font", edit); - //toolbar->AddTool(wxID_SAVE, "Save Regression Results", save); - //toolbar->Realize(); - //Connect(wxID_EDIT, wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(RegressionReportDlg::OnFontChanged)); - //Connect(wxID_SAVE, wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(RegressionReportDlg::OnFontChanged)); + + wxBitmap save = wxArtProvider::GetBitmap(wxART_FILE_SAVE); + wxToolBar *toolbar = CreateToolBar(); + + toolbar->AddTool(wxID_SAVE, "Save Regression Results", save); + toolbar->Realize(); + Connect(wxID_SAVE, wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(RegressionReportDlg::OnSaveToFile)); Center(); } @@ -121,6 +122,55 @@ void RegressionReportDlg::OnMouseEvent(wxMouseEvent& event) event.GetPosition().x, event.GetPosition().y); } +void RegressionReportDlg::OnSaveToFile(wxCommandEvent& event) +{ + wxFileDialog dlg( this, "Regression Output Text File", wxEmptyString, + wxEmptyString, + "TXT files (*.txt)|*.txt", + wxFD_SAVE ); + if (dlg.ShowModal() != wxID_OK) return; + + wxFileName new_txt_fname(dlg.GetPath()); + wxString new_main_dir = new_txt_fname.GetPathWithSep(); + wxString new_main_name = new_txt_fname.GetName(); + wxString new_txt = new_main_dir + new_main_name + ".txt"; + + // Prompt for overwrite permission + if (wxFileExists(new_txt)) { + wxString msg; + msg << new_txt << " already exists. OK to overwrite?"; + wxMessageDialog dlg (this, msg, "Overwrite?", + wxYES_NO | wxCANCEL | wxNO_DEFAULT); + if (dlg.ShowModal() != wxID_YES) return; + } + + bool failed = false; + // Automatically overwrite existing csv since we have + // permission to overwrite. + + if (wxFileExists(new_txt) && !wxRemoveFile(new_txt)) failed = true; + + if (!failed) { + // write logReport to a text file + wxFFileOutputStream output(new_txt); + if (output.IsOk()) { + wxTextOutputStream txt_out( output ); + txt_out << m_textbox->GetValue(); + txt_out.Flush(); + output.Close(); + } else { + failed = true; + } + } + + if (failed) { + wxString msg; + msg << "Unable to overwrite " << new_txt; + wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR); + dlg.ShowModal(); + } +} + void RegressionReportDlg::OnFontChanged(wxCommandEvent& event) { wxFontData data; diff --git a/DialogTools/RegressionReportDlg.h b/DialogTools/RegressionReportDlg.h index b8da14178..4f54f4ddc 100644 --- a/DialogTools/RegressionReportDlg.h +++ b/DialogTools/RegressionReportDlg.h @@ -47,6 +47,7 @@ class RegressionReportDlg: public wxFrame void OnClose(wxCloseEvent& event); void OnMouseEvent(wxMouseEvent& event); void OnFontChanged(wxCommandEvent& event); + void OnSaveToFile(wxCommandEvent& event); void AddNewReport(const wxString report); void SetReport(const wxString report);