GUI.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. # Datadir provided on the command line.
  51. our $datadir;
  52. # If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
  53. our $no_plater;
  54. our @cb;
  55. our $small_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  56. $small_font->SetPointSize(11) if &Wx::wxMAC;
  57. our $small_bold_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  58. $small_bold_font->SetPointSize(11) if &Wx::wxMAC;
  59. $small_bold_font->SetWeight(wxFONTWEIGHT_BOLD);
  60. our $medium_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  61. $medium_font->SetPointSize(12);
  62. our $grey = Wx::Colour->new(200,200,200);
  63. sub OnInit {
  64. my ($self) = @_;
  65. $self->SetAppName('Slic3rPE');
  66. $self->SetAppDisplayName('Slic3r Prusa Edition');
  67. Slic3r::debugf "wxWidgets version %s, Wx version %s\n", &Wx::wxVERSION_STRING, $Wx::VERSION;
  68. # Set the Slic3r data directory at the Slic3r XS module.
  69. # Unix: ~/.Slic3r
  70. # Windows: "C:\Users\username\AppData\Roaming\Slic3r" or "C:\Documents and Settings\username\Application Data\Slic3r"
  71. # Mac: "~/Library/Application Support/Slic3r"
  72. Slic3r::set_data_dir($datadir || Wx::StandardPaths::Get->GetUserDataDir);
  73. Slic3r::GUI::set_wxapp($self);
  74. $self->{notifier} = Slic3r::GUI::Notifier->new;
  75. $self->{app_config} = Slic3r::GUI::AppConfig->new;
  76. $self->{preset_bundle} = Slic3r::GUI::PresetBundle->new;
  77. # just checking for existence of Slic3r::data_dir is not enough: it may be an empty directory
  78. # supplied as argument to --datadir; in that case we should still run the wizard
  79. eval { $self->{preset_bundle}->setup_directories() };
  80. if ($@) {
  81. warn $@ . "\n";
  82. fatal_error(undef, $@);
  83. }
  84. my $run_wizard = ! $self->{app_config}->exists;
  85. # load settings
  86. $self->{app_config}->load if ! $run_wizard;
  87. $self->{app_config}->set('version', $Slic3r::VERSION);
  88. $self->{app_config}->save;
  89. # Suppress the '- default -' presets.
  90. $self->{preset_bundle}->set_default_suppressed($self->{app_config}->get('no_defaults') ? 1 : 0);
  91. eval { $self->{preset_bundle}->load_presets };
  92. if ($@) {
  93. warn $@ . "\n";
  94. show_error(undef, $@);
  95. }
  96. eval { $self->{preset_bundle}->load_selections($self->{app_config}) };
  97. $run_wizard = 1 if $self->{preset_bundle}->has_defauls_only;
  98. # application frame
  99. Wx::Image::FindHandlerType(wxBITMAP_TYPE_PNG) || Wx::Image::AddHandler(Wx::PNGHandler->new);
  100. $self->{mainframe} = my $frame = Slic3r::GUI::MainFrame->new(
  101. # If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
  102. no_controller => $self->{app_config}->get('no_controller'),
  103. no_plater => $no_plater,
  104. );
  105. $self->SetTopWindow($frame);
  106. EVT_IDLE($frame, sub {
  107. while (my $cb = shift @cb) {
  108. $cb->();
  109. }
  110. $self->{app_config}->save if $self->{app_config}->dirty;
  111. });
  112. if ($run_wizard) {
  113. # On OSX the UI was not initialized correctly if the wizard was called
  114. # before the UI was up and running.
  115. $self->CallAfter(sub {
  116. # Run the config wizard, don't offer the "reset user profile" checkbox.
  117. $self->{mainframe}->config_wizard(1);
  118. });
  119. }
  120. return 1;
  121. }
  122. sub about {
  123. my ($self) = @_;
  124. my $about = Slic3r::GUI::AboutDialog->new(undef);
  125. $about->ShowModal;
  126. $about->Destroy;
  127. }
  128. sub system_info {
  129. my ($self) = @_;
  130. my $slic3r_info = Slic3r::slic3r_info(format => 'html');
  131. my $copyright_info = Slic3r::copyright_info(format => 'html');
  132. my $system_info = Slic3r::system_info(format => 'html');
  133. my $opengl_info;
  134. my $opengl_info_txt = '';
  135. if (defined($self->{mainframe}) && defined($self->{mainframe}->{plater}) &&
  136. defined($self->{mainframe}->{plater}->{canvas3D})) {
  137. $opengl_info = $self->{mainframe}->{plater}->{canvas3D}->opengl_info(format => 'html');
  138. $opengl_info_txt = $self->{mainframe}->{plater}->{canvas3D}->opengl_info;
  139. }
  140. my $about = Slic3r::GUI::SystemInfo->new(
  141. parent => undef,
  142. slic3r_info => $slic3r_info,
  143. # copyright_info => $copyright_info,
  144. system_info => $system_info,
  145. opengl_info => $opengl_info,
  146. text_info => Slic3r::slic3r_info . Slic3r::system_info . $opengl_info_txt,
  147. );
  148. $about->ShowModal;
  149. $about->Destroy;
  150. }
  151. # static method accepting a wxWindow object as first parameter
  152. sub catch_error {
  153. my ($self, $cb, $message_dialog) = @_;
  154. if (my $err = $@) {
  155. $cb->() if $cb;
  156. $message_dialog
  157. ? $message_dialog->($err, 'Error', wxOK | wxICON_ERROR)
  158. : Slic3r::GUI::show_error($self, $err);
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. # static method accepting a wxWindow object as first parameter
  164. sub show_error {
  165. my ($parent, $message) = @_;
  166. Wx::MessageDialog->new($parent, $message, 'Error', wxOK | wxICON_ERROR)->ShowModal;
  167. }
  168. # static method accepting a wxWindow object as first parameter
  169. sub show_info {
  170. my ($parent, $message, $title) = @_;
  171. Wx::MessageDialog->new($parent, $message, $title || 'Notice', wxOK | wxICON_INFORMATION)->ShowModal;
  172. }
  173. # static method accepting a wxWindow object as first parameter
  174. sub fatal_error {
  175. show_error(@_);
  176. exit 1;
  177. }
  178. # static method accepting a wxWindow object as first parameter
  179. sub warning_catcher {
  180. my ($self, $message_dialog) = @_;
  181. return sub {
  182. my $message = shift;
  183. return if $message =~ /GLUquadricObjPtr|Attempt to free unreferenced scalar/;
  184. my @params = ($message, 'Warning', wxOK | wxICON_WARNING);
  185. $message_dialog
  186. ? $message_dialog->(@params)
  187. : Wx::MessageDialog->new($self, @params)->ShowModal;
  188. };
  189. }
  190. sub notify {
  191. my ($self, $message) = @_;
  192. my $frame = $self->GetTopWindow;
  193. # try harder to attract user attention on OS X
  194. $frame->RequestUserAttention(&Wx::wxMAC ? wxUSER_ATTENTION_ERROR : wxUSER_ATTENTION_INFO)
  195. unless ($frame->IsActive);
  196. $self->{notifier}->notify($message);
  197. }
  198. # Called after the Preferences dialog is closed and the program settings are saved.
  199. # Update the UI based on the current preferences.
  200. sub update_ui_from_settings {
  201. my ($self) = @_;
  202. $self->{mainframe}->update_ui_from_settings;
  203. }
  204. sub open_model {
  205. my ($self, $window) = @_;
  206. my $dialog = Wx::FileDialog->new($window // $self->GetTopWindow, 'Choose one or more files (STL/OBJ/AMF/PRUSA):',
  207. $self->{app_config}->get_last_dir, "",
  208. MODEL_WILDCARD, wxFD_OPEN | wxFD_MULTIPLE | wxFD_FILE_MUST_EXIST);
  209. if ($dialog->ShowModal != wxID_OK) {
  210. $dialog->Destroy;
  211. return;
  212. }
  213. my @input_files = $dialog->GetPaths;
  214. $dialog->Destroy;
  215. return @input_files;
  216. }
  217. sub CallAfter {
  218. my ($self, $cb) = @_;
  219. push @cb, $cb;
  220. }
  221. sub append_menu_item {
  222. my ($self, $menu, $string, $description, $cb, $id, $icon, $kind) = @_;
  223. $id //= &Wx::NewId();
  224. my $item = Wx::MenuItem->new($menu, $id, $string, $description // '', $kind // 0);
  225. $self->set_menu_item_icon($item, $icon);
  226. $menu->Append($item);
  227. EVT_MENU($self, $id, $cb);
  228. return $item;
  229. }
  230. sub append_submenu {
  231. my ($self, $menu, $string, $description, $submenu, $id, $icon) = @_;
  232. $id //= &Wx::NewId();
  233. my $item = Wx::MenuItem->new($menu, $id, $string, $description // '');
  234. $self->set_menu_item_icon($item, $icon);
  235. $item->SetSubMenu($submenu);
  236. $menu->Append($item);
  237. return $item;
  238. }
  239. sub set_menu_item_icon {
  240. my ($self, $menuItem, $icon) = @_;
  241. # SetBitmap was not available on OS X before Wx 0.9927
  242. if ($icon && $menuItem->can('SetBitmap')) {
  243. $menuItem->SetBitmap(Wx::Bitmap->new(Slic3r::var($icon), wxBITMAP_TYPE_PNG));
  244. }
  245. }
  246. sub save_window_pos {
  247. my ($self, $window, $name) = @_;
  248. $self->{app_config}->set("${name}_pos", join ',', $window->GetScreenPositionXY);
  249. $self->{app_config}->set("${name}_size", join ',', $window->GetSizeWH);
  250. $self->{app_config}->set("${name}_maximized", $window->IsMaximized);
  251. $self->{app_config}->save;
  252. }
  253. sub restore_window_pos {
  254. my ($self, $window, $name) = @_;
  255. if ($self->{app_config}->has("${name}_pos")) {
  256. my $size = [ split ',', $self->{app_config}->get("${name}_size"), 2 ];
  257. $window->SetSize($size);
  258. my $display = Wx::Display->new->GetClientArea();
  259. my $pos = [ split ',', $self->{app_config}->get("${name}_pos"), 2 ];
  260. if (($pos->[0] + $size->[0]/2) < $display->GetRight && ($pos->[1] + $size->[1]/2) < $display->GetBottom) {
  261. $window->Move($pos);
  262. }
  263. $window->Maximize(1) if $self->{app_config}->get("${name}_maximized");
  264. }
  265. }
  266. 1;