From b52fdb3155f38b3e759617495e17305e779215c1 Mon Sep 17 00:00:00 2001 From: David Given Date: Sat, 26 Feb 2022 19:33:20 +0100 Subject: [PATCH] Additional settings pane now works. --- lib/flags.cc | 48 +++--- lib/flags.h | 6 +- lib/utils.cc | 18 ++ lib/utils.h | 3 + src/gui/layout.cpp | 34 ++-- src/gui/layout.fbp | 390 +++++++++++++++--------------------------- src/gui/layout.h | 8 +- src/gui/main.cc | 2 +- src/gui/mainwindow.cc | 32 ++++ src/gui/mainwindow.h | 1 + 10 files changed, 245 insertions(+), 297 deletions(-) diff --git a/lib/flags.cc b/lib/flags.cc index 0c91ed04..475a9c4d 100644 --- a/lib/flags.cc +++ b/lib/flags.cc @@ -182,32 +182,38 @@ void FlagGroup::parseFlagsWithConfigFiles(int argc, const char* argv[], { parseFlags(argc, argv, [&](const auto& filename) { - const auto& it = configFiles.find(filename); - if (it != configFiles.end()) - { - ConfigProto newConfig; - if (!newConfig.ParseFromString(it->second)) - Error() << "couldn't load built-in config proto"; - config.MergeFrom(newConfig); - } - else - { - std::ifstream f(filename, std::ios::out); - if (f.fail()) - Error() << fmt::format("Cannot open '{}': {}", filename, strerror(errno)); - - std::ostringstream ss; - ss << f.rdbuf(); - - if (!google::protobuf::TextFormat::MergeFromString(ss.str(), &config)) - Error() << "couldn't load external config proto"; - } - + parseConfigFile(filename, configFiles); return true; } ); } +void FlagGroup::parseConfigFile( + const std::string& filename, + const std::map& configFiles) +{ + const auto& it = configFiles.find(filename); + if (it != configFiles.end()) + { + ConfigProto newConfig; + if (!newConfig.ParseFromString(it->second)) + Error() << "couldn't load built-in config proto"; + config.MergeFrom(newConfig); + } + else + { + std::ifstream f(filename, std::ios::out); + if (f.fail()) + Error() << fmt::format("Cannot open '{}': {}", filename, strerror(errno)); + + std::ostringstream ss; + ss << f.rdbuf(); + + if (!google::protobuf::TextFormat::MergeFromString(ss.str(), &config)) + Error() << "couldn't load external config proto"; + } +} + void FlagGroup::checkInitialised() const { if (!_initialised) diff --git a/lib/flags.h b/lib/flags.h index 5bb8599e..3c66badc 100644 --- a/lib/flags.h +++ b/lib/flags.h @@ -15,11 +15,15 @@ public: void parseFlags(int argc, const char* argv[], std::function callback = [](const auto&){ return false; }); - std::vector parseFlagsWithFilenames(int argc, const char* argv[], + std::vector parseFlagsWithFilenames( + int argc, const char* argv[], std::function callback = [](const auto&){ return false; }); void parseFlagsWithConfigFiles(int argc, const char* argv[], const std::map& configFiles); + static void parseConfigFile( + const std::string& filename, + const std::map& configFiles); void addFlag(Flag* flag); void checkInitialised() const; diff --git a/lib/utils.cc b/lib/utils.cc index d34cae9d..20f19d17 100644 --- a/lib/utils.cc +++ b/lib/utils.cc @@ -3,6 +3,8 @@ bool emergencyStop = false; +static const char* WHITESPACE = " \t\n\r\f\v"; + bool beginsWith(const std::string& value, const std::string& ending) { if (ending.size() > value.size()) @@ -21,6 +23,22 @@ bool endsWith(const std::string& value, const std::string& ending) std::equal(ending.rbegin(), ending.rend(), lowercase.begin()); } +void leftTrimWhitespace(std::string& value) +{ + value.erase(0, value.find_first_not_of(WHITESPACE)); +} + +void rightTrimWhitespace(std::string& value) +{ + value.erase(value.find_last_not_of(WHITESPACE) + 1); +} + +void trimWhitespace(std::string& value) +{ + leftTrimWhitespace(value); + rightTrimWhitespace(value); +} + void testForEmergencyStop() { if (emergencyStop) diff --git a/lib/utils.h b/lib/utils.h index c42d8fdc..e8fccdb3 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -5,6 +5,9 @@ extern bool beginsWith(const std::string& value, const std::string& beginning); extern bool endsWith(const std::string& value, const std::string& ending); +extern void leftTrimWhitespace(std::string& value); +extern void rightTrimWhitespace(std::string& value); +extern void trimWhitespace(std::string& value); /* If set, any running job will terminate as soon as possible (with an error). */ diff --git a/src/gui/layout.cpp b/src/gui/layout.cpp index 68ecf38b..0b188c29 100644 --- a/src/gui/layout.cpp +++ b/src/gui/layout.cpp @@ -84,20 +84,6 @@ MainWindowGen::MainWindowGen( wxWindow* parent, wxWindowID id, const wxString& t highDensityToggle = new wxCheckBox( this, wxID_ANY, wxT("High density disk"), wxDefaultPosition, wxDefaultSize, 0 ); fgSizer3->Add( highDensityToggle, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL, 5 ); - m_staticText7 = new wxStaticText( this, wxID_ANY, wxT("Cylinders:"), wxDefaultPosition, wxDefaultSize, 0 ); - m_staticText7->Wrap( -1 ); - fgSizer3->Add( m_staticText7, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL, 5 ); - - cylindersText = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 ); - fgSizer3->Add( cylindersText, 0, wxALL|wxEXPAND, 5 ); - - m_staticText9 = new wxStaticText( this, wxID_ANY, wxT("Heads:"), wxDefaultPosition, wxDefaultSize, 0 ); - m_staticText9->Wrap( -1 ); - fgSizer3->Add( m_staticText9, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL, 5 ); - - headsText = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 ); - fgSizer3->Add( headsText, 0, wxALL|wxEXPAND, 5 ); - fgSizer2->Add( fgSizer3, 1, wxEXPAND, 5 ); @@ -110,8 +96,8 @@ MainWindowGen::MainWindowGen( wxWindow* parent, wxWindowID id, const wxString& t fgSizer5->SetFlexibleDirection( wxBOTH ); fgSizer5->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED ); - additionSettingsEntry = new wxTextCtrl( m_panel1, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ); - fgSizer5->Add( additionSettingsEntry, 0, wxALL|wxEXPAND, 5 ); + additionalSettingsEntry = new wxTextCtrl( m_panel1, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ); + fgSizer5->Add( additionalSettingsEntry, 0, wxALL|wxEXPAND, 5 ); m_panel1->SetSizer( fgSizer5 ); @@ -134,6 +120,22 @@ MainWindowGen::MainWindowGen( wxWindow* parent, wxWindowID id, const wxString& t m_panel2->Layout(); fgSizer8->Fit( m_panel2 ); notebook->AddPage( m_panel2, wxT("Logs"), false ); + m_panel3 = new wxPanel( notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL ); + wxFlexGridSizer* fgSizer9; + fgSizer9 = new wxFlexGridSizer( 0, 2, 0, 0 ); + fgSizer9->AddGrowableCol( 0 ); + fgSizer9->AddGrowableRow( 0 ); + fgSizer9->SetFlexibleDirection( wxBOTH ); + fgSizer9->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED ); + + protoConfigEntry = new wxTextCtrl( m_panel3, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_READONLY ); + fgSizer9->Add( protoConfigEntry, 0, wxALL|wxEXPAND, 5 ); + + + m_panel3->SetSizer( fgSizer9 ); + m_panel3->Layout(); + fgSizer9->Fit( m_panel3 ); + notebook->AddPage( m_panel3, wxT("Proto config"), false ); fgSizer2->Add( notebook, 1, wxEXPAND | wxALL, 5 ); diff --git a/src/gui/layout.fbp b/src/gui/layout.fbp index 737a5e24..04326130 100644 --- a/src/gui/layout.fbp +++ b/src/gui/layout.fbp @@ -705,256 +705,6 @@ - - 5 - wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL - 0 - - 1 - 1 - 1 - 1 - - - - - - - - 1 - 0 - 1 - - 1 - 0 - Dock - 0 - Left - 1 - - 1 - - 0 - 0 - wxID_ANY - Cylinders: - 0 - - 0 - - - 0 - - 1 - m_staticText7 - 1 - - - protected - 1 - - Resizable - 1 - - - ; ; forward_declare - 0 - - - - - -1 - - - - 5 - wxALL|wxEXPAND - 0 - - 1 - 1 - 1 - 1 - - - - - - - - 1 - 0 - 1 - - 1 - 0 - Dock - 0 - Left - 1 - - 1 - - 0 - 0 - wxID_ANY - - 0 - - - - 0 - - 1 - cylindersText - 1 - - - protected - 1 - - Resizable - 1 - - - ; ; forward_declare - 0 - - - wxFILTER_NONE - wxDefaultValidator - - - - - - - - - 5 - wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL - 0 - - 1 - 1 - 1 - 1 - - - - - - - - 1 - 0 - 1 - - 1 - 0 - Dock - 0 - Left - 1 - - 1 - - 0 - 0 - wxID_ANY - Heads: - 0 - - 0 - - - 0 - - 1 - m_staticText9 - 1 - - - protected - 1 - - Resizable - 1 - - - ; ; forward_declare - 0 - - - - - -1 - - - - 5 - wxALL|wxEXPAND - 0 - - 1 - 1 - 1 - 1 - - - - - - - - 1 - 0 - 1 - - 1 - 0 - Dock - 0 - Left - 1 - - 1 - - 0 - 0 - wxID_ANY - - 0 - - - - 0 - - 1 - headsText - 1 - - - protected - 1 - - Resizable - 1 - - - ; ; forward_declare - 0 - - - wxFILTER_NONE - wxDefaultValidator - - - - - - - @@ -1017,7 +767,7 @@ Additional settings - 0 + 1 1 1 @@ -1121,7 +871,7 @@ 0 1 - additionSettingsEntry + additionalSettingsEntry 1 @@ -1151,7 +901,7 @@ Logs - 1 + 0 1 1 @@ -1282,6 +1032,140 @@ + + + Proto config + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + + 0 + + + 0 + + 1 + m_panel3 + 1 + + + protected + 1 + + Resizable + 1 + + ; ; forward_declare + 0 + + + + wxTAB_TRAVERSAL + + 2 + wxBOTH + 0 + 0 + 0 + + fgSizer9 + wxFLEX_GROWMODE_SPECIFIED + none + 0 + 0 + + 5 + wxALL|wxEXPAND + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 1 + + 1 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + + 0 + + + + 0 + + 1 + protoConfigEntry + 1 + + + protected + 1 + + Resizable + 1 + + wxTE_MULTILINE|wxTE_READONLY + ; ; forward_declare + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + + + + + + diff --git a/src/gui/layout.h b/src/gui/layout.h index c242699c..b1467e38 100644 --- a/src/gui/layout.h +++ b/src/gui/layout.h @@ -50,15 +50,13 @@ class MainWindowGen : public wxFrame wxStaticText* m_staticText51; wxChoice* formatChoice; wxCheckBox* highDensityToggle; - wxStaticText* m_staticText7; - wxTextCtrl* cylindersText; - wxStaticText* m_staticText9; - wxTextCtrl* headsText; wxNotebook* notebook; wxPanel* m_panel1; - wxTextCtrl* additionSettingsEntry; + wxTextCtrl* additionalSettingsEntry; wxPanel* m_panel2; wxTextCtrl* logEntry; + wxPanel* m_panel3; + wxTextCtrl* protoConfigEntry; wxButton* readFluxButton; wxButton* readImageButton; wxButton* writeFluxButton; diff --git a/src/gui/main.cc b/src/gui/main.cc index 45cd2149..64e808db 100644 --- a/src/gui/main.cc +++ b/src/gui/main.cc @@ -59,7 +59,7 @@ wxThread::ExitCode FluxEngineApp::Entry() } catch (const EmergencyStopException& e) { - std::cerr << "worker thread emergency stop\n"; + Logger() << "Emergency stop!\n"; } runOnUiThread( diff --git a/src/gui/mainwindow.cc b/src/gui/mainwindow.cc index 0e4e8c55..d419b9de 100644 --- a/src/gui/mainwindow.cc +++ b/src/gui/mainwindow.cc @@ -72,7 +72,18 @@ void MainWindow::OnReadFluxButton(wxCommandEvent&) else setProtoByString(&config, "usb.serial", serial); + ApplyCustomSettings(config); + + { + std::string s; + google::protobuf::TextFormat::PrintToString(config, &s); + protoConfigEntry->Clear(); + protoConfigEntry->AppendText(s); + } + visualiser->Clear(); + logEntry->Clear(); + _currentDisk = nullptr; runOnWorkerThread( [config, this]() { ::config = config; @@ -88,6 +99,27 @@ void MainWindow::OnReadFluxButton(wxCommandEvent&) ); } +void MainWindow::ApplyCustomSettings(ConfigProto& config) +{ + for (int i=0; i < additionalSettingsEntry->GetNumberOfLines(); i++) + { + auto setting = additionalSettingsEntry->GetLineText(i).ToStdString(); + trimWhitespace(setting); + if (setting.size() == 0) + continue; + + auto equals = setting.find('='); + if (equals != std::string::npos) + { + auto key = setting.substr(0, equals); + auto value = setting.substr(equals+1); + setProtoByString(&config, key, value); + } + else + FlagGroup::parseConfigFile(setting, formats); + } +} + void MainWindow::OnLogMessage(std::shared_ptr message) { logEntry->AppendText(Logger::toString(*message)); diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index 40253e01..1a4bd7eb 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -22,6 +22,7 @@ private: public: void UpdateState(); void UpdateDevices(); + void ApplyCustomSettings(ConfigProto& config); private: std::vector> _formats;