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

@ -13,8 +13,6 @@ sub new {
my $class = shift;
my ($parent, %params) = @_;
my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxBK_LEFT | wxTAB_TRAVERSAL);
$self->{options} = []; # array of option names handled by this tab
$self->{$_} = $params{$_} for qw(on_value_change);
$self->SetScrollbars(1, 1, 1, 1);
@ -34,29 +32,26 @@ sub new {
return $self;
}
sub append_optgroup {
my $self = shift;
my %params = @_;
sub init_config_options {
my ($self, @opt_keys) = @_;
$self->{config}->apply(Slic3r::Config->new_from_defaults(@opt_keys));
}
sub new_optgroup {
my ($self, $title, %params) = @_;
# apply default values
{
my @options = @{$params{options}};
$_ =~ s/#.+// for @options;
my $config = Slic3r::Config->new_from_defaults(@options);
$self->{config}->apply($config);
}
my $class = $params{class} || 'Slic3r::GUI::ConfigOptionsGroup';
my $optgroup = $class->new(
parent => $self,
config => $self->{config},
label_width => 200,
on_change => sub { $self->on_value_change(@_) },
%params,
my $optgroup = Slic3r::GUI::ConfigOptionsGroup->new(
parent => $self,
title => $title,
config => $self->{config},
label_width => $params{label_width} // 200,
on_change => sub { $self->_on_value_change(@_) },
);
push @{$self->{optgroups}}, $optgroup;
($params{sizer} || $self->{vsizer})->Add($optgroup->sizer, 0, wxEXPAND | wxALL, 10);
$self->{vsizer}->Add($optgroup->sizer, 0, wxEXPAND | wxALL, 10);
return $optgroup;
}
sub load_config_file {
@ -71,29 +66,27 @@ sub load_config {
my $self = shift;
my ($config) = @_;
foreach my $opt_key (grep $self->{config}->has($_), @{$config->get_keys}) {
my $value = $config->get($opt_key);
$self->{config}->set($opt_key, $value);
$_->set_value($opt_key, $value) for @{$self->{optgroups}};
foreach my $opt_key (@{$self->{config}->get_keys}) {
next unless $config->has($opt_key);
$self->{config}->set($opt_key, $config->get($opt_key));
}
$_->reload_config for @{$self->{optgroups}};
}
sub set_value {
my $self = shift;
my ($opt_key, $value) = @_;
my $changed = 0;
foreach my $optgroup (@{$self->{optgroups}}) {
$changed = 1 if $optgroup->set_value($opt_key, $value);
}
return $changed;
}
sub load_presets {}
sub is_dirty { 0 }
sub config { $_[0]->{config}->clone }
# propagate event to the parent
sub on_value_change {
my ($self, $cb) = @_;
$self->{on_value_change} = $cb;
}
sub on_presets_changed {}
# propagate event to the parent
sub _on_value_change {
my $self = shift;
$self->{on_value_change}->(@_) if $self->{on_value_change};
}
@ -109,50 +102,63 @@ sub title { 'Print Settings' }
sub build {
my $self = shift;
$self->append_optgroup(
title => 'General',
options => [qw(layer_height perimeters top_solid_layers bottom_solid_layers)],
lines => [
Slic3r::GUI::OptionsGroup->single_option_line('layer_height'),
Slic3r::GUI::OptionsGroup->single_option_line('perimeters'),
{
label => 'Solid layers',
options => [qw(top_solid_layers bottom_solid_layers)],
},
],
);
$self->init_config_options(qw(
layer_height perimeters top_solid_layers bottom_solid_layers
fill_density fill_pattern support_material support_material_spacing raft_layers
perimeter_speed infill_speed travel_speed
brim_width
complete_objects extruder_clearance_radius extruder_clearance_height
));
$self->append_optgroup(
title => 'Infill',
options => [qw(fill_density fill_pattern)],
);
{
my $optgroup = $self->new_optgroup('General');
$optgroup->append_single_option_line('layer_height');
$optgroup->append_single_option_line('perimeters');
my $line = Slic3r::GUI::OptionsGroup::Line->new(
label => 'Solid layers',
);
$line->append_option($optgroup->get_option('top_solid_layers'));
$line->append_option($optgroup->get_option('bottom_solid_layers'));
$optgroup->append_line($line);
}
$self->append_optgroup(
title => 'Support material',
options => [qw(support_material support_material_spacing raft_layers)],
);
{
my $optgroup = $self->new_optgroup('Infill');
$optgroup->append_single_option_line('fill_density');
$optgroup->append_single_option_line('fill_pattern');
}
$self->append_optgroup(
title => 'Speed',
options => [qw(perimeter_speed infill_speed travel_speed)],
);
{
my $optgroup = $self->new_optgroup('Support material');
$optgroup->append_single_option_line('support_material');
$optgroup->append_single_option_line('support_material_spacing');
$optgroup->append_single_option_line('raft_layers');
}
$self->append_optgroup(
title => 'Brim',
options => [qw(brim_width)],
);
{
my $optgroup = $self->new_optgroup('Speed');
$optgroup->append_single_option_line('perimeter_speed');
$optgroup->append_single_option_line('infill_speed');
$optgroup->append_single_option_line('travel_speed');
}
$self->append_optgroup(
title => 'Sequential printing',
options => [qw(complete_objects extruder_clearance_radius extruder_clearance_height)],
lines => [
Slic3r::GUI::OptionsGroup->single_option_line('complete_objects'),
{
label => 'Extruder clearance (mm)',
options => [qw(extruder_clearance_radius extruder_clearance_height)],
},
],
);
{
my $optgroup = $self->new_optgroup('Brim');
$optgroup->append_single_option_line('brim_width');
}
{
my $optgroup = $self->new_optgroup('Sequential printing');
$optgroup->append_single_option_line('complete_objects');
my $line = Slic3r::GUI::OptionsGroup::Line->new(
label => 'Extruder clearance (mm)',
);
$line->append_option($optgroup->get_option('extruder_clearance_radius'));
$line->append_option($optgroup->get_option('extruder_clearance_height'));
$optgroup->append_line($line);
}
}
package Slic3r::GUI::SimpleTab::Filament;
@ -164,25 +170,38 @@ sub title { 'Filament Settings' }
sub build {
my $self = shift;
$self->append_optgroup(
title => 'Filament',
options => ['filament_diameter#0', 'extrusion_multiplier#0'],
);
$self->init_config_options(qw(
filament_diameter extrusion_multiplier
temperature first_layer_temperature bed_temperature first_layer_bed_temperature
));
$self->append_optgroup(
title => 'Temperature (°C)',
options => ['temperature#0', 'first_layer_temperature#0', qw(bed_temperature first_layer_bed_temperature)],
lines => [
{
label => 'Extruder',
options => ['first_layer_temperature#0', 'temperature#0'],
},
{
label => 'Bed',
options => [qw(first_layer_bed_temperature bed_temperature)],
},
],
);
{
my $optgroup = $self->new_optgroup('Filament');
$optgroup->append_single_option_line('filament_diameter', 0);
$optgroup->append_single_option_line('extrusion_multiplier', 0);
}
{
my $optgroup = $self->new_optgroup('Temperature (°C)');
{
my $line = Slic3r::GUI::OptionsGroup::Line->new(
label => 'Extruder',
);
$line->append_option($optgroup->get_option('first_layer_temperature', 0));
$line->append_option($optgroup->get_option('temperature', 0));
$optgroup->append_line($line);
}
{
my $line = Slic3r::GUI::OptionsGroup::Line->new(
label => 'Bed',
);
$line->append_option($optgroup->get_option('first_layer_bed_temperature'));
$line->append_option($optgroup->get_option('bed_temperature'));
$optgroup->append_line($line);
}
}
}
package Slic3r::GUI::SimpleTab::Printer;
@ -194,37 +213,56 @@ sub title { 'Printer Settings' }
sub build {
my $self = shift;
$self->append_optgroup(
title => 'Size and coordinates',
options => [qw(bed_size z_offset)],
);
$self->init_config_options(qw(
z_offset
gcode_flavor
nozzle_diameter
retract_length retract_lift
start_gcode
end_gcode
));
$self->append_optgroup(
title => 'Firmware',
options => [qw(gcode_flavor)],
);
{
my $optgroup = $self->new_optgroup('Size and coordinates');
# TODO: add bed_shape
$optgroup->append_single_option_line('z_offset');
}
$self->append_optgroup(
title => 'Extruder',
options => ['nozzle_diameter#0'],
);
{
my $optgroup = $self->new_optgroup('Firmware');
$optgroup->append_single_option_line('gcode_flavor');
}
$self->append_optgroup(
title => 'Retraction',
options => ['retract_length#0', 'retract_lift#0'],
);
{
my $optgroup = $self->new_optgroup('Extruder');
$optgroup->append_single_option_line('nozzle_diameter', 0);
}
$self->append_optgroup(
title => 'Start G-code',
no_labels => 1,
options => [qw(start_gcode)],
);
{
my $optgroup = $self->new_optgroup('Retraction');
$optgroup->append_single_option_line('retract_length', 0);
$optgroup->append_single_option_line('retract_lift', 0);
}
$self->append_optgroup(
title => 'End G-code',
no_labels => 1,
options => [qw(end_gcode)],
);
{
my $optgroup = $self->new_optgroup('Start G-code',
label_width => 0,
);
my $option = $optgroup->get_option('start_gcode');
$option->full_width(1);
$option->height(150);
$optgroup->append_single_option_line($option);
}
{
my $optgroup = $self->new_optgroup('End G-code',
label_width => 0,
);
my $option = $optgroup->get_option('end_gcode');
$option->full_width(1);
$option->height(150);
$optgroup->append_single_option_line($option);
}
}
1;