Refactored OptionsGroup class for cleaner OOP model and cleaner event model

This commit is contained in:
Alessandro Ranellucci 2014-07-01 16:40:56 +02:00
parent 85c59d78ac
commit 04b67f0cb0
12 changed files with 1609 additions and 1175 deletions

View file

@ -69,8 +69,9 @@ sub _init_tabpanel {
$self->{tabpanel} = my $panel = Wx::Notebook->new($self, -1, wxDefaultPosition, wxDefaultSize, wxNB_TOP | wxTAB_TRAVERSAL);
$panel->AddPage($self->{plater} = Slic3r::GUI::Plater->new($panel), "Plater")
unless $self->{no_plater};
if (!$self->{no_plater}) {
$panel->AddPage($self->{plater} = Slic3r::GUI::Plater->new($panel), "Plater");
}
$self->{options_tabs} = {};
my $simple_config;
@ -82,31 +83,44 @@ sub _init_tabpanel {
my $class_prefix = $self->{mode} eq 'simple' ? "Slic3r::GUI::SimpleTab::" : "Slic3r::GUI::Tab::";
for my $tab_name (qw(print filament printer)) {
my $tab;
$tab = $self->{options_tabs}{$tab_name} = ($class_prefix . ucfirst $tab_name)->new(
$panel,
on_value_change => sub {
$self->{plater}->on_config_change(@_) if $self->{plater}; # propagate config change events to the plater
if ($self->{loaded}) { # don't save while loading for the first time
if ($self->{mode} eq 'simple') {
# save config
$self->config->save("$Slic3r::GUI::datadir/simple.ini");
# save a copy into each preset section
# so that user gets the config when switching to expert mode
$tab->config->save(sprintf "$Slic3r::GUI::datadir/%s/%s.ini", $tab->name, 'Simple Mode');
$Slic3r::GUI::Settings->{presets}{$tab->name} = 'Simple Mode.ini';
wxTheApp->save_settings;
}
$self->config->save($Slic3r::GUI::autosave) if $Slic3r::GUI::autosave;
$tab = $self->{options_tabs}{$tab_name} = ($class_prefix . ucfirst $tab_name)->new($panel);
$tab->on_value_change(sub {
my $config = $tab->config;
$self->{plater}->on_config_change($config) if $self->{plater}; # propagate config change events to the plater
if ($self->{loaded}) { # don't save while loading for the first time
if ($self->{mode} eq 'simple') {
# save config
$self->config->save("$Slic3r::GUI::datadir/simple.ini");
# save a copy into each preset section
# so that user gets the config when switching to expert mode
$config->save(sprintf "$Slic3r::GUI::datadir/%s/%s.ini", $tab->name, 'Simple Mode');
$Slic3r::GUI::Settings->{presets}{$tab->name} = 'Simple Mode.ini';
wxTheApp->save_settings;
}
},
on_presets_changed => sub {
$self->{plater}->update_presets($tab_name, @_) if $self->{plater};
},
);
$self->config->save($Slic3r::GUI::autosave) if $Slic3r::GUI::autosave;
}
});
$tab->on_presets_changed(sub {
if ($self->{plater}) {
$self->{plater}->update_presets($tab_name, @_);
$self->{plater}->on_config_change($tab->config);
}
});
$tab->load_presets;
$panel->AddPage($tab, $tab->title);
$tab->load_config($simple_config) if $simple_config;
}
if ($self->{plater}) {
$self->{plater}->on_select_preset(sub {
my ($group, $preset) = @_;
$self->{options_tabs}{$group}->select_preset($preset);
});
# load initial config
$self->{plater}->on_config_change($self->config);
}
}
sub _init_menubar {
@ -155,11 +169,11 @@ sub _init_menubar {
$fileMenu->AppendSeparator();
$self->_append_menu_item($fileMenu, "Preferences…", 'Application preferences', sub {
Slic3r::GUI::Preferences->new($self)->ShowModal;
});
}, wxID_PREFERENCES);
$fileMenu->AppendSeparator();
$self->_append_menu_item($fileMenu, "&Quit", 'Quit Slic3r', sub {
$self->Close(0);
});
}, wxID_EXIT);
}
# Plater menu
@ -547,7 +561,7 @@ sub load_config {
my ($config) = @_;
foreach my $tab (values %{$self->{options_tabs}}) {
$tab->set_value($_, $config->$_) for @{$config->get_keys};
$tab->load_config($config);
}
}
@ -636,6 +650,11 @@ This method collects all config values from the tabs and merges them into a sing
sub config {
my $self = shift;
return Slic3r::Config->new_from_defaults
if !exists $self->{options_tabs}{print}
|| !exists $self->{options_tabs}{filament}
|| !exists $self->{options_tabs}{printer};
# retrieve filament presets and build a single config object for them
my $filament_config;
if (!$self->{plater} || $self->{plater}->filament_presets == 1 || $self->{mode} eq 'simple') {
@ -682,17 +701,6 @@ sub config {
return $config;
}
sub set_value {
my $self = shift;
my ($opt_key, $value) = @_;
my $changed = 0;
foreach my $tab (values %{$self->{options_tabs}}) {
$changed = 1 if $tab->set_value($opt_key, $value);
}
return $changed;
}
sub check_unsaved_changes {
my $self = shift;
@ -713,9 +721,9 @@ sub select_tab {
}
sub _append_menu_item {
my ($self, $menu, $string, $description, $cb) = @_;
my ($self, $menu, $string, $description, $cb, $id) = @_;
my $id = &Wx::NewId();
$id //= &Wx::NewId();
my $item = $menu->Append($id, $string, $description);
EVT_MENU($self, $id, $cb);
return $item;