Skip to content

Commit

Permalink
#409 save DID regression results to file
Browse files Browse the repository at this point in the history
  • Loading branch information
lixun910 committed May 4, 2016
1 parent c48880f commit a3bc080
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
66 changes: 58 additions & 8 deletions DialogTools/RegressionReportDlg.cpp
Expand Up @@ -19,6 +19,8 @@

// For compilers that support precompilation, includes <wx/wx.h>.
#include <wx/wxprec.h>
#include <wx/wfstream.h>
#include <wx/txtstrm.h>

#ifndef WX_PRECOMP
#include <wx/wx.h>
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions DialogTools/RegressionReportDlg.h
Expand Up @@ -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);
Expand Down

0 comments on commit a3bc080

Please sign in to comment.