Skip to content

Commit

Permalink
Added ability to import multiple files (#2439)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nickleus27 committed Dec 4, 2021
1 parent 9c2796c commit 55d09c2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
40 changes: 30 additions & 10 deletions synfig-studio/src/gui/app.cpp
Expand Up @@ -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<std::string> &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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<std::string> 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<std::string> &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)
{
Expand Down
2 changes: 2 additions & 0 deletions synfig-studio/src/gui/app.h
Expand Up @@ -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<std::string> &filenames, std::string preference, bool allow_multiple_selection);

/*
-- ** -- P U B L I C M E T H O D S -----------------------------------------
Expand Down Expand Up @@ -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<std::string> &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);
Expand Down
13 changes: 9 additions & 4 deletions synfig-studio/src/gui/canvasview.cpp
Expand Up @@ -3570,10 +3570,13 @@ void
CanvasView::import_file()
{
// String filename(dirname(get_canvas()->get_file_name()));
String filename("*.*");
std::vector<std::string> 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
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 55d09c2

Please sign in to comment.