From 474bed0eeffffc27d5e2b4f4eccf5e60eb4d1ca8 Mon Sep 17 00:00:00 2001
From: Tristan Walter <twalter@orn.mpg.de>
Date: Tue, 27 Oct 2020 17:50:00 +0100
Subject: [PATCH] fixing crashes here and there

---
 .../src/commons/common/gui/FileChooser.cpp    | 21 +++++-----
 .../src/commons/common/gui/FileChooser.h      |  3 +-
 Application/src/tracker/VideoOpener.cpp       | 35 ++++++++++------
 Application/src/tracker/VideoOpener.h         |  7 ++--
 Application/src/tracker/main.cpp              | 40 ++++++++++---------
 5 files changed, 62 insertions(+), 44 deletions(-)

diff --git a/Application/src/commons/common/gui/FileChooser.cpp b/Application/src/commons/common/gui/FileChooser.cpp
index 4508f86..5cf4509 100644
--- a/Application/src/commons/common/gui/FileChooser.cpp
+++ b/Application/src/commons/common/gui/FileChooser.cpp
@@ -193,16 +193,19 @@ void FileChooser::set_tabs(const std::vector<Settings>& tabs) {
         _tabs_bar = nullptr;
     }
     
-    if(_tabs_bar) {
-        _rows->set_children(std::vector<Layout::Ptr>{
-            _description, _textfield, _list
-        });
-    } else {
-        _rows->set_children(std::vector<Layout::Ptr>{
-            _description, _textfield, _list
-        });
+    std::vector<Layout::Ptr> childs;
+    if(_tabs_bar)
+        childs.push_back(_tabs_bar);
+    
+    childs.push_back(_columns);
+    
+    if(_selected_text && _button) {
+        childs.push_back(_selected_text);
+        childs.push_back(_button);
     }
     
+    _overall->set_children(childs);
+    
     if(!_tabs.empty())
         set_tab(tabs.front().name);
     else
@@ -359,7 +362,7 @@ void FileChooser::update_size() {
     if(_tabs_bar) _tabs_bar->auto_size(Margin{0,0});
     if(_tabs_bar) _tabs_bar->update_layout();
     
-    float left_column_height = _graph->height() - 70 - 10 - (_overall->children().size() > 1 ? _button->height() + 10 : 0) - (_tabs_bar ? _tabs_bar->height() + 10 : 0) - (_selected_text && !_selected_file.empty() ? _selected_text->height() : 0);
+    float left_column_height = _graph->height() - 70 - 10 - (!_selected_file.empty() ? _button->height() + 10 : 0) - (_tabs_bar ? _tabs_bar->height() + 10 : 0) - (_selected_text && !_selected_file.empty() ? _selected_text->height() : 0);
     _list->set_bounds(Bounds(0, 0, left_column_width, left_column_height - 70));
     
     _textfield->set_bounds(Bounds(0, 0, left_column_width, 30));
diff --git a/Application/src/commons/common/gui/FileChooser.h b/Application/src/commons/common/gui/FileChooser.h
index 94ac55f..f67bd30 100644
--- a/Application/src/commons/common/gui/FileChooser.h
+++ b/Application/src/commons/common/gui/FileChooser.h
@@ -60,7 +60,8 @@ protected:
     bool _running;
     
     std::set<file::Path, std::function<bool(const file::Path&, const file::Path&)>> _files;
-    file::Path _selected_file, _confirmed_file;
+    file::Path _selected_file;
+    GETTER(file::Path, confirmed_file)
     std::function<void(const file::Path&, std::string)> _callback, _on_select_callback;
     std::function<void(DrawStructure&)> _on_update;
     std::function<bool(file::Path)> _validity;
diff --git a/Application/src/tracker/VideoOpener.cpp b/Application/src/tracker/VideoOpener.cpp
index b050c2b..a9eb886 100644
--- a/Application/src/tracker/VideoOpener.cpp
+++ b/Application/src/tracker/VideoOpener.cpp
@@ -43,27 +43,37 @@ VideoOpener::VideoOpener() {
     _text_fields["threshold"] = LabeledField("threshold");
     _text_fields["threshold"]._text_field->set_text(SETTING(threshold).get().valueString());
     _text_fields["threshold"]._text_field->on_text_changed([this](){
-        auto number = Meta::fromStr<int>(_text_fields["threshold"]._text_field->text());
-        SETTING(threshold) = number;
-        
-        if(_buffer) {
-            _buffer->_threshold = number;
+        try {
+            auto number = Meta::fromStr<int>(_text_fields["threshold"]._text_field->text());
+            SETTING(threshold) = number;
+            
+            if(_buffer) {
+                _buffer->_threshold = number;
+            }
+            
+        } catch(...) {
+            
         }
     });
     _text_fields["average_samples"] = LabeledField("average_samples");
     _text_fields["average_samples"]._text_field->set_text(SETTING(average_samples).get().valueString());
     _text_fields["average_samples"]._text_field->on_text_changed([this](){
-        auto number = Meta::fromStr<int>(_text_fields["average_samples"]._text_field->text());
-        SETTING(average_samples) = number;
-        
-        if(_buffer) {
-            _buffer->restart_background();
+        try {
+            auto number = Meta::fromStr<int>(_text_fields["average_samples"]._text_field->text());
+            SETTING(average_samples) = number;
+            
+            if(_buffer) {
+                _buffer->restart_background();
+            }
+        } catch(...) {
+            
         }
     });
     
     std::vector<Layout::Ptr> objects{};
     for(auto &[key, ptr] : _text_fields) {
-        objects.push_back(ptr._joint);
+        objects.push_back(ptr._text);
+        objects.push_back(ptr._text_field);
     }
     
     _raw_settings->set_children(objects);
@@ -146,10 +156,11 @@ VideoOpener::VideoOpener() {
                 _result.extra_command_lines = str;
             _result.tab = _file_chooser->current_tab();
             _result.tab.content = nullptr;
+            _result.selected_file = _file_chooser->confirmed_file();
             
             if(_result.tab.extension == "pv") {
                 // PV file, no need to add cmd
-            } else {
+            } else if(!_result.selected_file.empty()) {
                 _result.cmd = "-i '" + path.str() + "' " + "-o '"+SETTING(output_name).value<file::Path>().str()+"' -threshold "+SETTING(threshold).get().valueString()+" -average_samples "+SETTING(average_samples).get().valueString()+ " -reset_average";
             }
             
diff --git a/Application/src/tracker/VideoOpener.h b/Application/src/tracker/VideoOpener.h
index 0b5050b..a680da7 100644
--- a/Application/src/tracker/VideoOpener.h
+++ b/Application/src/tracker/VideoOpener.h
@@ -16,6 +16,7 @@ public:
         std::string load_results_from;
         std::string cmd;
         FileChooser::Settings tab;
+        file::Path selected_file;
         bool load_results;
         
         Result() : load_results(false) {}
@@ -76,12 +77,12 @@ public:
     struct LabeledField {
         gui::derived_ptr<gui::Text> _text;
         gui::derived_ptr<gui::Textfield> _text_field;
-        gui::derived_ptr<gui::HorizontalLayout> _joint;
+        //gui::derived_ptr<gui::HorizontalLayout> _joint;
         
         LabeledField(const std::string& name = "")
             : _text(std::make_shared<gui::Text>(name)),
-              _text_field(std::make_shared<gui::Textfield>("", Bounds(0, 0, 500 - _text->width() - 80, 33))),
-              _joint(std::make_shared<gui::HorizontalLayout>(std::vector<Layout::Ptr>{_text, _text_field}))
+              _text_field(std::make_shared<gui::Textfield>("", Bounds(0, 0, 400, 33)))
+              //_joint(std::make_shared<gui::HorizontalLayout>(std::vector<Layout::Ptr>{_text, _text_field}))
         {
             _text->set_font(Font(0.75, Style::Bold));
             _text->set_color(White);
diff --git a/Application/src/tracker/main.cpp b/Application/src/tracker/main.cpp
index acc4f26..04175cc 100644
--- a/Application/src/tracker/main.cpp
+++ b/Application/src/tracker/main.cpp
@@ -526,25 +526,27 @@ int main(int argc, char** argv)
         if((GlobalSettings::map().has("nowindow") ? SETTING(nowindow).value<bool>() : false) == false) {
             gui::VideoOpener opener;
             opening_result = opener._result;
-            
-            if(opening_result.tab.extension == "pv") {
-                if(opening_result.load_results)
-                    load_results = true;
-                if(!opening_result.load_results_from.empty())
-                    load_results_from = opening_result.load_results_from;
-            } else {
-                auto wd = SETTING(wd).value<file::Path>();
-                Debug("Opening a video file: '%S', '%S'", &opening_result.tab.name, &wd.str());
-#if defined(__APPLE__)
-                wd = wd / ".." / ".." / ".." / "TGrabs.app" / "Contents" / "MacOS" / "TGrabs";
-#else
-                wd = wd / "tgrabs";
-#endif
-                auto exec = wd.str() + " " + opening_result.cmd;
-                Debug("Executing '%S'", &exec);
-                file::exec(exec.c_str());
-                exit(0);
-            }
+            if(!opening_result.selected_file.empty()) {
+                if(opening_result.tab.extension == "pv") {
+                    if(opening_result.load_results)
+                        load_results = true;
+                    if(!opening_result.load_results_from.empty())
+                        load_results_from = opening_result.load_results_from;
+                } else {
+                    auto wd = SETTING(wd).value<file::Path>();
+                    Debug("Opening a video file: '%S', '%S'", &opening_result.tab.name, &wd.str());
+    #if defined(__APPLE__)
+                    wd = wd / ".." / ".." / ".." / "TGrabs.app" / "Contents" / "MacOS" / "TGrabs";
+    #else
+                    wd = wd / "tgrabs";
+    #endif
+                    auto exec = wd.str() + " " + opening_result.cmd;
+                    Debug("Executing '%S'", &exec);
+                    file::exec(exec.c_str());
+                    exit(0);
+                }
+            } else
+                SETTING(filename) = file::Path();
         }
         
         if(SETTING(filename).value<Path>().empty()) {
-- 
GitLab