From b11d9708edd7ad4041c2a91b865430047628a6f8 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Thu, 2 Nov 2017 21:51:06 +0100 Subject: [PATCH] Updated Controller after the presets C++ port. --- lib/Slic3r/GUI/Controller.pm | 53 ++++++++++++++---------------------- lib/Slic3r/GUI/MainFrame.pm | 4 +-- lib/Slic3r/GUI/Plater.pm | 2 +- xs/xsp/Config.xsp | 2 ++ xs/xsp/GUI_Preset.xsp | 13 --------- 5 files changed, 26 insertions(+), 48 deletions(-) diff --git a/lib/Slic3r/GUI/Controller.pm b/lib/Slic3r/GUI/Controller.pm index 6f2636d20..6aa7b34cb 100644 --- a/lib/Slic3r/GUI/Controller.pm +++ b/lib/Slic3r/GUI/Controller.pm @@ -10,6 +10,7 @@ use utf8; use Wx qw(wxTheApp :frame :id :misc :sizer :bitmap :button :icon :dialog); use Wx::Event qw(EVT_CLOSE EVT_LEFT_DOWN EVT_MENU); use base qw(Wx::ScrolledWindow Class::Accessor); +use List::Util qw(first); __PACKAGE__->mk_accessors(qw(_selected_printer_preset)); @@ -38,21 +39,19 @@ sub new { if $btn->can('SetToolTipString'); EVT_LEFT_DOWN($btn, sub { - my $menu = Wx::Menu->new; - my $presets = wxTheApp->{preset_bundle}->printer->presets_hash; - + my $menu = Wx::Menu->new; + my @panels = $self->print_panels; # remove printers that already exist - my @panels = $self->print_panels; - delete $presets->{$_} for map $_->printer_name, @panels; - - foreach my $preset_name (sort keys %{$presets}) { - my $config = Slic3r::Config->load($presets->{$preset_name}); - next if !$config->serial_port; - + # update configs of currently loaded print panels + foreach my $preset (@{wxTheApp->{preset_bundle}->printer}) { + my $preset_name = $preset->name; + next if ! $preset->config->serial_port || + defined first { defined $_ && $_->printer_name eq $preset_name } @panels; + my $myconfig = $preset->config->clone_only(\@ConfigOptions); my $id = &Wx::NewId(); $menu->Append($id, $preset_name); EVT_MENU($menu, $id, sub { - $self->add_printer($preset_name, $config); + $self->add_printer($preset_name, $myconfig); }); } $self->PopupMenu($menu, $btn->GetPosition); @@ -98,12 +97,8 @@ sub OnActivate { my ($self) = @_; # get all available presets - my %presets = (); - { - my $all = wxTheApp->{preset_bundle}->printer->presets_hash; - my %configs = map { my $name = $_; $name => Slic3r::Config->load($all->{$name}) } keys %{$all}; - %presets = map { $_ => $configs{$_} } grep $configs{$_}->serial_port, keys %{$all}; - } + my %presets = map { $_->name => $_->config->clone_only(\@ConfigOptions) } + grep { $_->config->serial_port } @{wxTheApp->{preset_bundle}->printer}; # decide which ones we want to keep my %active = (); @@ -177,25 +172,19 @@ sub print_panels { map $_->GetWindow, $self->{sizer}->GetChildren; } -# Called by -# Slic3r::GUI::Tab::Print::_on_presets_changed -# Slic3r::GUI::Tab::Filament::_on_presets_changed -# Slic3r::GUI::Tab::Printer::_on_presets_changed -# when the presets are loaded or the user select another preset. +# Called by Slic3r::GUI::Tab::Printer::_on_presets_changed +# when the presets are loaded or the user selects another preset. sub update_presets { - my ($self, $group, $presets, $default_suppressed, $selected, $is_dirty) = @_; - + my ($self, $presets) = @_; # update configs of currently loaded print panels + my @presets = @$presets; foreach my $panel ($self->print_panels) { - foreach my $preset (@$presets) { - if ($panel->printer_name eq $preset->name) { - my $config = $preset->config(\@ConfigOptions); - $panel->config->apply($config); - } - } + my $preset = $presets->find_preset($panel->printer_name, 0); + $panel->config($preset->config->clone_only(\@ConfigOptions)) + if defined $preset; } - - $self->_selected_printer_preset($presets->[$selected]->name); + + $self->_selected_printer_preset($presets->get_selected_preset->name); } 1; diff --git a/lib/Slic3r/GUI/MainFrame.pm b/lib/Slic3r/GUI/MainFrame.pm index c7f3f1cf9..836c3e9d0 100644 --- a/lib/Slic3r/GUI/MainFrame.pm +++ b/lib/Slic3r/GUI/MainFrame.pm @@ -128,8 +128,8 @@ sub _init_tabpanel { # Update preset combo boxes (Print settings, Filament, Printer) from their respective tabs. $self->{plater}->update_presets($tab_name, @_); $self->{plater}->on_config_change($tab->{presets}->get_current_preset->config); - if ($self->{controller}) { - $self->{controller}->update_presets($tab_name, @_); + if ($self->{controller} && $tab_name eq 'printer') { + $self->{controller}->update_presets(@_); } } }); diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index 06adbe525..6b853e410 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -1535,7 +1535,7 @@ sub export_amf { sub _get_export_file { my ($self, $format) = @_; my $suffix = $format eq 'STL' ? '.stl' : '.amf.xml'; - my $output_file = $self->{print}->output_filepath($main::opt{output}); + my $output_file = $self->{print}->output_filepath($main::opt{output} // ''); $output_file =~ s/\.[gG][cC][oO][dD][eE]$/$suffix/; my $dlg = Wx::FileDialog->new($self, "Save $format file as:", dirname($output_file), basename($output_file), &Slic3r::GUI::MODEL_WILDCARD, wxFD_SAVE | wxFD_OVERWRITE_PROMPT); diff --git a/xs/xsp/Config.xsp b/xs/xsp/Config.xsp index f78b3bf60..6adfc49a2 100644 --- a/xs/xsp/Config.xsp +++ b/xs/xsp/Config.xsp @@ -11,6 +11,8 @@ static DynamicPrintConfig* new_from_defaults(); static DynamicPrintConfig* new_from_defaults_keys(std::vector keys); DynamicPrintConfig* clone() %code{% RETVAL = new DynamicPrintConfig(*THIS); %}; + DynamicPrintConfig* clone_only(std::vector keys) + %code{% RETVAL = new DynamicPrintConfig(); RETVAL->apply_only(*THIS, keys, true); %}; bool has(t_config_option_key opt_key); SV* as_hash() %code{% RETVAL = ConfigBase__as_hash(THIS); %}; diff --git a/xs/xsp/GUI_Preset.xsp b/xs/xsp/GUI_Preset.xsp index e76b5c704..a084d7f2a 100644 --- a/xs/xsp/GUI_Preset.xsp +++ b/xs/xsp/GUI_Preset.xsp @@ -87,19 +87,6 @@ PresetCollection::arrayref() OUTPUT: RETVAL -SV* -PresetCollection::presets_hash() - CODE: - HV* hv = newHV(); - for (size_t i = 1; i < THIS->size(); ++ i) { - const Slic3r::Preset &preset = THIS->preset(i); - if (! preset.is_default && ! preset.is_external) - (void)hv_store(hv, preset.name.c_str(), - int(preset.name.size()), newSVpvn_utf8(preset.file.c_str(), preset.file.size(), true), 0); - } - RETVAL = (SV*)newRV_noinc((SV*)hv); - OUTPUT: - RETVAL - %} };