GUI.pm 9.7 KB

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