GUI.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. $self->{mainframe}->Close;
  126. }
  127. };
  128. if ($@) {
  129. show_error(undef, $@);
  130. $self->{mainframe}->Close;
  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++) to inform about
  150. # the newer Slic3r application version avaiable online.
  151. EVT_COMMAND($self, -1, $VERSION_ONLINE_EVENT, sub {
  152. my ($self, $event) = @_;
  153. my $version = $event->GetString;
  154. $self->{app_config}->set('version_online', $version);
  155. $self->{app_config}->save;
  156. });
  157. return 1;
  158. }
  159. sub recreate_GUI{
  160. print STDERR "recreate_GUI\n";
  161. my ($self) = @_;
  162. my $topwindow = $self->GetTopWindow();
  163. $self->{mainframe} = my $frame = Slic3r::GUI::MainFrame->new(
  164. # If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
  165. no_controller => $self->{app_config}->get('no_controller'),
  166. no_plater => $no_plater,
  167. lang_ch_event => $LANGUAGE_CHANGE_EVENT,
  168. preferences_event => $PREFERENCES_EVENT,
  169. );
  170. if($topwindow)
  171. {
  172. $self->SetTopWindow($frame);
  173. $topwindow->Destroy;
  174. }
  175. EVT_IDLE($self->{mainframe}, sub {
  176. while (my $cb = shift @cb) {
  177. $cb->();
  178. }
  179. $self->{app_config}->save if $self->{app_config}->dirty;
  180. });
  181. # On OSX the UI was not initialized correctly if the wizard was called
  182. # before the UI was up and running.
  183. $self->CallAfter(sub {
  184. # Run the config wizard, don't offer the "reset user profile" checkbox.
  185. Slic3r::GUI::config_wizard_startup(1);
  186. });
  187. }
  188. sub system_info {
  189. my ($self) = @_;
  190. my $slic3r_info = Slic3r::slic3r_info(format => 'html');
  191. my $copyright_info = Slic3r::copyright_info(format => 'html');
  192. my $system_info = Slic3r::system_info(format => 'html');
  193. my $opengl_info;
  194. my $opengl_info_txt = '';
  195. if (defined($self->{mainframe}) && defined($self->{mainframe}->{plater}) &&
  196. defined($self->{mainframe}->{plater}->{canvas3D})) {
  197. $opengl_info = Slic3r::GUI::_3DScene::get_gl_info(1, 1);
  198. $opengl_info_txt = Slic3r::GUI::_3DScene::get_gl_info(0, 1);
  199. }
  200. my $about = Slic3r::GUI::SystemInfo->new(
  201. parent => undef,
  202. slic3r_info => $slic3r_info,
  203. # copyright_info => $copyright_info,
  204. system_info => $system_info,
  205. opengl_info => $opengl_info,
  206. text_info => Slic3r::slic3r_info . Slic3r::system_info . $opengl_info_txt,
  207. );
  208. $about->ShowModal;
  209. $about->Destroy;
  210. }
  211. # static method accepting a wxWindow object as first parameter
  212. sub catch_error {
  213. my ($self, $cb, $message_dialog) = @_;
  214. if (my $err = $@) {
  215. $cb->() if $cb;
  216. $message_dialog
  217. ? $message_dialog->($err, 'Error', wxOK | wxICON_ERROR)
  218. : Slic3r::GUI::show_error($self, $err);
  219. return 1;
  220. }
  221. return 0;
  222. }
  223. # static method accepting a wxWindow object as first parameter
  224. sub show_error {
  225. my ($parent, $message) = @_;
  226. Slic3r::GUI::show_error_id($parent ? $parent->GetId() : 0, $message);
  227. }
  228. # static method accepting a wxWindow object as first parameter
  229. sub show_info {
  230. my ($parent, $message, $title) = @_;
  231. Wx::MessageDialog->new($parent, $message, $title || 'Notice', wxOK | wxICON_INFORMATION)->ShowModal;
  232. }
  233. # static method accepting a wxWindow object as first parameter
  234. sub fatal_error {
  235. show_error(@_);
  236. exit 1;
  237. }
  238. # static method accepting a wxWindow object as first parameter
  239. sub warning_catcher {
  240. my ($self, $message_dialog) = @_;
  241. return sub {
  242. my $message = shift;
  243. return if $message =~ /GLUquadricObjPtr|Attempt to free unreferenced scalar/;
  244. my @params = ($message, 'Warning', wxOK | wxICON_WARNING);
  245. $message_dialog
  246. ? $message_dialog->(@params)
  247. : Wx::MessageDialog->new($self, @params)->ShowModal;
  248. };
  249. }
  250. sub notify {
  251. my ($self, $message) = @_;
  252. my $frame = $self->GetTopWindow;
  253. # try harder to attract user attention on OS X
  254. $frame->RequestUserAttention(&Wx::wxMAC ? wxUSER_ATTENTION_ERROR : wxUSER_ATTENTION_INFO)
  255. unless ($frame->IsActive);
  256. # There used to be notifier using a Growl application for OSX, but Growl is dead.
  257. # The notifier also supported the Linux X D-bus notifications, but that support was broken.
  258. #TODO use wxNotificationMessage?
  259. }
  260. # Called after the Preferences dialog is closed and the program settings are saved.
  261. # Update the UI based on the current preferences.
  262. sub update_ui_from_settings {
  263. my ($self) = @_;
  264. $self->{mainframe}->update_ui_from_settings;
  265. }
  266. sub open_model {
  267. my ($self, $window) = @_;
  268. my $dlg_title = L('Choose one or more files (STL/OBJ/AMF/3MF/PRUSA):');
  269. my $dialog = Wx::FileDialog->new($window // $self->GetTopWindow, $dlg_title,
  270. $self->{app_config}->get_last_dir, "",
  271. MODEL_WILDCARD, wxFD_OPEN | wxFD_MULTIPLE | wxFD_FILE_MUST_EXIST);
  272. if ($dialog->ShowModal != wxID_OK) {
  273. $dialog->Destroy;
  274. return;
  275. }
  276. my @input_files = $dialog->GetPaths;
  277. $dialog->Destroy;
  278. return @input_files;
  279. }
  280. sub CallAfter {
  281. my ($self, $cb) = @_;
  282. push @cb, $cb;
  283. }
  284. sub append_menu_item {
  285. my ($self, $menu, $string, $description, $cb, $id, $icon, $kind) = @_;
  286. $id //= &Wx::NewId();
  287. my $item = Wx::MenuItem->new($menu, $id, $string, $description // '', $kind // 0);
  288. $self->set_menu_item_icon($item, $icon);
  289. $menu->Append($item);
  290. EVT_MENU($self, $id, $cb);
  291. return $item;
  292. }
  293. sub append_submenu {
  294. my ($self, $menu, $string, $description, $submenu, $id, $icon) = @_;
  295. $id //= &Wx::NewId();
  296. my $item = Wx::MenuItem->new($menu, $id, $string, $description // '');
  297. $self->set_menu_item_icon($item, $icon);
  298. $item->SetSubMenu($submenu);
  299. $menu->Append($item);
  300. return $item;
  301. }
  302. sub set_menu_item_icon {
  303. my ($self, $menuItem, $icon) = @_;
  304. # SetBitmap was not available on OS X before Wx 0.9927
  305. if ($icon && $menuItem->can('SetBitmap')) {
  306. $menuItem->SetBitmap(Wx::Bitmap->new(Slic3r::var($icon), wxBITMAP_TYPE_PNG));
  307. }
  308. }
  309. sub save_window_pos {
  310. my ($self, $window, $name) = @_;
  311. $self->{app_config}->set("${name}_pos", join ',', $window->GetScreenPositionXY);
  312. $self->{app_config}->set("${name}_size", join ',', $window->GetSizeWH);
  313. $self->{app_config}->set("${name}_maximized", $window->IsMaximized);
  314. $self->{app_config}->save;
  315. }
  316. sub restore_window_pos {
  317. my ($self, $window, $name) = @_;
  318. if ($self->{app_config}->has("${name}_pos")) {
  319. my $size = [ split ',', $self->{app_config}->get("${name}_size"), 2 ];
  320. $window->SetSize($size);
  321. my $display = Wx::Display->new->GetClientArea();
  322. my $pos = [ split ',', $self->{app_config}->get("${name}_pos"), 2 ];
  323. if (($pos->[0] + $size->[0]/2) < $display->GetRight && ($pos->[1] + $size->[1]/2) < $display->GetBottom) {
  324. $window->Move($pos);
  325. }
  326. $window->Maximize(1) if $self->{app_config}->get("${name}_maximized");
  327. }
  328. }
  329. 1;