OptionsGroup.pm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. my ($opt_id, $value) = @_;
  134. $self->_on_change($opt_id, $value)
  135. unless $self->_disabled;
  136. };
  137. my $on_kill_focus = sub {
  138. my ($opt_id) = @_;
  139. $self->_on_kill_focus($opt_id);
  140. };
  141. my $type = $opt->{gui_type} || $opt->{type};
  142. my $field;
  143. if ($type eq 'bool') {
  144. $field = Slic3r::GUI::OptionsGroup::Field::Checkbox->new(
  145. parent => $self->parent,
  146. option => $opt,
  147. );
  148. } elsif ($type eq 'i') {
  149. $field = Slic3r::GUI::OptionsGroup::Field::SpinCtrl->new(
  150. parent => $self->parent,
  151. option => $opt,
  152. );
  153. } elsif ($type eq 'color') {
  154. $field = Slic3r::GUI::OptionsGroup::Field::ColourPicker->new(
  155. parent => $self->parent,
  156. option => $opt,
  157. );
  158. } elsif ($type =~ /^(f|s|s@|percent)$/) {
  159. $field = Slic3r::GUI::OptionsGroup::Field::TextCtrl->new(
  160. parent => $self->parent,
  161. option => $opt,
  162. );
  163. } elsif ($type eq 'select' || $type eq 'select_open') {
  164. $field = Slic3r::GUI::OptionsGroup::Field::Choice->new(
  165. parent => $self->parent,
  166. option => $opt,
  167. );
  168. } elsif ($type eq 'f_enum_open' || $type eq 'i_enum_open' || $type eq 'i_enum_closed') {
  169. $field = Slic3r::GUI::OptionsGroup::Field::NumericChoice->new(
  170. parent => $self->parent,
  171. option => $opt,
  172. );
  173. } elsif ($type eq 'point') {
  174. $field = Slic3r::GUI::OptionsGroup::Field::Point->new(
  175. parent => $self->parent,
  176. option => $opt,
  177. );
  178. } elsif ($type eq 'slider') {
  179. $field = Slic3r::GUI::OptionsGroup::Field::Slider->new(
  180. parent => $self->parent,
  181. option => $opt,
  182. );
  183. }
  184. return undef if !$field;
  185. $field->on_change($on_change);
  186. $field->on_kill_focus($on_kill_focus);
  187. $self->_fields->{$opt_id} = $field;
  188. return $field->isa('Slic3r::GUI::OptionsGroup::Field::wxWindow')
  189. ? $field->wxWindow
  190. : $field->wxSizer;
  191. }
  192. sub get_option {
  193. my ($self, $opt_id) = @_;
  194. return undef if !exists $self->_options->{$opt_id};
  195. return $self->_options->{$opt_id};
  196. }
  197. sub get_field {
  198. my ($self, $opt_id) = @_;
  199. return undef if !exists $self->_fields->{$opt_id};
  200. return $self->_fields->{$opt_id};
  201. }
  202. sub get_value {
  203. my ($self, $opt_id) = @_;
  204. return if !exists $self->_fields->{$opt_id};
  205. return $self->_fields->{$opt_id}->get_value;
  206. }
  207. sub set_value {
  208. my ($self, $opt_id, $value) = @_;
  209. return if !exists $self->_fields->{$opt_id};
  210. $self->_fields->{$opt_id}->set_value($value);
  211. }
  212. sub _on_change {
  213. my ($self, $opt_id, $value) = @_;
  214. $self->on_change->($opt_id, $value);
  215. }
  216. sub enable {
  217. my ($self) = @_;
  218. $_->enable for values %{$self->_fields};
  219. }
  220. sub disable {
  221. my ($self) = @_;
  222. $_->disable for values %{$self->_fields};
  223. }
  224. sub _on_kill_focus {
  225. my ($self, $opt_id) = @_;
  226. # nothing
  227. }
  228. package Slic3r::GUI::OptionsGroup::Line;
  229. use Moo;
  230. has 'label' => (is => 'rw', default => sub { "" });
  231. has 'full_width' => (is => 'rw', default => sub { 0 });
  232. has 'label_tooltip' => (is => 'rw', default => sub { "" });
  233. has 'sizer' => (is => 'rw');
  234. has 'widget' => (is => 'rw');
  235. has '_options' => (is => 'ro', default => sub { [] });
  236. # Extra UI components after the label and the edit widget of the option.
  237. has '_extra_widgets' => (is => 'ro', default => sub { [] });
  238. # this method accepts a Slic3r::GUI::OptionsGroup::Option object
  239. sub append_option {
  240. my ($self, $option) = @_;
  241. push @{$self->_options}, $option;
  242. }
  243. sub append_widget {
  244. my ($self, $widget) = @_;
  245. push @{$self->_extra_widgets}, $widget;
  246. }
  247. sub get_options {
  248. my ($self) = @_;
  249. return [ @{$self->_options} ];
  250. }
  251. sub get_extra_widgets {
  252. my ($self) = @_;
  253. return [ @{$self->_extra_widgets} ];
  254. }
  255. # Configuration of an option.
  256. # This very much reflects the content of the C++ ConfigOptionDef class.
  257. package Slic3r::GUI::OptionsGroup::Option;
  258. use Moo;
  259. has 'opt_id' => (is => 'rw', required => 1);
  260. has 'type' => (is => 'rw', required => 1);
  261. has 'default' => (is => 'rw', required => 1);
  262. has 'gui_type' => (is => 'rw', default => sub { undef });
  263. has 'gui_flags' => (is => 'rw', default => sub { "" });
  264. has 'label' => (is => 'rw', default => sub { "" });
  265. has 'sidetext' => (is => 'rw', default => sub { "" });
  266. has 'tooltip' => (is => 'rw', default => sub { "" });
  267. has 'multiline' => (is => 'rw', default => sub { 0 });
  268. has 'full_width' => (is => 'rw', default => sub { 0 });
  269. has 'width' => (is => 'rw', default => sub { undef });
  270. has 'height' => (is => 'rw', default => sub { undef });
  271. has 'min' => (is => 'rw', default => sub { undef });
  272. has 'max' => (is => 'rw', default => sub { undef });
  273. has 'labels' => (is => 'rw', default => sub { [] });
  274. has 'values' => (is => 'rw', default => sub { [] });
  275. has 'readonly' => (is => 'rw', default => sub { 0 });
  276. has 'side_widget' => (is => 'rw', default => sub { undef });
  277. package Slic3r::GUI::ConfigOptionsGroup;
  278. use Moo;
  279. use List::Util qw(first);
  280. extends 'Slic3r::GUI::OptionsGroup';
  281. has 'config' => (is => 'ro', required => 1);
  282. has 'full_labels' => (is => 'ro', default => sub { 0 });
  283. has '_opt_map' => (is => 'ro', default => sub { {} });
  284. sub get_option {
  285. my ($self, $opt_key, $opt_index) = @_;
  286. $opt_index //= -1;
  287. if (!$self->config->has($opt_key)) {
  288. die "No $opt_key in ConfigOptionsGroup config";
  289. }
  290. my $opt_id = ($opt_index == -1 ? $opt_key : "${opt_key}#${opt_index}");
  291. $self->_opt_map->{$opt_id} = [ $opt_key, $opt_index ];
  292. # Slic3r::Config::Options is a C++ Slic3r::PrintConfigDef exported as a Perl hash of hashes.
  293. # The C++ counterpart is a constant singleton.
  294. my $optdef = $Slic3r::Config::Options->{$opt_key}; # we should access this from $self->config
  295. my $default_value = $self->_get_config_value($opt_key, $opt_index, $optdef->{gui_flags} =~ /\bserialized\b/);
  296. return Slic3r::GUI::OptionsGroup::Option->new(
  297. opt_id => $opt_id,
  298. type => $optdef->{type},
  299. default => $default_value,
  300. gui_type => $optdef->{gui_type},
  301. gui_flags => $optdef->{gui_flags},
  302. label => ($self->full_labels && defined $optdef->{full_label}) ? $optdef->{full_label} : $optdef->{label},
  303. sidetext => $optdef->{sidetext},
  304. # calling serialize() ensures we get a stringified value
  305. tooltip => $optdef->{tooltip} . " (default: " . $self->config->serialize($opt_key) . ")",
  306. multiline => $optdef->{multiline},
  307. width => $optdef->{width},
  308. min => $optdef->{min},
  309. max => $optdef->{max},
  310. labels => $optdef->{labels},
  311. values => $optdef->{values},
  312. readonly => $optdef->{readonly},
  313. );
  314. }
  315. sub create_single_option_line {
  316. my ($self, $opt_key, $opt_index) = @_;
  317. my $option;
  318. if (ref($opt_key)) {
  319. $option = $opt_key;
  320. } else {
  321. $option = $self->get_option($opt_key, $opt_index);
  322. }
  323. return $self->SUPER::create_single_option_line($option);
  324. }
  325. sub append_single_option_line {
  326. my ($self, $option, $opt_index) = @_;
  327. return $self->append_line($self->create_single_option_line($option, $opt_index));
  328. }
  329. # Initialize UI components with the config values.
  330. sub reload_config {
  331. my ($self) = @_;
  332. foreach my $opt_id (keys %{ $self->_opt_map }) {
  333. my ($opt_key, $opt_index) = @{ $self->_opt_map->{$opt_id} };
  334. my $option = $self->_options->{$opt_id};
  335. $self->set_value($opt_id, $self->_get_config_value($opt_key, $opt_index, $option->gui_flags =~ /\bserialized\b/));
  336. }
  337. }
  338. sub get_fieldc {
  339. my ($self, $opt_key, $opt_index) = @_;
  340. $opt_index //= -1;
  341. my $opt_id = first { $self->_opt_map->{$_}[0] eq $opt_key && $self->_opt_map->{$_}[1] == $opt_index }
  342. keys %{$self->_opt_map};
  343. return defined($opt_id) ? $self->get_field($opt_id) : undef;
  344. }
  345. sub _get_config_value {
  346. my ($self, $opt_key, $opt_index, $deserialize) = @_;
  347. if ($deserialize) {
  348. # Want to edit a vector value (currently only multi-strings) in a single edit box.
  349. # Aggregate the strings the old way.
  350. # Currently used for the post_process config value only.
  351. die "Can't deserialize option indexed value" if $opt_index != -1;
  352. return join(';', @{$self->config->get($opt_key)});
  353. } else {
  354. return $opt_index == -1
  355. ? $self->config->get($opt_key)
  356. : $self->config->get_at($opt_key, $opt_index);
  357. }
  358. }
  359. sub _on_change {
  360. my ($self, $opt_id, $value) = @_;
  361. if (exists $self->_opt_map->{$opt_id}) {
  362. my ($opt_key, $opt_index) = @{ $self->_opt_map->{$opt_id} };
  363. my $option = $self->_options->{$opt_id};
  364. # get value
  365. my $field_value = $self->get_value($opt_id);
  366. if ($option->gui_flags =~ /\bserialized\b/) {
  367. die "Can't set serialized option indexed value" if $opt_index != -1;
  368. # Split a string to multiple strings by a semi-colon. This is the old way of storing multi-string values.
  369. # Currently used for the post_process config value only.
  370. my @values = split /;/, $field_value;
  371. $self->config->set($opt_key, \@values);
  372. } else {
  373. if ($opt_index == -1) {
  374. $self->config->set($opt_key, $field_value);
  375. } else {
  376. my $value = $self->config->get($opt_key);
  377. $value->[$opt_index] = $field_value;
  378. $self->config->set($opt_key, $value);
  379. }
  380. }
  381. }
  382. $self->SUPER::_on_change($opt_id, $value);
  383. }
  384. sub _on_kill_focus {
  385. my ($self, $opt_id) = @_;
  386. # when a field loses focus, reapply the config value to it
  387. # (thus discarding any invalid input and reverting to the last
  388. # accepted value)
  389. $self->reload_config;
  390. }
  391. # Static text shown among the options.
  392. # Currently used for the filament cooling legend only.
  393. package Slic3r::GUI::OptionsGroup::StaticText;
  394. use Wx qw(:misc :systemsettings);
  395. use base 'Wx::StaticText';
  396. sub new {
  397. my ($class, $parent) = @_;
  398. my $self = $class->SUPER::new($parent, -1, "", wxDefaultPosition, wxDefaultSize);
  399. my $font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  400. $self->SetFont($font);
  401. return $self;
  402. }
  403. sub SetText {
  404. my ($self, $value) = @_;
  405. $self->SetLabel($value);
  406. $self->Wrap(400);
  407. $self->GetParent->Layout;
  408. }
  409. 1;