GUI.pm 9.8 KB

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