GUI.pm 17 KB

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