GUI.pm 16 KB

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