ochess/src/engine_tab/EngineTab.cpp

160 lines
5.5 KiB
C++
Raw Normal View History

2022-02-26 17:05:47 +01:00
#include "EngineTab.hpp"
2022-02-26 20:34:42 +01:00
EngineTab::EngineTab(wxWindow *parent, uciadapter::UCI *engine,
2023-01-31 10:26:40 +01:00
std::string engine_path)
2022-02-28 13:02:27 +01:00
: TabEngine(parent), TabInfos(TabInfos::ENGINE),
2023-01-31 10:26:40 +01:00
enginePath(engine_path), engine(engine) {
// Init engine name and location
2022-02-26 17:05:47 +01:00
SetLabel("New Engine");
2023-01-31 10:26:40 +01:00
engine_location->SetValue(engine_path);
2022-02-26 19:21:52 +01:00
2023-01-31 10:26:40 +01:00
// Fetch engine id
2022-02-26 18:46:06 +01:00
CONFIG_OPEN(conf);
2023-01-31 10:26:40 +01:00
conf->SetPath("engines/");
engine_id=conf->GetNumberOfGroups();
2022-02-26 20:34:42 +01:00
CONFIG_CLOSE(conf);
2023-01-31 10:26:40 +01:00
confGroup = "engines/" + std::to_string(engine_id);
// Init data
2022-02-26 20:34:42 +01:00
InitConfiguration();
2022-02-26 21:43:09 +01:00
LoadConfiguration();
RefreshItemList();
2022-02-26 18:46:06 +01:00
2022-02-26 21:43:09 +01:00
Bind(wxEVT_BUTTON, &EngineTab::OnSave, this, ENGINE_SAVE_CONF_BUTTON);
Bind(wxEVT_BUTTON, &EngineTab::OnDelete, this, ENGINE_DELETE_CONF_BUTTON);
2023-05-10 10:49:31 +02:00
Bind(wxEVT_PG_CHANGED, [p=this](wxPropertyGridEvent& event){ UNUSED(event); p->is_dirty=true;});
2022-02-26 21:43:09 +01:00
}
2023-01-31 10:26:40 +01:00
EngineTab::EngineTab(wxWindow *parent, std::uint32_t id)
2022-12-31 20:45:03 +01:00
: TabEngine(parent), TabInfos(TabInfos::ENGINE), engine(nullptr) {
2023-01-31 10:26:40 +01:00
// Init engine group
std::string id_str=std::to_string(id);
confGroup = "engines/" + std::to_string(id);
// Fetch name and path
2022-02-26 21:43:09 +01:00
CONFIG_OPEN(conf);
2023-01-31 10:26:40 +01:00
wxString name=conf->Read(confGroup + "/name");
SetLabel(name);
engine_name->SetValue(name);
2022-02-26 21:43:09 +01:00
engine_location->SetValue(conf->Read(confGroup + "/path"));
2023-02-01 12:00:24 +01:00
engine_id=id;
2022-02-26 21:43:09 +01:00
CONFIG_CLOSE(conf);
2023-01-31 10:26:40 +01:00
// Load existing configuration
2022-02-26 21:43:09 +01:00
LoadConfiguration();
Bind(wxEVT_BUTTON, &EngineTab::OnSave, this, ENGINE_SAVE_CONF_BUTTON);
Bind(wxEVT_BUTTON, &EngineTab::OnDelete, this, ENGINE_DELETE_CONF_BUTTON);
2023-05-10 10:49:31 +02:00
Bind(wxEVT_PG_CHANGED, [p=this](wxPropertyGridEvent& event){ UNUSED(event); p->is_dirty=true;});
2022-02-26 21:43:09 +01:00
}
EngineTab::~EngineTab() {
2022-12-31 20:45:03 +01:00
if (engine != nullptr) {
wxLogDebug("EngineTab destructor: destroying engine!");
engine->quit();
delete engine;
}
}
2022-02-26 21:43:09 +01:00
void EngineTab::OnDelete(wxCommandEvent &event) {
2023-05-10 10:49:31 +02:00
UNUSED(event);
2022-02-26 21:43:09 +01:00
CONFIG_OPEN(conf);
conf->DeleteGroup(confGroup);
CONFIG_CLOSE(conf);
RefreshItemList();
wxCommandEvent closeTabEvent(CLOSE_TAB_EVENT, GetId());
closeTabEvent.SetEventObject(this);
ProcessEvent(closeTabEvent);
}
void EngineTab::RefreshItemList() {
wxCommandEvent refreshEngineList(REFRESH_ENGINE_LIST, GetId());
refreshEngineList.SetEventObject(this);
ProcessEvent(refreshEngineList);
}
void EngineTab::LoadConfiguration() {
2022-02-26 18:46:06 +01:00
// Build wxPropertyGrid according to engine configuration
CONFIG_OPEN(conf2);
long index;
std::string optsPath = confGroup + "/options";
conf2->SetPath(optsPath);
wxString opt_name;
if (conf2->GetFirstGroup(opt_name, index)) {
do {
wxString optPath = opt_name + "/";
wxString type = conf2->Read(optPath + "type");
wxString default_value_wxString = conf2->Read(optPath + "value");
std::string default_value = default_value_wxString.ToStdString();
if (type == "check") {
engine_parameters->Append(
new wxBoolProperty(opt_name, wxPG_LABEL, default_value == "true"));
} else if (type == "spin") {
engine_parameters->Append(
new wxIntProperty(opt_name, wxPG_LABEL, std::stoi(default_value)));
} else if (type == "string" || type == "button") {
engine_parameters->Append(
new wxStringProperty(opt_name, wxPG_LABEL, default_value));
}
} while (conf2->GetNextGroup(opt_name, index));
}
CONFIG_CLOSE(conf2);
}
void EngineTab::OnSave(wxCommandEvent &event) {
2023-05-10 10:49:31 +02:00
UNUSED(event);
2022-02-26 18:46:06 +01:00
CONFIG_OPEN(conf2);
2023-01-31 10:26:40 +01:00
// Update engine name:
conf2->Write(confGroup + "/name", engine_name->GetValue());
// Update engine configuration:
2022-02-26 18:46:06 +01:00
long index;
std::string optsPath = confGroup + "/options";
conf2->SetPath(optsPath);
wxString opt_name;
if (conf2->GetFirstGroup(opt_name, index)) {
do {
wxString optPath = opt_name + "/";
wxString type = conf2->Read(optPath + "type");
wxPGProperty *property = engine_parameters->GetProperty(opt_name);
wxVariant value = property->GetValue();
if (value.IsType(wxPG_VARIANT_TYPE_BOOL)) {
conf2->Write(optPath + "/value", value.GetBool());
} else if (value.IsType(wxPG_VARIANT_TYPE_LONG)) {
conf2->Write(optPath + "/value", value.GetLong());
} else if (value.IsType(wxPG_VARIANT_TYPE_STRING)) {
conf2->Write(optPath + "/value", value.GetString());
}
} while (conf2->GetNextGroup(opt_name, index));
}
CONFIG_CLOSE(conf2);
2023-01-13 10:42:57 +01:00
// Notify all other tabs about this new configuration
2022-02-26 21:43:09 +01:00
RefreshItemList();
2022-02-26 18:46:06 +01:00
}
void EngineTab::InitConfiguration() {
wxLogDebug("Called!");
CONFIG_OPEN(conf);
conf->Write(confGroup + "/path", wxString(enginePath));
conf->Write(confGroup + "/name", wxString(engine->GetName()));
conf->Write(confGroup + "/authors", wxString(engine->GetAuthor()));
2022-02-26 17:05:47 +01:00
std::vector<uciadapter::Option> opts = engine->GetOptions();
for (uciadapter::Option &opt : opts) {
2022-02-26 18:46:06 +01:00
std::string optPath = confGroup + "/options/" + opt.name;
conf->Write(wxString(optPath + "/type"), wxString(opt.type));
2022-02-26 17:05:47 +01:00
if (opt.type == "check") {
2022-02-26 18:46:06 +01:00
conf->Write(wxString(optPath + "/value"), opt.default_value == "true");
2022-02-26 17:05:47 +01:00
} else if (opt.type == "spin") {
2022-02-26 18:46:06 +01:00
conf->Write(wxString(optPath + "/value"), std::stoi(opt.default_value));
conf->Write(wxString(optPath + "/min"), std::stoi(opt.min));
conf->Write(wxString(optPath + "/max"), std::stoi(opt.max));
2022-02-26 17:05:47 +01:00
} else if (opt.type == "string") {
2022-02-26 18:46:06 +01:00
conf->Write(wxString(optPath + "/value"), wxString(opt.default_value));
} else if (opt.type == "button") {
conf->Write(wxString(optPath + "/name"), wxString(opt.default_value));
2022-02-26 17:05:47 +01:00
}
}
2022-02-26 18:46:06 +01:00
CONFIG_CLOSE(conf);
}