PrusaSlicer-NonPlainar/lib/Slic3r/GUI/OptionsGroup.pm

124 lines
5.1 KiB
Perl
Raw Normal View History

2011-10-03 09:55:32 +00:00
package Slic3r::GUI::OptionsGroup;
use strict;
use warnings;
use Wx qw(:sizer wxSYS_DEFAULT_GUI_FONT);
use Wx::Event qw(EVT_TEXT EVT_CHECKBOX EVT_CHOICE);
2011-10-03 09:55:32 +00:00
use base 'Wx::StaticBoxSizer';
2011-10-05 16:13:47 +00:00
# not very elegant, but this solution is temporary waiting for a better GUI
2012-04-30 12:56:01 +00:00
our @reload_callbacks = ();
2012-04-11 15:38:56 +00:00
our %fields = (); # $key => [$control]
2011-10-05 16:13:47 +00:00
2011-10-03 09:55:32 +00:00
sub new {
my $class = shift;
my ($parent, %p) = @_;
my $box = Wx::StaticBox->new($parent, -1, $p{title});
my $self = $class->SUPER::new($box, wxVERTICAL);
my $grid_sizer = Wx::FlexGridSizer->new(scalar(@{$p{options}}), 2, 2, 0);
#grab the default font, to fix Windows font issues/keep things consistent
my $bold_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
2011-12-02 16:02:36 +00:00
$bold_font->SetWeight(&Wx::wxFONTWEIGHT_BOLD);
2011-10-03 09:55:32 +00:00
2011-10-05 16:13:47 +00:00
foreach my $opt_key (@{$p{options}}) {
my $index;
$opt_key =~ s/#(\d+)$// and $index = $1;
2011-10-05 16:13:47 +00:00
my $opt = $Slic3r::Config::Options->{$opt_key};
2012-02-25 20:01:00 +00:00
my $label = Wx::StaticText->new($parent, -1, "$opt->{label}:", Wx::wxDefaultPosition,
[$p{label_width} || 180, -1]);
$label->Wrap($p{label_width} || 180); # needed to avoid Linux/GTK bug
#set the bold font point size to the same size as all the other labels (for consistency)
$bold_font->SetPointSize($label->GetFont()->GetPointSize());
2011-12-02 16:02:36 +00:00
$label->SetFont($bold_font) if $opt->{important};
2011-10-03 09:55:32 +00:00
my $field;
if ($opt->{type} =~ /^(i|f|s|s@)$/) {
my $style = 0;
my $size = Wx::wxDefaultSize;
if ($opt->{multiline}) {
$style = &Wx::wxTE_MULTILINE;
$size = Wx::Size->new($opt->{width} || -1, $opt->{height} || -1);
}
# if it's an array type but no index was specified, use the serialized version
my ($get_m, $set_m) = $opt->{type} =~ /\@$/ && !defined $index
? qw(serialize deserialize)
: qw(get_raw set);
my $get = sub {
my $val = Slic3r::Config->$get_m($opt_key);
$val = $val->[$index] if defined $index;
return $val;
};
$field = Wx::TextCtrl->new($parent, -1, $get->(), Wx::wxDefaultPosition, $size, $style);
push @reload_callbacks, sub { $field->SetValue($get->()) };
my $set = sub {
my $val = $field->GetValue;
if (defined $index) {
Slic3r::Config->$get_m($opt_key)->[$index] = $val;
} else {
Slic3r::Config->$set_m($opt_key, $val);
}
};
EVT_TEXT($parent, $field, sub { $set->() });
2011-10-03 09:55:32 +00:00
} elsif ($opt->{type} eq 'bool') {
$field = Wx::CheckBox->new($parent, -1, "");
$field->SetValue(Slic3r::Config->get_raw($opt_key));
2011-10-05 16:13:47 +00:00
EVT_CHECKBOX($parent, $field, sub { Slic3r::Config->set($opt_key, $field->GetValue) });
push @reload_callbacks, sub { $field->SetValue(Slic3r::Config->get_raw($opt_key)) };
2011-10-03 09:55:32 +00:00
} elsif ($opt->{type} eq 'point') {
$field = Wx::BoxSizer->new(wxHORIZONTAL);
my $field_size = Wx::Size->new(40, -1);
my $value = Slic3r::Config->get_raw($opt_key);
2011-10-03 09:55:32 +00:00
$field->Add($_) for (
Wx::StaticText->new($parent, -1, "x:"),
2011-10-05 16:13:47 +00:00
my $x_field = Wx::TextCtrl->new($parent, -1, $value->[0], Wx::wxDefaultPosition, $field_size),
2011-10-03 09:55:32 +00:00
Wx::StaticText->new($parent, -1, " y:"),
2011-10-05 16:13:47 +00:00
my $y_field = Wx::TextCtrl->new($parent, -1, $value->[1], Wx::wxDefaultPosition, $field_size),
2011-10-03 09:55:32 +00:00
);
2011-10-05 16:13:47 +00:00
my $set_value = sub {
my ($i, $value) = @_;
my $val = Slic3r::Config->get_raw($opt_key);
2011-10-05 16:13:47 +00:00
$val->[$i] = $value;
Slic3r::Config->set($opt_key, $val);
};
EVT_TEXT($parent, $x_field, sub { $set_value->(0, $x_field->GetValue) });
EVT_TEXT($parent, $y_field, sub { $set_value->(1, $y_field->GetValue) });
push @reload_callbacks, sub {
my $value = Slic3r::Config->get_raw($opt_key);
2011-10-05 16:13:47 +00:00
$x_field->SetValue($value->[0]);
$y_field->SetValue($value->[1]);
};
2012-04-11 15:38:56 +00:00
$fields{$opt_key} = [$x_field, $y_field];
} elsif ($opt->{type} eq 'select') {
2011-11-26 21:41:23 +00:00
$field = Wx::Choice->new($parent, -1, Wx::wxDefaultPosition, Wx::wxDefaultSize, $opt->{labels} || $opt->{values});
2012-04-11 15:38:56 +00:00
EVT_CHOICE($parent, $field, sub {
Slic3r::Config->set($opt_key, $opt->{values}[$field->GetSelection]);
});
push @reload_callbacks, sub {
my $value = Slic3r::Config->get_raw($opt_key);
$field->SetSelection(grep $opt->{values}[$_] eq $value, 0..$#{$opt->{values}});
};
$reload_callbacks[-1]->();
2011-10-03 09:55:32 +00:00
} else {
die "Unsupported option type: " . $opt->{type};
}
$grid_sizer->Add($_) for $label, $field;
2012-04-11 15:38:56 +00:00
$fields{$opt_key} ||= [$field];
2011-10-03 09:55:32 +00:00
}
$self->Add($grid_sizer, 0, wxEXPAND);
return $self;
}
1;