Commit 829af363 authored by Loic Guegan's avatar Loic Guegan
Browse files

Cleaning code

parent 798d6782
Pipeline #216539770 failed with stage
in 7 minutes and 54 seconds
......@@ -79,40 +79,6 @@ void MainFrame::OnClose(wxCloseEvent &event) {
pair.second->Destroy();
}
wxMenu* MainFrame::getPiecesMenu() {
wxMenu *themes = new wxMenu;
short id = 200;
for (auto &theme : CNF.ListPiecesThemes()) {
themes->Append(id, theme, "");
id++;
}
return themes;
}
wxMenu* MainFrame::getSquaresMenu() {
wxMenu *themes = new wxMenu;
short id = 300;
for (auto &theme : CNF.ListBoardThemes()) {
themes->Append(id, theme, "");
id++;
}
return themes;
}
void MainFrame::ChangeSquares(wxCommandEvent &event) {
wxMenu *menu = getSquaresMenu();
wxMenuItem *item = menu->FindItem(event.GetId());
//p->BP->SetBoardSkin(item->GetItemLabel().ToStdString());
(void) item;
}
void MainFrame::ChangePieces(wxCommandEvent &event) {
wxMenu *menu = getPiecesMenu();
wxMenuItem *item = menu->FindItem(event.GetId());
(void) item;
//p->BP->SetPiecesSkin(item->GetItemLabel().ToStdString());
}
void MainFrame::MenuEventHandler(wxCommandEvent &event){
switch(event.GetId()){
......
......@@ -43,6 +43,8 @@ public:
* @param size The size.
*/
MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size);
/// @brief Allows other windows to communication using the MainFrame.
wxWindow* operator[](WIN name);
private:
/**
......@@ -52,10 +54,6 @@ private:
unordered_map<WIN,wxWindow*> Windows;
void MenuEventHandler(wxCommandEvent &event);
void ChangePieces(wxCommandEvent &event);
void ChangeSquares(wxCommandEvent &event);
wxMenu* getPiecesMenu();
wxMenu* getSquaresMenu();
void OnClose(wxCloseEvent &event);
void Create(WIN name);
......
......@@ -9,8 +9,19 @@
namespace ochess {
namespace gui {
/**
* All panels part of the Settings left menu should implement this interface.
*/
class Applicable {
public:
/**
* @brief Apply the configuration of the current panel.
*
* This method should apply the configuration of the actual panel
* using the CNF global variable.
*/
virtual void Apply()=0;
};
}
......
......@@ -23,6 +23,9 @@ typedef enum BOARD_SETTING_MODE {
BOARD_SETTING_GENERAL,BOARD_SETTING_APPEARANCE
} BOARD_SETTING_MODE;
/**
*
*/
class BoardSettings: public wxPanel, public Applicable {
private:
wxBoxSizer *VBox;
......
......@@ -9,116 +9,231 @@
namespace ochess {
namespace gui {
EngineSettings::EngineSettings(wxWindow* parent):wxPanel(parent,wxID_ANY,wxDefaultPosition) {
wxBoxSizer *VBox=new wxBoxSizer(wxVERTICAL);
// Engine list
wxStaticText* engineListLabel=new wxStaticText(this, wxID_ANY, "Engine list");
wxFont font=engineListLabel->GetFont().Bold();
EngineSelection::EngineSelection(wxWindow *parent) :
wxDialog(parent, -1, "Board Settings", wxDefaultPosition,
wxSize(800, 600)) {
// Add file selector
wxBoxSizer *VBox = new wxBoxSizer(wxVERTICAL);
FileSelect =
new wxFileCtrl(this, wxID_ANY,
"/home/loic/.local/bin/source/scid_vs_pc/engines/stockfish10/Linux/", //wxEmptyString, // Default directory
wxEmptyString, // Default filename
wxFileSelectorDefaultWildcardStr,
wxFC_DEFAULT_STYLE, wxDefaultPosition, wxSize(800, 500));
VBox->Add(FileSelect);
// Add buttons
wxBoxSizer *HBox = new wxBoxSizer(wxHORIZONTAL);
wxButton *Ok = new wxButton(this, 654, "Ok");
wxButton *Cancel = new wxButton(this, 23, "Cancel");
this->Bind(wxEVT_BUTTON, &EngineSelection::ButtonHandler, this);
HBox->AddStretchSpacer();
HBox->Add(Cancel, 0);
HBox->Add(Ok, 0, wxLEFT, 10);
VBox->Add(HBox, 0, wxEXPAND | wxTOP, 20);
// Finishing setup
wxBoxSizer *TopSizer = new wxBoxSizer(wxVERTICAL);
TopSizer->Add(VBox, 0, wxALL, 10);
this->SetSizer(TopSizer);
this->Bind(wxEVT_CLOSE_WINDOW, &EngineSelection::OnClose, this);
}
string EngineSelection::GetEnginePath() {
return (FileSelect->GetPath().ToStdString());
}
void EngineSelection::ButtonHandler(wxCommandEvent &event) {
if (event.GetId() == 654) {
NotifyAll(SBJ_ENGINE_SELECTED);
this->Close();
} else if (event.GetId() == 23)
this->Close();
}
void EngineSelection::OnClose(wxCloseEvent &event) {
this->Destroy();
}
EngineSettings::EngineSettings(wxWindow *parent) :
wxPanel(parent, wxID_ANY, wxDefaultPosition) {
wxBoxSizer *VBox = new wxBoxSizer(wxVERTICAL);
// Configure engine list
wxStaticText *engineListLabel = new wxStaticText(this, wxID_ANY,
"Engine list");
wxFont font = engineListLabel->GetFont().Bold();
engineListLabel->SetFont(font);
VBox->Add(engineListLabel);
EngineList=new wxListCtrl(this,wxID_ANY,wxDefaultPosition,wxSize(-1,150),wxLC_REPORT);
EngineList->AppendColumn("Name");
EngineList->AppendColumn("Type");
VBox->Add(EngineList,0,wxEXPAND|wxTOP,10);
this->Bind(wxEVT_LIST_ITEM_DESELECTED,&EngineSettings::EngineListHandler,this);
this->Bind(wxEVT_LIST_ITEM_SELECTED,&EngineSettings::EngineListHandler,this);
wxBoxSizer *HBox=new wxBoxSizer(wxHORIZONTAL);
EngineList = new wxListCtrl(this, wxID_ANY, wxDefaultPosition,
wxSize(-1, 150), wxLC_REPORT | wxLC_EDIT_LABELS);
EngineList->AppendColumn("Name",wxLIST_FORMAT_LEFT,100);
EngineList->AppendColumn("Location",wxLIST_FORMAT_LEFT,300);
VBox->Add(EngineList, 0, wxEXPAND | wxTOP, 10);
this->Bind(wxEVT_LIST_ITEM_DESELECTED, &EngineSettings::EngineListHandler,
this);
this->Bind(wxEVT_LIST_ITEM_SELECTED, &EngineSettings::EngineListHandler,
this);
this->Bind(wxEVT_LIST_END_LABEL_EDIT,&EngineSettings::EngineListHandler,
this);
// Adding button below the engine list
wxBoxSizer *HBox = new wxBoxSizer(wxHORIZONTAL);
wxButton *Add = new wxButton(this, 124, "Add");
Delete = new wxButton(this, 125, "Delete");
Delete->Disable();
Delete->Disable(); // Turn it off (nothing selected)
HBox->AddStretchSpacer();
HBox->Add(Delete);
HBox->Add(Add,0,wxLEFT,5);
VBox->Add(HBox,0,wxEXPAND|wxTOP,10);
HBox->Add(Add, 0, wxLEFT, 5);
VBox->Add(HBox, 0, wxEXPAND | wxTOP, 10);
this->Bind(wxEVT_BUTTON, &EngineSettings::ButtonHandler, this);
// Engine configuration
wxStaticText* optionLabel=new wxStaticText(this, wxID_ANY, "Options");
wxStaticText *optionLabel = new wxStaticText(this, wxID_ANY, "Options");
optionLabel->SetFont(font);
VBox->Add(optionLabel,0,wxEXPAND|wxTOP,10);
EngineConf = new wxPropertyGrid(this,wxID_ANY,wxDefaultPosition,wxSize(-1,150));
VBox->Add(EngineConf,0,wxEXPAND|wxTOP,10);
VBox->Add(optionLabel, 0, wxEXPAND | wxTOP, 10);
EngineConf = new wxPropertyGrid(this, wxID_ANY, wxDefaultPosition,
wxSize(-1, 150));
VBox->Add(EngineConf, 0, wxEXPAND | wxTOP, 10);
// Finishing configuration
this->SetSizer(VBox);
Config=CNF.GetPtree("engines");
ConfigureEngineList();
Config = CNF.GetPtree("engines"); // Fetching the actual engine configuration
RefreshEngineList(); // Udate engine list
}
void EngineSettings::Apply(){
void EngineSettings::Apply() {
CNF.SetPtree("engines", Config); // Apply the configuration
}
void EngineSettings::SetupEngineConf(string engine){
void EngineSettings::SetupEngineConf(string engine) {
EngineConf->Clear();
string optPath="list."+engine+".options";
std::cout << "Path: " << optPath << std::endl << std::flush;
ptree opt_list=Config.get_child(optPath);
for (const ptree::value_type& opt : opt_list) {
string optName=opt.first;
string curOptPath=optPath+"."+optName;
string value=Config.get<string>(curOptPath+".value");
string type=Config.get<string>(curOptPath+".type");
if(type=="spin"){
wxIntProperty * prop=new wxIntProperty(optName, optName, std::stoi(value));
prop->SetAttribute("Min", Config.get<string>(curOptPath+".min"));
prop->SetAttribute("Max", Config.get<string>(curOptPath+".max"));
string optsPath = "list." + engine + ".options";
for (const ptree::value_type &opt : Config.get_child(optsPath)) {
string optId = opt.first;
string curOptPath = optsPath + "." + optId;
string name = Config.get<string>(curOptPath + ".name");
string value = Config.get<string>(curOptPath + ".value");
string type = Config.get<string>(curOptPath + ".type");
if (type == "spin") {
wxIntProperty *prop = new wxIntProperty(name, name,
std::stoi(value));
prop->SetAttribute("Min", Config.get<string>(curOptPath + ".min"));
prop->SetAttribute("Max", Config.get<string>(curOptPath + ".max"));
EngineConf->Append(prop);
}
else if(type=="check"){
wxBoolProperty * prop=new wxBoolProperty(optName, optName, value=="true");
} else if (type == "check") {
wxBoolProperty *prop = new wxBoolProperty(name, name,
value == "true");
EngineConf->Append(prop);
}
else if(type=="combo"){
wxArrayString choices;
for (const ptree::value_type& choice : Config.get_child(curOptPath+".var")) {
choices.push_back(Config.get<string>(curOptPath+".var."+choice.first));
} else if (type == "combo") {
wxArrayString choices;
for (const ptree::value_type &choice : Config.get_child(
curOptPath + ".var")) {
choices.push_back(
Config.get<string>(
curOptPath + ".var." + choice.first));
}
wxEnumProperty *prop=new wxEnumProperty(optName,optName,choices);
wxEnumProperty *prop = new wxEnumProperty(name, name, choices);
EngineConf->Append(prop);
}
else{
wxStringProperty * prop=new wxStringProperty(optName, optName, value);
} else {
wxStringProperty *prop = new wxStringProperty(name, name, value);
EngineConf->Append(prop);
}
}
}
void EngineSettings::ButtonHandler(wxCommandEvent &event){
if(event.GetId()==125){
int id=EngineList->GetNextItem(-1,wxLIST_NEXT_ALL,wxLIST_STATE_SELECTED);
if(id!=-1){
void EngineSettings::ButtonHandler(wxCommandEvent &event) {
if (event.GetId() == 125) {
int id = EngineList->GetNextItem(-1, wxLIST_NEXT_ALL,
wxLIST_STATE_SELECTED);
if (id != -1) {
Config.get_child("list").erase(to_string(id));
EngineList->DeleteItem(id);
Delete->Disable();
}
}
else if (event.GetId()==124){
EngineSel=new EngineSelection(this);
} else if (event.GetId() == 124) {
EngineSel = new EngineSelection(this);
EngineSel->Subscribe(this);
EngineSel->ShowModal();
}
}
void EngineSettings::ConfigureEngineList(){
int id=0;
while(id>=0){
id=EngineList->GetNextItem(-1,wxLIST_NEXT_ALL,wxLIST_STATE_DONTCARE);
if(id>=0)
void EngineSettings::RefreshEngineList() {
int id = 0;
while (id >= 0) {
id = EngineList->GetNextItem(-1, wxLIST_NEXT_ALL,
wxLIST_STATE_DONTCARE);
if (id >= 0)
EngineList->DeleteItem(id);
}
ptree engine_list=Config.get_child("list");
int i=0;
for (const ptree::value_type& engine : engine_list) {
string path="list."+engine.first;
EngineList->InsertItem(i, Config.get<string>(path+".name"));
EngineList->SetItem(i, 1, Config.get<string>(path+".name"));
EngineList->SetItemData(i,std::stoi(engine.first));
ptree engine_list = Config.get_child("list");
int i = 0;
for (const ptree::value_type &engine : engine_list) {
string path = "list." + engine.first;
EngineList->InsertItem(i, Config.get<string>(path + ".name"));
EngineList->SetItem(i, 1, Config.get<string>(path + ".path"));
EngineList->SetItemData(i, std::stoi(engine.first));
i++;
}
}
void EngineSettings::EngineListHandler(wxListEvent &event) {
if (event.GetEventType() == wxEVT_LIST_ITEM_DESELECTED)
Delete->Disable();
else if (event.GetEventType()==wxEVT_LIST_END_LABEL_EDIT){
string engine=std::to_string(event.GetItem().GetData());
string newName=event.GetItem().GetText().ToStdString();
Config.put("list."+engine+".name",newName);
RefreshEngineList();
}
else {
Delete->Enable();
SetupEngineConf(std::to_string(event.GetItem().GetData()));
}
}
void EngineSettings::Notify(SUBJECT sbj) {
if (sbj == SBJ_ENGINE_SELECTED) {
string engine = EngineSel->GetEnginePath();
if (engine.size() != 0) {
Engine e(engine);
std::map<std::string, UCI_OPT> options = e.GetOptions();
// Find next engine ID
int id = 0;
bool found = true;
while (found) {
found = false;
for (const ptree::value_type &engine : Config.get_child("list")) {
found = (engine.first == std::to_string(id));
if (found)
break;
}
id++;
}
// Add engine to config
string path = "list." + std::to_string(id);
Config.put(path + ".name", "NewEngine");
Config.put(path + ".path", engine);
Config.put(path + ".options", ""); // If the string is not empty json parser will crash (since
// in the next loop you are trying to assign chile to a node that has a value)
int j = 0;
for (auto pair : options) {
string curOpt = path + ".options." + std::to_string(j);
Config.put(curOpt + ".name", pair.first);
Config.put(curOpt + ".type", pair.second.type);
Config.put(curOpt + ".value", pair.second.value);
Config.put(curOpt + ".min", pair.second.min);
Config.put(curOpt + ".max", pair.second.max);
int i = 0;
for (string pairVar : pair.second.var) {
Config.put(curOpt + ".var." + std::to_string(i), pairVar);
i++;
}
j++;
}
e.quit();
RefreshEngineList();
}
}
}
} /* namespace gui */
} /* namespace ochess */
......@@ -11,137 +11,75 @@
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include "Applicable.hpp"
#include <wx/listctrl.h>
#include "ochess.hpp"
#include <boost/foreach.hpp>
#include <wx/propgrid/propgrid.h>
#include <wx/propgrid/property.h>
#include <wx/propgrid/advprops.h>
#include <wx/filectrl.h>
#include <algorithm>
#include "Applicable.hpp"
#include "ochess.hpp"
#include "engine/Engine.hpp"
#include "utils/observer/Subject.hpp"
#include "utils/observer/Observer.hpp"
#include <algorithm>
using namespace ochess::engine;
namespace ochess {
namespace gui {
/**
* @brief Select and engine from the file system.
*
* User of this class can subscribe to it and and
* call the GetEngine() method on Notify().
*/
class EngineSelection: public Subject, public wxDialog {
wxFileCtrl *FileSelect;
public:
EngineSelection(wxWindow *parent) :
wxDialog(parent, -1, "Board Settings", wxDefaultPosition,
wxSize(800, 600)) {
wxBoxSizer *TopSizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *VBox = new wxBoxSizer(wxVERTICAL);
FileSelect =
new wxFileCtrl(this, wxID_ANY,
"/home/loic/.local/bin/source/scid_vs_pc/engines/stockfish10/Linux/", //wxEmptyString, // Default directory
wxEmptyString, // Default filename
wxFileSelectorDefaultWildcardStr,
wxFC_DEFAULT_STYLE, wxDefaultPosition,
wxSize(800, 500));
VBox->Add(FileSelect);
wxBoxSizer *HBox = new wxBoxSizer(wxHORIZONTAL);
wxButton *Ok = new wxButton(this, 654, "Ok");
wxButton *Cancel = new wxButton(this, 23, "Cancel");
this->Bind(wxEVT_BUTTON, &EngineSelection::ButtonHandler, this);
HBox->AddStretchSpacer();
HBox->Add(Cancel, 0);
HBox->Add(Ok, 0, wxLEFT, 10);
VBox->Add(HBox, 0, wxEXPAND | wxTOP, 20);
TopSizer->Add(VBox, 0, wxALL, 10);
this->SetSizer(TopSizer);
this->Bind(wxEVT_CLOSE_WINDOW, &EngineSelection::OnClose, this);
}
string GetEnginePath() {
return (FileSelect->GetPath().ToStdString());
}
void ButtonHandler(wxCommandEvent &event) {
if (event.GetId() == 654) {
NotifyAll(SBJ_ENGINE_SELECTED);
this->Close();
} else if (event.GetId() == 23)
this->Close();
}
void OnClose(wxCloseEvent &event) {
this->Destroy();
}
EngineSelection(wxWindow *parent);
/**
* @brief Retrieve the selected file absolute path
* @return Empty string if nothing selected or the selected engine absolute path otherwise
*/
string GetEnginePath();
void ButtonHandler(wxCommandEvent &event);
void OnClose(wxCloseEvent &event);
};
/**
* @brief Engine configuration panel
*
* This class allows to add, remove and configure the engines.
* It use the ochess::engine::Engine API to retrieve engine
* configuration options.
*/
class EngineSettings: public wxPanel, public Applicable, public Observer {
/// @brief List the available engines
wxListCtrl *EngineList;
/// @brief Configure the available engines
wxPropertyGrid *EngineConf;
/// @brief Local copy of the engines configuration @see ochess::ConfigManager
ptree Config;
/// @see EngineSelection
EngineSelection *EngineSel;
/// @brief To be able to enable/disable the button when engine are selected from the @a EngineList
wxButton *Delete;
private:
void SetupEngineConf(string engine);
public:
EngineSettings(wxWindow *parent);
/// @see Applicable::Apply()
void Apply() override;
void ConfigureEngineList();
void EngineListHandler(wxListEvent &event) {
if (event.GetEventType() == wxEVT_LIST_ITEM_DESELECTED)
Delete->Disable();
else {
Delete->Enable();
std::cout << "Update:" << std::to_string(event.GetItem().GetData())
<< std::flush << std::endl;
SetupEngineConf(std::to_string(event.GetItem().GetData()));
}
//std::cout << event.GetIndex() << std::endl;
}
void Notify(SUBJECT sbj) override {
if (sbj == SBJ_ENGINE_SELECTED) {
string engine = EngineSel->GetEnginePath();
if (engine.size() != 0) {
Engine e(engine);
std::map<std::string, UCI_OPT> options = e.GetOptions();
// Find next engine ID
int id = 0;
bool found = true;
while (found) {
found = false;
for (const ptree::value_type &engine : Config.get_child(
"list")) {
found = (engine.first == std::to_string(id));
if (found)
break;
}
id++;
}
// Add engine to config
string path = "list." + std::to_string(id);
Config.put(path + ".name", "NewEngine");
Config.put(path + ".options", "EngineOptions");
for (auto pair : options) {
string curOpt = path + ".options." + pair.first;
curOpt.erase(std::remove(curOpt.begin(), curOpt.end(), ' '),
curOpt.end());
Config.put(curOpt + ".type", pair.second.type);
Config.put(curOpt + ".value", pair.second.value);
Config.put(curOpt + ".min", pair.second.min);
Config.put(curOpt + ".max", pair.second.max);
int i=0;
for(string pairVar: pair.second.var){
Config.put(curOpt + ".var."+std::to_string(i),pairVar);
i++;
}
}
e.quit();
ConfigureEngineList();
}
}
}
/// @brief Redraw the engine list based on @a Config
void RefreshEngineList();
/// @brief Refresh @a EngineConf etc..
void EngineListHandler(wxListEvent &event);
/// @see ochess::Observer::Notify()
void Notify(SUBJECT sbj) override;
/// @brief Button handler for the EngineSettings panel
void ButtonHandler(wxCommandEvent &event);
};
......