Add working config file system

`fc2dconfig` will write a config file, and `fc2d` will read it.
This commit is contained in:
Epicalert 2021-07-01 22:47:13 +08:00
parent 6d6658c610
commit d63178a37b
No known key found for this signature in database
GPG key ID: CAA46F858D0979BD
5 changed files with 69 additions and 5 deletions

View file

@ -65,10 +65,12 @@ add_executable( fc2d
src/tomlcpp.cpp src/tomlcpp.cpp
src/error.cpp src/error.cpp
src/eye.cpp src/eye.cpp
src/configfile.cpp
) )
target_link_libraries( fc2d ${OpenCV_LIBS} ${OPENGL_LIBRARIES} ${WEBP_LIBRARIES} target_link_libraries( fc2d ${OpenCV_LIBS} ${OPENGL_LIBRARIES} ${WEBP_LIBRARIES}
FreeGLUT::freeglut GLEW::glew zip Boxer fmt ) FreeGLUT::freeglut GLEW::glew zip Boxer fmt )
add_executable( fc2dconfig add_executable( fc2dconfig
src/fc2dconfig.cpp src/fc2dconfig.cpp
src/paths.cpp
) )
target_link_libraries( fc2dconfig ${wxWidgets_LIBRARIES} ) target_link_libraries( fc2dconfig ${wxWidgets_LIBRARIES} )

25
src/configfile.cpp Normal file
View file

@ -0,0 +1,25 @@
#include <vector>
#include <iostream>
#include <configfile.hpp>
#include <paths.hpp>
#include <error.hpp>
#include <tomlcpp.hpp>
bool configFileOpen(struct optData* data) {
auto configFile = toml::parseFile(prefixCustom + "config.toml");
if (!configFile.table) {
return false;
}
auto useHaarResult = configFile.table->getBool("use_haar");
if (useHaarResult.first) {
data->useHaar = useHaarResult.second;
}
auto modelNameResult = configFile.table->getString("model");
if (modelNameResult.first) {
data->model = modelNameResult.second;
}
return true;
}

8
src/configfile.hpp Normal file
View file

@ -0,0 +1,8 @@
#ifndef CONFIGFILE_HPP
#define CONFIGFILE_HPP
#include <args.hpp>
bool configFileOpen(struct optData* optData);
#endif

View file

@ -1,4 +1,8 @@
#include <fstream>
#include <config.hpp> #include <config.hpp>
#include <args.hpp>
#include <paths.hpp>
#include <wx/wxprec.h> #include <wx/wxprec.h>
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
@ -14,13 +18,18 @@ class ConfigurationFrame : public wxFrame {
public: public:
ConfigurationFrame(); ConfigurationFrame();
private: private:
wxCheckBox* useHaarCheckBox;
wxTextCtrl* modelNameText;
void OnExit(wxCommandEvent& event); void OnExit(wxCommandEvent& event);
}; };
wxIMPLEMENT_APP(ConfigurationApp); wxIMPLEMENT_APP(ConfigurationApp);
bool ConfigurationApp::OnInit() { bool ConfigurationApp::OnInit() {
initPrefixes();
// TODO: put config file creation here // TODO: put config file creation here
ConfigurationFrame* frame = new ConfigurationFrame(); ConfigurationFrame* frame = new ConfigurationFrame();
frame->Show(true); frame->Show(true);
return true; return true;
@ -29,11 +38,18 @@ bool ConfigurationApp::OnInit() {
ConfigurationFrame::ConfigurationFrame() : wxFrame(NULL, wxID_ANY, "Configure " PROJECT_NAME) { ConfigurationFrame::ConfigurationFrame() : wxFrame(NULL, wxID_ANY, "Configure " PROJECT_NAME) {
wxPanel* panel = new wxPanel(this, wxID_ANY); wxPanel* panel = new wxPanel(this, wxID_ANY);
// TODO: load config file and populate values
wxStaticText* placeholderText = new wxStaticText(panel, wxID_ANY, "There's nothing here right now, just click OK."); wxStaticText* placeholderText = new wxStaticText(panel, wxID_ANY, "There's nothing here right now, just click OK.");
useHaarCheckBox = new wxCheckBox(panel, wxID_ANY, "Disable DNN face detection");
// TODO: make this a dropdown with the detected model files
modelNameText = new wxTextCtrl(panel, wxID_ANY, "test");
// TODO: cancel button to exit without saving settings
wxButton* okButton = new wxButton(panel, wxID_OK, "OK"); wxButton* okButton = new wxButton(panel, wxID_OK, "OK");
wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(placeholderText, 0, wxALL | wxALIGN_LEFT, 10); sizer->Add(placeholderText, 0, wxALL | wxALIGN_LEFT, 10);
sizer->Add(useHaarCheckBox, 0, wxALL | wxALIGN_LEFT, 5);
sizer->Add(modelNameText, 0, wxALL | wxALIGN_LEFT, 5);
sizer->AddStretchSpacer(1); sizer->AddStretchSpacer(1);
sizer->Add(okButton, 0, wxALIGN_RIGHT | wxALL, 10); sizer->Add(okButton, 0, wxALIGN_RIGHT | wxALL, 10);
@ -43,6 +59,12 @@ ConfigurationFrame::ConfigurationFrame() : wxFrame(NULL, wxID_ANY, "Configure "
} }
void ConfigurationFrame::OnExit(wxCommandEvent& event) { void ConfigurationFrame::OnExit(wxCommandEvent& event) {
// TODO: write config file here // write options to config file
std::ofstream configFile;
configFile.open(prefixCustom + "config.toml");
configFile << "use_haar = " << (useHaarCheckBox->GetValue() ? "true" : "false") << std::endl;
configFile << "model = \"" << modelNameText->GetValue() << "\""<< std::endl;
configFile.close();
Close(true); Close(true);
} }

View file

@ -4,18 +4,25 @@
#include <args.hpp> #include <args.hpp>
#include <config.hpp> #include <config.hpp>
#include <model.hpp> #include <model.hpp>
#include <configfile.hpp>
#include <iostream> #include <iostream>
#include <cstring> #include <cstring>
int main (int argc, char** argv) { int main (int argc, char** argv) {
#ifndef _WIN32
argp_parse(&argp, argc, argv, 0, 0, 0);
#endif
std::cout << PROJECT_NAME " is starting..." << std::endl; std::cout << PROJECT_NAME " is starting..." << std::endl;
initPrefixes(); initPrefixes();
// load config file and apply it
configFileOpen(&optData);
#ifndef _WIN32
// parse arguments and apply them
argp_parse(&argp, argc, argv, 0, 0, 0);
#endif
std::cout << "Custom asset prefix: " << prefixCustom << std::endl; std::cout << "Custom asset prefix: " << prefixCustom << std::endl;
std::cout << "Default asset prefix: " << prefixDefault << std::endl; std::cout << "Default asset prefix: " << prefixDefault << std::endl;