GUI.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. package Slic3r::GUI;
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use Wx 0.9901 qw(:bitmap :dialog :icon :id :misc :systemsettings :toplevelwindow
  6. :filedialog :font);
  7. use Wx::Event qw(EVT_MENU);
  8. BEGIN {
  9. # Wrap the Wx::_load_plugin() function which doesn't work with non-ASCII paths
  10. no warnings 'redefine';
  11. my $orig = *Wx::_load_plugin{CODE};
  12. *Wx::_load_plugin = sub {
  13. $_[0] = Slic3r::decode_path($_[0]);
  14. $orig->(@_);
  15. };
  16. }
  17. use File::Basename qw(basename);
  18. use FindBin;
  19. use List::Util qw(first any);
  20. use Slic3r::Geometry qw(X Y);
  21. use Slic3r::GUI::2DBed;
  22. use Slic3r::GUI::AboutDialog;
  23. use Slic3r::GUI::BedShapeDialog;
  24. use Slic3r::GUI::BonjourBrowser;
  25. use Slic3r::GUI::ConfigWizard;
  26. use Slic3r::GUI::Controller;
  27. use Slic3r::GUI::Controller::ManualControlDialog;
  28. use Slic3r::GUI::Controller::PrinterPanel;
  29. use Slic3r::GUI::MainFrame;
  30. use Slic3r::GUI::Notifier;
  31. use Slic3r::GUI::Plater;
  32. use Slic3r::GUI::Plater::2D;
  33. use Slic3r::GUI::Plater::2DToolpaths;
  34. use Slic3r::GUI::Plater::3D;
  35. use Slic3r::GUI::Plater::3DPreview;
  36. use Slic3r::GUI::Plater::ObjectPartsPanel;
  37. use Slic3r::GUI::Plater::ObjectCutDialog;
  38. use Slic3r::GUI::Plater::ObjectSettingsDialog;
  39. use Slic3r::GUI::Plater::LambdaObjectDialog;
  40. use Slic3r::GUI::Plater::OverrideSettingsPanel;
  41. use Slic3r::GUI::Plater::SplineControl;
  42. use Slic3r::GUI::Preferences;
  43. use Slic3r::GUI::ProgressStatusBar;
  44. use Slic3r::GUI::Projector;
  45. use Slic3r::GUI::OptionsGroup;
  46. use Slic3r::GUI::OptionsGroup::Field;
  47. use Slic3r::GUI::Preset;
  48. use Slic3r::GUI::PresetEditor;
  49. use Slic3r::GUI::PresetEditorDialog;
  50. use Slic3r::GUI::SLAPrintOptions;
  51. use Slic3r::GUI::ReloadDialog;
  52. our $have_OpenGL = eval "use Slic3r::GUI::3DScene; 1";
  53. our $have_LWP = eval "use LWP::UserAgent; 1";
  54. use Wx::Event qw(EVT_IDLE EVT_COMMAND);
  55. use base 'Wx::App';
  56. use constant FILE_WILDCARDS => {
  57. known => 'Known files (*.stl, *.obj, *.amf, *.xml, *.3mf)|*.3mf;*.3MF;*.stl;*.STL;*.obj;*.OBJ;*.amf;*.AMF;*.xml;*.XML',
  58. stl => 'STL files (*.stl)|*.stl;*.STL',
  59. obj => 'OBJ files (*.obj)|*.obj;*.OBJ',
  60. amf => 'AMF files (*.amf)|*.amf;*.AMF;*.xml;*.XML',
  61. tmf => '3MF files (*.3mf)|*.3mf;*.3MF',
  62. ini => 'INI files *.ini|*.ini;*.INI',
  63. gcode => 'G-code files (*.gcode, *.gco, *.g, *.ngc)|*.gcode;*.GCODE;*.gco;*.GCO;*.g;*.G;*.ngc;*.NGC',
  64. svg => 'SVG files *.svg|*.svg;*.SVG',
  65. };
  66. use constant MODEL_WILDCARD => join '|', @{&FILE_WILDCARDS}{qw(known stl obj amf tmf)};
  67. use constant STL_MODEL_WILDCARD => join '|', @{&FILE_WILDCARDS}{qw(stl)};
  68. use constant AMF_MODEL_WILDCARD => join '|', @{&FILE_WILDCARDS}{qw(amf)};
  69. use constant TMF_MODEL_WILDCARD => join '|', @{&FILE_WILDCARDS}{qw(tmf)};
  70. our $datadir;
  71. # If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
  72. our $autosave;
  73. our $threads;
  74. our @cb;
  75. our $Settings = {
  76. _ => {
  77. version_check => 1,
  78. autocenter => 1,
  79. autoalignz => 1,
  80. invert_zoom => 0,
  81. background_processing => 0,
  82. threads => $Slic3r::Config::Options->{threads}{default},
  83. color_toolpaths_by => 'role',
  84. tabbed_preset_editors => 1,
  85. show_host => 0,
  86. nudge_val => 1,
  87. reload_hide_dialog => 0,
  88. reload_behavior => 0
  89. },
  90. };
  91. our $have_button_icons = &Wx::wxVERSION_STRING =~ / (?:2\.9\.[1-9]|3\.)/;
  92. our $small_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  93. $small_font->SetPointSize(11) if &Wx::wxMAC;
  94. our $small_bold_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  95. $small_bold_font->SetPointSize(11) if &Wx::wxMAC;
  96. $small_bold_font->SetWeight(wxFONTWEIGHT_BOLD);
  97. our $medium_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  98. $medium_font->SetPointSize(12);
  99. our $grey = Wx::Colour->new(200,200,200);
  100. # to use in ScrolledWindow::SetScrollRate(xstep, ystep)
  101. # step related to system font point size
  102. our $scroll_step = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)->GetPointSize;
  103. our $VERSION_CHECK_EVENT : shared = Wx::NewEventType;
  104. our $DLP_projection_screen;
  105. sub OnInit {
  106. my ($self) = @_;
  107. $self->SetAppName('Slic3r');
  108. Slic3r::debugf "wxWidgets version %s, Wx version %s\n", &Wx::wxVERSION_STRING, $Wx::VERSION;
  109. $self->{notifier} = Slic3r::GUI::Notifier->new;
  110. $self->{presets} = { print => [], filament => [], printer => [] };
  111. # locate or create data directory
  112. # Unix: ~/.Slic3r
  113. # Windows: "C:\Users\username\AppData\Roaming\Slic3r" or "C:\Documents and Settings\username\Application Data\Slic3r"
  114. # Mac: "~/Library/Application Support/Slic3r"
  115. $datadir ||= Slic3r::decode_path(Wx::StandardPaths::Get->GetUserDataDir);
  116. my $enc_datadir = Slic3r::encode_path($datadir);
  117. Slic3r::debugf "Data directory: %s\n", $datadir;
  118. # just checking for existence of $datadir is not enough: it may be an empty directory
  119. # supplied as argument to --datadir; in that case we should still run the wizard
  120. my $run_wizard = (-d $enc_datadir && -e "$enc_datadir/slic3r.ini") ? 0 : 1;
  121. foreach my $dir ($enc_datadir, "$enc_datadir/print", "$enc_datadir/filament", "$enc_datadir/printer") {
  122. next if -d $dir;
  123. if (!mkdir $dir) {
  124. my $error = "Slic3r was unable to create its data directory at $dir ($!).";
  125. warn "$error\n";
  126. fatal_error(undef, $error);
  127. }
  128. }
  129. # load settings
  130. my $last_version;
  131. if (-f "$enc_datadir/slic3r.ini") {
  132. my $ini = eval { Slic3r::Config->read_ini("$datadir/slic3r.ini") };
  133. if ($ini) {
  134. $last_version = $ini->{_}{version};
  135. $ini->{_}{$_} = $Settings->{_}{$_}
  136. for grep !exists $ini->{_}{$_}, keys %{$Settings->{_}};
  137. $Settings = $ini;
  138. }
  139. delete $Settings->{_}{mode}; # handle legacy
  140. }
  141. $Settings->{_}{version} = $Slic3r::VERSION;
  142. $Settings->{_}{threads} = $threads if $threads;
  143. $self->save_settings;
  144. if (-f "$enc_datadir/simple.ini") {
  145. # The Simple Mode settings were already automatically duplicated to presets
  146. # named "Simple Mode" in each group, so we already support retrocompatibility.
  147. unlink "$enc_datadir/simple.ini";
  148. }
  149. $self->load_presets;
  150. # application frame
  151. Wx::Image::AddHandler(Wx::PNGHandler->new);
  152. $self->{mainframe} = my $frame = Slic3r::GUI::MainFrame->new;
  153. $self->SetTopWindow($frame);
  154. # load init bundle
  155. {
  156. my @dirs = ($FindBin::Bin);
  157. if (&Wx::wxMAC) {
  158. push @dirs, qw();
  159. } elsif (&Wx::wxMSW) {
  160. push @dirs, qw();
  161. }
  162. my $init_bundle = first { -e $_ } map "$_/.init_bundle.ini", @dirs;
  163. if ($init_bundle) {
  164. Slic3r::debugf "Loading config bundle from %s\n", $init_bundle;
  165. $self->{mainframe}->load_configbundle($init_bundle, 1);
  166. $run_wizard = 0;
  167. }
  168. }
  169. if (!$run_wizard && (!defined $last_version || $last_version ne $Slic3r::VERSION)) {
  170. # user was running another Slic3r version on this computer
  171. if (!defined $last_version || $last_version =~ /^0\./) {
  172. show_info($self->{mainframe}, "Hello! Support material was improved since the "
  173. . "last version of Slic3r you used. It is strongly recommended to revert "
  174. . "your support material settings to the factory defaults and start from "
  175. . "those. Enjoy and provide feedback!", "Support Material");
  176. }
  177. if (!defined $last_version || $last_version =~ /^(?:0|1\.[01])\./) {
  178. show_info($self->{mainframe}, "Hello! In this version a new Bed Shape option was "
  179. . "added. If the bed coordinates in the plater preview screen look wrong, go "
  180. . "to Print Settings and click the \"Set\" button next to \"Bed Shape\".", "Bed Shape");
  181. }
  182. }
  183. $self->{mainframe}->config_wizard if $run_wizard;
  184. $self->check_version
  185. if $self->have_version_check
  186. && ($Settings->{_}{version_check} // 1)
  187. && (!$Settings->{_}{last_version_check} || (time - $Settings->{_}{last_version_check}) >= 86400);
  188. EVT_IDLE($frame, sub {
  189. while (my $cb = shift @cb) {
  190. $cb->();
  191. }
  192. });
  193. EVT_COMMAND($self, -1, $VERSION_CHECK_EVENT, sub {
  194. my ($self, $event) = @_;
  195. my ($success, $response, $manual_check) = @{$event->GetData};
  196. if ($success) {
  197. if ($response =~ /^obsolete ?= ?([a-z0-9.-]+,)*\Q$Slic3r::VERSION\E(?:,|$)/) {
  198. my $res = Wx::MessageDialog->new(undef, "A new version is available. Do you want to open the Slic3r website now?",
  199. 'Update', wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_INFORMATION | wxICON_ERROR)->ShowModal;
  200. Wx::LaunchDefaultBrowser('http://slic3r.org/') if $res == wxID_YES;
  201. } else {
  202. Slic3r::GUI::show_info(undef, "You're using the latest version. No updates are available.") if $manual_check;
  203. }
  204. $Settings->{_}{last_version_check} = time();
  205. $self->save_settings;
  206. } else {
  207. Slic3r::GUI::show_error(undef, "Failed to check for updates. Try later.") if $manual_check;
  208. }
  209. });
  210. return 1;
  211. }
  212. sub about {
  213. my ($self) = @_;
  214. my $about = Slic3r::GUI::AboutDialog->new(undef);
  215. $about->ShowModal;
  216. $about->Destroy;
  217. }
  218. # static method accepting a wxWindow object as first parameter
  219. sub catch_error {
  220. my ($self, $cb, $message_dialog) = @_;
  221. if (my $err = $@) {
  222. $cb->() if $cb;
  223. $message_dialog
  224. ? $message_dialog->($err, 'Error', wxOK | wxICON_ERROR)
  225. : Slic3r::GUI::show_error($self, $err);
  226. return 1;
  227. }
  228. return 0;
  229. }
  230. # static method accepting a wxWindow object as first parameter
  231. sub show_error {
  232. my ($parent, $message) = @_;
  233. Wx::MessageDialog->new($parent, $message, 'Error', wxOK | wxICON_ERROR)->ShowModal;
  234. }
  235. # static method accepting a wxWindow object as first parameter
  236. sub show_info {
  237. my ($parent, $message, $title) = @_;
  238. Wx::MessageDialog->new($parent, $message, $title || 'Notice', wxOK | wxICON_INFORMATION)->ShowModal;
  239. }
  240. # static method accepting a wxWindow object as first parameter
  241. sub fatal_error {
  242. show_error(@_);
  243. exit 1;
  244. }
  245. # static method accepting a wxWindow object as first parameter
  246. sub warning_catcher {
  247. my ($self, $message_dialog) = @_;
  248. return sub {
  249. my $message = shift;
  250. return if $message =~ /GLUquadricObjPtr|Attempt to free unreferenced scalar/;
  251. my @params = ($message, 'Warning', wxOK | wxICON_WARNING);
  252. $message_dialog
  253. ? $message_dialog->(@params)
  254. : Wx::MessageDialog->new($self, @params)->ShowModal;
  255. };
  256. }
  257. sub notify {
  258. my ($self, $message) = @_;
  259. my $frame = $self->GetTopWindow;
  260. # try harder to attract user attention on OS X
  261. $frame->RequestUserAttention(&Wx::wxMAC ? wxUSER_ATTENTION_ERROR : wxUSER_ATTENTION_INFO)
  262. unless ($frame->IsActive);
  263. $self->{notifier}->notify($message);
  264. }
  265. sub save_settings {
  266. my ($self) = @_;
  267. Slic3r::Config->write_ini("$datadir/slic3r.ini", $Settings);
  268. }
  269. sub presets { return $_[0]->{presets}; }
  270. sub load_presets {
  271. my ($self) = @_;
  272. for my $group (qw(printer filament print)) {
  273. my $presets = $self->{presets}{$group};
  274. # keep external or dirty presets
  275. @$presets = grep { ($_->external && $_->file_exists) || $_->dirty } @$presets;
  276. my $dir = "$Slic3r::GUI::datadir/$group";
  277. opendir my $dh, Slic3r::encode_path($dir)
  278. or die "Failed to read directory $dir (errno: $!)\n";
  279. foreach my $file (grep /\.ini$/i, readdir $dh) {
  280. $file = Slic3r::decode_path($file);
  281. my $name = basename($file);
  282. $name =~ s/\.ini$//i;
  283. # skip if we already have it
  284. next if any { $_->name eq $name } @$presets;
  285. push @$presets, Slic3r::GUI::Preset->new(
  286. group => $group,
  287. name => $name,
  288. file => "$dir/$file",
  289. );
  290. }
  291. closedir $dh;
  292. @$presets = sort { $a->name cmp $b->name } @$presets;
  293. unshift @$presets, Slic3r::GUI::Preset->new(
  294. group => $group,
  295. default => 1,
  296. name => '- default -',
  297. );
  298. }
  299. }
  300. sub add_external_preset {
  301. my ($self, $file) = @_;
  302. my $name = basename($file); # keep .ini suffix
  303. for my $group (qw(printer filament print)) {
  304. my $presets = $self->{presets}{$group};
  305. # remove any existing preset with the same name
  306. @$presets = grep { $_->name ne $name } @$presets;
  307. push @$presets, Slic3r::GUI::Preset->new(
  308. group => $group,
  309. name => $name,
  310. file => $file,
  311. external => 1,
  312. );
  313. }
  314. return $name;
  315. }
  316. sub have_version_check {
  317. my ($self) = @_;
  318. # return an explicit 0
  319. return ($Slic3r::have_threads && $Slic3r::VERSION !~ /-dev$/ && $have_LWP) || 0;
  320. }
  321. sub check_version {
  322. my ($self, $manual_check) = @_;
  323. Slic3r::debugf "Checking for updates...\n";
  324. @_ = ();
  325. threads->create(sub {
  326. my $ua = LWP::UserAgent->new;
  327. $ua->timeout(10);
  328. my $response = $ua->get('http://slic3r.org/updatecheck');
  329. Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $VERSION_CHECK_EVENT,
  330. threads::shared::shared_clone([ $response->is_success, $response->decoded_content, $manual_check ])));
  331. Slic3r::thread_cleanup();
  332. })->detach;
  333. }
  334. sub output_path {
  335. my ($self, $dir) = @_;
  336. return ($Settings->{_}{last_output_path} && $Settings->{_}{remember_output_path})
  337. ? $Settings->{_}{last_output_path}
  338. : $dir;
  339. }
  340. sub open_model {
  341. my ($self, $window) = @_;
  342. my $dir = $Slic3r::GUI::Settings->{recent}{skein_directory}
  343. || $Slic3r::GUI::Settings->{recent}{config_directory}
  344. || '';
  345. my $dialog = Wx::FileDialog->new($window // $self->GetTopWindow, 'Choose one or more files (STL/OBJ/AMF/3MF):', $dir, "",
  346. MODEL_WILDCARD, wxFD_OPEN | wxFD_MULTIPLE | wxFD_FILE_MUST_EXIST);
  347. if ($dialog->ShowModal != wxID_OK) {
  348. $dialog->Destroy;
  349. return;
  350. }
  351. my @input_files = map Slic3r::decode_path($_), $dialog->GetPaths;
  352. $dialog->Destroy;
  353. return @input_files;
  354. }
  355. sub CallAfter {
  356. my ($self, $cb) = @_;
  357. push @cb, $cb;
  358. }
  359. sub scan_serial_ports {
  360. my ($self) = @_;
  361. my @ports = ();
  362. if ($^O eq 'MSWin32') {
  363. # Windows
  364. if (eval "use Win32::TieRegistry; 1") {
  365. my $ts = Win32::TieRegistry->new("HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM",
  366. { Access => 'KEY_READ' });
  367. if ($ts) {
  368. # when no serial ports are available, the registry key doesn't exist and
  369. # TieRegistry->new returns undef
  370. $ts->Tie(\my %reg);
  371. push @ports, sort values %reg;
  372. }
  373. }
  374. } else {
  375. # UNIX and OS X
  376. push @ports, glob '/dev/{ttyUSB,ttyACM,tty.,cu.,rfcomm}*';
  377. }
  378. return grep !/Bluetooth|FireFly/, @ports;
  379. }
  380. sub append_menu_item {
  381. my ($self, $menu, $string, $description, $cb, $id, $icon, $kind) = @_;
  382. $id //= &Wx::NewId();
  383. my $item = Wx::MenuItem->new($menu, $id, $string, $description // '', $kind // 0);
  384. $self->set_menu_item_icon($item, $icon);
  385. $menu->Append($item);
  386. EVT_MENU($self, $id, $cb);
  387. return $item;
  388. }
  389. sub append_submenu {
  390. my ($self, $menu, $string, $description, $submenu, $id, $icon) = @_;
  391. $id //= &Wx::NewId();
  392. my $item = Wx::MenuItem->new($menu, $id, $string, $description // '');
  393. $self->set_menu_item_icon($item, $icon);
  394. $item->SetSubMenu($submenu);
  395. $menu->Append($item);
  396. return $item;
  397. }
  398. sub set_menu_item_icon {
  399. my ($self, $menuItem, $icon) = @_;
  400. # SetBitmap was not available on OS X before Wx 0.9927
  401. if ($icon && $menuItem->can('SetBitmap')) {
  402. $menuItem->SetBitmap(Wx::Bitmap->new($Slic3r::var->($icon), wxBITMAP_TYPE_PNG));
  403. }
  404. }
  405. sub save_window_pos {
  406. my ($self, $window, $name) = @_;
  407. $Settings->{_}{"${name}_pos"} = join ',', $window->GetScreenPositionXY;
  408. $Settings->{_}{"${name}_size"} = join ',', $window->GetSizeWH;
  409. $Settings->{_}{"${name}_maximized"} = $window->IsMaximized;
  410. $self->save_settings;
  411. }
  412. sub restore_window_pos {
  413. my ($self, $window, $name) = @_;
  414. if (defined $Settings->{_}{"${name}_pos"}) {
  415. my $size = [ split ',', $Settings->{_}{"${name}_size"}, 2 ];
  416. $window->SetSize($size);
  417. my $display = Wx::Display->new->GetClientArea();
  418. my $pos = [ split ',', $Settings->{_}{"${name}_pos"}, 2 ];
  419. if (($pos->[X] + $size->[X]/2) < $display->GetRight && ($pos->[Y] + $size->[Y]/2) < $display->GetBottom) {
  420. $window->Move($pos);
  421. }
  422. $window->Maximize(1) if $Settings->{_}{"${name}_maximized"};
  423. }
  424. }
  425. 1;