GUI.pm 17 KB

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