GUI.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package Slic3r::GUI;
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use File::Basename qw(basename);
  6. use FindBin;
  7. use Slic3r::GUI::AboutDialog;
  8. use Slic3r::GUI::BedShapeDialog;
  9. use Slic3r::GUI::BonjourBrowser;
  10. use Slic3r::GUI::ConfigWizard;
  11. use Slic3r::GUI::Controller;
  12. use Slic3r::GUI::Controller::PrinterPanel;
  13. use Slic3r::GUI::MainFrame;
  14. use Slic3r::GUI::Notifier;
  15. use Slic3r::GUI::Plater;
  16. use Slic3r::GUI::Plater::2D;
  17. use Slic3r::GUI::Plater::2DToolpaths;
  18. use Slic3r::GUI::Plater::3D;
  19. use Slic3r::GUI::Plater::3DPreview;
  20. use Slic3r::GUI::Plater::ObjectPartsPanel;
  21. use Slic3r::GUI::Plater::ObjectCutDialog;
  22. use Slic3r::GUI::Plater::ObjectSettingsDialog;
  23. use Slic3r::GUI::Plater::OverrideSettingsPanel;
  24. use Slic3r::GUI::Preferences;
  25. use Slic3r::GUI::ProgressStatusBar;
  26. use Slic3r::GUI::OptionsGroup;
  27. use Slic3r::GUI::OptionsGroup::Field;
  28. use Slic3r::GUI::SimpleTab;
  29. use Slic3r::GUI::Tab;
  30. our $have_OpenGL = eval "use Slic3r::GUI::3DScene; 1";
  31. our $have_LWP = eval "use LWP::UserAgent; 1";
  32. use Wx 0.9901 qw(:bitmap :dialog :icon :id :misc :systemsettings :toplevelwindow
  33. :filedialog);
  34. use Wx::Event qw(EVT_IDLE EVT_COMMAND);
  35. use base 'Wx::App';
  36. use constant FILE_WILDCARDS => {
  37. known => 'Known files (*.stl, *.obj, *.amf, *.xml)|*.stl;*.STL;*.obj;*.OBJ;*.amf;*.AMF;*.xml;*.XML',
  38. stl => 'STL files (*.stl)|*.stl;*.STL',
  39. obj => 'OBJ files (*.obj)|*.obj;*.OBJ',
  40. amf => 'AMF files (*.amf)|*.amf;*.AMF;*.xml;*.XML',
  41. ini => 'INI files *.ini|*.ini;*.INI',
  42. gcode => 'G-code files (*.gcode, *.gco, *.g, *.ngc)|*.gcode;*.GCODE;*.gco;*.GCO;*.g;*.G;*.ngc;*.NGC',
  43. svg => 'SVG files *.svg|*.svg;*.SVG',
  44. };
  45. use constant MODEL_WILDCARD => join '|', @{&FILE_WILDCARDS}{qw(known stl obj amf)};
  46. our $datadir;
  47. our $no_plater;
  48. our $mode;
  49. our $autosave;
  50. our @cb;
  51. our $Settings = {
  52. _ => {
  53. mode => 'simple',
  54. version_check => 1,
  55. autocenter => 1,
  56. background_processing => 1,
  57. },
  58. };
  59. our $have_button_icons = &Wx::wxVERSION_STRING =~ / (?:2\.9\.[1-9]|3\.)/;
  60. our $small_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  61. $small_font->SetPointSize(11) if !&Wx::wxMSW;
  62. our $medium_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  63. $medium_font->SetPointSize(12);
  64. our $grey = Wx::Colour->new(200,200,200);
  65. our $VERSION_CHECK_EVENT : shared = Wx::NewEventType;
  66. sub OnInit {
  67. my ($self) = @_;
  68. $self->SetAppName('Slic3r');
  69. Slic3r::debugf "wxWidgets version %s, Wx version %s\n", &Wx::wxVERSION_STRING, $Wx::VERSION;
  70. $self->{notifier} = Slic3r::GUI::Notifier->new;
  71. # locate or create data directory
  72. $datadir ||= Slic3r::decode_path(Wx::StandardPaths::Get->GetUserDataDir);
  73. my $enc_datadir = Slic3r::encode_path($datadir);
  74. Slic3r::debugf "Data directory: %s\n", $datadir;
  75. # just checking for existence of $datadir is not enough: it may be an empty directory
  76. # supplied as argument to --datadir; in that case we should still run the wizard
  77. my $run_wizard = (-d $enc_datadir && -e "$enc_datadir/slic3r.ini") ? 0 : 1;
  78. foreach my $dir ($enc_datadir, "$enc_datadir/print", "$enc_datadir/filament", "$enc_datadir/printer") {
  79. next if -d $dir;
  80. if (!mkdir $dir) {
  81. my $error = "Slic3r was unable to create its data directory at $dir ($!).";
  82. warn "$error\n";
  83. fatal_error(undef, $error);
  84. }
  85. }
  86. # load settings
  87. my $last_version;
  88. if (-f "$enc_datadir/slic3r.ini") {
  89. my $ini = eval { Slic3r::Config->read_ini("$datadir/slic3r.ini") };
  90. $Settings = $ini if $ini;
  91. $last_version = $Settings->{_}{version};
  92. $Settings->{_}{mode} ||= 'expert';
  93. $Settings->{_}{autocenter} //= 1;
  94. $Settings->{_}{background_processing} //= 1;
  95. }
  96. $Settings->{_}{version} = $Slic3r::VERSION;
  97. $self->save_settings;
  98. # application frame
  99. Wx::Image::AddHandler(Wx::PNGHandler->new);
  100. $self->{mainframe} = my $frame = Slic3r::GUI::MainFrame->new(
  101. mode => $mode // $Settings->{_}{mode},
  102. no_plater => $no_plater,
  103. );
  104. $self->SetTopWindow($frame);
  105. if (!$run_wizard && (!defined $last_version || $last_version ne $Slic3r::VERSION)) {
  106. # user was running another Slic3r version on this computer
  107. if (!defined $last_version || $last_version =~ /^0\./) {
  108. show_info($self->{mainframe}, "Hello! Support material was improved since the "
  109. . "last version of Slic3r you used. It is strongly recommended to revert "
  110. . "your support material settings to the factory defaults and start from "
  111. . "those. Enjoy and provide feedback!", "Support Material");
  112. }
  113. if (!defined $last_version || $last_version =~ /^(?:0|1\.[01])\./) {
  114. show_info($self->{mainframe}, "Hello! In this version a new Bed Shape option was "
  115. . "added. If the bed coordinates in the plater preview screen look wrong, go "
  116. . "to Print Settings and click the \"Set\" button next to \"Bed Shape\".", "Bed Shape");
  117. }
  118. }
  119. $self->{mainframe}->config_wizard if $run_wizard;
  120. $self->check_version
  121. if $self->have_version_check
  122. && ($Settings->{_}{version_check} // 1)
  123. && (!$Settings->{_}{last_version_check} || (time - $Settings->{_}{last_version_check}) >= 86400);
  124. EVT_IDLE($frame, sub {
  125. while (my $cb = shift @cb) {
  126. $cb->();
  127. }
  128. });
  129. EVT_COMMAND($self, -1, $VERSION_CHECK_EVENT, sub {
  130. my ($self, $event) = @_;
  131. my ($success, $response, $manual_check) = @{$event->GetData};
  132. if ($success) {
  133. if ($response =~ /^obsolete ?= ?([a-z0-9.-]+,)*\Q$Slic3r::VERSION\E(?:,|$)/) {
  134. my $res = Wx::MessageDialog->new(undef, "A new version is available. Do you want to open the Slic3r website now?",
  135. 'Update', wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_INFORMATION | wxICON_ERROR)->ShowModal;
  136. Wx::LaunchDefaultBrowser('http://slic3r.org/') if $res == wxID_YES;
  137. } else {
  138. Slic3r::GUI::show_info(undef, "You're using the latest version. No updates are available.") if $manual_check;
  139. }
  140. $Settings->{_}{last_version_check} = time();
  141. $self->save_settings;
  142. } else {
  143. Slic3r::GUI::show_error(undef, "Failed to check for updates. Try later.") if $manual_check;
  144. }
  145. });
  146. return 1;
  147. }
  148. sub about {
  149. my ($self) = @_;
  150. my $about = Slic3r::GUI::AboutDialog->new(undef);
  151. $about->ShowModal;
  152. $about->Destroy;
  153. }
  154. # static method accepting a wxWindow object as first parameter
  155. sub catch_error {
  156. my ($self, $cb, $message_dialog) = @_;
  157. if (my $err = $@) {
  158. $cb->() if $cb;
  159. $message_dialog
  160. ? $message_dialog->($err, 'Error', wxOK | wxICON_ERROR)
  161. : Slic3r::GUI::show_error($self, $err);
  162. return 1;
  163. }
  164. return 0;
  165. }
  166. # static method accepting a wxWindow object as first parameter
  167. sub show_error {
  168. my ($parent, $message) = @_;
  169. Wx::MessageDialog->new($parent, $message, 'Error', wxOK | wxICON_ERROR)->ShowModal;
  170. }
  171. # static method accepting a wxWindow object as first parameter
  172. sub show_info {
  173. my ($parent, $message, $title) = @_;
  174. Wx::MessageDialog->new($parent, $message, $title || 'Notice', wxOK | wxICON_INFORMATION)->ShowModal;
  175. }
  176. # static method accepting a wxWindow object as first parameter
  177. sub fatal_error {
  178. show_error(@_);
  179. exit 1;
  180. }
  181. # static method accepting a wxWindow object as first parameter
  182. sub warning_catcher {
  183. my ($self, $message_dialog) = @_;
  184. return sub {
  185. my $message = shift;
  186. return if $message =~ /GLUquadricObjPtr|Attempt to free unreferenced scalar/;
  187. my @params = ($message, 'Warning', wxOK | wxICON_WARNING);
  188. $message_dialog
  189. ? $message_dialog->(@params)
  190. : Wx::MessageDialog->new($self, @params)->ShowModal;
  191. };
  192. }
  193. sub notify {
  194. my ($self, $message) = @_;
  195. my $frame = $self->GetTopWindow;
  196. # try harder to attract user attention on OS X
  197. $frame->RequestUserAttention(&Wx::wxMAC ? wxUSER_ATTENTION_ERROR : wxUSER_ATTENTION_INFO)
  198. unless ($frame->IsActive);
  199. $self->{notifier}->notify($message);
  200. }
  201. sub save_settings {
  202. my ($self) = @_;
  203. Slic3r::Config->write_ini("$datadir/slic3r.ini", $Settings);
  204. }
  205. sub presets {
  206. my ($self, $section) = @_;
  207. my %presets = ();
  208. opendir my $dh, Slic3r::encode_path("$Slic3r::GUI::datadir/$section")
  209. or die "Failed to read directory $Slic3r::GUI::datadir/$section (errno: $!)\n";
  210. foreach my $file (grep /\.ini$/i, readdir $dh) {
  211. $file = Slic3r::decode_path($file);
  212. my $name = basename($file);
  213. $name =~ s/\.ini$//;
  214. $presets{$name} = "$Slic3r::GUI::datadir/$section/$file";
  215. }
  216. closedir $dh;
  217. return %presets;
  218. }
  219. sub have_version_check {
  220. my ($self) = @_;
  221. # return an explicit 0
  222. return ($Slic3r::have_threads && $Slic3r::build && $have_LWP) || 0;
  223. }
  224. sub check_version {
  225. my ($self, $manual_check) = @_;
  226. Slic3r::debugf "Checking for updates...\n";
  227. @_ = ();
  228. threads->create(sub {
  229. my $ua = LWP::UserAgent->new;
  230. $ua->timeout(10);
  231. my $response = $ua->get('http://slic3r.org/updatecheck');
  232. Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $VERSION_CHECK_EVENT,
  233. threads::shared::shared_clone([ $response->is_success, $response->decoded_content, $manual_check ])));
  234. Slic3r::thread_cleanup();
  235. })->detach;
  236. }
  237. sub output_path {
  238. my ($self, $dir) = @_;
  239. return ($Settings->{_}{last_output_path} && $Settings->{_}{remember_output_path})
  240. ? $Settings->{_}{last_output_path}
  241. : $dir;
  242. }
  243. sub open_model {
  244. my ($self, $window) = @_;
  245. my $dir = $Slic3r::GUI::Settings->{recent}{skein_directory}
  246. || $Slic3r::GUI::Settings->{recent}{config_directory}
  247. || '';
  248. my $dialog = Wx::FileDialog->new($window // $self->GetTopWindow, 'Choose one or more files (STL/OBJ/AMF):', $dir, "",
  249. MODEL_WILDCARD, wxFD_OPEN | wxFD_MULTIPLE | wxFD_FILE_MUST_EXIST);
  250. if ($dialog->ShowModal != wxID_OK) {
  251. $dialog->Destroy;
  252. return;
  253. }
  254. my @input_files = map Slic3r::decode_path($_), $dialog->GetPaths;
  255. $dialog->Destroy;
  256. return @input_files;
  257. }
  258. sub CallAfter {
  259. my ($self, $cb) = @_;
  260. push @cb, $cb;
  261. }
  262. sub scan_serial_ports {
  263. my ($self) = @_;
  264. my @ports = ();
  265. if ($^O eq 'MSWin32') {
  266. # Windows
  267. my %reg;
  268. if (eval "use Win32::TieRegistry (TiedHash => \\%reg); 1") {
  269. push @ports, sort values %{$reg{"HKEY_CURRENT_USER\\HARDWARE\\DEVICEMAP\\SERIALCOMM"}};
  270. }
  271. } else {
  272. # UNIX and OS X
  273. push @ports, glob '/dev/{ttyUSB,ttyACM,tty.,cu.,rfcomm}*';
  274. }
  275. return @ports;
  276. }
  277. 1;