GUI.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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::Controller;
  10. use Slic3r::GUI::Controller::ManualControlDialog;
  11. use Slic3r::GUI::Controller::PrinterPanel;
  12. use Slic3r::GUI::MainFrame;
  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::LambdaObjectDialog;
  22. use Slic3r::GUI::Plater::OverrideSettingsPanel;
  23. use Slic3r::GUI::ProgressStatusBar;
  24. use Slic3r::GUI::OptionsGroup;
  25. use Slic3r::GUI::OptionsGroup::Field;
  26. use Slic3r::GUI::SystemInfo;
  27. use Wx::Locale gettext => 'L';
  28. our $have_OpenGL = eval "use Slic3r::GUI::3DScene; 1";
  29. use Wx 0.9901 qw(:bitmap :dialog :icon :id :misc :systemsettings :toplevelwindow :filedialog :font);
  30. use Wx::Event qw(EVT_IDLE EVT_COMMAND EVT_MENU);
  31. use base 'Wx::App';
  32. use constant FILE_WILDCARDS => {
  33. known => 'Known files (*.stl, *.obj, *.amf, *.xml, *.3mf, *.prusa)|*.stl;*.STL;*.obj;*.OBJ;*.zip.amf;*.amf;*.AMF;*.xml;*.XML;*.3mf;*.3MF;*.prusa;*.PRUSA',
  34. stl => 'STL files (*.stl)|*.stl;*.STL',
  35. obj => 'OBJ files (*.obj)|*.obj;*.OBJ',
  36. amf => 'AMF files (*.amf)|*.zip.amf;*.amf;*.AMF;*.xml;*.XML',
  37. threemf => '3MF files (*.3mf)|*.3mf;*.3MF',
  38. prusa => 'Prusa Control files (*.prusa)|*.prusa;*.PRUSA',
  39. ini => 'INI files *.ini|*.ini;*.INI',
  40. gcode => 'G-code files (*.gcode, *.gco, *.g, *.ngc)|*.gcode;*.GCODE;*.gco;*.GCO;*.g;*.G;*.ngc;*.NGC',
  41. svg => 'SVG files *.svg|*.svg;*.SVG',
  42. };
  43. use constant MODEL_WILDCARD => join '|', @{&FILE_WILDCARDS}{qw(known stl obj amf threemf prusa)};
  44. # Datadir provided on the command line.
  45. our $datadir;
  46. # If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
  47. our $no_plater;
  48. our @cb;
  49. our $small_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  50. $small_font->SetPointSize(11) if &Wx::wxMAC;
  51. our $small_bold_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  52. $small_bold_font->SetPointSize(11) if &Wx::wxMAC;
  53. $small_bold_font->SetWeight(wxFONTWEIGHT_BOLD);
  54. our $medium_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  55. $medium_font->SetPointSize(12);
  56. our $grey = Wx::Colour->new(200,200,200);
  57. # Events to be sent from a C++ menu implementation:
  58. # 1) To inform about a change of the application language.
  59. our $LANGUAGE_CHANGE_EVENT = Wx::NewEventType;
  60. # 2) To inform about a change of Preferences.
  61. our $PREFERENCES_EVENT = Wx::NewEventType;
  62. # To inform AppConfig about Slic3r version available online
  63. our $VERSION_ONLINE_EVENT = Wx::NewEventType;
  64. sub OnInit {
  65. my ($self) = @_;
  66. $self->SetAppName('Slic3rPE');
  67. $self->SetAppDisplayName('Slic3r Prusa Edition');
  68. Slic3r::debugf "wxWidgets version %s, Wx version %s\n", &Wx::wxVERSION_STRING, $Wx::VERSION;
  69. # Set the Slic3r data directory at the Slic3r XS module.
  70. # Unix: ~/.Slic3r
  71. # Windows: "C:\Users\username\AppData\Roaming\Slic3r" or "C:\Documents and Settings\username\Application Data\Slic3r"
  72. # Mac: "~/Library/Application Support/Slic3r"
  73. Slic3r::set_data_dir($datadir || Wx::StandardPaths::Get->GetUserDataDir);
  74. Slic3r::GUI::set_wxapp($self);
  75. $self->{app_config} = Slic3r::GUI::AppConfig->new;
  76. Slic3r::GUI::set_app_config($self->{app_config});
  77. $self->{preset_bundle} = Slic3r::GUI::PresetBundle->new;
  78. Slic3r::GUI::set_preset_bundle($self->{preset_bundle});
  79. # just checking for existence of Slic3r::data_dir is not enough: it may be an empty directory
  80. # supplied as argument to --datadir; in that case we should still run the wizard
  81. eval { $self->{preset_bundle}->setup_directories() };
  82. if ($@) {
  83. warn $@ . "\n";
  84. fatal_error(undef, $@);
  85. }
  86. my $app_conf_exists = $self->{app_config}->exists;
  87. # load settings
  88. $self->{app_config}->load if $app_conf_exists;
  89. $self->{app_config}->set('version', $Slic3r::VERSION);
  90. $self->{app_config}->save;
  91. $self->{preset_updater} = Slic3r::PresetUpdater->new($VERSION_ONLINE_EVENT);
  92. Slic3r::GUI::set_preset_updater($self->{preset_updater});
  93. Slic3r::GUI::load_language();
  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($self->{app_config}); };
  97. if ($@) {
  98. warn $@ . "\n";
  99. show_error(undef, $@);
  100. }
  101. # application frame
  102. print STDERR "Creating main frame...\n";
  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. preferences_event => $PREFERENCES_EVENT,
  110. );
  111. $self->SetTopWindow($frame);
  112. # This makes CallAfter() work
  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. # On OS X the UI tends to freeze in weird ways if modal dialogs (config wizard, update notifications, ...)
  120. # are shown before or in the same event callback with the main frame creation.
  121. # Therefore we schedule them for later using CallAfter.
  122. $self->CallAfter(sub {
  123. eval {
  124. if (! $self->{preset_updater}->config_update()) {
  125. exit 0;
  126. }
  127. };
  128. if ($@) {
  129. warn $@ . "\n";
  130. fatal_error(undef, $@);
  131. }
  132. });
  133. $self->CallAfter(sub {
  134. if (! Slic3r::GUI::config_wizard_startup($app_conf_exists)) {
  135. # Only notify if there was not wizard so as not to bother too much ...
  136. $self->{preset_updater}->slic3r_update_notify();
  137. }
  138. $self->{preset_updater}->sync($self->{preset_bundle});
  139. });
  140. # The following event is emited by the C++ menu implementation of application language change.
  141. EVT_COMMAND($self, -1, $LANGUAGE_CHANGE_EVENT, sub{
  142. print STDERR "LANGUAGE_CHANGE_EVENT\n";
  143. $self->recreate_GUI;
  144. });
  145. # The following event is emited by the C++ menu implementation of preferences change.
  146. EVT_COMMAND($self, -1, $PREFERENCES_EVENT, sub{
  147. $self->update_ui_from_settings;
  148. });
  149. # The following event is emited by PresetUpdater (C++)
  150. EVT_COMMAND($self, -1, $VERSION_ONLINE_EVENT, sub {
  151. my ($self, $event) = @_;
  152. my $version = $event->GetString;
  153. $self->{app_config}->set('version_online', $version);
  154. $self->{app_config}->save;
  155. });
  156. return 1;
  157. }
  158. sub recreate_GUI{
  159. print STDERR "recreate_GUI\n";
  160. my ($self) = @_;
  161. my $topwindow = $self->GetTopWindow();
  162. $self->{mainframe} = my $frame = Slic3r::GUI::MainFrame->new(
  163. # If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
  164. no_controller => $self->{app_config}->get('no_controller'),
  165. no_plater => $no_plater,
  166. lang_ch_event => $LANGUAGE_CHANGE_EVENT,
  167. preferences_event => $PREFERENCES_EVENT,
  168. );
  169. if($topwindow)
  170. {
  171. $self->SetTopWindow($frame);
  172. $topwindow->Destroy;
  173. }
  174. EVT_IDLE($self->{mainframe}, sub {
  175. while (my $cb = shift @cb) {
  176. $cb->();
  177. }
  178. $self->{app_config}->save if $self->{app_config}->dirty;
  179. });
  180. # On OSX the UI was not initialized correctly if the wizard was called
  181. # before the UI was up and running.
  182. $self->CallAfter(sub {
  183. # Run the config wizard, don't offer the "reset user profile" checkbox.
  184. Slic3r::GUI::config_wizard_startup(1);
  185. });
  186. }
  187. sub system_info {
  188. my ($self) = @_;
  189. my $slic3r_info = Slic3r::slic3r_info(format => 'html');
  190. my $copyright_info = Slic3r::copyright_info(format => 'html');
  191. my $system_info = Slic3r::system_info(format => 'html');
  192. my $opengl_info;
  193. my $opengl_info_txt = '';
  194. if (defined($self->{mainframe}) && defined($self->{mainframe}->{plater}) &&
  195. defined($self->{mainframe}->{plater}->{canvas3D})) {
  196. $opengl_info = Slic3r::GUI::_3DScene::get_gl_info(1, 1);
  197. $opengl_info_txt = Slic3r::GUI::_3DScene::get_gl_info(0, 1);
  198. }
  199. my $about = Slic3r::GUI::SystemInfo->new(
  200. parent => undef,
  201. slic3r_info => $slic3r_info,
  202. # copyright_info => $copyright_info,
  203. system_info => $system_info,
  204. opengl_info => $opengl_info,
  205. text_info => Slic3r::slic3r_info . Slic3r::system_info . $opengl_info_txt,
  206. );
  207. $about->ShowModal;
  208. $about->Destroy;
  209. }
  210. # static method accepting a wxWindow object as first parameter
  211. sub catch_error {
  212. my ($self, $cb, $message_dialog) = @_;
  213. if (my $err = $@) {
  214. $cb->() if $cb;
  215. $message_dialog
  216. ? $message_dialog->($err, 'Error', wxOK | wxICON_ERROR)
  217. : Slic3r::GUI::show_error($self, $err);
  218. return 1;
  219. }
  220. return 0;
  221. }
  222. # static method accepting a wxWindow object as first parameter
  223. sub show_error {
  224. my ($parent, $message) = @_;
  225. Slic3r::GUI::show_error_id($parent ? $parent->GetId() : 0, $message);
  226. }
  227. # static method accepting a wxWindow object as first parameter
  228. sub show_info {
  229. my ($parent, $message, $title) = @_;
  230. Wx::MessageDialog->new($parent, $message, $title || 'Notice', wxOK | wxICON_INFORMATION)->ShowModal;
  231. }
  232. # static method accepting a wxWindow object as first parameter
  233. sub fatal_error {
  234. show_error(@_);
  235. exit 1;
  236. }
  237. # static method accepting a wxWindow object as first parameter
  238. sub warning_catcher {
  239. my ($self, $message_dialog) = @_;
  240. return sub {
  241. my $message = shift;
  242. return if $message =~ /GLUquadricObjPtr|Attempt to free unreferenced scalar/;
  243. my @params = ($message, 'Warning', wxOK | wxICON_WARNING);
  244. $message_dialog
  245. ? $message_dialog->(@params)
  246. : Wx::MessageDialog->new($self, @params)->ShowModal;
  247. };
  248. }
  249. sub notify {
  250. my ($self, $message) = @_;
  251. my $frame = $self->GetTopWindow;
  252. # try harder to attract user attention on OS X
  253. $frame->RequestUserAttention(&Wx::wxMAC ? wxUSER_ATTENTION_ERROR : wxUSER_ATTENTION_INFO)
  254. unless ($frame->IsActive);
  255. # There used to be notifier using a Growl application for OSX, but Growl is dead.
  256. # The notifier also supported the Linux X D-bus notifications, but that support was broken.
  257. #TODO use wxNotificationMessage?
  258. }
  259. # Called after the Preferences dialog is closed and the program settings are saved.
  260. # Update the UI based on the current preferences.
  261. sub update_ui_from_settings {
  262. my ($self) = @_;
  263. $self->{mainframe}->update_ui_from_settings;
  264. }
  265. sub open_model {
  266. my ($self, $window) = @_;
  267. my $dlg_title = L('Choose one or more files (STL/OBJ/AMF/3MF/PRUSA):');
  268. my $dialog = Wx::FileDialog->new($window // $self->GetTopWindow, $dlg_title,
  269. $self->{app_config}->get_last_dir, "",
  270. MODEL_WILDCARD, wxFD_OPEN | wxFD_MULTIPLE | wxFD_FILE_MUST_EXIST);
  271. if ($dialog->ShowModal != wxID_OK) {
  272. $dialog->Destroy;
  273. return;
  274. }
  275. my @input_files = $dialog->GetPaths;
  276. $dialog->Destroy;
  277. return @input_files;
  278. }
  279. sub CallAfter {
  280. my ($self, $cb) = @_;
  281. push @cb, $cb;
  282. }
  283. sub append_menu_item {
  284. my ($self, $menu, $string, $description, $cb, $id, $icon, $kind) = @_;
  285. $id //= &Wx::NewId();
  286. my $item = Wx::MenuItem->new($menu, $id, $string, $description // '', $kind // 0);
  287. $self->set_menu_item_icon($item, $icon);
  288. $menu->Append($item);
  289. EVT_MENU($self, $id, $cb);
  290. return $item;
  291. }
  292. sub append_submenu {
  293. my ($self, $menu, $string, $description, $submenu, $id, $icon) = @_;
  294. $id //= &Wx::NewId();
  295. my $item = Wx::MenuItem->new($menu, $id, $string, $description // '');
  296. $self->set_menu_item_icon($item, $icon);
  297. $item->SetSubMenu($submenu);
  298. $menu->Append($item);
  299. return $item;
  300. }
  301. sub set_menu_item_icon {
  302. my ($self, $menuItem, $icon) = @_;
  303. # SetBitmap was not available on OS X before Wx 0.9927
  304. if ($icon && $menuItem->can('SetBitmap')) {
  305. $menuItem->SetBitmap(Wx::Bitmap->new(Slic3r::var($icon), wxBITMAP_TYPE_PNG));
  306. }
  307. }
  308. sub save_window_pos {
  309. my ($self, $window, $name) = @_;
  310. $self->{app_config}->set("${name}_pos", join ',', $window->GetScreenPositionXY);
  311. $self->{app_config}->set("${name}_size", join ',', $window->GetSizeWH);
  312. $self->{app_config}->set("${name}_maximized", $window->IsMaximized);
  313. $self->{app_config}->save;
  314. }
  315. sub restore_window_pos {
  316. my ($self, $window, $name) = @_;
  317. if ($self->{app_config}->has("${name}_pos")) {
  318. my $size = [ split ',', $self->{app_config}->get("${name}_size"), 2 ];
  319. $window->SetSize($size);
  320. my $display = Wx::Display->new->GetClientArea();
  321. my $pos = [ split ',', $self->{app_config}->get("${name}_pos"), 2 ];
  322. if (($pos->[0] + $size->[0]/2) < $display->GetRight && ($pos->[1] + $size->[1]/2) < $display->GetBottom) {
  323. $window->Move($pos);
  324. }
  325. $window->Maximize(1) if $self->{app_config}->get("${name}_maximized");
  326. }
  327. }
  328. 1;