Tab.pm 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  1. # The "Expert" tab at the right of the main tabbed window.
  2. # This file implements following packages:
  3. # Slic3r::GUI::Tab;
  4. # Slic3r::GUI::Tab::Print;
  5. # Slic3r::GUI::Tab::Filament;
  6. # Slic3r::GUI::Tab::Printer;
  7. # Slic3r::GUI::Tab::Page
  8. # - Option page: For example, the Slic3r::GUI::Tab::Print has option pages "Layers and perimeters", "Infill", "Skirt and brim" ...
  9. # Slic3r::GUI::SavePresetWindow
  10. # - Dialog to select a new preset name to store the configuration.
  11. # Slic3r::GUI::Tab::Preset;
  12. # - Single preset item: name, file is default or external.
  13. package Slic3r::GUI::Tab;
  14. use strict;
  15. use warnings;
  16. use utf8;
  17. use File::Basename qw(basename);
  18. use List::Util qw(first);
  19. use Wx qw(:bookctrl :dialog :keycode :icon :id :misc :panel :sizer :treectrl :window
  20. :button wxTheApp);
  21. use Wx::Event qw(EVT_BUTTON EVT_CHOICE EVT_KEY_DOWN EVT_TREE_SEL_CHANGED);
  22. use base qw(Wx::Panel Class::Accessor);
  23. # Index of the currently active preset.
  24. __PACKAGE__->mk_accessors(qw(current_preset));
  25. sub new {
  26. my $class = shift;
  27. my ($parent, %params) = @_;
  28. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxBK_LEFT | wxTAB_TRAVERSAL);
  29. # Vertical sizer to hold the choice menu and the rest of the page.
  30. $self->{sizer} = Wx::BoxSizer->new(wxVERTICAL);
  31. $self->{sizer}->SetSizeHints($self);
  32. $self->SetSizer($self->{sizer});
  33. # preset chooser
  34. {
  35. # choice menu
  36. $self->{presets_choice} = Wx::Choice->new($self, -1, wxDefaultPosition, [270, -1], []);
  37. $self->{presets_choice}->SetFont($Slic3r::GUI::small_font);
  38. # buttons
  39. $self->{btn_save_preset} = Wx::BitmapButton->new($self, -1, Wx::Bitmap->new($Slic3r::var->("disk.png"), wxBITMAP_TYPE_PNG),
  40. wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
  41. $self->{btn_delete_preset} = Wx::BitmapButton->new($self, -1, Wx::Bitmap->new($Slic3r::var->("delete.png"), wxBITMAP_TYPE_PNG),
  42. wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
  43. $self->{btn_save_preset}->SetToolTipString("Save current " . lc($self->title));
  44. $self->{btn_delete_preset}->SetToolTipString("Delete this preset");
  45. $self->{btn_delete_preset}->Disable;
  46. my $hsizer = Wx::BoxSizer->new(wxHORIZONTAL);
  47. $self->{sizer}->Add($hsizer, 0, wxBOTTOM, 3);
  48. $hsizer->Add($self->{presets_choice}, 1, wxLEFT | wxRIGHT | wxTOP | wxALIGN_CENTER_VERTICAL, 3);
  49. $hsizer->Add($self->{btn_save_preset}, 0, wxALIGN_CENTER_VERTICAL);
  50. $hsizer->Add($self->{btn_delete_preset}, 0, wxALIGN_CENTER_VERTICAL);
  51. }
  52. # Horizontal sizer to hold the tree and the selected page.
  53. $self->{hsizer} = Wx::BoxSizer->new(wxHORIZONTAL);
  54. $self->{sizer}->Add($self->{hsizer}, 1, wxEXPAND, 0);
  55. # left vertical sizer
  56. my $left_sizer = Wx::BoxSizer->new(wxVERTICAL);
  57. $self->{hsizer}->Add($left_sizer, 0, wxEXPAND | wxLEFT | wxTOP | wxBOTTOM, 3);
  58. # tree
  59. $self->{treectrl} = Wx::TreeCtrl->new($self, -1, wxDefaultPosition, [185, -1], wxTR_NO_BUTTONS | wxTR_HIDE_ROOT | wxTR_SINGLE | wxTR_NO_LINES | wxBORDER_SUNKEN | wxWANTS_CHARS);
  60. $left_sizer->Add($self->{treectrl}, 1, wxEXPAND);
  61. $self->{icons} = Wx::ImageList->new(16, 16, 1);
  62. $self->{treectrl}->AssignImageList($self->{icons});
  63. $self->{iconcount} = -1;
  64. $self->{treectrl}->AddRoot("root");
  65. $self->{pages} = [];
  66. $self->{treectrl}->SetIndent(0);
  67. $self->{disable_tree_sel_changed_event} = 0;
  68. EVT_TREE_SEL_CHANGED($parent, $self->{treectrl}, sub {
  69. return if $self->{disable_tree_sel_changed_event};
  70. my $page = first { $_->{title} eq $self->{treectrl}->GetItemText($self->{treectrl}->GetSelection) } @{$self->{pages}}
  71. or return;
  72. $_->Hide for @{$self->{pages}};
  73. $page->Show;
  74. $self->{hsizer}->Layout;
  75. $self->Refresh;
  76. });
  77. EVT_KEY_DOWN($self->{treectrl}, sub {
  78. my ($treectrl, $event) = @_;
  79. if ($event->GetKeyCode == WXK_TAB) {
  80. $treectrl->Navigate($event->ShiftDown ? &Wx::wxNavigateBackward : &Wx::wxNavigateForward);
  81. } else {
  82. $event->Skip;
  83. }
  84. });
  85. EVT_CHOICE($parent, $self->{presets_choice}, sub {
  86. $self->on_select_preset;
  87. $self->_on_presets_changed;
  88. });
  89. EVT_BUTTON($self, $self->{btn_save_preset}, sub { $self->save_preset });
  90. EVT_BUTTON($self, $self->{btn_delete_preset}, sub {
  91. my $i = $self->current_preset;
  92. # Don't let the user delete the '- default -' configuration.
  93. # This shall not happen as the 'delete' button is disabled for the '- default -' entry,
  94. # but better be safe than sorry.
  95. return if ($i == 0 && $self->get_current_preset->{default});
  96. my $res = Wx::MessageDialog->new($self, "Are you sure you want to delete the selected preset?", 'Delete Preset', wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION)->ShowModal;
  97. return unless $res == wxID_YES;
  98. # Delete the file.
  99. my $path = $self->{presets}[$i]->file;
  100. my $enc_path = Slic3r::encode_path($path);
  101. if (-e $enc_path && ! unlink $enc_path) {
  102. # Cannot delete the file, therefore the item will not be removed from the selection.
  103. Slic3r::GUI::show_error($self, "Cannot delete file $path : $!");
  104. return;
  105. }
  106. # Delete the preset.
  107. splice @{$self->{presets}}, $i, 1;
  108. # Delete the item from the UI component.
  109. $self->{presets_choice}->Delete($i - $self->{default_suppressed});
  110. $self->current_preset(undef);
  111. if ($self->{default_suppressed} && scalar(@{$self->{presets}}) == 1) {
  112. # Empty selection. Add the '- default -' item into the drop down selection.
  113. $self->{presets_choice}->Append($self->{presets}->[0]->name);
  114. # and remember that the '- default -' is shown.
  115. $self->{default_suppressed} = 0;
  116. }
  117. # Select the 0th item. If default is suppressed, select the first valid.
  118. $self->select_preset($self->{default_suppressed});
  119. $self->_on_presets_changed;
  120. });
  121. # C++ instance DynamicPrintConfig
  122. $self->{config} = Slic3r::Config->new;
  123. # Initialize the DynamicPrintConfig by default keys/values.
  124. # Possible %params keys: no_controller
  125. $self->build(%params);
  126. $self->update_tree;
  127. $self->_update;
  128. if ($self->hidden_options) {
  129. $self->{config}->apply(Slic3r::Config->new_from_defaults($self->hidden_options));
  130. }
  131. return $self;
  132. }
  133. # Are the '- default -' selections suppressed by the Slic3r GUI preferences?
  134. sub no_defaults {
  135. return $Slic3r::GUI::Settings->{_}{no_defaults} ? 1 : 0;
  136. }
  137. # Get a currently active preset (Perl class Slic3r::GUI::Tab::Preset).
  138. sub get_current_preset {
  139. my $self = shift;
  140. return $self->get_preset($self->current_preset);
  141. }
  142. # Get a preset (Perl class Slic3r::GUI::Tab::Preset) with an index $i.
  143. sub get_preset {
  144. my ($self, $i) = @_;
  145. return $self->{presets}[$i];
  146. }
  147. sub save_preset {
  148. my ($self, $name) = @_;
  149. # since buttons (and choices too) don't get focus on Mac, we set focus manually
  150. # to the treectrl so that the EVT_* events are fired for the input field having
  151. # focus currently. is there anything better than this?
  152. $self->{treectrl}->SetFocus;
  153. if (!defined $name) {
  154. my $preset = $self->get_current_preset;
  155. my $default_name = $preset->default ? 'Untitled' : $preset->name;
  156. $default_name =~ s/\.[iI][nN][iI]$//;
  157. my $dlg = Slic3r::GUI::SavePresetWindow->new($self,
  158. title => lc($self->title),
  159. default => $default_name,
  160. values => [ map $_->name, grep !$_->default && !$_->external, @{$self->{presets}} ],
  161. );
  162. return unless $dlg->ShowModal == wxID_OK;
  163. $name = $dlg->get_name;
  164. }
  165. $self->config->save(sprintf "$Slic3r::GUI::datadir/%s/%s.ini", $self->name, $name);
  166. $self->load_presets;
  167. $self->select_preset_by_name($name);
  168. $self->_on_presets_changed;
  169. }
  170. sub on_value_change {
  171. my ($self, $cb) = @_;
  172. $self->{on_value_change} = $cb;
  173. }
  174. sub on_presets_changed {
  175. my ($self, $cb) = @_;
  176. $self->{on_presets_changed} = $cb;
  177. }
  178. # This method is called whenever an option field is changed by the user.
  179. # Propagate event to the parent through the 'on_value_changed' callback
  180. # and call _update.
  181. sub _on_value_change {
  182. my ($self, $key, $value) = @_;
  183. $self->{on_value_change}->($key, $value) if $self->{on_value_change};
  184. $self->_update({ $key => 1 });
  185. }
  186. # Override this to capture changes of configuration caused either by loading or switching a preset,
  187. # or by a user changing an option field.
  188. sub _update {}
  189. sub _on_presets_changed {
  190. my $self = shift;
  191. $self->{on_presets_changed}->(
  192. $self->{presets},
  193. $self->{default_suppressed},
  194. scalar($self->{presets_choice}->GetSelection) + $self->{default_suppressed},
  195. $self->is_dirty,
  196. ) if $self->{on_presets_changed};
  197. }
  198. sub on_preset_loaded {}
  199. sub hidden_options {}
  200. sub config { $_[0]->{config}->clone }
  201. sub select_default_preset {
  202. my $self = shift;
  203. $self->select_preset(0);
  204. }
  205. sub select_preset {
  206. my ($self, $i) = @_;
  207. if ($self->{default_suppressed} && $i == 0) {
  208. # Selecting the '- default -'. Add it to the combo box.
  209. $self->{default_suppressed} = 0;
  210. $self->{presets_choice}->Insert($self->{presets}->[0]->name, 0);
  211. } elsif ($self->no_defaults && ! $self->{default_suppressed} && $i > 0) {
  212. # The user wants to hide the '- default -' items and a non-default item has been added to the presets.
  213. # Hide the '- default -' item.
  214. $self->{presets_choice}->Delete(0);
  215. $self->{default_suppressed} = 1;
  216. }
  217. $self->{presets_choice}->SetSelection($i - $self->{default_suppressed});
  218. $self->on_select_preset;
  219. }
  220. sub select_preset_by_name {
  221. my ($self, $name) = @_;
  222. $name = Slic3r::normalize_utf8_nfc($name);
  223. $self->select_preset(first { $self->{presets}[$_]->name eq $name } 0 .. $#{$self->{presets}});
  224. }
  225. sub on_select_preset {
  226. my $self = shift;
  227. if ($self->is_dirty) {
  228. # Display a dialog showing the dirty options in a human readable form.
  229. my $old_preset = $self->get_current_preset;
  230. my $name = $old_preset->default ? 'Default preset' : "Preset \"" . $old_preset->name . "\"";
  231. my @option_names = ();
  232. foreach my $opt_key (@{$self->dirty_options}) {
  233. my $opt = $Slic3r::Config::Options->{$opt_key};
  234. my $name = $opt->{full_label} // $opt->{label};
  235. if ($opt->{category}) {
  236. $name = $opt->{category} . " > $name";
  237. }
  238. push @option_names, $name;
  239. }
  240. my $changes = join "\n", map "- $_", @option_names;
  241. my $confirm = Wx::MessageDialog->new($self, "$name has unsaved changes:\n$changes\n\nDiscard changes and continue anyway?",
  242. 'Unsaved Changes', wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
  243. if ($confirm->ShowModal == wxID_NO) {
  244. $self->{presets_choice}->SetSelection($self->current_preset - $self->{default_suppressed});
  245. # trigger the on_presets_changed event so that we also restore the previous value
  246. # in the plater selector
  247. $self->_on_presets_changed;
  248. return;
  249. }
  250. }
  251. $self->current_preset($self->{presets_choice}->GetSelection + $self->{default_suppressed});
  252. my $preset = $self->get_current_preset;
  253. my $preset_config = $self->get_preset_config($preset);
  254. eval {
  255. local $SIG{__WARN__} = Slic3r::GUI::warning_catcher($self);
  256. my %keys_modified = ();
  257. foreach my $opt_key (@{$self->{config}->get_keys}) {
  258. if ($preset_config->has($opt_key)) {
  259. if ($self->{config}->serialize($opt_key) ne $preset_config->serialize($opt_key)) {
  260. $self->{config}->set($opt_key, $preset_config->get($opt_key));
  261. $keys_modified{$opt_key} = 1;
  262. }
  263. }
  264. }
  265. ($preset->default || $preset->external)
  266. ? $self->{btn_delete_preset}->Disable
  267. : $self->{btn_delete_preset}->Enable;
  268. $self->_update(\%keys_modified);
  269. $self->on_preset_loaded;
  270. $self->reload_config;
  271. $Slic3r::GUI::Settings->{presets}{$self->name} = $preset->file ? basename($preset->file) : '';
  272. };
  273. if ($@) {
  274. $@ = "I was unable to load the selected config file: $@";
  275. Slic3r::GUI::catch_error($self);
  276. $self->select_default_preset;
  277. }
  278. # use CallAfter because some field triggers schedule on_change calls using CallAfter,
  279. # and we don't want them to be called after this update_dirty() as they would mark the
  280. # preset dirty again
  281. # (not sure this is true anymore now that update_dirty is idempotent)
  282. wxTheApp->CallAfter(sub {
  283. $self->_on_presets_changed;
  284. $self->update_dirty;
  285. });
  286. wxTheApp->save_settings;
  287. }
  288. sub init_config_options {
  289. my ($self, @opt_keys) = @_;
  290. $self->{config}->apply(Slic3r::Config->new_from_defaults(@opt_keys));
  291. }
  292. sub add_options_page {
  293. my $self = shift;
  294. my ($title, $icon, %params) = @_;
  295. if ($icon) {
  296. my $bitmap = Wx::Bitmap->new($Slic3r::var->($icon), wxBITMAP_TYPE_PNG);
  297. $self->{icons}->Add($bitmap);
  298. $self->{iconcount}++;
  299. }
  300. my $page = Slic3r::GUI::Tab::Page->new($self, $title, $self->{iconcount});
  301. $page->Hide;
  302. $self->{hsizer}->Add($page, 1, wxEXPAND | wxLEFT, 5);
  303. push @{$self->{pages}}, $page;
  304. return $page;
  305. }
  306. sub reload_config {
  307. my $self = shift;
  308. $_->reload_config for @{$self->{pages}};
  309. }
  310. sub update_tree {
  311. my ($self) = @_;
  312. # get label of the currently selected item
  313. my $selected = $self->{treectrl}->GetItemText($self->{treectrl}->GetSelection);
  314. my $rootItem = $self->{treectrl}->GetRootItem;
  315. $self->{treectrl}->DeleteChildren($rootItem);
  316. my $have_selection = 0;
  317. foreach my $page (@{$self->{pages}}) {
  318. my $itemId = $self->{treectrl}->AppendItem($rootItem, $page->{title}, $page->{iconID});
  319. if ($page->{title} eq $selected) {
  320. $self->{disable_tree_sel_changed_event} = 1;
  321. $self->{treectrl}->SelectItem($itemId);
  322. $self->{disable_tree_sel_changed_event} = 0;
  323. $have_selection = 1;
  324. }
  325. }
  326. if (!$have_selection) {
  327. # this is triggered on first load, so we don't disable the sel change event
  328. $self->{treectrl}->SelectItem($self->{treectrl}->GetFirstChild($rootItem));
  329. }
  330. }
  331. sub update_dirty {
  332. my $self = shift;
  333. my $list_updated;
  334. foreach my $i ($self->{default_suppressed}..$#{$self->{presets}}) {
  335. my $preset = $self->get_preset($i);
  336. my $label = ($i == $self->current_preset && $self->is_dirty) ? $preset->name . " (modified)" : $preset->name;
  337. my $idx = $i - $self->{default_suppressed};
  338. if ($self->{presets_choice}->GetString($idx) ne $label) {
  339. $self->{presets_choice}->SetString($idx, $label);
  340. $list_updated = 1;
  341. }
  342. }
  343. $self->{presets_choice}->SetSelection($self->current_preset - $self->{default_suppressed}) if ($list_updated); # http://trac.wxwidgets.org/ticket/13769
  344. $self->_on_presets_changed;
  345. }
  346. sub is_dirty {
  347. my $self = shift;
  348. return @{$self->dirty_options} > 0;
  349. }
  350. sub dirty_options {
  351. my $self = shift;
  352. return [] if !defined $self->current_preset; # happens during initialization
  353. return $self->get_preset_config($self->get_current_preset)->diff($self->{config});
  354. }
  355. sub load_presets {
  356. my $self = shift;
  357. $self->{presets} = [
  358. Slic3r::GUI::Tab::Preset->new(
  359. default => 1,
  360. name => '- default -',
  361. ),
  362. ];
  363. my %presets = wxTheApp->presets($self->name);
  364. foreach my $preset_name (sort keys %presets) {
  365. push @{$self->{presets}}, Slic3r::GUI::Tab::Preset->new(
  366. name => $preset_name,
  367. file => $presets{$preset_name},
  368. );
  369. }
  370. $self->current_preset(undef);
  371. $self->{default_suppressed} = Slic3r::GUI::Tab->no_defaults && scalar(@{$self->{presets}}) > 1;
  372. $self->{presets_choice}->Clear;
  373. foreach my $preset (@{$self->{presets}}) {
  374. next if ($preset->default && $self->{default_suppressed});
  375. $self->{presets_choice}->Append($preset->name);
  376. }
  377. {
  378. # load last used preset
  379. my $i = first { basename($self->{presets}[$_]->file) eq ($Slic3r::GUI::Settings->{presets}{$self->name} || '') } 1 .. $#{$self->{presets}};
  380. $self->select_preset($i || $self->{default_suppressed});
  381. }
  382. $self->_on_presets_changed;
  383. }
  384. sub load_config_file {
  385. my $self = shift;
  386. my ($file) = @_;
  387. # look for the loaded config among the existing menu items
  388. my $i = first { $self->{presets}[$_]{file} eq $file && $self->{presets}[$_]{external} } 1..$#{$self->{presets}};
  389. if (!$i) {
  390. my $preset_name = basename($file); # keep the .ini suffix
  391. my $preset_new = Slic3r::GUI::Tab::Preset->new(
  392. file => $file,
  393. name => $preset_name,
  394. external => 1,
  395. );
  396. # Try to load the config file before it is entered into the list. If the loading fails, an undef is returned.
  397. return undef if ! defined $preset_new->config;
  398. push @{$self->{presets}}, $preset_new;
  399. $self->{presets_choice}->Append($preset_name);
  400. $i = $#{$self->{presets}};
  401. }
  402. $self->{presets_choice}->SetSelection($i - $self->{default_suppressed});
  403. $self->on_select_preset;
  404. $self->_on_presets_changed;
  405. return 1;
  406. }
  407. sub load_config {
  408. my $self = shift;
  409. my ($config) = @_;
  410. my %keys_modified = ();
  411. foreach my $opt_key (@{$self->{config}->diff($config)}) {
  412. $self->{config}->set($opt_key, $config->get($opt_key));
  413. $keys_modified{$opt_key} = 1;
  414. }
  415. if (keys(%keys_modified)) {
  416. $self->update_dirty;
  417. # Initialize UI components with the config values.
  418. $self->reload_config;
  419. $self->_update(\%keys_modified);
  420. }
  421. }
  422. sub get_preset_config {
  423. my ($self, $preset) = @_;
  424. return $preset->config($self->{config}->get_keys);
  425. }
  426. sub get_field {
  427. my ($self, $opt_key, $opt_index) = @_;
  428. foreach my $page (@{ $self->{pages} }) {
  429. my $field = $page->get_field($opt_key, $opt_index);
  430. return $field if defined $field;
  431. }
  432. return undef;
  433. }
  434. sub set_value {
  435. my $self = shift;
  436. my ($opt_key, $value) = @_;
  437. my $changed = 0;
  438. foreach my $page (@{ $self->{pages} }) {
  439. $changed = 1 if $page->set_value($opt_key, $value);
  440. }
  441. return $changed;
  442. }
  443. package Slic3r::GUI::Tab::Print;
  444. use base 'Slic3r::GUI::Tab';
  445. use List::Util qw(first);
  446. use Wx qw(:icon :dialog :id);
  447. sub name { 'print' }
  448. sub title { 'Print Settings' }
  449. sub build {
  450. my $self = shift;
  451. $self->init_config_options(qw(
  452. layer_height first_layer_height
  453. perimeters spiral_vase
  454. top_solid_layers bottom_solid_layers
  455. extra_perimeters ensure_vertical_shell_thickness avoid_crossing_perimeters thin_walls overhangs
  456. seam_position external_perimeters_first only_one_perimeter_top
  457. fill_density fill_pattern top_fill_pattern bottom_fill_pattern
  458. infill_every_layers infill_only_where_needed
  459. solid_infill_every_layers fill_angle bridge_angle solid_infill_below_area
  460. only_retract_when_crossing_perimeters infill_first
  461. max_print_speed max_volumetric_speed
  462. max_volumetric_extrusion_rate_slope_positive max_volumetric_extrusion_rate_slope_negative
  463. perimeter_speed small_perimeter_speed external_perimeter_speed infill_speed
  464. solid_infill_speed top_solid_infill_speed support_material_speed
  465. support_material_xy_spacing
  466. support_material_interface_speed bridge_speed gap_fill_speed
  467. travel_speed
  468. first_layer_speed
  469. perimeter_acceleration infill_acceleration bridge_acceleration
  470. first_layer_acceleration default_acceleration
  471. skirts skirt_distance skirt_height min_skirt_length
  472. brim_width
  473. support_material support_material_threshold support_material_enforce_layers
  474. raft_layers
  475. support_material_pattern support_material_with_sheath support_material_spacing support_material_synchronize_layers support_material_angle
  476. support_material_interface_layers support_material_interface_spacing support_material_interface_contact_loops
  477. support_material_contact_distance support_material_buildplate_only dont_support_bridges
  478. notes
  479. complete_objects extruder_clearance_radius extruder_clearance_height
  480. gcode_comments output_filename_format
  481. post_process
  482. perimeter_extruder infill_extruder solid_infill_extruder
  483. support_material_extruder support_material_interface_extruder
  484. ooze_prevention standby_temperature_delta
  485. interface_shells
  486. extrusion_width first_layer_extrusion_width perimeter_extrusion_width
  487. external_perimeter_extrusion_width infill_extrusion_width solid_infill_extrusion_width
  488. top_infill_extrusion_width support_material_extrusion_width
  489. infill_overlap bridge_flow_ratio
  490. clip_multipart_objects elefant_foot_compensation xy_size_compensation threads resolution
  491. wipe_tower wipe_tower_x wipe_tower_y wipe_tower_width wipe_tower_per_color_wipe
  492. ));
  493. $self->{config}->set('print_settings_id', '');
  494. {
  495. my $page = $self->add_options_page('Layers and perimeters', 'layers.png');
  496. {
  497. my $optgroup = $page->new_optgroup('Layer height');
  498. $optgroup->append_single_option_line('layer_height');
  499. $optgroup->append_single_option_line('first_layer_height');
  500. }
  501. {
  502. my $optgroup = $page->new_optgroup('Vertical shells');
  503. $optgroup->append_single_option_line('perimeters');
  504. $optgroup->append_single_option_line('spiral_vase');
  505. }
  506. {
  507. my $optgroup = $page->new_optgroup('Horizontal shells');
  508. my $line = Slic3r::GUI::OptionsGroup::Line->new(
  509. label => 'Solid layers',
  510. );
  511. $line->append_option($optgroup->get_option('top_solid_layers'));
  512. $line->append_option($optgroup->get_option('bottom_solid_layers'));
  513. $optgroup->append_line($line);
  514. }
  515. {
  516. my $optgroup = $page->new_optgroup('Quality (slower slicing)');
  517. $optgroup->append_single_option_line('extra_perimeters');
  518. $optgroup->append_single_option_line('only_one_perimeter_top');
  519. $optgroup->append_single_option_line('ensure_vertical_shell_thickness');
  520. $optgroup->append_single_option_line('avoid_crossing_perimeters');
  521. $optgroup->append_single_option_line('thin_walls');
  522. $optgroup->append_single_option_line('overhangs');
  523. }
  524. {
  525. my $optgroup = $page->new_optgroup('Advanced');
  526. $optgroup->append_single_option_line('seam_position');
  527. $optgroup->append_single_option_line('external_perimeters_first');
  528. }
  529. }
  530. {
  531. my $page = $self->add_options_page('Infill', 'infill.png');
  532. {
  533. my $optgroup = $page->new_optgroup('Infill');
  534. $optgroup->append_single_option_line('fill_density');
  535. $optgroup->append_single_option_line('fill_pattern');
  536. $optgroup->append_single_option_line('top_fill_pattern');
  537. $optgroup->append_single_option_line('bottom_fill_pattern');
  538. }
  539. {
  540. my $optgroup = $page->new_optgroup('Reducing printing time');
  541. $optgroup->append_single_option_line('infill_every_layers');
  542. $optgroup->append_single_option_line('infill_only_where_needed');
  543. }
  544. {
  545. my $optgroup = $page->new_optgroup('Advanced');
  546. $optgroup->append_single_option_line('solid_infill_every_layers');
  547. $optgroup->append_single_option_line('fill_angle');
  548. $optgroup->append_single_option_line('solid_infill_below_area');
  549. $optgroup->append_single_option_line('bridge_angle');
  550. $optgroup->append_single_option_line('only_retract_when_crossing_perimeters');
  551. $optgroup->append_single_option_line('infill_first');
  552. }
  553. }
  554. {
  555. my $page = $self->add_options_page('Skirt and brim', 'box.png');
  556. {
  557. my $optgroup = $page->new_optgroup('Skirt');
  558. $optgroup->append_single_option_line('skirts');
  559. $optgroup->append_single_option_line('skirt_distance');
  560. $optgroup->append_single_option_line('skirt_height');
  561. $optgroup->append_single_option_line('min_skirt_length');
  562. }
  563. {
  564. my $optgroup = $page->new_optgroup('Brim');
  565. $optgroup->append_single_option_line('brim_width');
  566. }
  567. }
  568. {
  569. my $page = $self->add_options_page('Support material', 'building.png');
  570. {
  571. my $optgroup = $page->new_optgroup('Support material');
  572. $optgroup->append_single_option_line('support_material');
  573. $optgroup->append_single_option_line('support_material_threshold');
  574. $optgroup->append_single_option_line('support_material_enforce_layers');
  575. }
  576. {
  577. my $optgroup = $page->new_optgroup('Raft');
  578. $optgroup->append_single_option_line('raft_layers');
  579. # $optgroup->append_single_option_line('raft_contact_distance');
  580. }
  581. {
  582. my $optgroup = $page->new_optgroup('Options for support material and raft');
  583. $optgroup->append_single_option_line('support_material_contact_distance');
  584. $optgroup->append_single_option_line('support_material_pattern');
  585. $optgroup->append_single_option_line('support_material_with_sheath');
  586. $optgroup->append_single_option_line('support_material_spacing');
  587. $optgroup->append_single_option_line('support_material_angle');
  588. $optgroup->append_single_option_line('support_material_interface_layers');
  589. $optgroup->append_single_option_line('support_material_interface_spacing');
  590. $optgroup->append_single_option_line('support_material_interface_contact_loops');
  591. $optgroup->append_single_option_line('support_material_buildplate_only');
  592. $optgroup->append_single_option_line('support_material_xy_spacing');
  593. $optgroup->append_single_option_line('dont_support_bridges');
  594. $optgroup->append_single_option_line('support_material_synchronize_layers');
  595. }
  596. }
  597. {
  598. my $page = $self->add_options_page('Speed', 'time.png');
  599. {
  600. my $optgroup = $page->new_optgroup('Speed for print moves');
  601. $optgroup->append_single_option_line('perimeter_speed');
  602. $optgroup->append_single_option_line('small_perimeter_speed');
  603. $optgroup->append_single_option_line('external_perimeter_speed');
  604. $optgroup->append_single_option_line('infill_speed');
  605. $optgroup->append_single_option_line('solid_infill_speed');
  606. $optgroup->append_single_option_line('top_solid_infill_speed');
  607. $optgroup->append_single_option_line('support_material_speed');
  608. $optgroup->append_single_option_line('support_material_interface_speed');
  609. $optgroup->append_single_option_line('bridge_speed');
  610. $optgroup->append_single_option_line('gap_fill_speed');
  611. }
  612. {
  613. my $optgroup = $page->new_optgroup('Speed for non-print moves');
  614. $optgroup->append_single_option_line('travel_speed');
  615. }
  616. {
  617. my $optgroup = $page->new_optgroup('Modifiers');
  618. $optgroup->append_single_option_line('first_layer_speed');
  619. }
  620. {
  621. my $optgroup = $page->new_optgroup('Acceleration control (advanced)');
  622. $optgroup->append_single_option_line('perimeter_acceleration');
  623. $optgroup->append_single_option_line('infill_acceleration');
  624. $optgroup->append_single_option_line('bridge_acceleration');
  625. $optgroup->append_single_option_line('first_layer_acceleration');
  626. $optgroup->append_single_option_line('default_acceleration');
  627. }
  628. {
  629. my $optgroup = $page->new_optgroup('Autospeed (advanced)');
  630. $optgroup->append_single_option_line('max_print_speed');
  631. $optgroup->append_single_option_line('max_volumetric_speed');
  632. $optgroup->append_single_option_line('max_volumetric_extrusion_rate_slope_positive');
  633. $optgroup->append_single_option_line('max_volumetric_extrusion_rate_slope_negative');
  634. }
  635. }
  636. {
  637. my $page = $self->add_options_page('Multiple Extruders', 'funnel.png');
  638. {
  639. my $optgroup = $page->new_optgroup('Extruders');
  640. $optgroup->append_single_option_line('perimeter_extruder');
  641. $optgroup->append_single_option_line('infill_extruder');
  642. $optgroup->append_single_option_line('solid_infill_extruder');
  643. $optgroup->append_single_option_line('support_material_extruder');
  644. $optgroup->append_single_option_line('support_material_interface_extruder');
  645. }
  646. {
  647. my $optgroup = $page->new_optgroup('Ooze prevention');
  648. $optgroup->append_single_option_line('ooze_prevention');
  649. $optgroup->append_single_option_line('standby_temperature_delta');
  650. }
  651. {
  652. my $optgroup = $page->new_optgroup('Wipe tower');
  653. $optgroup->append_single_option_line('wipe_tower');
  654. $optgroup->append_single_option_line('wipe_tower_x');
  655. $optgroup->append_single_option_line('wipe_tower_y');
  656. $optgroup->append_single_option_line('wipe_tower_width');
  657. $optgroup->append_single_option_line('wipe_tower_per_color_wipe');
  658. }
  659. {
  660. my $optgroup = $page->new_optgroup('Advanced');
  661. $optgroup->append_single_option_line('interface_shells');
  662. }
  663. }
  664. {
  665. my $page = $self->add_options_page('Advanced', 'wrench.png');
  666. {
  667. my $optgroup = $page->new_optgroup('Extrusion width',
  668. label_width => 180,
  669. );
  670. $optgroup->append_single_option_line('extrusion_width');
  671. $optgroup->append_single_option_line('first_layer_extrusion_width');
  672. $optgroup->append_single_option_line('perimeter_extrusion_width');
  673. $optgroup->append_single_option_line('external_perimeter_extrusion_width');
  674. $optgroup->append_single_option_line('infill_extrusion_width');
  675. $optgroup->append_single_option_line('solid_infill_extrusion_width');
  676. $optgroup->append_single_option_line('top_infill_extrusion_width');
  677. $optgroup->append_single_option_line('support_material_extrusion_width');
  678. }
  679. {
  680. my $optgroup = $page->new_optgroup('Overlap');
  681. $optgroup->append_single_option_line('infill_overlap');
  682. }
  683. {
  684. my $optgroup = $page->new_optgroup('Flow');
  685. $optgroup->append_single_option_line('bridge_flow_ratio');
  686. }
  687. {
  688. my $optgroup = $page->new_optgroup('Other');
  689. $optgroup->append_single_option_line('clip_multipart_objects');
  690. $optgroup->append_single_option_line('elefant_foot_compensation');
  691. $optgroup->append_single_option_line('xy_size_compensation');
  692. $optgroup->append_single_option_line('threads') if $Slic3r::have_threads;
  693. $optgroup->append_single_option_line('resolution');
  694. }
  695. }
  696. {
  697. my $page = $self->add_options_page('Output options', 'page_white_go.png');
  698. {
  699. my $optgroup = $page->new_optgroup('Sequential printing');
  700. $optgroup->append_single_option_line('complete_objects');
  701. my $line = Slic3r::GUI::OptionsGroup::Line->new(
  702. label => 'Extruder clearance (mm)',
  703. );
  704. foreach my $opt_key (qw(extruder_clearance_radius extruder_clearance_height)) {
  705. my $option = $optgroup->get_option($opt_key);
  706. $option->width(60);
  707. $line->append_option($option);
  708. }
  709. $optgroup->append_line($line);
  710. }
  711. {
  712. my $optgroup = $page->new_optgroup('Output file');
  713. $optgroup->append_single_option_line('gcode_comments');
  714. {
  715. my $option = $optgroup->get_option('output_filename_format');
  716. $option->full_width(1);
  717. $optgroup->append_single_option_line($option);
  718. }
  719. }
  720. {
  721. my $optgroup = $page->new_optgroup('Post-processing scripts',
  722. label_width => 0,
  723. );
  724. my $option = $optgroup->get_option('post_process');
  725. $option->full_width(1);
  726. $option->height(50);
  727. $optgroup->append_single_option_line($option);
  728. }
  729. }
  730. {
  731. my $page = $self->add_options_page('Notes', 'note.png');
  732. {
  733. my $optgroup = $page->new_optgroup('Notes',
  734. label_width => 0,
  735. );
  736. my $option = $optgroup->get_option('notes');
  737. $option->full_width(1);
  738. $option->height(250);
  739. $optgroup->append_single_option_line($option);
  740. }
  741. }
  742. }
  743. # Slic3r::GUI::Tab::Print::_update is called after a configuration preset is loaded or switched, or when a single option is modifed by the user.
  744. sub _update {
  745. # $keys_modified is a reference to hash with modified keys set to 1, unmodified keys missing.
  746. my ($self, $keys_modified) = @_;
  747. $keys_modified //= {};
  748. my $config = $self->{config};
  749. if ($config->spiral_vase && !($config->perimeters == 1 && $config->top_solid_layers == 0 && $config->fill_density == 0)) {
  750. my $dialog = Wx::MessageDialog->new($self,
  751. "The Spiral Vase mode requires:\n"
  752. . "- one perimeter\n"
  753. . "- no top solid layers\n"
  754. . "- 0% fill density\n"
  755. . "- no support material\n"
  756. . "- no ensure_vertical_shell_thickness\n"
  757. . "\nShall I adjust those settings in order to enable Spiral Vase?",
  758. 'Spiral Vase', wxICON_WARNING | wxYES | wxNO);
  759. my $new_conf = Slic3r::Config->new;
  760. if ($dialog->ShowModal() == wxID_YES) {
  761. $new_conf->set("perimeters", 1);
  762. $new_conf->set("top_solid_layers", 0);
  763. $new_conf->set("fill_density", 0);
  764. $new_conf->set("support_material", 0);
  765. $new_conf->set("ensure_vertical_shell_thickness", 0);
  766. } else {
  767. $new_conf->set("spiral_vase", 0);
  768. }
  769. $self->load_config($new_conf);
  770. }
  771. if ($config->wipe_tower &&
  772. ($config->first_layer_height != 0.2 || $config->layer_height < 0.15 || $config->layer_height > 0.35)) {
  773. my $dialog = Wx::MessageDialog->new($self,
  774. "The Wipe Tower currently supports only:\n"
  775. . "- first layer height 0.2mm\n"
  776. . "- layer height from 0.15mm to 0.35mm\n"
  777. . "\nShall I adjust those settings in order to enable the Wipe Tower?",
  778. 'Wipe Tower', wxICON_WARNING | wxYES | wxNO);
  779. my $new_conf = Slic3r::Config->new;
  780. if ($dialog->ShowModal() == wxID_YES) {
  781. $new_conf->set("first_layer_height", 0.2);
  782. $new_conf->set("layer_height", 0.15) if $config->layer_height < 0.15;
  783. $new_conf->set("layer_height", 0.35) if $config->layer_height > 0.35;
  784. } else {
  785. $new_conf->set("wipe_tower", 0);
  786. }
  787. $self->load_config($new_conf);
  788. }
  789. if ($config->wipe_tower && $config->support_material && $config->support_material_contact_distance > 0. &&
  790. ($config->support_material_extruder != 0 || $config->support_material_interface_extruder != 0)) {
  791. my $dialog = Wx::MessageDialog->new($self,
  792. "The Wipe Tower currently supports the non-soluble supports only\n"
  793. . "if they are printed with the current extruder without triggering a tool change.\n"
  794. . "(both support_material_extruder and support_material_interface_extruder need to be set to 0).\n"
  795. . "\nShall I adjust those settings in order to enable the Wipe Tower?",
  796. 'Wipe Tower', wxICON_WARNING | wxYES | wxNO);
  797. my $new_conf = Slic3r::Config->new;
  798. if ($dialog->ShowModal() == wxID_YES) {
  799. $new_conf->set("support_material_extruder", 0);
  800. $new_conf->set("support_material_interface_extruder", 0);
  801. } else {
  802. $new_conf->set("wipe_tower", 0);
  803. }
  804. $self->load_config($new_conf);
  805. }
  806. if ($config->wipe_tower && $config->support_material && $config->support_material_contact_distance == 0 &&
  807. ! $config->support_material_synchronize_layers) {
  808. my $dialog = Wx::MessageDialog->new($self,
  809. "For the Wipe Tower to work with the soluble supports, the support layers\n"
  810. . "need to be synchronized with the object layers.\n"
  811. . "\nShall I synchronize support layers in order to enable the Wipe Tower?",
  812. 'Wipe Tower', wxICON_WARNING | wxYES | wxNO);
  813. my $new_conf = Slic3r::Config->new;
  814. if ($dialog->ShowModal() == wxID_YES) {
  815. $new_conf->set("support_material_synchronize_layers", 1);
  816. } else {
  817. $new_conf->set("wipe_tower", 0);
  818. }
  819. $self->load_config($new_conf);
  820. }
  821. if ($keys_modified->{'layer_height'}) {
  822. # If the user had set the variable layer height, reset it and let the user know.
  823. }
  824. if ($config->support_material) {
  825. # Ask only once.
  826. if (! $self->{support_material_overhangs_queried}) {
  827. $self->{support_material_overhangs_queried} = 1;
  828. if ($config->overhangs != 1) {
  829. my $dialog = Wx::MessageDialog->new($self,
  830. "Supports work better, if the following feature is enabled:\n"
  831. . "- Detect bridging perimeters\n"
  832. . "\nShall I adjust those settings for supports?",
  833. 'Support Generator', wxICON_WARNING | wxYES | wxNO | wxCANCEL);
  834. my $answer = $dialog->ShowModal();
  835. my $new_conf = Slic3r::Config->new;
  836. if ($answer == wxID_YES) {
  837. # Enable "detect bridging perimeters".
  838. $new_conf->set("overhangs", 1);
  839. } elsif ($answer == wxID_NO) {
  840. # Do nothing, leave supports on and "detect bridging perimeters" off.
  841. } elsif ($answer == wxID_CANCEL) {
  842. # Disable supports.
  843. $new_conf->set("support_material", 0);
  844. $self->{support_material_overhangs_queried} = 0;
  845. }
  846. $self->load_config($new_conf);
  847. }
  848. }
  849. } else {
  850. $self->{support_material_overhangs_queried} = 0;
  851. }
  852. if ($config->fill_density == 100
  853. && (!first { $_ eq $config->fill_pattern } @{$Slic3r::Config::Options->{top_fill_pattern}{values}}
  854. || !first { $_ eq $config->fill_pattern } @{$Slic3r::Config::Options->{bottom_fill_pattern}{values}})) {
  855. my $dialog = Wx::MessageDialog->new($self,
  856. "The " . $config->fill_pattern . " infill pattern is not supposed to work at 100% density.\n"
  857. . "\nShall I switch to rectilinear fill pattern?",
  858. 'Infill', wxICON_WARNING | wxYES | wxNO);
  859. my $new_conf = Slic3r::Config->new;
  860. if ($dialog->ShowModal() == wxID_YES) {
  861. $new_conf->set("fill_pattern", 'rectilinear');
  862. $new_conf->set("fill_density", 100);
  863. } else {
  864. $new_conf->set("fill_density", 40);
  865. }
  866. $self->load_config($new_conf);
  867. }
  868. my $have_perimeters = $config->perimeters > 0;
  869. $self->get_field($_)->toggle($have_perimeters)
  870. for qw(extra_perimeters ensure_vertical_shell_thickness thin_walls overhangs seam_position external_perimeters_first
  871. external_perimeter_extrusion_width only_one_perimeter_top
  872. perimeter_speed small_perimeter_speed external_perimeter_speed);
  873. my $have_infill = $config->fill_density > 0;
  874. # infill_extruder uses the same logic as in Print::extruders()
  875. $self->get_field($_)->toggle($have_infill)
  876. for qw(fill_pattern infill_every_layers infill_only_where_needed solid_infill_every_layers
  877. solid_infill_below_area infill_extruder);
  878. my $have_solid_infill = ($config->top_solid_layers > 0) || ($config->bottom_solid_layers > 0);
  879. # solid_infill_extruder uses the same logic as in Print::extruders()
  880. $self->get_field($_)->toggle($have_solid_infill)
  881. for qw(infill_first solid_infill_extruder solid_infill_extrusion_width
  882. solid_infill_speed);
  883. $self->get_field($_)->toggle($have_infill || $have_solid_infill)
  884. for qw(fill_angle bridge_angle infill_extrusion_width infill_speed bridge_speed);
  885. $self->get_field('gap_fill_speed')->toggle($have_perimeters && $have_infill);
  886. my $have_top_solid_infill = $config->top_solid_layers > 0;
  887. $self->get_field($_)->toggle($have_top_solid_infill)
  888. for qw(top_fill_pattern top_infill_extrusion_width top_solid_infill_speed);
  889. my $have_bottom_solid_infill = $config->bottom_solid_layers > 0;
  890. $self->get_field($_)->toggle($have_bottom_solid_infill)
  891. for qw(bottom_fill_pattern);
  892. my $have_default_acceleration = $config->default_acceleration > 0;
  893. $self->get_field($_)->toggle($have_default_acceleration)
  894. for qw(perimeter_acceleration infill_acceleration bridge_acceleration first_layer_acceleration);
  895. my $have_skirt = $config->skirts > 0 || $config->min_skirt_length > 0;
  896. $self->get_field($_)->toggle($have_skirt)
  897. for qw(skirt_distance skirt_height);
  898. my $have_brim = $config->brim_width > 0;
  899. # perimeter_extruder uses the same logic as in Print::extruders()
  900. $self->get_field('perimeter_extruder')->toggle($have_perimeters || $have_brim);
  901. my $have_raft = $config->raft_layers > 0;
  902. my $have_support_material = $config->support_material || $have_raft;
  903. my $have_support_interface = $config->support_material_interface_layers > 0;
  904. my $have_support_soluble = $have_support_material && $config->support_material_contact_distance == 0;
  905. $self->get_field($_)->toggle($have_support_material)
  906. for qw(support_material_threshold support_material_pattern support_material_with_sheath
  907. support_material_spacing support_material_angle
  908. support_material_interface_layers dont_support_bridges
  909. support_material_extrusion_width support_material_contact_distance support_material_xy_spacing);
  910. $self->get_field($_)->toggle($have_support_material && $have_support_interface)
  911. for qw(support_material_interface_spacing support_material_interface_extruder
  912. support_material_interface_speed support_material_interface_contact_loops);
  913. $self->get_field('support_material_synchronize_layers')->toggle($have_support_soluble);
  914. $self->get_field('perimeter_extrusion_width')->toggle($have_perimeters || $have_skirt || $have_brim);
  915. $self->get_field('support_material_extruder')->toggle($have_support_material || $have_skirt);
  916. $self->get_field('support_material_speed')->toggle($have_support_material || $have_brim || $have_skirt);
  917. my $have_sequential_printing = $config->complete_objects;
  918. $self->get_field($_)->toggle($have_sequential_printing)
  919. for qw(extruder_clearance_radius extruder_clearance_height);
  920. my $have_ooze_prevention = $config->ooze_prevention;
  921. $self->get_field($_)->toggle($have_ooze_prevention)
  922. for qw(standby_temperature_delta);
  923. my $have_wipe_tower = $config->wipe_tower;
  924. $self->get_field($_)->toggle($have_wipe_tower)
  925. for qw(wipe_tower_x wipe_tower_y wipe_tower_width wipe_tower_per_color_wipe);
  926. }
  927. sub hidden_options { !$Slic3r::have_threads ? qw(threads) : () }
  928. package Slic3r::GUI::Tab::Filament;
  929. use base 'Slic3r::GUI::Tab';
  930. sub name { 'filament' }
  931. sub title { 'Filament Settings' }
  932. sub build {
  933. my $self = shift;
  934. $self->init_config_options(qw(
  935. filament_colour filament_diameter filament_type filament_soluble filament_notes filament_max_volumetric_speed extrusion_multiplier filament_density filament_cost
  936. temperature first_layer_temperature bed_temperature first_layer_bed_temperature
  937. fan_always_on cooling
  938. min_fan_speed max_fan_speed bridge_fan_speed disable_fan_first_layers
  939. fan_below_layer_time slowdown_below_layer_time min_print_speed
  940. start_filament_gcode end_filament_gcode
  941. ));
  942. $self->{config}->set('filament_settings_id', '');
  943. {
  944. my $page = $self->add_options_page('Filament', 'spool.png');
  945. {
  946. my $optgroup = $page->new_optgroup('Filament');
  947. $optgroup->append_single_option_line('filament_colour', 0);
  948. $optgroup->append_single_option_line('filament_diameter', 0);
  949. $optgroup->append_single_option_line('extrusion_multiplier', 0);
  950. $optgroup->append_single_option_line('filament_density', 0);
  951. $optgroup->append_single_option_line('filament_cost', 0);
  952. }
  953. {
  954. my $optgroup = $page->new_optgroup('Temperature (°C)');
  955. {
  956. my $line = Slic3r::GUI::OptionsGroup::Line->new(
  957. label => 'Extruder',
  958. );
  959. $line->append_option($optgroup->get_option('first_layer_temperature', 0));
  960. $line->append_option($optgroup->get_option('temperature', 0));
  961. $optgroup->append_line($line);
  962. }
  963. {
  964. my $line = Slic3r::GUI::OptionsGroup::Line->new(
  965. label => 'Bed',
  966. );
  967. $line->append_option($optgroup->get_option('first_layer_bed_temperature', 0));
  968. $line->append_option($optgroup->get_option('bed_temperature', 0));
  969. $optgroup->append_line($line);
  970. }
  971. }
  972. }
  973. {
  974. my $page = $self->add_options_page('Cooling', 'hourglass.png');
  975. {
  976. my $optgroup = $page->new_optgroup('Enable');
  977. $optgroup->append_single_option_line('fan_always_on', 0);
  978. $optgroup->append_single_option_line('cooling', 0);
  979. my $line = Slic3r::GUI::OptionsGroup::Line->new(
  980. label => '',
  981. full_width => 1,
  982. widget => sub {
  983. my ($parent) = @_;
  984. return $self->{description_line} = Slic3r::GUI::OptionsGroup::StaticText->new($parent);
  985. },
  986. );
  987. $optgroup->append_line($line);
  988. }
  989. {
  990. my $optgroup = $page->new_optgroup('Fan settings');
  991. {
  992. my $line = Slic3r::GUI::OptionsGroup::Line->new(
  993. label => 'Fan speed',
  994. );
  995. $line->append_option($optgroup->get_option('min_fan_speed', 0));
  996. $line->append_option($optgroup->get_option('max_fan_speed', 0));
  997. $optgroup->append_line($line);
  998. }
  999. $optgroup->append_single_option_line('bridge_fan_speed', 0);
  1000. $optgroup->append_single_option_line('disable_fan_first_layers', 0);
  1001. }
  1002. {
  1003. my $optgroup = $page->new_optgroup('Cooling thresholds',
  1004. label_width => 250,
  1005. );
  1006. $optgroup->append_single_option_line('fan_below_layer_time', 0);
  1007. $optgroup->append_single_option_line('slowdown_below_layer_time', 0);
  1008. $optgroup->append_single_option_line('min_print_speed', 0);
  1009. }
  1010. }
  1011. {
  1012. my $page = $self->add_options_page('Advanced', 'wrench.png');
  1013. {
  1014. my $optgroup = $page->new_optgroup('Filament properties');
  1015. $optgroup->append_single_option_line('filament_type', 0);
  1016. $optgroup->append_single_option_line('filament_soluble', 0);
  1017. $optgroup = $page->new_optgroup('Print speed override');
  1018. $optgroup->append_single_option_line('filament_max_volumetric_speed', 0);
  1019. }
  1020. }
  1021. {
  1022. my $page = $self->add_options_page('Custom G-code', 'cog.png');
  1023. {
  1024. my $optgroup = $page->new_optgroup('Start G-code',
  1025. label_width => 0,
  1026. );
  1027. my $option = $optgroup->get_option('start_filament_gcode', 0);
  1028. $option->full_width(1);
  1029. $option->height(150);
  1030. $optgroup->append_single_option_line($option);
  1031. }
  1032. {
  1033. my $optgroup = $page->new_optgroup('End G-code',
  1034. label_width => 0,
  1035. );
  1036. my $option = $optgroup->get_option('end_filament_gcode', 0);
  1037. $option->full_width(1);
  1038. $option->height(150);
  1039. $optgroup->append_single_option_line($option);
  1040. }
  1041. }
  1042. {
  1043. my $page = $self->add_options_page('Notes', 'note.png');
  1044. {
  1045. my $optgroup = $page->new_optgroup('Notes',
  1046. label_width => 0,
  1047. );
  1048. my $option = $optgroup->get_option('filament_notes', 0);
  1049. $option->full_width(1);
  1050. $option->height(250);
  1051. $optgroup->append_single_option_line($option);
  1052. }
  1053. }
  1054. }
  1055. # Slic3r::GUI::Tab::Filament::_update is called after a configuration preset is loaded or switched, or when a single option is modifed by the user.
  1056. sub _update {
  1057. # $keys_modified is a reference to hash with modified keys set to 1, unmodified keys missing.
  1058. my ($self, $keys_modified) = @_;
  1059. $self->_update_description;
  1060. my $cooling = $self->{config}->cooling->[0];
  1061. my $fan_always_on = $cooling || $self->{config}->fan_always_on->[0];
  1062. $self->get_field($_, 0)->toggle($cooling)
  1063. for qw(max_fan_speed fan_below_layer_time slowdown_below_layer_time min_print_speed);
  1064. $self->get_field($_, 0)->toggle($fan_always_on)
  1065. for qw(min_fan_speed disable_fan_first_layers);
  1066. }
  1067. sub _update_description {
  1068. my $self = shift;
  1069. my $config = $self->config;
  1070. my $msg = "";
  1071. my $fan_other_layers = $config->fan_always_on->[0]
  1072. ? sprintf "will always run at %d%%%s.", $config->min_fan_speed->[0],
  1073. ($config->disable_fan_first_layers->[0] > 1
  1074. ? " except for the first " . $config->disable_fan_first_layers->[0] . " layers"
  1075. : $config->disable_fan_first_layers->[0] == 1
  1076. ? " except for the first layer"
  1077. : "")
  1078. : "will be turned off.";
  1079. if ($config->cooling->[0]) {
  1080. $msg = sprintf "If estimated layer time is below ~%ds, fan will run at %d%% and print speed will be reduced so that no less than %ds are spent on that layer (however, speed will never be reduced below %dmm/s).",
  1081. $config->slowdown_below_layer_time->[0], $config->max_fan_speed->[0], $config->slowdown_below_layer_time->[0], $config->min_print_speed->[0];
  1082. if ($config->fan_below_layer_time->[0] > $config->slowdown_below_layer_time->[0]) {
  1083. $msg .= sprintf "\nIf estimated layer time is greater, but still below ~%ds, fan will run at a proportionally decreasing speed between %d%% and %d%%.",
  1084. $config->fan_below_layer_time->[0], $config->max_fan_speed->[0], $config->min_fan_speed->[0];
  1085. }
  1086. $msg .= "\nDuring the other layers, fan $fan_other_layers"
  1087. } else {
  1088. $msg = "Fan $fan_other_layers";
  1089. }
  1090. $self->{description_line}->SetText($msg);
  1091. }
  1092. package Slic3r::GUI::Tab::Printer;
  1093. use base 'Slic3r::GUI::Tab';
  1094. use Wx qw(wxTheApp :sizer :button :bitmap :misc :id :icon :dialog);
  1095. use Wx::Event qw(EVT_BUTTON);
  1096. sub name { 'printer' }
  1097. sub title { 'Printer Settings' }
  1098. sub build {
  1099. my $self = shift;
  1100. my (%params) = @_;
  1101. $self->init_config_options(qw(
  1102. bed_shape z_offset
  1103. gcode_flavor use_relative_e_distances
  1104. serial_port serial_speed
  1105. octoprint_host octoprint_apikey
  1106. use_firmware_retraction
  1107. use_volumetric_e variable_layer_height
  1108. single_extruder_multi_material start_gcode end_gcode before_layer_gcode layer_gcode toolchange_gcode
  1109. nozzle_diameter extruder_offset
  1110. retract_length retract_lift retract_speed deretract_speed retract_before_wipe retract_restart_extra retract_before_travel retract_layer_change wipe
  1111. retract_length_toolchange retract_restart_extra_toolchange
  1112. extruder_colour printer_notes
  1113. ));
  1114. $self->{config}->set('printer_settings_id', '');
  1115. my $bed_shape_widget = sub {
  1116. my ($parent) = @_;
  1117. my $btn = Wx::Button->new($parent, -1, "Set…", wxDefaultPosition, wxDefaultSize,
  1118. wxBU_LEFT | wxBU_EXACTFIT);
  1119. $btn->SetFont($Slic3r::GUI::small_font);
  1120. $btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("cog.png"), wxBITMAP_TYPE_PNG));
  1121. my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  1122. $sizer->Add($btn);
  1123. EVT_BUTTON($self, $btn, sub {
  1124. my $dlg = Slic3r::GUI::BedShapeDialog->new($self, $self->{config}->bed_shape);
  1125. if ($dlg->ShowModal == wxID_OK) {
  1126. my $value = $dlg->GetValue;
  1127. $self->{config}->set('bed_shape', $value);
  1128. $self->update_dirty;
  1129. $self->_on_value_change('bed_shape', $value);
  1130. }
  1131. });
  1132. return $sizer;
  1133. };
  1134. $self->{extruders_count} = 1;
  1135. {
  1136. my $page = $self->add_options_page('General', 'printer_empty.png');
  1137. {
  1138. my $optgroup = $page->new_optgroup('Size and coordinates');
  1139. my $line = Slic3r::GUI::OptionsGroup::Line->new(
  1140. label => 'Bed shape',
  1141. widget => $bed_shape_widget,
  1142. );
  1143. $optgroup->append_line($line);
  1144. $optgroup->append_single_option_line('z_offset');
  1145. }
  1146. {
  1147. my $optgroup = $page->new_optgroup('Capabilities');
  1148. {
  1149. my $option = Slic3r::GUI::OptionsGroup::Option->new(
  1150. opt_id => 'extruders_count',
  1151. type => 'i',
  1152. default => 1,
  1153. label => 'Extruders',
  1154. tooltip => 'Number of extruders of the printer.',
  1155. min => 1,
  1156. );
  1157. $optgroup->append_single_option_line($option);
  1158. $optgroup->append_single_option_line('single_extruder_multi_material');
  1159. }
  1160. $optgroup->on_change(sub {
  1161. my ($opt_id) = @_;
  1162. if ($opt_id eq 'extruders_count') {
  1163. wxTheApp->CallAfter(sub {
  1164. $self->_extruders_count_changed($optgroup->get_value('extruders_count'));
  1165. });
  1166. $self->update_dirty;
  1167. }
  1168. });
  1169. }
  1170. if (!$params{no_controller})
  1171. {
  1172. my $optgroup = $page->new_optgroup('USB/Serial connection');
  1173. my $line = Slic3r::GUI::OptionsGroup::Line->new(
  1174. label => 'Serial port',
  1175. );
  1176. my $serial_port = $optgroup->get_option('serial_port');
  1177. $serial_port->side_widget(sub {
  1178. my ($parent) = @_;
  1179. my $btn = Wx::BitmapButton->new($parent, -1, Wx::Bitmap->new($Slic3r::var->("arrow_rotate_clockwise.png"), wxBITMAP_TYPE_PNG),
  1180. wxDefaultPosition, wxDefaultSize, &Wx::wxBORDER_NONE);
  1181. $btn->SetToolTipString("Rescan serial ports")
  1182. if $btn->can('SetToolTipString');
  1183. EVT_BUTTON($self, $btn, \&_update_serial_ports);
  1184. return $btn;
  1185. });
  1186. my $serial_test = sub {
  1187. my ($parent) = @_;
  1188. my $btn = $self->{serial_test_btn} = Wx::Button->new($parent, -1,
  1189. "Test", wxDefaultPosition, wxDefaultSize, wxBU_LEFT | wxBU_EXACTFIT);
  1190. $btn->SetFont($Slic3r::GUI::small_font);
  1191. $btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("wrench.png"), wxBITMAP_TYPE_PNG));
  1192. EVT_BUTTON($self, $btn, sub {
  1193. my $sender = Slic3r::GCode::Sender->new;
  1194. my $res = $sender->connect(
  1195. $self->{config}->serial_port,
  1196. $self->{config}->serial_speed,
  1197. );
  1198. if ($res && $sender->wait_connected) {
  1199. Slic3r::GUI::show_info($self, "Connection to printer works correctly.", "Success!");
  1200. } else {
  1201. Slic3r::GUI::show_error($self, "Connection failed.");
  1202. }
  1203. });
  1204. return $btn;
  1205. };
  1206. $line->append_option($serial_port);
  1207. $line->append_option($optgroup->get_option('serial_speed'));
  1208. $line->append_widget($serial_test);
  1209. $optgroup->append_line($line);
  1210. }
  1211. {
  1212. my $optgroup = $page->new_optgroup('OctoPrint upload');
  1213. # append two buttons to the Host line
  1214. my $octoprint_host_browse = sub {
  1215. my ($parent) = @_;
  1216. my $btn = Wx::Button->new($parent, -1, "Browse…", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
  1217. $btn->SetFont($Slic3r::GUI::small_font);
  1218. $btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("zoom.png"), wxBITMAP_TYPE_PNG));
  1219. if (!eval "use Net::Bonjour; 1") {
  1220. $btn->Disable;
  1221. }
  1222. EVT_BUTTON($self, $btn, sub {
  1223. # look for devices
  1224. my $entries;
  1225. {
  1226. my $res = Net::Bonjour->new('http');
  1227. $res->discover;
  1228. $entries = [ $res->entries ];
  1229. }
  1230. if (@{$entries}) {
  1231. my $dlg = Slic3r::GUI::BonjourBrowser->new($self, $entries);
  1232. if ($dlg->ShowModal == wxID_OK) {
  1233. my $value = $dlg->GetValue . ":" . $dlg->GetPort;
  1234. $self->{config}->set('octoprint_host', $value);
  1235. $self->update_dirty;
  1236. $self->_on_value_change('octoprint_host', $value);
  1237. $self->reload_config;
  1238. }
  1239. } else {
  1240. Wx::MessageDialog->new($self, 'No Bonjour device found', 'Device Browser', wxOK | wxICON_INFORMATION)->ShowModal;
  1241. }
  1242. });
  1243. return $btn;
  1244. };
  1245. my $octoprint_host_test = sub {
  1246. my ($parent) = @_;
  1247. my $btn = $self->{octoprint_host_test_btn} = Wx::Button->new($parent, -1,
  1248. "Test", wxDefaultPosition, wxDefaultSize, wxBU_LEFT | wxBU_EXACTFIT);
  1249. $btn->SetFont($Slic3r::GUI::small_font);
  1250. $btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("wrench.png"), wxBITMAP_TYPE_PNG));
  1251. EVT_BUTTON($self, $btn, sub {
  1252. my $ua = LWP::UserAgent->new;
  1253. $ua->timeout(10);
  1254. my $res = $ua->get(
  1255. "http://" . $self->{config}->octoprint_host . "/api/version",
  1256. 'X-Api-Key' => $self->{config}->octoprint_apikey,
  1257. );
  1258. if ($res->is_success) {
  1259. Slic3r::GUI::show_info($self, "Connection to OctoPrint works correctly.", "Success!");
  1260. } else {
  1261. Slic3r::GUI::show_error($self,
  1262. "I wasn't able to connect to OctoPrint (" . $res->status_line . "). "
  1263. . "Check hostname and OctoPrint version (at least 1.1.0 is required).");
  1264. }
  1265. });
  1266. return $btn;
  1267. };
  1268. my $host_line = $optgroup->create_single_option_line('octoprint_host');
  1269. $host_line->append_widget($octoprint_host_browse);
  1270. $host_line->append_widget($octoprint_host_test);
  1271. $optgroup->append_line($host_line);
  1272. $optgroup->append_single_option_line('octoprint_apikey');
  1273. }
  1274. {
  1275. my $optgroup = $page->new_optgroup('Firmware');
  1276. $optgroup->append_single_option_line('gcode_flavor');
  1277. }
  1278. {
  1279. my $optgroup = $page->new_optgroup('Advanced');
  1280. $optgroup->append_single_option_line('use_relative_e_distances');
  1281. $optgroup->append_single_option_line('use_firmware_retraction');
  1282. $optgroup->append_single_option_line('use_volumetric_e');
  1283. $optgroup->append_single_option_line('variable_layer_height');
  1284. }
  1285. }
  1286. {
  1287. my $page = $self->add_options_page('Custom G-code', 'cog.png');
  1288. {
  1289. my $optgroup = $page->new_optgroup('Start G-code',
  1290. label_width => 0,
  1291. );
  1292. my $option = $optgroup->get_option('start_gcode');
  1293. $option->full_width(1);
  1294. $option->height(150);
  1295. $optgroup->append_single_option_line($option);
  1296. }
  1297. {
  1298. my $optgroup = $page->new_optgroup('End G-code',
  1299. label_width => 0,
  1300. );
  1301. my $option = $optgroup->get_option('end_gcode');
  1302. $option->full_width(1);
  1303. $option->height(150);
  1304. $optgroup->append_single_option_line($option);
  1305. }
  1306. {
  1307. my $optgroup = $page->new_optgroup('Before layer change G-code',
  1308. label_width => 0,
  1309. );
  1310. my $option = $optgroup->get_option('before_layer_gcode');
  1311. $option->full_width(1);
  1312. $option->height(150);
  1313. $optgroup->append_single_option_line($option);
  1314. }
  1315. {
  1316. my $optgroup = $page->new_optgroup('After layer change G-code',
  1317. label_width => 0,
  1318. );
  1319. my $option = $optgroup->get_option('layer_gcode');
  1320. $option->full_width(1);
  1321. $option->height(150);
  1322. $optgroup->append_single_option_line($option);
  1323. }
  1324. {
  1325. my $optgroup = $page->new_optgroup('Tool change G-code',
  1326. label_width => 0,
  1327. );
  1328. my $option = $optgroup->get_option('toolchange_gcode');
  1329. $option->full_width(1);
  1330. $option->height(150);
  1331. $optgroup->append_single_option_line($option);
  1332. }
  1333. }
  1334. {
  1335. my $page = $self->add_options_page('Notes', 'note.png');
  1336. {
  1337. my $optgroup = $page->new_optgroup('Notes',
  1338. label_width => 0,
  1339. );
  1340. my $option = $optgroup->get_option('printer_notes');
  1341. $option->full_width(1);
  1342. $option->height(250);
  1343. $optgroup->append_single_option_line($option);
  1344. }
  1345. }
  1346. $self->{extruder_pages} = [];
  1347. $self->_build_extruder_pages;
  1348. $self->_update_serial_ports if (!$params{no_controller});
  1349. }
  1350. sub _update_serial_ports {
  1351. my ($self) = @_;
  1352. $self->get_field('serial_port')->set_values([ wxTheApp->scan_serial_ports ]);
  1353. }
  1354. sub _extruders_count_changed {
  1355. my ($self, $extruders_count) = @_;
  1356. $self->{extruders_count} = $extruders_count;
  1357. $self->_build_extruder_pages;
  1358. $self->_on_value_change('extruders_count', $extruders_count);
  1359. }
  1360. sub _extruder_options {
  1361. qw(nozzle_diameter min_layer_height max_layer_height extruder_offset
  1362. retract_length retract_lift retract_lift_above retract_lift_below retract_speed deretract_speed
  1363. retract_before_wipe retract_restart_extra retract_before_travel wipe
  1364. retract_layer_change retract_length_toolchange retract_restart_extra_toolchange extruder_colour) }
  1365. sub _build_extruder_pages {
  1366. my $self = shift;
  1367. my $default_config = Slic3r::Config::Full->new;
  1368. foreach my $extruder_idx (@{$self->{extruder_pages}} .. $self->{extruders_count}-1) {
  1369. # extend options
  1370. foreach my $opt_key ($self->_extruder_options) {
  1371. my $values = $self->{config}->get($opt_key);
  1372. if (!defined $values) {
  1373. $values = [ $default_config->get_at($opt_key, 0) ];
  1374. } else {
  1375. # use last extruder's settings for the new one
  1376. my $last_value = $values->[-1];
  1377. $values->[$extruder_idx] //= $last_value;
  1378. }
  1379. $self->{config}->set($opt_key, $values)
  1380. or die "Unable to extend $opt_key";
  1381. }
  1382. # build page
  1383. my $page = $self->{extruder_pages}[$extruder_idx] = $self->add_options_page("Extruder " . ($extruder_idx + 1), 'funnel.png');
  1384. {
  1385. my $optgroup = $page->new_optgroup('Size');
  1386. $optgroup->append_single_option_line('nozzle_diameter', $extruder_idx);
  1387. }
  1388. {
  1389. my $optgroup = $page->new_optgroup('Layer height limits');
  1390. $optgroup->append_single_option_line($_, $extruder_idx)
  1391. for qw(min_layer_height max_layer_height);
  1392. }
  1393. {
  1394. my $optgroup = $page->new_optgroup('Position (for multi-extruder printers)');
  1395. $optgroup->append_single_option_line('extruder_offset', $extruder_idx);
  1396. }
  1397. {
  1398. my $optgroup = $page->new_optgroup('Retraction');
  1399. $optgroup->append_single_option_line($_, $extruder_idx)
  1400. for qw(retract_length retract_lift);
  1401. {
  1402. my $line = Slic3r::GUI::OptionsGroup::Line->new(
  1403. label => 'Only lift Z',
  1404. );
  1405. $line->append_option($optgroup->get_option('retract_lift_above', $extruder_idx));
  1406. $line->append_option($optgroup->get_option('retract_lift_below', $extruder_idx));
  1407. $optgroup->append_line($line);
  1408. }
  1409. $optgroup->append_single_option_line($_, $extruder_idx)
  1410. for qw(retract_speed deretract_speed retract_restart_extra retract_before_travel retract_layer_change wipe retract_before_wipe);
  1411. }
  1412. {
  1413. my $optgroup = $page->new_optgroup('Retraction when tool is disabled (advanced settings for multi-extruder setups)');
  1414. $optgroup->append_single_option_line($_, $extruder_idx)
  1415. for qw(retract_length_toolchange retract_restart_extra_toolchange);
  1416. }
  1417. {
  1418. my $optgroup = $page->new_optgroup('Preview');
  1419. $optgroup->append_single_option_line('extruder_colour', $extruder_idx);
  1420. }
  1421. }
  1422. # remove extra pages
  1423. if ($self->{extruders_count} <= $#{$self->{extruder_pages}}) {
  1424. $_->Destroy for @{$self->{extruder_pages}}[$self->{extruders_count}..$#{$self->{extruder_pages}}];
  1425. splice @{$self->{extruder_pages}}, $self->{extruders_count};
  1426. }
  1427. # remove extra config values
  1428. foreach my $opt_key ($self->_extruder_options) {
  1429. my $values = $self->{config}->get($opt_key);
  1430. splice @$values, $self->{extruders_count} if $self->{extruders_count} <= $#$values;
  1431. $self->{config}->set($opt_key, $values)
  1432. or die "Unable to truncate $opt_key";
  1433. }
  1434. # rebuild page list
  1435. my @pages_without_extruders = (grep $_->{title} !~ /^Extruder \d+/, @{$self->{pages}});
  1436. my $page_notes = pop @pages_without_extruders;
  1437. @{$self->{pages}} = (
  1438. @pages_without_extruders,
  1439. @{$self->{extruder_pages}}[ 0 .. $self->{extruders_count}-1 ],
  1440. $page_notes
  1441. );
  1442. $self->update_tree;
  1443. }
  1444. # Slic3r::GUI::Tab::Printer::_update is called after a configuration preset is loaded or switched, or when a single option is modifed by the user.
  1445. sub _update {
  1446. # $keys_modified is a reference to hash with modified keys set to 1, unmodified keys missing.
  1447. my ($self, $keys_modified) = @_;
  1448. my $config = $self->{config};
  1449. my $serial_speed = $self->get_field('serial_speed');
  1450. if ($serial_speed) {
  1451. $self->get_field('serial_speed')->toggle($config->get('serial_port'));
  1452. if ($config->get('serial_speed') && $config->get('serial_port')) {
  1453. $self->{serial_test_btn}->Enable;
  1454. } else {
  1455. $self->{serial_test_btn}->Disable;
  1456. }
  1457. }
  1458. if ($config->get('octoprint_host') && eval "use LWP::UserAgent; 1") {
  1459. $self->{octoprint_host_test_btn}->Enable;
  1460. } else {
  1461. $self->{octoprint_host_test_btn}->Disable;
  1462. }
  1463. $self->get_field('octoprint_apikey')->toggle($config->get('octoprint_host'));
  1464. my $have_multiple_extruders = $self->{extruders_count} > 1;
  1465. $self->get_field('toolchange_gcode')->toggle($have_multiple_extruders);
  1466. $self->get_field('single_extruder_multi_material')->toggle($have_multiple_extruders);
  1467. for my $i (0 .. ($self->{extruders_count}-1)) {
  1468. my $have_retract_length = $config->get_at('retract_length', $i) > 0;
  1469. # when using firmware retraction, firmware decides retraction length
  1470. $self->get_field('retract_length', $i)->toggle(!$config->use_firmware_retraction);
  1471. # user can customize travel length if we have retraction length or we're using
  1472. # firmware retraction
  1473. $self->get_field('retract_before_travel', $i)->toggle($have_retract_length || $config->use_firmware_retraction);
  1474. # user can customize other retraction options if retraction is enabled
  1475. my $retraction = ($have_retract_length || $config->use_firmware_retraction);
  1476. $self->get_field($_, $i)->toggle($retraction)
  1477. for qw(retract_lift retract_layer_change);
  1478. # retract lift above/below only applies if using retract lift
  1479. $self->get_field($_, $i)->toggle($retraction && $config->get_at('retract_lift', $i) > 0)
  1480. for qw(retract_lift_above retract_lift_below);
  1481. # some options only apply when not using firmware retraction
  1482. $self->get_field($_, $i)->toggle($retraction && !$config->use_firmware_retraction)
  1483. for qw(retract_speed deretract_speed retract_before_wipe retract_restart_extra wipe);
  1484. my $wipe = $config->get_at('wipe', $i);
  1485. $self->get_field('retract_before_wipe', $i)->toggle($wipe);
  1486. if ($config->use_firmware_retraction && $wipe) {
  1487. my $dialog = Wx::MessageDialog->new($self,
  1488. "The Wipe option is not available when using the Firmware Retraction mode.\n"
  1489. . "\nShall I disable it in order to enable Firmware Retraction?",
  1490. 'Firmware Retraction', wxICON_WARNING | wxYES | wxNO);
  1491. my $new_conf = Slic3r::Config->new;
  1492. if ($dialog->ShowModal() == wxID_YES) {
  1493. my $wipe = $config->wipe;
  1494. $wipe->[$i] = 0;
  1495. $new_conf->set("wipe", $wipe);
  1496. } else {
  1497. $new_conf->set("use_firmware_retraction", 0);
  1498. }
  1499. $self->load_config($new_conf);
  1500. }
  1501. $self->get_field('retract_length_toolchange', $i)->toggle($have_multiple_extruders);
  1502. my $toolchange_retraction = $config->get_at('retract_length_toolchange', $i) > 0;
  1503. $self->get_field('retract_restart_extra_toolchange', $i)->toggle
  1504. ($have_multiple_extruders && $toolchange_retraction);
  1505. }
  1506. }
  1507. # this gets executed after preset is loaded and before GUI fields are updated
  1508. sub on_preset_loaded {
  1509. my $self = shift;
  1510. # update the extruders count field
  1511. {
  1512. # update the GUI field according to the number of nozzle diameters supplied
  1513. my $extruders_count = scalar @{ $self->{config}->nozzle_diameter };
  1514. $self->set_value('extruders_count', $extruders_count);
  1515. $self->_extruders_count_changed($extruders_count);
  1516. }
  1517. }
  1518. sub load_config_file {
  1519. my $self = shift;
  1520. if ($self->SUPER::load_config_file(@_)) {
  1521. Slic3r::GUI::warning_catcher($self)->(
  1522. "Your configuration was imported. However, Slic3r is currently only able to import settings "
  1523. . "for the first defined filament. We recommend you don't use exported configuration files "
  1524. . "for multi-extruder setups and rely on the built-in preset management system instead.")
  1525. if @{ $self->{config}->nozzle_diameter } > 1;
  1526. return 1;
  1527. }
  1528. return undef;
  1529. }
  1530. package Slic3r::GUI::Tab::Page;
  1531. use Wx qw(wxTheApp :misc :panel :sizer);
  1532. use base 'Wx::ScrolledWindow';
  1533. sub new {
  1534. my $class = shift;
  1535. my ($parent, $title, $iconID) = @_;
  1536. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
  1537. $self->{optgroups} = [];
  1538. $self->{title} = $title;
  1539. $self->{iconID} = $iconID;
  1540. $self->SetScrollbars(1, 1, 1, 1);
  1541. $self->{vsizer} = Wx::BoxSizer->new(wxVERTICAL);
  1542. $self->SetSizer($self->{vsizer});
  1543. return $self;
  1544. }
  1545. sub new_optgroup {
  1546. my ($self, $title, %params) = @_;
  1547. my $optgroup = Slic3r::GUI::ConfigOptionsGroup->new(
  1548. parent => $self,
  1549. title => $title,
  1550. config => $self->GetParent->{config},
  1551. label_width => $params{label_width} // 200,
  1552. on_change => sub {
  1553. my ($opt_key, $value) = @_;
  1554. wxTheApp->CallAfter(sub {
  1555. $self->GetParent->update_dirty;
  1556. $self->GetParent->_on_value_change($opt_key, $value);
  1557. });
  1558. },
  1559. );
  1560. push @{$self->{optgroups}}, $optgroup;
  1561. $self->{vsizer}->Add($optgroup->sizer, 0, wxEXPAND | wxALL, 10);
  1562. return $optgroup;
  1563. }
  1564. sub reload_config {
  1565. my ($self) = @_;
  1566. $_->reload_config for @{$self->{optgroups}};
  1567. }
  1568. sub get_field {
  1569. my ($self, $opt_key, $opt_index) = @_;
  1570. foreach my $optgroup (@{ $self->{optgroups} }) {
  1571. my $field = $optgroup->get_fieldc($opt_key, $opt_index);
  1572. return $field if defined $field;
  1573. }
  1574. return undef;
  1575. }
  1576. sub set_value {
  1577. my $self = shift;
  1578. my ($opt_key, $value) = @_;
  1579. my $changed = 0;
  1580. foreach my $optgroup (@{$self->{optgroups}}) {
  1581. $changed = 1 if $optgroup->set_value($opt_key, $value);
  1582. }
  1583. return $changed;
  1584. }
  1585. package Slic3r::GUI::SavePresetWindow;
  1586. use Wx qw(:combobox :dialog :id :misc :sizer);
  1587. use Wx::Event qw(EVT_BUTTON EVT_TEXT_ENTER);
  1588. use base 'Wx::Dialog';
  1589. sub new {
  1590. my $class = shift;
  1591. my ($parent, %params) = @_;
  1592. my $self = $class->SUPER::new($parent, -1, "Save preset", wxDefaultPosition, wxDefaultSize);
  1593. my @values = @{$params{values}};
  1594. my $text = Wx::StaticText->new($self, -1, "Save " . lc($params{title}) . " as:", wxDefaultPosition, wxDefaultSize);
  1595. $self->{combo} = Wx::ComboBox->new($self, -1, $params{default}, wxDefaultPosition, wxDefaultSize, \@values,
  1596. wxTE_PROCESS_ENTER);
  1597. my $buttons = $self->CreateStdDialogButtonSizer(wxOK | wxCANCEL);
  1598. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  1599. $sizer->Add($text, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 10);
  1600. $sizer->Add($self->{combo}, 0, wxEXPAND | wxLEFT | wxRIGHT, 10);
  1601. $sizer->Add($buttons, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  1602. EVT_BUTTON($self, wxID_OK, \&accept);
  1603. EVT_TEXT_ENTER($self, $self->{combo}, \&accept);
  1604. $self->SetSizer($sizer);
  1605. $sizer->SetSizeHints($self);
  1606. return $self;
  1607. }
  1608. sub accept {
  1609. my ($self, $event) = @_;
  1610. if (($self->{chosen_name} = $self->{combo}->GetValue)) {
  1611. if ($self->{chosen_name} !~ /^[^<>:\/\\|?*\"]+$/) {
  1612. Slic3r::GUI::show_error($self, "The supplied name is not valid; the following characters are not allowed: <>:/\|?*\"");
  1613. } elsif ($self->{chosen_name} eq '- default -') {
  1614. Slic3r::GUI::show_error($self, "The supplied name is not available.");
  1615. } else {
  1616. $self->EndModal(wxID_OK);
  1617. }
  1618. }
  1619. }
  1620. sub get_name {
  1621. my $self = shift;
  1622. return $self->{chosen_name};
  1623. }
  1624. package Slic3r::GUI::Tab::Preset;
  1625. use Moo;
  1626. use List::Util qw(any);
  1627. # The preset represents a "default" set of properties.
  1628. has 'default' => (is => 'ro', default => sub { 0 });
  1629. has 'external' => (is => 'ro', default => sub { 0 });
  1630. has 'name' => (is => 'rw', required => 1);
  1631. has 'file' => (is => 'rw');
  1632. sub config {
  1633. my ($self, $keys) = @_;
  1634. if ($self->default) {
  1635. # Perl class Slic3r::Config extends the C++ class Slic3r::DynamicPrintConfig
  1636. return Slic3r::Config->new_from_defaults(@$keys);
  1637. } else {
  1638. if (!-e Slic3r::encode_path($self->file)) {
  1639. Slic3r::GUI::show_error(undef, "The selected preset does not exist anymore (" . $self->file . ").");
  1640. return undef;
  1641. }
  1642. # apply preset values on top of defaults
  1643. my $config = Slic3r::Config->new_from_defaults(@$keys);
  1644. my $external_config = eval { Slic3r::Config->load($self->file); };
  1645. if ($@) {
  1646. Slic3r::GUI::show_error(undef, $@);
  1647. return undef;
  1648. }
  1649. $config->set($_, $external_config->get($_))
  1650. for grep $external_config->has($_), @$keys;
  1651. if (any { $_ eq 'nozzle_diameter' } @$keys) {
  1652. # Loaded the Printer settings. Verify, that all extruder dependent values have enough values.
  1653. my $nozzle_diameter = $config->nozzle_diameter;
  1654. my $num_extruders = scalar(@{$nozzle_diameter});
  1655. foreach my $key (qw(deretract_speed extruder_colour retract_before_wipe)) {
  1656. my $vec = $config->get($key);
  1657. push @{$vec}, ($vec->[0]) x ($num_extruders - @{$vec});
  1658. $config->set($key, $vec);
  1659. }
  1660. }
  1661. return $config;
  1662. }
  1663. }
  1664. 1;