Settings saving last session path.

This commit is contained in:
brunoherbelin
2020-05-09 22:10:50 +02:00
parent 7634e62054
commit db0acc9ae2
6 changed files with 88 additions and 39 deletions

View File

@@ -22,13 +22,13 @@ using namespace tinyxml2;
#include "Mixer.h" #include "Mixer.h"
// static objects for multithreaded session loading // static objects for multithreaded session loading
static std::atomic<bool> sessionLoadPending_ = false; static std::atomic<bool> sessionThreadActive_ = false;
static std::atomic<bool> sessionLoadFinished_ = false;
static std::string sessionThreadFilename_ = ""; static std::string sessionThreadFilename_ = "";
static std::atomic<bool> sessionLoadFinished_ = false;
static void loadSession(const std::string& filename, Session *session) static void loadSession(const std::string& filename, Session *session)
{ {
sessionLoadPending_ = true; sessionThreadActive_ = true;
sessionLoadFinished_ = false; sessionLoadFinished_ = false;
sessionThreadFilename_ = ""; sessionThreadFilename_ = "";
@@ -41,22 +41,20 @@ static void loadSession(const std::string& filename, Session *session)
} }
else { else {
// loaded ok // loaded ok
Log::Info("Session file %s loaded. %d source(s) created.", filename.c_str(), session->numSource()); Log::Info("Session %s loaded. %d source(s) created.", filename.c_str(), session->numSource());
} }
sessionThreadFilename_ = filename; sessionThreadFilename_ = filename;
sessionLoadFinished_ = true; sessionLoadFinished_ = true;
sessionLoadPending_ = false; sessionThreadActive_ = false;
} }
// static objects for multithreaded session saving // static objects for multithreaded session saving
static std::atomic<bool> sessionSavePending_ = false;
static std::atomic<bool> sessionSaveFinished_ = false; static std::atomic<bool> sessionSaveFinished_ = false;
static void saveSession(const std::string& filename, Session *session) static void saveSession(const std::string& filename, Session *session)
{ {
// reset // reset
sessionSavePending_ = true; sessionThreadActive_ = true;
sessionSaveFinished_ = false; sessionSaveFinished_ = false;
sessionThreadFilename_ = ""; sessionThreadFilename_ = "";
@@ -95,10 +93,13 @@ static void saveSession(const std::string& filename, Session *session)
// save file to disk // save file to disk
XMLSaveDoc(&xmlDoc, filename); XMLSaveDoc(&xmlDoc, filename);
// loaded ok
Log::Info("Session %s saved.", filename.c_str());
// all ok // all ok
sessionThreadFilename_ = filename; sessionThreadFilename_ = filename;
sessionSaveFinished_ = true; sessionSaveFinished_ = true;
sessionSavePending_ = false; sessionThreadActive_ = false;
} }
Mixer::Mixer() : session_(nullptr), back_session_(nullptr), current_view_(nullptr) Mixer::Mixer() : session_(nullptr), back_session_(nullptr), current_view_(nullptr)
@@ -318,12 +319,18 @@ View *Mixer::currentView()
void Mixer::save() void Mixer::save()
{ {
if (sessionThreadActive_)
return;
if (!sessionFilename_.empty()) if (!sessionFilename_.empty())
saveas(sessionFilename_); saveas(sessionFilename_);
} }
void Mixer::saveas(const std::string& filename) void Mixer::saveas(const std::string& filename)
{ {
if (sessionThreadActive_)
return;
// optional copy of views config // optional copy of views config
session_->config(View::MIXING)->copyTransform( mixing_.scene.root() ); session_->config(View::MIXING)->copyTransform( mixing_.scene.root() );
session_->config(View::GEOMETRY)->copyTransform( geometry_.scene.root() ); session_->config(View::GEOMETRY)->copyTransform( geometry_.scene.root() );
@@ -335,6 +342,9 @@ void Mixer::saveas(const std::string& filename)
void Mixer::open(const std::string& filename) void Mixer::open(const std::string& filename)
{ {
if (sessionThreadActive_)
return;
if (back_session_) if (back_session_)
delete back_session_; delete back_session_;

View File

@@ -14,6 +14,7 @@ using namespace tinyxml2;
Settings::Application Settings::application; Settings::Application Settings::application;
static string settingsFilename = ""; static string settingsFilename = "";
void Settings::Save() void Settings::Save()
{ {
XMLDocument xmlDoc; XMLDocument xmlDoc;
@@ -94,6 +95,7 @@ void Settings::Save()
XMLElement *recent = xmlDoc.NewElement( "Recent" ); XMLElement *recent = xmlDoc.NewElement( "Recent" );
XMLElement *recentsession = xmlDoc.NewElement( "Session" ); XMLElement *recentsession = xmlDoc.NewElement( "Session" );
recentsession->SetAttribute("path", application.recentSessions.path.c_str());
recentsession->SetAttribute("auto", application.recentSessions.automatic); recentsession->SetAttribute("auto", application.recentSessions.automatic);
for(auto it = application.recentSessions.filenames.begin(); for(auto it = application.recentSessions.filenames.begin();
it != application.recentSessions.filenames.end(); it++) { it != application.recentSessions.filenames.end(); it++) {
@@ -221,6 +223,11 @@ void Settings::Load()
XMLElement * pSession = pElement->FirstChildElement("Session"); XMLElement * pSession = pElement->FirstChildElement("Session");
if (pSession) if (pSession)
{ {
const char *path_ = pSession->Attribute("path");
if (path_)
application.recentSessions.path = std::string(path_);
else
application.recentSessions.path = SystemToolkit::home_path();
pSession->QueryBoolAttribute("auto", &application.recentSessions.automatic); pSession->QueryBoolAttribute("auto", &application.recentSessions.automatic);
application.recentSessions.filenames.clear(); application.recentSessions.filenames.clear();
XMLElement* path = pSession->FirstChildElement("path"); XMLElement* path = pSession->FirstChildElement("path");

View File

@@ -36,6 +36,7 @@ struct ViewConfig
struct History struct History
{ {
std::string path;
std::list<std::string> filenames; std::list<std::string> filenames;
bool automatic; bool automatic;

View File

@@ -58,7 +58,7 @@ string SystemToolkit::date_time_string()
std::string SystemToolkit::base_filename(const std::string& uri) std::string SystemToolkit::base_filename(const std::string& uri)
{ {
std::string basefilename = uri.substr(uri.find_last_of("/\\") + 1); std::string basefilename = uri.substr(uri.find_last_of(PATH_SEP) + 1);
const size_t period_idx = basefilename.rfind('.'); const size_t period_idx = basefilename.rfind('.');
if (std::string::npos != period_idx) if (std::string::npos != period_idx)
{ {
@@ -67,7 +67,15 @@ std::string SystemToolkit::base_filename(const std::string& uri)
return basefilename; return basefilename;
} }
string SystemToolkit::settings_path() std::string SystemToolkit::path_filename(const std::string& uri)
{
std::string path = uri.substr(0, uri.find_last_of(PATH_SEP) + 1);
return path;
}
std::string SystemToolkit::home_path()
{ {
// 1. find home // 1. find home
char *mHomePath; char *mHomePath;
@@ -80,7 +88,13 @@ string SystemToolkit::settings_path()
// try the $HOME environment variable // try the $HOME environment variable
mHomePath = getenv("HOME"); mHomePath = getenv("HOME");
} }
string home(mHomePath);
return string(mHomePath) + PATH_SEP;
}
string SystemToolkit::settings_path()
{
string home(home_path());
// 2. try to access user settings folder // 2. try to access user settings folder
string settingspath = home + PATH_SETTINGS; string settingspath = home + PATH_SETTINGS;

View File

@@ -15,6 +15,9 @@ namespace SystemToolkit
// get fixed length string (17 chars) YYYYMMDDHHmmssiii // get fixed length string (17 chars) YYYYMMDDHHmmssiii
std::string date_time_string(); std::string date_time_string();
// get the OS dependent home path
std::string home_path();
// get the OS dependent path where to store settings // get the OS dependent path where to store settings
std::string settings_path(); std::string settings_path();
@@ -24,6 +27,9 @@ namespace SystemToolkit
// extract the base file name from a full URI (e.g. file:://home/me/toto.mpg -> toto) // extract the base file name from a full URI (e.g. file:://home/me/toto.mpg -> toto)
std::string base_filename(const std::string& uri); std::string base_filename(const std::string& uri);
// extract the path of a file from a full URI (e.g. file:://home/me/toto.mpg -> file:://home/me/)
std::string path_filename(const std::string& uri);
// true of file exists // true of file exists
bool file_exists(const std::string& path); bool file_exists(const std::string& path);

View File

@@ -57,9 +57,9 @@ void ShowAboutOpengl(bool* p_open);
void ShowAbout(bool* p_open); void ShowAbout(bool* p_open);
// static objects for multithreaded file dialog // static objects for multithreaded file dialog
static bool FileDialogPending_ = false; static std::atomic<bool> FileDialogPending_ = false;
static bool FileDialogLoadFinished_ = false; static std::atomic<bool> FileDialogLoadFinished_ = false;
static bool FileDialogSaveFinished_ = false; static std::atomic<bool> FileDialogSaveFinished_ = false;
static std::string FileDialogFilename_ = ""; static std::string FileDialogFilename_ = "";
static void FileDialogOpen(std::string path) static void FileDialogOpen(std::string path)
@@ -67,19 +67,15 @@ static void FileDialogOpen(std::string path)
FileDialogPending_ = true; FileDialogPending_ = true;
FileDialogLoadFinished_ = false; FileDialogLoadFinished_ = false;
char const * lTheOpenFileName; char const * open_file_name;
char const * lFilterPatterns[2] = { "*.vmx" }; char const * open_pattern[1] = { "*.vmx" };
lTheOpenFileName = tinyfd_openFileDialog( "Open a session file", path.c_str(), 1, lFilterPatterns, nullptr, 0); open_file_name = tinyfd_openFileDialog( "Open a session file", path.c_str(), 1, open_pattern, nullptr, 0);
if (!lTheOpenFileName) if (!open_file_name)
{
FileDialogFilename_ = ""; FileDialogFilename_ = "";
}
else else
{ FileDialogFilename_ = std::string( open_file_name );
FileDialogFilename_ = std::string( lTheOpenFileName );
}
FileDialogLoadFinished_ = true; FileDialogLoadFinished_ = true;
} }
@@ -90,14 +86,12 @@ static void FileDialogSave(std::string path)
FileDialogSaveFinished_ = false; FileDialogSaveFinished_ = false;
char const * save_file_name; char const * save_file_name;
char const * save_pattern[2] = { "*.vmx" }; char const * save_pattern[1] = { "*.vmx" };
save_file_name = tinyfd_saveFileDialog( "Save a session file", path.c_str(), 1, save_pattern, "vimix session"); save_file_name = tinyfd_saveFileDialog( "Save a session file", path.c_str(), 1, save_pattern, "vimix session");
if (!save_file_name) if (!save_file_name)
{
FileDialogFilename_ = ""; FileDialogFilename_ = "";
}
else else
{ {
FileDialogFilename_ = std::string( save_file_name ); FileDialogFilename_ = std::string( save_file_name );
@@ -191,7 +185,7 @@ void UserInterface::handleKeyboard()
} }
else if (ImGui::IsKeyPressed( GLFW_KEY_O )) { else if (ImGui::IsKeyPressed( GLFW_KEY_O )) {
// Open session // Open session
std::thread (FileDialogOpen, "./").detach(); std::thread (FileDialogOpen, Settings::application.recentSessions.path).detach();
navigator.hidePannel(); navigator.hidePannel();
} }
else if (ImGui::IsKeyPressed( GLFW_KEY_S )) { else if (ImGui::IsKeyPressed( GLFW_KEY_S )) {
@@ -341,20 +335,36 @@ void UserInterface::NewFrame()
handleKeyboard(); handleKeyboard();
handleMouse(); handleMouse();
// navigator bar first
navigator.Render();
// handle FileDialog // handle FileDialog
if (FileDialogLoadFinished_) { if (FileDialogLoadFinished_) {
FileDialogLoadFinished_ = false; FileDialogLoadFinished_ = false;
FileDialogPending_ = false; FileDialogPending_ = false;
Mixer::manager().open(FileDialogFilename_); if (!FileDialogFilename_.empty()) {
Mixer::manager().open(FileDialogFilename_);
Settings::application.recentSessions.path = SystemToolkit::path_filename(FileDialogFilename_);
}
} }
if (FileDialogSaveFinished_) { if (FileDialogSaveFinished_) {
FileDialogSaveFinished_ = false; FileDialogSaveFinished_ = false;
FileDialogPending_ = false; FileDialogPending_ = false;
Mixer::manager().saveas(FileDialogFilename_); if (!FileDialogFilename_.empty()) {
Mixer::manager().saveas(FileDialogFilename_);
Settings::application.recentSessions.path = SystemToolkit::path_filename(FileDialogFilename_);
}
} }
// overlay when disabled
if (FileDialogPending_){
ImGui::OpenPopup("Busy");
if (ImGui::BeginPopupModal("Busy", NULL, ImGuiWindowFlags_AlwaysAutoResize))
{
ImGui::Text("Close dialog to resume...");
ImGui::EndPopup();
}
}
// navigator bar first
navigator.Render();
} }
void UserInterface::Render() void UserInterface::Render()
@@ -415,20 +425,20 @@ void UserInterface::Terminate()
void UserInterface::showMenuFile() void UserInterface::showMenuFile()
{ {
if (ImGui::MenuItem( ICON_FA_FILE " New", "Ctrl+W", false, !FileDialogPending_)) { if (ImGui::MenuItem( ICON_FA_FILE " New", "Ctrl+W")) {
Mixer::manager().newSession(); Mixer::manager().newSession();
navigator.hidePannel(); navigator.hidePannel();
} }
if (ImGui::MenuItem( ICON_FA_FILE_UPLOAD " Open", "Ctrl+O", false, !FileDialogPending_)) { if (ImGui::MenuItem( ICON_FA_FILE_UPLOAD " Open", "Ctrl+O")) {
// launch file dialog to open a session file // launch file dialog to open a session file
std::thread (FileDialogOpen, "./").detach(); std::thread (FileDialogOpen, Settings::application.recentSessions.path).detach();
navigator.hidePannel(); navigator.hidePannel();
} }
if (ImGui::MenuItem( ICON_FA_FILE_DOWNLOAD " Save", "Ctrl+S", false, !FileDialogPending_)) { if (ImGui::MenuItem( ICON_FA_FILE_DOWNLOAD " Save", "Ctrl+S")) {
Mixer::manager().save(); Mixer::manager().save();
navigator.hidePannel(); navigator.hidePannel();
} }
if (ImGui::MenuItem( ICON_FA_FOLDER_OPEN " Save as", NULL, false, !FileDialogPending_)) { if (ImGui::MenuItem( ICON_FA_FOLDER_OPEN " Save as")) {
std::thread (FileDialogSave, "./").detach(); std::thread (FileDialogSave, "./").detach();
navigator.hidePannel(); navigator.hidePannel();
} }
@@ -1049,7 +1059,8 @@ void Navigator::RenderMainPannel()
{ {
std::for_each(Settings::application.recentSessions.filenames.begin(), std::for_each(Settings::application.recentSessions.filenames.begin(),
Settings::application.recentSessions.filenames.end(), [](std::string& filename) { Settings::application.recentSessions.filenames.end(), [](std::string& filename) {
if (ImGui::Selectable( filename.substr( filename.size() - 40 ).c_str() )) { int right = MIN( 40, filename.size());
if (ImGui::Selectable( filename.substr( filename.size() - right ).c_str() )) {
Mixer::manager().open( filename ); Mixer::manager().open( filename );
recentselected = true; recentselected = true;
} }