From 55d09c27f3dd81bdf79c9d43303f5412280938f9 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Sat, 4 Dec 2021 06:26:28 -0800 Subject: [PATCH] Added ability to import multiple files (#2439) * allow import multiple files at once * added bool allow_multiple_selection for backwards compatability * made dialog_open_fil_ext a private method * all imported files selected in layers list --- synfig-studio/src/gui/app.cpp | 40 +++++++++++++++++++++------- synfig-studio/src/gui/app.h | 2 ++ synfig-studio/src/gui/canvasview.cpp | 13 ++++++--- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/synfig-studio/src/gui/app.cpp b/synfig-studio/src/gui/app.cpp index 54fbaffcb87..e695ddee12c 100644 --- a/synfig-studio/src/gui/app.cpp +++ b/synfig-studio/src/gui/app.cpp @@ -2343,7 +2343,7 @@ gint Signal_Open_Ok (GtkWidget */*widget*/, int *val){*val=1; return 0;} gint Signal_Open_Cancel(GtkWidget */*widget*/, int *val){*val=2; return 0;} bool -App::dialog_open_file(const std::string &title, std::string &filename, std::string preference) +App::dialog_open_file_ext(const std::string &title, std::vector &filenames, std::string preference, bool allow_multiple_selection) { // info("App::dialog_open_file('%s', '%s', '%s')", title.c_str(), filename.c_str(), preference.c_str()); // TODO: Win32 native dialod not ready yet @@ -2402,6 +2402,7 @@ App::dialog_open_file(const std::string &title, std::string &filename, std::stri dialog->set_current_folder(prev_path); dialog->add_button(_("Cancel"), Gtk::RESPONSE_CANCEL)->set_image_from_icon_name("gtk-cancel", Gtk::ICON_SIZE_BUTTON); dialog->add_button(_("Import"), Gtk::RESPONSE_ACCEPT)->set_image_from_icon_name("gtk-open", Gtk::ICON_SIZE_BUTTON); + dialog->set_select_multiple(allow_multiple_selection); // 0 All supported files // 0.1 Synfig documents. sfg is not supported to import @@ -2503,29 +2504,48 @@ App::dialog_open_file(const std::string &title, std::string &filename, std::stri dialog->add_filter(filter_video); dialog->add_filter(filter_lipsync); dialog->add_filter(filter_any); - + dialog->set_extra_widget(*scale_imported_box()); - if (filename.empty()) + if (filenames.empty()) dialog->set_filename(prev_path); - else if (is_absolute_path(filename)) - dialog->set_filename(filename); + else if (is_absolute_path(filenames.front())) + dialog->set_filename(filenames.front()); else - dialog->set_filename(prev_path + ETL_DIRECTORY_SEPARATOR + filename); + dialog->set_filename(prev_path + ETL_DIRECTORY_SEPARATOR + filenames.front()); if(dialog->run() == Gtk::RESPONSE_ACCEPT) { - filename = dialog->get_filename(); - // info("Saving preference %s = '%s' in App::dialog_open_file()", preference.c_str(), dirname(filename).c_str()); - _preferences.set_value(preference, dirname(filename)); + filenames = dialog->get_filenames(); + if(!filenames.empty()){ + // info("Saving preference %s = '%s' in App::dialog_open_file()", preference.c_str(), dirname(filename).c_str()); + _preferences.set_value(preference, dirname(filenames.front())); + } delete dialog; return true; } - delete dialog; return false; #endif // not USE_WIN32_FILE_DIALOGS } +bool +App::dialog_open_file(const std::string &title, std::string &filename, std::string preference) +{ + std::vector filenames; + if (!filename.empty()) + filenames.push_back(filename); + if(dialog_open_file_ext(title, filenames, preference, false)) { + filename = filenames.front(); + return true; + } + return false; +} + +bool App::dialog_open_file(const std::string &title, std::vector &filenames, std::string preference) +{ + return dialog_open_file_ext(title, filenames, preference, true); +} + bool App::dialog_open_file_spal(const std::string &title, std::string &filename, std::string preference) { diff --git a/synfig-studio/src/gui/app.h b/synfig-studio/src/gui/app.h index 498ce6a9106..aa145312510 100644 --- a/synfig-studio/src/gui/app.h +++ b/synfig-studio/src/gui/app.h @@ -307,6 +307,7 @@ class App : public Gtk::Main, private IconController private: static void add_recent_file(const std::string &filename, bool emit_signal); + static bool dialog_open_file_ext(const std::string &title, std::vector &filenames, std::string preference, bool allow_multiple_selection); /* -- ** -- P U B L I C M E T H O D S ----------------------------------------- @@ -416,6 +417,7 @@ class App : public Gtk::Main, private IconController static bool dialog_select_importer(const std::string& filename, std::string& plugin); static bool dialog_open_file(const std::string &title, std::string &filename, std::string preference); + static bool dialog_open_file(const std::string &title, std::vector &filenames, std::string preference); static bool dialog_open_file_spal(const std::string &title, std::string &filename, std::string preference); static bool dialog_open_file_sketch(const std::string &title, std::string &filename, std::string preference); static bool dialog_open_file_image(const std::string &title, std::string &filename, std::string preference); diff --git a/synfig-studio/src/gui/canvasview.cpp b/synfig-studio/src/gui/canvasview.cpp index 8ced36207e7..3d016f928db 100644 --- a/synfig-studio/src/gui/canvasview.cpp +++ b/synfig-studio/src/gui/canvasview.cpp @@ -3570,10 +3570,13 @@ void CanvasView::import_file() { // String filename(dirname(get_canvas()->get_file_name())); - String filename("*.*"); + std::vector filenames; + LayerTree::LayerList layers; + filenames.push_back("*.*"); String errors, warnings; - if(App::dialog_open_file(_("Please select files"), filename, IMAGE_DIR_PREFERENCE)) + if(App::dialog_open_file(_("Please select files"), filenames, IMAGE_DIR_PREFERENCE)) { + for(const std::string &filename : filenames){ // Don't let user import a file to itself // Check if it's the same file of this canvas { @@ -3621,10 +3624,12 @@ CanvasView::import_file() _("Close")); if (layer) { - get_selection_manager()->clear_selected_layers(); - get_selection_manager()->set_selected_layer(layer); + layers.push_back(layer); } } + get_selection_manager()->clear_selected_layers(); + get_selection_manager()->set_selected_layers(layers); + } } void