2011-10-03 09:55:32 +00:00
|
|
|
package Slic3r::GUI::OptionsGroup;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2012-07-01 17:24:06 +00:00
|
|
|
use Wx qw(:combobox :font :misc :sizer :systemsettings :textctrl);
|
|
|
|
use Wx::Event qw(EVT_CHECKBOX EVT_COMBOBOX EVT_SPINCTRL EVT_TEXT);
|
2011-10-03 09:55:32 +00:00
|
|
|
use base 'Wx::StaticBoxSizer';
|
|
|
|
|
2011-12-06 21:36:42 +00:00
|
|
|
|
2011-10-05 16:13:47 +00:00
|
|
|
# not very elegant, but this solution is temporary waiting for a better GUI
|
2012-06-18 20:27:57 +00:00
|
|
|
our %reload_callbacks = (); # key => $cb
|
|
|
|
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);
|
|
|
|
|
2012-06-17 21:24:10 +00:00
|
|
|
my $grid_sizer = Wx::FlexGridSizer->new(scalar(@{$p{options}}), 2, ($p{no_labels} ? 1 : 2), 0);
|
2012-07-01 17:24:06 +00:00
|
|
|
$grid_sizer->SetFlexibleDirection(wxHORIZONTAL);
|
2012-06-18 09:26:21 +00:00
|
|
|
$grid_sizer->AddGrowableCol($p{no_labels} ? 0 : 1);
|
|
|
|
|
2012-06-18 08:19:24 +00:00
|
|
|
my $sidetext_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
|
2011-10-03 09:55:32 +00:00
|
|
|
|
2012-06-18 20:27:57 +00:00
|
|
|
my $onChange = $p{on_change} || sub {};
|
2012-07-18 18:36:34 +00:00
|
|
|
my $make_cb = sub {
|
|
|
|
my $cb = shift;
|
|
|
|
return sub {
|
|
|
|
$cb->(@_) if !$parent->{disabled};
|
|
|
|
};
|
|
|
|
};
|
2011-10-03 09:55:32 +00:00
|
|
|
|
2012-07-18 18:36:34 +00:00
|
|
|
foreach my $original_opt_key (@{$p{options}}) {
|
2012-06-28 12:44:54 +00:00
|
|
|
my $index;
|
2012-07-18 18:36:34 +00:00
|
|
|
my $opt_key = $original_opt_key; # leave original one untouched
|
2012-06-28 12:44:54 +00:00
|
|
|
$opt_key =~ s/#(\d+)$// and $index = $1;
|
|
|
|
|
2011-10-05 16:13:47 +00:00
|
|
|
my $opt = $Slic3r::Config::Options->{$opt_key};
|
2012-06-17 21:24:10 +00:00
|
|
|
my $label;
|
2012-07-18 18:36:34 +00:00
|
|
|
if (!$p{no_labels}) {
|
2012-07-01 17:24:06 +00:00
|
|
|
$label = Wx::StaticText->new($parent, -1, "$opt->{label}:", wxDefaultPosition, [$p{label_width} || 180, -1]);
|
2012-06-17 21:24:10 +00:00
|
|
|
$label->Wrap($p{label_width} || 180) ; # needed to avoid Linux/GTK bug
|
|
|
|
$grid_sizer->Add($label);
|
|
|
|
}
|
2011-12-06 21:36:42 +00:00
|
|
|
|
2011-10-03 09:55:32 +00:00
|
|
|
my $field;
|
2012-07-18 18:36:34 +00:00
|
|
|
$fields{$opt_key} = undef;
|
2012-02-20 11:50:05 +00:00
|
|
|
if ($opt->{type} =~ /^(i|f|s|s@)$/) {
|
2011-11-13 21:48:21 +00:00
|
|
|
my $style = 0;
|
2012-07-01 17:24:06 +00:00
|
|
|
$style = wxTE_MULTILINE if $opt->{multiline};
|
2012-06-18 08:28:53 +00:00
|
|
|
my $size = Wx::Size->new($opt->{width} || -1, $opt->{height} || -1);
|
2011-11-13 21:48:21 +00:00
|
|
|
|
2012-06-28 12:44:54 +00:00
|
|
|
# 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);
|
2012-07-18 18:36:34 +00:00
|
|
|
if (defined $index) {
|
|
|
|
$val = $val->[$index]; #/
|
|
|
|
}
|
2012-06-28 12:44:54 +00:00
|
|
|
return $val;
|
|
|
|
};
|
2012-07-18 13:48:04 +00:00
|
|
|
$field = $opt->{type} eq 'i'
|
|
|
|
? Wx::SpinCtrl->new($parent, -1, $get->(), wxDefaultPosition, $size, $style, $opt->{min} || 0, $opt->{max} || 100, $get->())
|
|
|
|
: Wx::TextCtrl->new($parent, -1, $get->(), wxDefaultPosition, $size, $style);
|
2012-07-18 18:36:34 +00:00
|
|
|
$reload_callbacks{$opt_key} = $make_cb->(sub { $field->SetValue($get->()) });
|
2012-02-20 11:50:05 +00:00
|
|
|
|
2012-06-28 12:44:54 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-07-18 13:48:04 +00:00
|
|
|
$onChange->($opt_key);
|
2012-06-28 12:44:54 +00:00
|
|
|
};
|
2012-07-18 13:48:04 +00:00
|
|
|
$opt->{type} eq 'i'
|
|
|
|
? EVT_SPINCTRL($parent, $field, $set)
|
|
|
|
: EVT_TEXT($parent, $field, $set);
|
2011-10-03 09:55:32 +00:00
|
|
|
} elsif ($opt->{type} eq 'bool') {
|
|
|
|
$field = Wx::CheckBox->new($parent, -1, "");
|
2012-06-19 13:32:56 +00:00
|
|
|
$field->SetValue(Slic3r::Config->get_raw($opt_key));
|
2012-07-18 18:36:34 +00:00
|
|
|
EVT_CHECKBOX($parent, $field, $make_cb->(sub { Slic3r::Config->set($opt_key, $field->GetValue); $onChange->($opt_key) }));
|
|
|
|
$reload_callbacks{$opt_key} = $make_cb->(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);
|
2012-06-19 13:32:56 +00:00
|
|
|
my $value = Slic3r::Config->get_raw($opt_key);
|
2012-06-18 11:49:14 +00:00
|
|
|
my @items = (
|
2011-10-03 09:55:32 +00:00
|
|
|
Wx::StaticText->new($parent, -1, "x:"),
|
2012-07-01 17:24:06 +00:00
|
|
|
my $x_field = Wx::TextCtrl->new($parent, -1, $value->[0], wxDefaultPosition, $field_size),
|
2011-10-03 09:55:32 +00:00
|
|
|
Wx::StaticText->new($parent, -1, " y:"),
|
2012-07-01 17:24:06 +00:00
|
|
|
my $y_field = Wx::TextCtrl->new($parent, -1, $value->[1], wxDefaultPosition, $field_size),
|
2011-10-03 09:55:32 +00:00
|
|
|
);
|
2012-06-18 11:49:14 +00:00
|
|
|
$field->Add($_) for @items;
|
|
|
|
if ($opt->{tooltip}) {
|
|
|
|
$_->SetToolTipString($opt->{tooltip}) for @items;
|
|
|
|
}
|
2011-10-05 16:13:47 +00:00
|
|
|
my $set_value = sub {
|
|
|
|
my ($i, $value) = @_;
|
2012-06-19 13:32:56 +00:00
|
|
|
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);
|
|
|
|
};
|
2012-07-18 18:36:34 +00:00
|
|
|
EVT_TEXT($parent, $x_field, $make_cb->(sub { $set_value->(0, $x_field->GetValue); $onChange->($opt_key) }));
|
|
|
|
EVT_TEXT($parent, $y_field, $make_cb->(sub { $set_value->(1, $y_field->GetValue); $onChange->($opt_key) }));
|
|
|
|
$reload_callbacks{$opt_key} = $make_cb->(sub {
|
2012-06-19 13:32:56 +00:00
|
|
|
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-07-18 18:36:34 +00:00
|
|
|
});
|
2012-04-11 15:38:56 +00:00
|
|
|
$fields{$opt_key} = [$x_field, $y_field];
|
2011-11-13 17:14:02 +00:00
|
|
|
} elsif ($opt->{type} eq 'select') {
|
2012-07-01 17:24:06 +00:00
|
|
|
$field = Wx::ComboBox->new($parent, -1, "", wxDefaultPosition, wxDefaultSize, $opt->{labels} || $opt->{values}, wxCB_READONLY);
|
2012-07-18 18:36:34 +00:00
|
|
|
EVT_COMBOBOX($parent, $field, $make_cb->(sub {
|
2012-04-11 15:38:56 +00:00
|
|
|
Slic3r::Config->set($opt_key, $opt->{values}[$field->GetSelection]);
|
2012-06-18 20:27:57 +00:00
|
|
|
$onChange->($opt_key);
|
2012-07-18 18:36:34 +00:00
|
|
|
}));
|
|
|
|
$reload_callbacks{$opt_key} = $make_cb->(sub {
|
2012-06-19 13:32:56 +00:00
|
|
|
my $value = Slic3r::Config->get_raw($opt_key);
|
2011-11-13 17:14:02 +00:00
|
|
|
$field->SetSelection(grep $opt->{values}[$_] eq $value, 0..$#{$opt->{values}});
|
2012-07-18 18:36:34 +00:00
|
|
|
});
|
2012-06-18 20:27:57 +00:00
|
|
|
$reload_callbacks{$opt_key}->();
|
2011-10-03 09:55:32 +00:00
|
|
|
} else {
|
|
|
|
die "Unsupported option type: " . $opt->{type};
|
|
|
|
}
|
2012-06-18 11:49:14 +00:00
|
|
|
$label->SetToolTipString($opt->{tooltip}) if $label && $opt->{tooltip};
|
|
|
|
$field->SetToolTipString($opt->{tooltip}) if $opt->{tooltip} && $field->can('SetToolTipString');
|
2012-06-18 08:19:24 +00:00
|
|
|
if ($opt->{sidetext}) {
|
2012-07-01 17:24:06 +00:00
|
|
|
my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
|
2012-06-18 08:19:24 +00:00
|
|
|
$sizer->Add($field);
|
2012-07-02 23:20:30 +00:00
|
|
|
my $sidetext = Wx::StaticText->new($parent, -1, $opt->{sidetext}, wxDefaultPosition, wxDefaultSize);
|
2012-06-18 08:19:24 +00:00
|
|
|
$sidetext->SetFont($sidetext_font);
|
2012-07-01 17:24:06 +00:00
|
|
|
$sizer->Add($sidetext, 0, wxLEFT | wxALIGN_CENTER_VERTICAL , 4);
|
2012-06-18 08:19:24 +00:00
|
|
|
$grid_sizer->Add($sizer);
|
|
|
|
} else {
|
2012-07-01 17:24:06 +00:00
|
|
|
$grid_sizer->Add($field, 0, $opt->{full_width} ? wxEXPAND : 0);
|
2012-06-18 08:19:24 +00:00
|
|
|
}
|
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;
|