GUI.pm 11 KB

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