GUI.pm 12 KB

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