OptionsGroup.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. # A dialog group object. Used by the Tab, Preferences dialog, ManualControlDialog etc.
  2. package Slic3r::GUI::OptionsGroup;
  3. use Moo;
  4. use List::Util qw(first);
  5. use Wx qw(:combobox :font :misc :sizer :systemsettings :textctrl wxTheApp);
  6. use Wx::Event qw(EVT_CHECKBOX EVT_COMBOBOX EVT_SPINCTRL EVT_TEXT EVT_KILL_FOCUS EVT_SLIDER);
  7. has 'parent' => (is => 'ro', required => 1);
  8. has 'title' => (is => 'ro', required => 1);
  9. has 'on_change' => (is => 'rw', default => sub { sub {} });
  10. has 'staticbox' => (is => 'ro', default => sub { 1 });
  11. has 'label_width' => (is => 'rw', default => sub { 180 });
  12. has 'extra_column' => (is => 'rw', default => sub { undef });
  13. has 'label_font' => (is => 'rw');
  14. has 'sidetext_font' => (is => 'rw', default => sub { Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) });
  15. has 'sizer' => (is => 'rw');
  16. has '_disabled' => (is => 'rw', default => sub { 0 });
  17. has '_grid_sizer' => (is => 'rw');
  18. has '_options' => (is => 'ro', default => sub { {} });
  19. has '_fields' => (is => 'ro', default => sub { {} });
  20. sub BUILD {
  21. my $self = shift;
  22. if ($self->staticbox) {
  23. my $box = Wx::StaticBox->new($self->parent, -1, $self->title);
  24. $self->sizer(Wx::StaticBoxSizer->new($box, wxVERTICAL));
  25. } else {
  26. $self->sizer(Wx::BoxSizer->new(wxVERTICAL));
  27. }
  28. my $num_columns = 1;
  29. ++$num_columns if $self->label_width != 0;
  30. ++$num_columns if $self->extra_column;
  31. $self->_grid_sizer(Wx::FlexGridSizer->new(0, $num_columns, 0, 0));
  32. $self->_grid_sizer->SetFlexibleDirection(wxHORIZONTAL);
  33. $self->_grid_sizer->AddGrowableCol($self->label_width != 0);
  34. # TODO: border size may be related to wxWidgets 2.8.x vs. 2.9.x instead of wxMAC specific
  35. $self->sizer->Add($self->_grid_sizer, 0, wxEXPAND | wxALL, &Wx::wxMAC ? 0 : 5);
  36. }
  37. # this method accepts a Slic3r::GUI::OptionsGroup::Line object
  38. sub append_line {
  39. my ($self, $line) = @_;
  40. if ($line->sizer || ($line->widget && $line->full_width)) {
  41. # full-width widgets are appended *after* the grid sizer, so after all the non-full-width lines
  42. my $sizer = $line->sizer // $line->widget->($self->parent);
  43. $self->sizer->Add($sizer, 0, wxEXPAND | wxALL, &Wx::wxMAC ? 0 : 15);
  44. return;
  45. }
  46. my $grid_sizer = $self->_grid_sizer;
  47. # if we have an extra column, build it
  48. if ($self->extra_column) {
  49. if (defined (my $item = $self->extra_column->($line))) {
  50. $grid_sizer->Add($item, 0, wxALIGN_CENTER_VERTICAL, 0);
  51. } else {
  52. # if the callback provides no sizer for the extra cell, put a spacer
  53. $grid_sizer->AddSpacer(1);
  54. }
  55. }
  56. # build label if we have it
  57. my $label;
  58. if ($self->label_width != 0) {
  59. $label = Wx::StaticText->new($self->parent, -1, $line->label ? $line->label . ":" : "", wxDefaultPosition, [$self->label_width, -1]);
  60. $label->SetFont($self->label_font) if $self->label_font;
  61. $label->Wrap($self->label_width) ; # needed to avoid Linux/GTK bug
  62. $grid_sizer->Add($label, 0, wxALIGN_CENTER_VERTICAL, 0);
  63. $label->SetToolTipString($line->label_tooltip) if $line->label_tooltip;
  64. }
  65. # if we have a widget, add it to the sizer
  66. if ($line->widget) {
  67. my $widget_sizer = $line->widget->($self->parent);
  68. $grid_sizer->Add($widget_sizer, 0, wxEXPAND | wxALL, &Wx::wxMAC ? 0 : 15);
  69. return;
  70. }
  71. # if we have a single option with no sidetext just add it directly to the grid sizer
  72. my @options = @{$line->get_options};
  73. $self->_options->{$_->opt_id} = $_ for @options;
  74. if (@options == 1 && !$options[0]->sidetext && !$options[0]->side_widget && !@{$line->get_extra_widgets}) {
  75. my $option = $options[0];
  76. my $field = $self->_build_field($option);
  77. $grid_sizer->Add($field, 0, ($option->full_width ? wxEXPAND : 0) | wxALIGN_CENTER_VERTICAL, 0);
  78. return;
  79. }
  80. # if we're here, we have more than one option or a single option with sidetext
  81. # so we need a horizontal sizer to arrange these things
  82. my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  83. $grid_sizer->Add($sizer, 0, 0, 0);
  84. foreach my $i (0..$#options) {
  85. my $option = $options[$i];
  86. # add label if any
  87. if ($option->label) {
  88. my $field_label = Wx::StaticText->new($self->parent, -1, $option->label . ":", wxDefaultPosition, wxDefaultSize);
  89. $field_label->SetFont($self->sidetext_font);
  90. $sizer->Add($field_label, 0, wxALIGN_CENTER_VERTICAL, 0);
  91. }
  92. # add field
  93. my $field = $self->_build_field($option);
  94. $sizer->Add($field, 0, wxALIGN_CENTER_VERTICAL, 0);
  95. # add sidetext if any
  96. if ($option->sidetext) {
  97. my $sidetext = Wx::StaticText->new($self->parent, -1, $option->sidetext, wxDefaultPosition, wxDefaultSize);
  98. $sidetext->SetFont($self->sidetext_font);
  99. $sizer->Add($sidetext, 0, wxLEFT | wxALIGN_CENTER_VERTICAL, 4);
  100. }
  101. # add side widget if any
  102. if ($option->side_widget) {
  103. $sizer->Add($option->side_widget->($self->parent), 0, wxLEFT | wxALIGN_CENTER_VERTICAL, 1);
  104. }
  105. if ($option != $#options) {
  106. $sizer->AddSpacer(4);
  107. }
  108. }
  109. # add extra sizers if any
  110. foreach my $extra_widget (@{$line->get_extra_widgets}) {
  111. $sizer->Add($extra_widget->($self->parent), 0, wxLEFT | wxALIGN_CENTER_VERTICAL , 4);
  112. }
  113. }
  114. sub create_single_option_line {
  115. my ($self, $option) = @_;
  116. my $line = Slic3r::GUI::OptionsGroup::Line->new(
  117. label => $option->label,
  118. label_tooltip => $option->tooltip,
  119. );
  120. $option->label("");
  121. $line->append_option($option);
  122. return $line;
  123. }
  124. sub append_single_option_line {
  125. my ($self, $option) = @_;
  126. return $self->append_line($self->create_single_option_line($option));
  127. }
  128. sub _build_field {
  129. my $self = shift;
  130. my ($opt) = @_;
  131. my $opt_id = $opt->opt_id;
  132. my $on_change = sub {
  133. #! This function will be called from Field.
  134. my ($opt_id, $value) = @_;
  135. #! Call OptionGroup._on_change(...)
  136. $self->_on_change($opt_id, $value)
  137. unless $self->_disabled;
  138. };
  139. my $on_kill_focus = sub {
  140. my ($opt_id) = @_;
  141. $self->_on_kill_focus($opt_id);
  142. };
  143. my $type = $opt->{gui_type} || $opt->{type};
  144. my $field;
  145. if ($type eq 'bool') {
  146. $field = Slic3r::GUI::OptionsGroup::Field::Checkbox->new(
  147. parent => $self->parent,
  148. option => $opt,
  149. );
  150. } elsif ($type eq 'i') {
  151. $field = Slic3r::GUI::OptionsGroup::Field::SpinCtrl->new(
  152. parent => $self->parent,
  153. option => $opt,
  154. );
  155. } elsif ($type eq 'color') {
  156. $field = Slic3r::GUI::OptionsGroup::Field::ColourPicker->new(
  157. parent => $self->parent,
  158. option => $opt,
  159. );
  160. } elsif ($type =~ /^(f|s|s@|percent)$/) {
  161. $field = Slic3r::GUI::OptionsGroup::Field::TextCtrl->new(
  162. parent => $self->parent,
  163. option => $opt,
  164. );
  165. } elsif ($type eq 'select' || $type eq 'select_open') {
  166. $field = Slic3r::GUI::OptionsGroup::Field::Choice->new(
  167. parent => $self->parent,
  168. option => $opt,
  169. );
  170. } elsif ($type eq 'f_enum_open' || $type eq 'i_enum_open' || $type eq 'i_enum_closed') {
  171. $field = Slic3r::GUI::OptionsGroup::Field::NumericChoice->new(
  172. parent => $self->parent,
  173. option => $opt,
  174. );
  175. } elsif ($type eq 'point') {
  176. $field = Slic3r::GUI::OptionsGroup::Field::Point->new(
  177. parent => $self->parent,
  178. option => $opt,
  179. );
  180. } elsif ($type eq 'slider') {
  181. $field = Slic3r::GUI::OptionsGroup::Field::Slider->new(
  182. parent => $self->parent,
  183. option => $opt,
  184. );
  185. }
  186. return undef if !$field;
  187. #! setting up a function that will be triggered when the field changes
  188. #! think of it as $field->on_change = ($on_change)
  189. $field->on_change($on_change);
  190. $field->on_kill_focus($on_kill_focus);
  191. $self->_fields->{$opt_id} = $field;
  192. return $field->isa('Slic3r::GUI::OptionsGroup::Field::wxWindow')
  193. ? $field->wxWindow
  194. : $field->wxSizer;
  195. }
  196. sub get_option {
  197. my ($self, $opt_id) = @_;
  198. return undef if !exists $self->_options->{$opt_id};
  199. return $self->_options->{$opt_id};
  200. }
  201. sub get_field {
  202. my ($self, $opt_id) = @_;
  203. return undef if !exists $self->_fields->{$opt_id};
  204. return $self->_fields->{$opt_id};
  205. }
  206. sub get_value {
  207. my ($self, $opt_id) = @_;
  208. return if !exists $self->_fields->{$opt_id};
  209. return $self->_fields->{$opt_id}->get_value;
  210. }
  211. sub set_value {
  212. my ($self, $opt_id, $value) = @_;
  213. return if !exists $self->_fields->{$opt_id};
  214. $self->_fields->{$opt_id}->set_value($value);
  215. }
  216. sub _on_change {
  217. my ($self, $opt_id, $value) = @_;
  218. $self->on_change->($opt_id, $value);
  219. }
  220. sub enable {
  221. my ($self) = @_;
  222. $_->enable for values %{$self->_fields};
  223. }
  224. sub disable {
  225. my ($self) = @_;
  226. $_->disable for values %{$self->_fields};
  227. }
  228. sub _on_kill_focus {
  229. my ($self, $opt_id) = @_;
  230. # nothing
  231. }
  232. package Slic3r::GUI::OptionsGroup::Line;
  233. use Moo;
  234. has 'label' => (is => 'rw', default => sub { "" });
  235. has 'full_width' => (is => 'rw', default => sub { 0 });
  236. has 'label_tooltip' => (is => 'rw', default => sub { "" });
  237. has 'sizer' => (is => 'rw');
  238. has 'widget' => (is => 'rw');
  239. has '_options' => (is => 'ro', default => sub { [] });
  240. # Extra UI components after the label and the edit widget of the option.
  241. has '_extra_widgets' => (is => 'ro', default => sub { [] });
  242. # this method accepts a Slic3r::GUI::OptionsGroup::Option object
  243. sub append_option {
  244. my ($self, $option) = @_;
  245. push @{$self->_options}, $option;
  246. }
  247. sub append_widget {
  248. my ($self, $widget) = @_;
  249. push @{$self->_extra_widgets}, $widget;
  250. }
  251. sub get_options {
  252. my ($self) = @_;
  253. return [ @{$self->_options} ];
  254. }
  255. sub get_extra_widgets {
  256. my ($self) = @_;
  257. return [ @{$self->_extra_widgets} ];
  258. }
  259. # Configuration of an option.
  260. # This very much reflects the content of the C++ ConfigOptionDef class.
  261. package Slic3r::GUI::OptionsGroup::Option;
  262. use Moo;
  263. has 'opt_id' => (is => 'rw', required => 1);
  264. has 'type' => (is => 'rw', required => 1);
  265. has 'default' => (is => 'rw', required => 1);
  266. has 'gui_type' => (is => 'rw', default => sub { undef });
  267. has 'gui_flags' => (is => 'rw', default => sub { "" });
  268. has 'label' => (is => 'rw', default => sub { "" });
  269. has 'sidetext' => (is => 'rw', default => sub { "" });
  270. has 'tooltip' => (is => 'rw', default => sub { "" });
  271. has 'multiline' => (is => 'rw', default => sub { 0 });
  272. has 'full_width' => (is => 'rw', default => sub { 0 });
  273. has 'width' => (is => 'rw', default => sub { undef });
  274. has 'height' => (is => 'rw', default => sub { undef });
  275. has 'min' => (is => 'rw', default => sub { undef });
  276. has 'max' => (is => 'rw', default => sub { undef });
  277. has 'labels' => (is => 'rw', default => sub { [] });
  278. has 'values' => (is => 'rw', default => sub { [] });
  279. has 'readonly' => (is => 'rw', default => sub { 0 });
  280. has 'side_widget' => (is => 'rw', default => sub { undef });
  281. package Slic3r::GUI::ConfigOptionsGroup;
  282. use Moo;
  283. use List::Util qw(first);
  284. extends 'Slic3r::GUI::OptionsGroup';
  285. has 'config' => (is => 'ro', required => 1);
  286. has 'full_labels' => (is => 'ro', default => sub { 0 });
  287. has '_opt_map' => (is => 'ro', default => sub { {} });
  288. sub get_option {
  289. my ($self, $opt_key, $opt_index) = @_;
  290. $opt_index //= -1;
  291. if (!$self->config->has($opt_key)) {
  292. die "No $opt_key in ConfigOptionsGroup config";
  293. }
  294. my $opt_id = ($opt_index == -1 ? $opt_key : "${opt_key}#${opt_index}");
  295. $self->_opt_map->{$opt_id} = [ $opt_key, $opt_index ];
  296. # Slic3r::Config::Options is a C++ Slic3r::PrintConfigDef exported as a Perl hash of hashes.
  297. # The C++ counterpart is a constant singleton.
  298. my $optdef = $Slic3r::Config::Options->{$opt_key}; # we should access this from $self->config
  299. my $default_value = $self->_get_config_value($opt_key, $opt_index, $optdef->{gui_flags} =~ /\bserialized\b/);
  300. return Slic3r::GUI::OptionsGroup::Option->new(
  301. opt_id => $opt_id,
  302. type => $optdef->{type},
  303. default => $default_value,
  304. gui_type => $optdef->{gui_type},
  305. gui_flags => $optdef->{gui_flags},
  306. label => ($self->full_labels && defined $optdef->{full_label}) ? $optdef->{full_label} : $optdef->{label},
  307. sidetext => $optdef->{sidetext},
  308. # calling serialize() ensures we get a stringified value
  309. tooltip => $optdef->{tooltip} . " (default: " . $self->config->serialize($opt_key) . ")",
  310. multiline => $optdef->{multiline},
  311. width => $optdef->{width},
  312. min => $optdef->{min},
  313. max => $optdef->{max},
  314. labels => $optdef->{labels},
  315. values => $optdef->{values},
  316. readonly => $optdef->{readonly},
  317. );
  318. }
  319. sub create_single_option_line {
  320. my ($self, $opt_key, $opt_index) = @_;
  321. my $option;
  322. if (ref($opt_key)) {
  323. $option = $opt_key;
  324. } else {
  325. $option = $self->get_option($opt_key, $opt_index);
  326. }
  327. return $self->SUPER::create_single_option_line($option);
  328. }
  329. sub append_single_option_line {
  330. my ($self, $option, $opt_index) = @_;
  331. return $self->append_line($self->create_single_option_line($option, $opt_index));
  332. }
  333. # Initialize UI components with the config values.
  334. sub reload_config {
  335. my ($self) = @_;
  336. foreach my $opt_id (keys %{ $self->_opt_map }) {
  337. my ($opt_key, $opt_index) = @{ $self->_opt_map->{$opt_id} };
  338. my $option = $self->_options->{$opt_id};
  339. $self->set_value($opt_id, $self->_get_config_value($opt_key, $opt_index, $option->gui_flags =~ /\bserialized\b/));
  340. }
  341. }
  342. sub get_fieldc {
  343. my ($self, $opt_key, $opt_index) = @_;
  344. $opt_index //= -1;
  345. my $opt_id = first { $self->_opt_map->{$_}[0] eq $opt_key && $self->_opt_map->{$_}[1] == $opt_index }
  346. keys %{$self->_opt_map};
  347. return defined($opt_id) ? $self->get_field($opt_id) : undef;
  348. }
  349. sub _get_config_value {
  350. my ($self, $opt_key, $opt_index, $deserialize) = @_;
  351. if ($deserialize) {
  352. # Want to edit a vector value (currently only multi-strings) in a single edit box.
  353. # Aggregate the strings the old way.
  354. # Currently used for the post_process config value only.
  355. die "Can't deserialize option indexed value" if $opt_index != -1;
  356. return join(';', @{$self->config->get($opt_key)});
  357. } else {
  358. return $opt_index == -1
  359. ? $self->config->get($opt_key)
  360. : $self->config->get_at($opt_key, $opt_index);
  361. }
  362. }
  363. sub _on_change {
  364. my ($self, $opt_id, $value) = @_;
  365. if (exists $self->_opt_map->{$opt_id}) {
  366. my ($opt_key, $opt_index) = @{ $self->_opt_map->{$opt_id} };
  367. my $option = $self->_options->{$opt_id};
  368. # get value
  369. my $field_value = $self->get_value($opt_id);
  370. if ($option->gui_flags =~ /\bserialized\b/) {
  371. die "Can't set serialized option indexed value" if $opt_index != -1;
  372. # Split a string to multiple strings by a semi-colon. This is the old way of storing multi-string values.
  373. # Currently used for the post_process config value only.
  374. my @values = split /;/, $field_value;
  375. $self->config->set($opt_key, \@values);
  376. } else {
  377. if ($opt_index == -1) {
  378. $self->config->set($opt_key, $field_value);
  379. } else {
  380. my $value = $self->config->get($opt_key);
  381. $value->[$opt_index] = $field_value;
  382. $self->config->set($opt_key, $value);
  383. }
  384. }
  385. }
  386. $self->SUPER::_on_change($opt_id, $value);
  387. }
  388. sub _on_kill_focus {
  389. my ($self, $opt_id) = @_;
  390. # when a field loses focus, reapply the config value to it
  391. # (thus discarding any invalid input and reverting to the last
  392. # accepted value)
  393. $self->reload_config;
  394. }
  395. # Static text shown among the options.
  396. # Currently used for the filament cooling legend only.
  397. package Slic3r::GUI::OptionsGroup::StaticText;
  398. use Wx qw(:misc :systemsettings);
  399. use base 'Wx::StaticText';
  400. sub new {
  401. my ($class, $parent) = @_;
  402. my $self = $class->SUPER::new($parent, -1, "", wxDefaultPosition, wxDefaultSize);
  403. my $font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  404. $self->SetFont($font);
  405. return $self;
  406. }
  407. sub SetText {
  408. my ($self, $value) = @_;
  409. $self->SetLabel($value);
  410. $self->Wrap(400);
  411. $self->GetParent->Layout;
  412. }
  413. 1;