OptionsGroup.pm 15 KB

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