GUI.pm 12 KB

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