1
package Slic3r::GUI::OptionsGroup;
4
use List::Util qw(first);
5
use Wx qw(:combobox :font :misc :sizer :systemsettings :textctrl);
6
use Wx::Event qw(EVT_CHECKBOX EVT_COMBOBOX EVT_SPINCTRL EVT_TEXT EVT_KILL_FOCUS EVT_SLIDER);
10
Slic3r::GUI::OptionsGroup - pre-filled Wx::StaticBoxSizer wrapper containing one or more options
14
my $optgroup = Slic3r::GUI::OptionsGroup->new(
15
parent => $self->parent,
19
opt_key => 'layer_height', # mandatory
20
type => 'f', # mandatory
21
label => 'Layer height',
22
tooltip => 'This setting controls the height (and thus the total number) of the slices/layers.',
31
default => 0.4, # mandatory
33
on_change => sub { print "new value is $_[0]\n" },
36
on_change => sub { print "new value for $_[0] is $_[1]\n" },
39
extra_column => sub { ... },
41
$sizer->Add($optgroup->sizer);
45
has 'parent' => (is => 'ro', required => 1);
46
has 'title' => (is => 'ro', required => 1);
47
has 'options' => (is => 'ro', required => 1, trigger => 1);
48
has 'lines' => (is => 'lazy');
49
has 'on_change' => (is => 'ro', default => sub { sub {} });
50
has 'no_labels' => (is => 'ro', default => sub { 0 });
51
has 'label_width' => (is => 'ro', default => sub { 180 });
52
has 'extra_column' => (is => 'ro');
53
has 'label_font' => (is => 'ro');
54
has 'sidetext_font' => (is => 'ro', default => sub { Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) });
55
has 'ignore_on_change_return' => (is => 'ro', default => sub { 1 });
57
has 'sizer' => (is => 'rw');
58
has '_triggers' => (is => 'ro', default => sub { {} });
59
has '_setters' => (is => 'ro', default => sub { {} });
61
sub _trigger_options {}
67
my $box = Wx::StaticBox->new($self->parent, -1, $self->title);
68
$self->sizer(Wx::StaticBoxSizer->new($box, wxVERTICAL));
71
my $num_columns = $self->extra_column ? 3 : 2;
72
my $grid_sizer = Wx::FlexGridSizer->new(scalar(@{$self->options}), $num_columns, 0, 0);
73
$grid_sizer->SetFlexibleDirection(wxHORIZONTAL);
74
$grid_sizer->AddGrowableCol($self->no_labels ? 0 : 1);
76
# TODO: border size may be related to wxWidgets 2.8.x vs. 2.9.x instead of wxMAC specific
77
$self->sizer->Add($grid_sizer, 0, wxEXPAND | wxALL, &Wx::wxMAC ? 0 : 5);
79
foreach my $line (@{$self->lines}) {
81
$self->sizer->Add($line->{sizer}, 0, wxEXPAND | wxALL, &Wx::wxMAC ? 0 : 15);
82
} elsif ($line->{widget}) {
83
my $window = $line->{widget}->GetWindow($self->parent);
84
$self->sizer->Add($window, 0, wxEXPAND | wxALL, &Wx::wxMAC ? 0 : 15);
86
$self->_build_line($line, $grid_sizer);
91
# default behavior: one option per line
96
foreach my $opt (@{$self->options}) {
98
label => $opt->{label},
99
sidetext => $opt->{sidetext},
100
full_width => $opt->{full_width},
101
options => [$opt->{opt_key}],
107
sub single_option_line {
112
label => $Slic3r::Config::Options->{$opt_key}{label},
113
sidetext => $Slic3r::Config::Options->{$opt_key}{sidetext},
114
options => [$opt_key],
120
my ($line, $grid_sizer) = @_;
122
if ($self->extra_column) {
123
if (defined (my $item = $self->extra_column->($line))) {
124
$grid_sizer->Add($item, 0, wxALIGN_CENTER_VERTICAL, 0);
126
$grid_sizer->AddSpacer(1);
131
if (!$self->no_labels) {
132
$label = Wx::StaticText->new($self->parent, -1, $line->{label} ? "$line->{label}:" : "", wxDefaultPosition, [$self->label_width, -1]);
133
$label->SetFont($self->label_font) if $self->label_font;
134
$label->Wrap($self->label_width) ; # needed to avoid Linux/GTK bug
135
$grid_sizer->Add($label, 0, wxALIGN_CENTER_VERTICAL, 0);
136
$label->SetToolTipString($line->{tooltip}) if $line->{tooltip};
140
my @field_labels = ();
141
foreach my $opt_key (@{$line->{options}}) {
142
my $opt = first { $_->{opt_key} eq $opt_key } @{$self->options};
143
push @fields, $self->_build_field($opt);
144
push @field_labels, $opt->{label};
146
if (@fields > 1 || $line->{sidetext}) {
147
my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
148
for my $i (0 .. $#fields) {
149
if (@fields > 1 && $field_labels[$i]) {
150
my $field_label = Wx::StaticText->new($self->parent, -1, "$field_labels[$i]:", wxDefaultPosition, wxDefaultSize);
151
$field_label->SetFont($self->sidetext_font);
152
$sizer->Add($field_label, 0, wxALIGN_CENTER_VERTICAL, 0);
154
$sizer->Add($fields[$i], 0, wxALIGN_CENTER_VERTICAL, 0);
156
if ($line->{sidetext}) {
157
my $sidetext = Wx::StaticText->new($self->parent, -1, $line->{sidetext}, wxDefaultPosition, wxDefaultSize);
158
$sidetext->SetFont($self->sidetext_font);
159
$sizer->Add($sidetext, 0, wxLEFT | wxALIGN_CENTER_VERTICAL , 4);
161
$grid_sizer->Add($sizer);
163
$grid_sizer->Add($fields[0], 0, ($line->{full_width} ? wxEXPAND : 0) | wxALIGN_CENTER_VERTICAL, 0);
171
my $opt_key = $opt->{opt_key};
172
$self->_triggers->{$opt_key} = $opt->{on_change} || sub { return 1 };
174
my $on_kill_focus = sub {
175
my ($s, $event) = @_;
177
# Without this, there will be nasty focus bugs on Windows.
178
# Also, docs for wxEvent::Skip() say "In general, it is recommended to skip all
179
# non-command events to allow the default handling to take place."
182
$self->on_kill_focus($opt_key);
186
my $tooltip = $opt->{tooltip};
187
if ($opt->{type} =~ /^(i|f|s|s@|percent|slider)$/) {
189
$style = wxTE_MULTILINE if $opt->{multiline};
190
# default width on Windows is too large
191
my $size = Wx::Size->new($opt->{width} || 60, $opt->{height} || -1);
193
my $on_change = sub {
194
my $value = $field->GetValue;
195
$value ||= 0 if $opt->{type} =~ /^(i|f|percent)$/; # prevent crash trying to pass empty strings to Config
196
$self->_on_change($opt_key, $value);
198
if ($opt->{type} eq 'i') {
199
$field = Wx::SpinCtrl->new($self->parent, -1, $opt->{default}, wxDefaultPosition, $size, $style, $opt->{min} || 0, $opt->{max} || 2147483647, $opt->{default});
200
$self->_setters->{$opt_key} = sub { $field->SetValue($_[0]) };
201
EVT_SPINCTRL ($self->parent, $field, $on_change);
202
EVT_TEXT ($self->parent, $field, $on_change);
203
EVT_KILL_FOCUS($field, $on_kill_focus);
204
} elsif ($opt->{values}) {
205
$field = Wx::ComboBox->new($self->parent, -1, $opt->{default}, wxDefaultPosition, $size, $opt->{labels} || $opt->{values});
206
$self->_setters->{$opt_key} = sub {
207
$field->SetValue($_[0]);
209
EVT_COMBOBOX($self->parent, $field, sub {
210
# Without CallAfter, the field text is not populated on Windows.
211
Slic3r::GUI->CallAfter(sub {
212
$field->SetValue($opt->{values}[ $field->GetSelection ]); # set the text field to the selected value
213
$self->_on_change($opt_key, $on_change);
216
EVT_TEXT($self->parent, $field, $on_change);
217
EVT_KILL_FOCUS($field, $on_kill_focus);
218
} elsif ($opt->{type} eq 'slider') {
220
$field = Wx::BoxSizer->new(wxHORIZONTAL);
221
my $slider = Wx::Slider->new($self->parent, -1, ($opt->{default} // $opt->{min})*$scale, ($opt->{min} // 0)*$scale, ($opt->{max} // 100)*$scale, wxDefaultPosition, $size);
222
my $statictext = Wx::StaticText->new($self->parent, -1, $slider->GetValue/$scale);
223
$field->Add($_, 0, wxALIGN_CENTER_VERTICAL, 0) for $slider, $statictext;
224
$self->_setters->{$opt_key} = sub {
225
$field->SetValue($_[0]*$scale);
227
EVT_SLIDER($self->parent, $slider, sub {
228
my $value = $slider->GetValue/$scale;
229
$statictext->SetLabel($value);
230
$self->_on_change($opt_key, $value);
233
$field = Wx::TextCtrl->new($self->parent, -1, $opt->{default}, wxDefaultPosition, $size, $style);
234
# value supplied to the setter callback might be undef in case user loads a config
235
# that has empty string for multi-value options like 'wipe'
236
$self->_setters->{$opt_key} = sub { $field->ChangeValue($_[0]) if defined $_[0] };
237
EVT_TEXT($self->parent, $field, $on_change);
238
EVT_KILL_FOCUS($field, $on_kill_focus);
240
$field->Disable if $opt->{readonly};
241
$tooltip .= " (default: " . $opt->{default} . ")" if ($opt->{default});
242
} elsif ($opt->{type} eq 'bool') {
243
$field = Wx::CheckBox->new($self->parent, -1, "");
244
$field->SetValue($opt->{default});
245
$field->Disable if $opt->{readonly};
246
EVT_CHECKBOX($self->parent, $field, sub { $self->_on_change($opt_key, $field->GetValue); });
247
$self->_setters->{$opt_key} = sub { $field->SetValue($_[0]) };
248
$tooltip .= " (default: " . ($opt->{default} ? 'yes' : 'no') . ")" if defined($opt->{default});
249
} elsif ($opt->{type} eq 'point') {
250
$field = Wx::BoxSizer->new(wxHORIZONTAL);
251
my $field_size = Wx::Size->new(40, -1);
253
Wx::StaticText->new($self->parent, -1, "x:"),
254
my $x_field = Wx::TextCtrl->new($self->parent, -1, $opt->{default}->[0], wxDefaultPosition, $field_size),
255
Wx::StaticText->new($self->parent, -1, " y:"),
256
my $y_field = Wx::TextCtrl->new($self->parent, -1, $opt->{default}->[1], wxDefaultPosition, $field_size),
258
$field->Add($_, 0, wxALIGN_CENTER_VERTICAL, 0) for @items;
260
$_->SetToolTipString(
261
$tooltip . " (default: " . join(",", @{$opt->{default}}) . ")"
264
foreach my $field ($x_field, $y_field) {
265
EVT_TEXT($self->parent, $field, sub { $self->_on_change($opt_key, [ $x_field->GetValue, $y_field->GetValue ]) });
266
EVT_KILL_FOCUS($field, $on_kill_focus);
268
$self->_setters->{$opt_key} = sub {
269
$x_field->SetValue($_[0][0]);
270
$y_field->SetValue($_[0][1]);
272
} elsif ($opt->{type} eq 'select') {
273
$field = Wx::ComboBox->new($self->parent, -1, "", wxDefaultPosition, wxDefaultSize, $opt->{labels} || $opt->{values}, wxCB_READONLY);
274
EVT_COMBOBOX($self->parent, $field, sub {
275
$self->_on_change($opt_key, $opt->{values}[$field->GetSelection]);
277
$self->_setters->{$opt_key} = sub {
278
$field->SetSelection(grep $opt->{values}[$_] eq $_[0], 0..$#{$opt->{values}});
280
$self->_setters->{$opt_key}->($opt->{default});
282
$tooltip .= " (default: "
283
. $opt->{labels}[ first { $opt->{values}[$_] eq $opt->{default} } 0..$#{$opt->{values}} ]
284
. ")" if ($opt->{default});
286
die "Unsupported option type: " . $opt->{type};
288
if ($tooltip && $field->can('SetToolTipString')) {
289
$field->SetToolTipString($tooltip);
298
return first { $_->{opt_key} eq $opt_key } @{$self->options};
303
my ($opt_key, $value) = @_;
305
return if $self->sizer->GetStaticBox->GetParent->{disabled};
306
$self->_triggers->{$opt_key}->($value) or $self->ignore_on_change_return or return;
307
$self->on_change->($opt_key, $value);
312
This method accepts an option key and a value. If this option group contains the supplied
313
option key, its field will be updated with the new value and the method will return a true
314
value, otherwise it will return false.
320
my ($opt_key, $value) = @_;
322
if ($self->_setters->{$opt_key}) {
323
$self->_setters->{$opt_key}->($value);
324
$self->_on_change($opt_key, $value);
333
package Slic3r::GUI::ConfigOptionsGroup;
336
extends 'Slic3r::GUI::OptionsGroup';
340
Slic3r::GUI::ConfigOptionsGroup - pre-filled Wx::StaticBoxSizer wrapper containing one or more config options
344
my $optgroup = Slic3r::GUI::ConfigOptionsGroup->new(
345
parent => $self->parent,
348
options => ['layer_height'],
349
on_change => sub { print "new value for $_[0] is $_[1]\n" },
353
$sizer->Add($optgroup->sizer);
357
use List::Util qw(first);
359
has 'config' => (is => 'ro', required => 1);
360
has 'full_labels' => (is => 'ro', default => sub {0});
361
has '+ignore_on_change_return' => (is => 'ro', default => sub { 0 });
363
sub _trigger_options {
366
@{$self->options} = map {
368
if (ref $opt ne 'HASH') {
370
my ($opt_key, $index) = $self->_split_key($full_key);
371
my $config_opt = $Slic3r::Config::Options->{$opt_key};
374
opt_key => $full_key,
376
label => ($self->full_labels && defined $config_opt->{full_label}) ? $config_opt->{full_label} : $config_opt->{label},
377
(map { $_ => $config_opt->{$_} } qw(type tooltip sidetext width height full_width min max labels values multiline readonly)),
378
default => $self->_get_config($opt_key, $index),
379
on_change => sub { return $self->_set_config($opt_key, $index, $_[0]) },
390
return first { $_->{opt_key} =~ /^\Q$opt_key\E(#.+)?$/ } @{$self->options};
395
my ($opt_key, $value) = @_;
397
my $opt = $self->_option($opt_key) or return 0;
399
# if user is setting a non-config option, forward the call to the parent
400
if (!$opt->{config}) {
401
return $self->SUPER::set_value($opt_key, $value);
405
foreach my $full_key (keys %{$self->_setters}) {
406
my ($key, $index) = $self->_split_key($full_key);
408
if ($key eq $opt_key) {
409
$self->config->set($key, $value);
410
$self->SUPER::set_value($full_key, $self->_get_config($key, $index));
418
my ($self, $full_key) = @_;
420
# when a field loses focus, reapply the config value to it
421
# (thus discarding any invalid input and reverting to the last
423
my ($key, $index) = $self->_split_key($full_key);
424
$self->SUPER::set_value($full_key, $self->_get_config($key, $index));
432
$opt_key =~ s/#(\d+)$// and $index = $1;
433
return ($opt_key, $index);
438
my ($opt_key, $index, $config) = @_;
440
my ($get_m, $serialized) = $self->_config_methods($opt_key, $index);
441
my $value = ($config // $self->config)->$get_m($opt_key);
442
if (defined $index) {
443
$value->[$index] //= $value->[0]; #/
444
$value = $value->[$index];
451
my ($opt_key, $index, $value) = @_;
453
my ($get_m, $serialized) = $self->_config_methods($opt_key, $index);
454
if (defined $index) {
455
my $values = $self->config->$get_m($opt_key);
456
$values->[$index] = $value;
458
# ignore set() return value
459
$self->config->set($opt_key, $values);
462
# ignore set_deserialize() return value
463
return $self->config->set_deserialize($opt_key, $value);
465
# ignore set() return value
466
return $self->config->set($opt_key, $value);
471
sub _config_methods {
473
my ($opt_key, $index) = @_;
475
# if it's an array type but no index was specified, use the serialized version
476
return ($Slic3r::Config::Options->{$opt_key}{type} =~ /\@$/ && !defined $index)
481
package Slic3r::GUI::OptionsGroup::StaticTextLine;
483
use Wx qw(:misc :systemsettings);
489
$self->{statictext} = Wx::StaticText->new($parent, -1, "foo", wxDefaultPosition, wxDefaultSize);
490
my $font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
491
$self->{statictext}->SetFont($font);
492
return $self->{statictext};
499
$self->{statictext}->SetLabel($value);
500
$self->{statictext}->Wrap(400);
501
$self->{statictext}->GetParent->Layout;