GUI.pm 17 KB

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