<commit_before>/*
 * MainFrame.cpp
 *
 *  Created on: 3 nov. 2014
 *      Author: <NAME>
 */

#include "MainFrame.h"

MainFrame::MainFrame() : wxFrame(NULL, wxID_ANY, "Boat Log Wiever", wxDefaultPosition, wxSize(800,640)){

	currentDataIndex = 0;

	wxMenuBar *menubar = new wxMenuBar;
	wxMenu *file = new wxMenu;

	file->Append(wxID_OPEN, wxT("&Open"));
	file->Append(wxID_CLOSE, wxT("&Close"));

	menubar->Append(file, wxT("&File"));

	wxMenu *edit =  new wxMenu;

	edit->Append(wxID_EXECUTE, wxT("&Start Replay"));
	edit->Append(wxID_STOP, wxT("&Pause"));

	menubar->Append(edit, wxT("&Edit"));


	wxMenu *help =  new wxMenu;
	help->Append(wxID_HELP, wxT("&Help"));
	help->AppendSeparator();
	help->Append(wxID_ABOUT, wxT("&About"));


	menubar->Append(help, wxT("&Help"));
	SetMenuBar(menubar);

	Connect(wxID_OPEN, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnOpen));
	Connect(wxID_CLOSE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnQuit));

	Connect(wxID_EXECUTE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnPlay));
	Connect(wxID_STOP, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnStop));

	Connect(wxID_HELP, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnAddWidget));
	Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnAbout));

	gs = new wxGridSizer(4, 4, 3, 3);

	//DataWidget *speed = new DataWidget(this, wxID_EXECUTE);
	TimeWidget *time = new TimeWidget(this, -1);
	GenericDataWidget *data = new GenericDataWidget("VsurfMes","Speed",  this, -1);


	gs->Add(time,0, wxEXPAND);
	gs->Add(data,0, wxEXPAND);

	widgets.push_back(time);
	widgets.push_back(data);


	EVT_TIMER(wxEVT_TIMER,MainFrame::OnUpdate)

	timer = new wxTimer(this);
	timer->SetOwner(this);
	Connect(timer->GetId(), wxEVT_TIMER, wxTimerEventHandler(MainFrame::OnUpdate));


}

MainFrame::~MainFrame(){
}


void MainFrame::OnOpen(wxCommandEvent& event) {

	wxFileDialog *chooseFileDialog = new wxFileDialog(this, "Choose first file", ".", "", "CSV files (*.csv)|**.csv", wxFD_OPEN | wxFD_MULTIPLE);

	if(chooseFileDialog->ShowModal() == wxID_OK) {
		wxArrayString listOfFiles;
		chooseFileDialog->GetPaths(listOfFiles);

		wxProgressDialog *progressBar = new wxProgressDialog("Loading", "Processing file", listOfFiles.GetCount(), this, wxPD_APP_MODAL|wxPD_AUTO_HIDE|wxPD_SMOOTH|wxPD_ELAPSED_TIME|wxPD_ESTIMATED_TIME|wxPD_REMAINING_TIME );
		progressBar->Update(0);
		for(auto file : listOfFiles) {
			std::cout << std::string(file) << std::endl;
			data.PopulateByReadingFile(std::string(file), 1);
			progressBar->Update(progressBar->GetValue()+1);
		}
	}

}

void MainFrame::OnQuit(wxCommandEvent& event) {

	wxMessageDialog *dialog = new wxMessageDialog(this, wxT("Are you sur to quit?"), wxT("Quit"), wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);

	if(dialog->ShowModal() == wxID_YES) {
		Close(true);
	}
}

void MainFrame::OnPlay(wxCommandEvent& event) {

	if(data.getNbData() > 0) {
		if(timer->IsRunning()) {
					wxMessageDialog *dialog = new wxMessageDialog(this, wxT("Program already running"), wxT("Warning"), wxICON_EXCLAMATION | wxOK);
					dialog->ShowModal();
	}
		else {
			updateScreen();
			timer->Start(1000);
		}
	} else {
		wxMessageDialog *dialog = new wxMessageDialog(this, wxT("Please load file(s) before launching"), wxT("Error"), wxICON_EXCLAMATION | wxOK);
		dialog->ShowModal();
	}
}
void MainFrame::OnAddWidget(wxCommandEvent& event) {

	wxArrayString keys;

	for(auto kv : data.getData()) {
		keys.Add(wxString(kv.first));
		std::cout << kv.first << std::endl;
	}
	wxString string = wxGetSingleChoice("Please choose a value","Choose", keys, this);

	GenericDataWidget *data = new GenericDataWidget("VsurfMes","Speed",  this, -1);

	gs->Add(data,0, wxEXPAND);

}

void MainFrame::OnUpdate(wxTimerEvent& event) {
	updateScreen();
}

void MainFrame::OnStop(wxCommandEvent& event) {
	timer->Stop();
}

void MainFrame::OnHelp(wxCommandEvent& event) {

	wxMessageDialog *dialog = new wxMessageDialog(this, wxT("No help yet"), wxT("Error"), wxICON_EXCLAMATION | wxOK);
	dialog->ShowModal();
}

void MainFrame::OnAbout(wxCommandEvent& event) {

    wxAboutDialogInfo info;
    info.SetName(_("Boat Log Viewer"));
    info.SetVersion(_("1.0"));
    info.SetDescription(_("A program to view data stocked by a boat on the sea."));
    info.AddDeveloper("<NAME>");

    wxAboutBox(info);
}

void MainFrame::updateScreen() {

	if(currentDataIndex < data.getNbData() ) {
		std::unordered_map<std::string, double> currentData;

		std::vector<std::string> keys;

		for(auto kv : data.getData()) {
			keys.push_back(kv.first);
		}

		// Creating current data
		for(auto key : keys) {
			double v = data.getData().at(key).at(currentDataIndex);
			currentData.insert({key, v});
		}

		// Updating all widgets
		for(auto widget : widgets) {
			widget->updateData(currentData);
		}

		++currentDataIndex;
	}
}<commit_msg>change the nb of widget and add a way to add widget<commit_after>/*
 * MainFrame.cpp
 *
 *  Created on: 3 nov. 2014
 *      Author: <NAME>
 */

#include "MainFrame.h"

MainFrame::MainFrame() : wxFrame(NULL, wxID_ANY, "Boat Log Wiever", wxDefaultPosition, wxSize(800,640)){

	currentDataIndex = 0;

	wxMenuBar *menubar = new wxMenuBar;
	wxMenu *file = new wxMenu;

	file->Append(wxID_OPEN, wxT("&Open"));
	file->Append(wxID_CLOSE, wxT("&Close"));

	menubar->Append(file, wxT("&File"));

	wxMenu *edit =  new wxMenu;

	edit->Append(wxID_EXECUTE, wxT("&Start Replay\tCtrl-L"));
	edit->Append(wxID_STOP, wxT("&Stop\tCtrl-P"));
	edit->Append(wxID_ADD, wxT("&Add widget\tCtrl-A"));

	menubar->Append(edit, wxT("&Edit"));


	wxMenu *help =  new wxMenu;
	help->Append(wxID_HELP, wxT("&Help"));
	help->AppendSeparator();
	help->Append(wxID_ABOUT, wxT("&About"));


	menubar->Append(help, wxT("&Help"));
	SetMenuBar(menubar);

	Connect(wxID_OPEN, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnOpen));
	Connect(wxID_CLOSE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnQuit));

	Connect(wxID_EXECUTE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnPlay));
	Connect(wxID_STOP, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnStop));
	Connect(wxID_ADD, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnAddWidget));

	Connect(wxID_HELP, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnHelp));
	Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnAbout));

	gs = new wxGridSizer(8, 4, 4, 4);

	//DataWidget *speed = new DataWidget(this, wxID_EXECUTE);
	TimeWidget *time = new TimeWidget(this, -1);

	gs->Add(time,0, wxEXPAND);

	widgets.push_back(time);

	SetSizer(gs);

	EVT_TIMER(wxEVT_TIMER,MainFrame::OnUpdate)

	timer = new wxTimer(this);
	timer->SetOwner(this);
	Connect(timer->GetId(), wxEVT_TIMER, wxTimerEventHandler(MainFrame::OnUpdate));

	this->SetBackgroundColour(wxColour(* wxBLACK));

}

MainFrame::~MainFrame(){
}


void MainFrame::OnOpen(wxCommandEvent& event) {

	wxFileDialog *chooseFileDialog = new wxFileDialog(this, "Choose first file", ".", "", "CSV files (*.csv)|**.csv", wxFD_OPEN | wxFD_MULTIPLE);

	if(chooseFileDialog->ShowModal() == wxID_OK) {
		wxArrayString listOfFiles;
		chooseFileDialog->GetPaths(listOfFiles);

		wxString stringNumber = "";
		long number = 0;

		while(number <= 0) {
			stringNumber = wxGetTextFromUser("Please the time for each data (in s)","Time", "1", this);
			stringNumber.ToLong(&number);
		}

		wxProgressDialog *progressBar = new wxProgressDialog("Loading", "Processing file", listOfFiles.GetCount(), this, wxPD_APP_MODAL|wxPD_AUTO_HIDE|wxPD_SMOOTH|wxPD_ELAPSED_TIME|wxPD_ESTIMATED_TIME|wxPD_REMAINING_TIME );
		progressBar->Update(0);
		for(auto file : listOfFiles) {
			std::cout << std::string(file) << std::endl;
			data.PopulateByReadingFile(std::string(file), number);
			progressBar->Update(progressBar->GetValue()+1);
		}
	}

}

void MainFrame::OnQuit(wxCommandEvent& event) {

	wxMessageDialog *dialog = new wxMessageDialog(this, wxT("Are you sur to quit?"), wxT("Quit"), wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);

	if(dialog->ShowModal() == wxID_YES) {
		Close(true);
	}
}

void MainFrame::OnPlay(wxCommandEvent& event) {

	if(data.getNbData() > 0) {
		if(timer->IsRunning()) {
					wxMessageDialog *dialog = new wxMessageDialog(this, wxT("Program already running"), wxT("Warning"), wxICON_EXCLAMATION | wxOK);
					dialog->ShowModal();
	}
		else {
			updateScreen();
			timer->Start(1000);
		}
	} else {
		wxMessageDialog *dialog = new wxMessageDialog(this, wxT("Please load file(s) before launching"), wxT("Error"), wxICON_EXCLAMATION | wxOK);
		dialog->ShowModal();
	}
}
void MainFrame::OnAddWidget(wxCommandEvent& event) {

	if(data.getNbData() > 0) {

		wxArrayString keys;

		for(auto kv : data.getData()) {
			keys.Add(wxString(kv.first));
			std::cout << kv.first << std::endl;
		}
		wxString string = wxGetSingleChoice("Please choose a value","Choose", keys, this);
		wxString name = wxGetTextFromUser("Name this value","Choose", string, this);


		GenericDataWidget *newWidget = new GenericDataWidget(string.ToStdString(),name.ToStdString(),  this, -1);

		widgets.push_back(newWidget);

		std::cout << widgets.size() << std::endl;

		gs->Add(newWidget,0, wxEXPAND, 0);
		Layout();

	} else {
		wxMessageDialog *dialog = new wxMessageDialog(this, wxT("Please load file(s) before adding widget"), wxT("Error"), wxICON_EXCLAMATION | wxOK);
		dialog->ShowModal();
	}

}

void MainFrame::OnUpdate(wxTimerEvent& event) {
	updateScreen();
}

void MainFrame::OnStop(wxCommandEvent& event) {
	timer->Stop();
}

void MainFrame::OnHelp(wxCommandEvent& event) {

	wxMessageDialog *dialog = new wxMessageDialog(this, wxT("No help yet"), wxT("Error"), wxICON_EXCLAMATION | wxOK);
	dialog->ShowModal();
}

void MainFrame::OnAbout(wxCommandEvent& event) {

    wxAboutDialogInfo info;
    info.SetName(_("Boat Log Viewer"));
    info.SetVersion(_("1.0"));
    info.SetDescription(_("A program to view data stocked by a boat on the sea."));
    info.AddDeveloper("<NAME>");

    wxAboutBox(info);
}

void MainFrame::updateScreen() {

	if(currentDataIndex < data.getNbData() ) {
		std::unordered_map<std::string, double> currentData;

		std::vector<std::string> keys;

		for(auto kv : data.getData()) {
			keys.push_back(kv.first);
		}

		// Creating current data
		for(auto key : keys) {
			double v = data.getData().at(key).at(currentDataIndex);
			currentData.insert({key, v});
		}

		// Updating all widgets
		for(auto widget : widgets) {
			widget->updateData(currentData);
		}

		++currentDataIndex;
	}
	else {
		timer->Stop();
		currentDataIndex = 0;
		wxMessageDialog *dialog = new wxMessageDialog(this, wxT("End of the data"), wxT("Finish"), wxICON_EXCLAMATION | wxOK);
		dialog->ShowModal();
	}
}
{'fraction_non_alphanumeric': 0.15756399593383236, 'fraction_numerical': 0.0061916643563441455, 'mean_word_length': 5.82776025236593, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '609085', 'n_tokens_mistral': 4145, 'n_tokens_neox': 3871, 'n_words': 696}