GUI.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package Slic3r::GUI;
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use FindBin;
  6. use Slic3r::GUI::AboutDialog;
  7. use Slic3r::GUI::ConfigWizard;
  8. use Slic3r::GUI::Plater;
  9. use Slic3r::GUI::Preferences;
  10. use Slic3r::GUI::OptionsGroup;
  11. use Slic3r::GUI::SkeinPanel;
  12. use Slic3r::GUI::SimpleTab;
  13. use Slic3r::GUI::Tab;
  14. use Wx 0.9901 qw(:bitmap :dialog :frame :icon :id :misc :systemsettings :toplevelwindow);
  15. use Wx::Event qw(EVT_CLOSE EVT_MENU);
  16. use base 'Wx::App';
  17. use constant MI_LOAD_CONF => &Wx::NewId;
  18. use constant MI_EXPORT_CONF => &Wx::NewId;
  19. use constant MI_QUICK_SLICE => &Wx::NewId;
  20. use constant MI_REPEAT_QUICK => &Wx::NewId;
  21. use constant MI_QUICK_SAVE_AS => &Wx::NewId;
  22. use constant MI_SLICE_SVG => &Wx::NewId;
  23. use constant MI_COMBINE_STLS => &Wx::NewId;
  24. use constant MI_PLATER_EXPORT_GCODE => &Wx::NewId;
  25. use constant MI_PLATER_EXPORT_STL => &Wx::NewId;
  26. use constant MI_PLATER_EXPORT_AMF => &Wx::NewId;
  27. use constant MI_TAB_PLATER => &Wx::NewId;
  28. use constant MI_TAB_PRINT => &Wx::NewId;
  29. use constant MI_TAB_FILAMENT => &Wx::NewId;
  30. use constant MI_TAB_PRINTER => &Wx::NewId;
  31. use constant MI_CONF_WIZARD => &Wx::NewId;
  32. use constant MI_WEBSITE => &Wx::NewId;
  33. use constant MI_DOCUMENTATION => &Wx::NewId;
  34. our $datadir;
  35. our $no_plater;
  36. our $mode;
  37. our $Settings = {
  38. _ => {
  39. mode => 'simple',
  40. },
  41. };
  42. our $small_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  43. $small_font->SetPointSize(11) if !&Wx::wxMSW;
  44. our $medium_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  45. $medium_font->SetPointSize(12);
  46. sub OnInit {
  47. my $self = shift;
  48. $self->SetAppName('Slic3r');
  49. Slic3r::debugf "wxWidgets version %s, Wx version %s\n", &Wx::wxVERSION_STRING, $Wx::VERSION;
  50. $self->{notifier} = Slic3r::GUI::Notifier->new;
  51. # locate or create data directory
  52. $datadir ||= Wx::StandardPaths::Get->GetUserDataDir;
  53. Slic3r::debugf "Data directory: %s\n", $datadir;
  54. my $encoded_datadir = Slic3r::encode_path($datadir);
  55. my $run_wizard = (-d $encoded_datadir) ? 0 : 1;
  56. for ($encoded_datadir, "$encoded_datadir/print", "$encoded_datadir/filament", "$encoded_datadir/printer") {
  57. mkdir or $self->fatal_error("Slic3r was unable to create its data directory at $_ (errno: $!).")
  58. unless -d $_;
  59. }
  60. # load settings
  61. if (-f "$encoded_datadir/slic3r.ini") {
  62. my $ini = eval { Slic3r::Config->read_ini("$datadir/slic3r.ini") };
  63. $Settings = $ini if $ini;
  64. $Settings->{_}{mode} ||= 'expert';
  65. }
  66. # application frame
  67. Wx::Image::AddHandler(Wx::PNGHandler->new);
  68. my $frame = Wx::Frame->new(undef, -1, 'Slic3r', wxDefaultPosition, [760, 470], wxDEFAULT_FRAME_STYLE);
  69. $frame->SetIcon(Wx::Icon->new("$Slic3r::var/Slic3r_128px.png", wxBITMAP_TYPE_PNG) );
  70. $self->{skeinpanel} = Slic3r::GUI::SkeinPanel->new($frame,
  71. mode => $mode // $Settings->{_}{mode},
  72. no_plater => $no_plater,
  73. );
  74. $self->SetTopWindow($frame);
  75. # status bar
  76. $frame->{statusbar} = Slic3r::GUI::ProgressStatusBar->new($frame, -1);
  77. $frame->{statusbar}->SetStatusText("Version $Slic3r::VERSION - Remember to check for updates at http://slic3r.org/");
  78. $frame->SetStatusBar($frame->{statusbar});
  79. # File menu
  80. my $fileMenu = Wx::Menu->new;
  81. {
  82. $fileMenu->Append(MI_LOAD_CONF, "&Load Config…\tCtrl+L", 'Load exported configuration file');
  83. $fileMenu->Append(MI_EXPORT_CONF, "&Export Config…\tCtrl+E", 'Export current configuration to file');
  84. $fileMenu->AppendSeparator();
  85. $fileMenu->Append(MI_QUICK_SLICE, "Q&uick Slice…\tCtrl+U", 'Slice file');
  86. $fileMenu->Append(MI_QUICK_SAVE_AS, "Quick Slice and Save &As…\tCtrl+Alt+U", 'Slice file and save as');
  87. my $repeat = $fileMenu->Append(MI_REPEAT_QUICK, "&Repeat Last Quick Slice\tCtrl+Shift+U", 'Repeat last quick slice');
  88. $repeat->Enable(0);
  89. $fileMenu->AppendSeparator();
  90. $fileMenu->Append(MI_SLICE_SVG, "Slice to SV&G…\tCtrl+G", 'Slice file to SVG');
  91. $fileMenu->AppendSeparator();
  92. $fileMenu->Append(MI_COMBINE_STLS, "Combine multi-material STL files…", 'Combine multiple STL files into a single multi-material AMF file');
  93. $fileMenu->AppendSeparator();
  94. $fileMenu->Append(wxID_PREFERENCES, "Preferences…", 'Application preferences');
  95. $fileMenu->AppendSeparator();
  96. $fileMenu->Append(wxID_EXIT, "&Quit", 'Quit Slic3r');
  97. EVT_MENU($frame, MI_LOAD_CONF, sub { $self->{skeinpanel}->load_config_file });
  98. EVT_MENU($frame, MI_EXPORT_CONF, sub { $self->{skeinpanel}->export_config });
  99. EVT_MENU($frame, MI_QUICK_SLICE, sub { $self->{skeinpanel}->do_slice;
  100. $repeat->Enable(defined $Slic3r::GUI::SkeinPanel::last_input_file) });
  101. EVT_MENU($frame, MI_REPEAT_QUICK, sub { $self->{skeinpanel}->do_slice(reslice => 1) });
  102. EVT_MENU($frame, MI_QUICK_SAVE_AS, sub { $self->{skeinpanel}->do_slice(save_as => 1);
  103. $repeat->Enable(defined $Slic3r::GUI::SkeinPanel::last_input_file) });
  104. EVT_MENU($frame, MI_SLICE_SVG, sub { $self->{skeinpanel}->do_slice(save_as => 1, export_svg => 1) });
  105. EVT_MENU($frame, MI_COMBINE_STLS, sub { $self->{skeinpanel}->combine_stls });
  106. EVT_MENU($frame, wxID_PREFERENCES, sub { Slic3r::GUI::Preferences->new($frame)->ShowModal });
  107. EVT_MENU($frame, wxID_EXIT, sub {$_[0]->Close(0)});
  108. }
  109. # Plater menu
  110. my $platerMenu = Wx::Menu->new;
  111. {
  112. $platerMenu->Append(MI_PLATER_EXPORT_GCODE, "Export G-code...", 'Export current plate as G-code');
  113. $platerMenu->Append(MI_PLATER_EXPORT_STL, "Export STL...", 'Export current plate as STL');
  114. $platerMenu->Append(MI_PLATER_EXPORT_AMF, "Export AMF...", 'Export current plate as AMF');
  115. EVT_MENU($frame, MI_PLATER_EXPORT_GCODE, sub { $self->{skeinpanel}{plater}->export_gcode });
  116. EVT_MENU($frame, MI_PLATER_EXPORT_STL, sub { $self->{skeinpanel}{plater}->export_stl });
  117. EVT_MENU($frame, MI_PLATER_EXPORT_AMF, sub { $self->{skeinpanel}{plater}->export_amf });
  118. }
  119. # Window menu
  120. my $windowMenu = Wx::Menu->new;
  121. {
  122. $windowMenu->Append(MI_TAB_PLATER, "Select &Plater Tab\tCtrl+1", 'Show the plater');
  123. $windowMenu->Append(MI_TAB_PRINT, "Select P&rint Settings Tab\tCtrl+2", 'Show the print settings');
  124. $windowMenu->Append(MI_TAB_FILAMENT, "Select &Filament Settings Tab\tCtrl+3", 'Show the filament settings');
  125. $windowMenu->Append(MI_TAB_PRINTER, "Select Print&er Settings Tab\tCtrl+4", 'Show the printer settings');
  126. EVT_MENU($frame, MI_TAB_PLATER, sub { $self->{skeinpanel}->select_tab(0) });
  127. EVT_MENU($frame, MI_TAB_PRINT, sub { $self->{skeinpanel}->select_tab(1) });
  128. EVT_MENU($frame, MI_TAB_FILAMENT, sub { $self->{skeinpanel}->select_tab(2) });
  129. EVT_MENU($frame, MI_TAB_PRINTER, sub { $self->{skeinpanel}->select_tab(3) });
  130. }
  131. # Help menu
  132. my $helpMenu = Wx::Menu->new;
  133. {
  134. $helpMenu->Append(MI_CONF_WIZARD, "&Configuration $Slic3r::GUI::ConfigWizard::wizard…", "Run Configuration $Slic3r::GUI::ConfigWizard::wizard");
  135. $helpMenu->AppendSeparator();
  136. $helpMenu->Append(MI_WEBSITE, "Slic3r &Website", 'Open the Slic3r website in your browser');
  137. $helpMenu->Append(MI_DOCUMENTATION, "&Documentation", 'Open the Slic3r documentation in your browser');
  138. $helpMenu->AppendSeparator();
  139. $helpMenu->Append(wxID_ABOUT, "&About Slic3r", 'Show about dialog');
  140. EVT_MENU($frame, MI_CONF_WIZARD, sub { $self->{skeinpanel}->config_wizard });
  141. EVT_MENU($frame, MI_WEBSITE, sub { Wx::LaunchDefaultBrowser('http://slic3r.org/') });
  142. EVT_MENU($frame, MI_DOCUMENTATION, sub { Wx::LaunchDefaultBrowser('https://github.com/alexrj/Slic3r/wiki/Documentation') });
  143. EVT_MENU($frame, wxID_ABOUT, \&about);
  144. }
  145. # menubar
  146. # assign menubar to frame after appending items, otherwise special items
  147. # will not be handled correctly
  148. {
  149. my $menubar = Wx::MenuBar->new;
  150. $menubar->Append($fileMenu, "&File");
  151. $menubar->Append($platerMenu, "&Plater");
  152. $menubar->Append($windowMenu, "&Window");
  153. $menubar->Append($helpMenu, "&Help");
  154. $frame->SetMenuBar($menubar);
  155. }
  156. EVT_CLOSE($frame, sub {
  157. my (undef, $event) = @_;
  158. if ($event->CanVeto && !$self->{skeinpanel}->check_unsaved_changes) {
  159. $event->Veto;
  160. return;
  161. }
  162. $event->Skip;
  163. });
  164. $frame->Fit;
  165. $frame->SetMinSize($frame->GetSize);
  166. $frame->Show;
  167. $frame->Layout;
  168. $self->{skeinpanel}->config_wizard if $run_wizard;
  169. return 1;
  170. }
  171. sub about {
  172. my $frame = shift;
  173. my $about = Slic3r::GUI::AboutDialog->new($frame);
  174. $about->ShowModal;
  175. $about->Destroy;
  176. }
  177. sub catch_error {
  178. my ($self, $cb, $message_dialog) = @_;
  179. if (my $err = $@) {
  180. $cb->() if $cb;
  181. my @params = ($err, 'Error', wxOK | wxICON_ERROR);
  182. $message_dialog
  183. ? $message_dialog->(@params)
  184. : Wx::MessageDialog->new($self, @params)->ShowModal;
  185. return 1;
  186. }
  187. return 0;
  188. }
  189. sub show_error {
  190. my $self = shift;
  191. my ($message) = @_;
  192. Wx::MessageDialog->new($self, $message, 'Error', wxOK | wxICON_ERROR)->ShowModal;
  193. }
  194. sub fatal_error {
  195. my $self = shift;
  196. $self->show_error(@_);
  197. exit 1;
  198. }
  199. sub warning_catcher {
  200. my ($self, $message_dialog) = @_;
  201. return sub {
  202. my $message = shift;
  203. my @params = ($message, 'Warning', wxOK | wxICON_WARNING);
  204. $message_dialog
  205. ? $message_dialog->(@params)
  206. : Wx::MessageDialog->new($self, @params)->ShowModal;
  207. };
  208. }
  209. sub notify {
  210. my $self = shift;
  211. my ($message) = @_;
  212. my $frame = $self->GetTopWindow;
  213. # try harder to attract user attention on OS X
  214. $frame->RequestUserAttention(&Wx::wxMAC ? wxUSER_ATTENTION_ERROR : wxUSER_ATTENTION_INFO)
  215. unless ($frame->IsActive);
  216. $self->{notifier}->notify($message);
  217. }
  218. sub save_settings {
  219. my $class = shift;
  220. Slic3r::Config->write_ini("$datadir/slic3r.ini", $Settings);
  221. }
  222. package Slic3r::GUI::ProgressStatusBar;
  223. use Wx qw(:gauge :misc);
  224. use base 'Wx::StatusBar';
  225. sub new {
  226. my $class = shift;
  227. my $self = $class->SUPER::new(@_);
  228. $self->{busy} = 0;
  229. $self->{timer} = Wx::Timer->new($self);
  230. $self->{prog} = Wx::Gauge->new($self, wxGA_HORIZONTAL, 100, wxDefaultPosition, wxDefaultSize);
  231. $self->{prog}->Hide;
  232. $self->{cancelbutton} = Wx::Button->new($self, -1, "Cancel", wxDefaultPosition, wxDefaultSize);
  233. $self->{cancelbutton}->Hide;
  234. $self->SetFieldsCount(3);
  235. $self->SetStatusWidths(-1, 150, 155);
  236. Wx::Event::EVT_TIMER($self, \&OnTimer, $self->{timer});
  237. Wx::Event::EVT_SIZE($self, \&OnSize);
  238. Wx::Event::EVT_BUTTON($self, $self->{cancelbutton}, sub {
  239. $self->{cancel_cb}->();
  240. $self->{cancelbutton}->Hide;
  241. });
  242. return $self;
  243. }
  244. sub DESTROY {
  245. my $self = shift;
  246. $self->{timer}->Stop if $self->{timer} && $self->{timer}->IsRunning;
  247. }
  248. sub OnSize {
  249. my ($self, $event) = @_;
  250. my %fields = (
  251. # 0 is reserved for status text
  252. 1 => $self->{cancelbutton},
  253. 2 => $self->{prog},
  254. );
  255. foreach (keys %fields) {
  256. my $rect = $self->GetFieldRect($_);
  257. my $offset = &Wx::wxGTK ? 1 : 0; # add a cosmetic 1 pixel offset on wxGTK
  258. my $pos = [$rect->GetX + $offset, $rect->GetY + $offset];
  259. $fields{$_}->Move($pos);
  260. $fields{$_}->SetSize($rect->GetWidth - $offset, $rect->GetHeight);
  261. }
  262. $event->Skip;
  263. }
  264. sub OnTimer {
  265. my ($self, $event) = @_;
  266. if ($self->{prog}->IsShown) {
  267. $self->{timer}->Stop;
  268. }
  269. $self->{prog}->Pulse if $self->{_busy};
  270. }
  271. sub SetCancelCallback {
  272. my $self = shift;
  273. my ($cb) = @_;
  274. $self->{cancel_cb} = $cb;
  275. $cb ? $self->{cancelbutton}->Show : $self->{cancelbutton}->Hide;
  276. }
  277. sub Run {
  278. my $self = shift;
  279. my $rate = shift || 100;
  280. if (!$self->{timer}->IsRunning) {
  281. $self->{timer}->Start($rate);
  282. }
  283. }
  284. sub GetProgress {
  285. my $self = shift;
  286. return $self->{prog}->GetValue;
  287. }
  288. sub SetProgress {
  289. my $self = shift;
  290. my ($val) = @_;
  291. if (!$self->{prog}->IsShown) {
  292. $self->ShowProgress(1);
  293. }
  294. if ($val == $self->{prog}->GetRange) {
  295. $self->{prog}->SetValue(0);
  296. $self->ShowProgress(0);
  297. } else {
  298. $self->{prog}->SetValue($val);
  299. }
  300. }
  301. sub SetRange {
  302. my $self = shift;
  303. my ($val) = @_;
  304. if ($val != $self->{prog}->GetRange) {
  305. $self->{prog}->SetRange($val);
  306. }
  307. }
  308. sub ShowProgress {
  309. my $self = shift;
  310. my ($show) = @_;
  311. $self->{prog}->Show($show);
  312. $self->{prog}->Pulse;
  313. }
  314. sub StartBusy {
  315. my $self = shift;
  316. my $rate = shift || 100;
  317. $self->{_busy} = 1;
  318. $self->ShowProgress(1);
  319. if (!$self->{timer}->IsRunning) {
  320. $self->{timer}->Start($rate);
  321. }
  322. }
  323. sub StopBusy {
  324. my $self = shift;
  325. $self->{timer}->Stop;
  326. $self->ShowProgress(0);
  327. $self->{prog}->SetValue(0);
  328. $self->{_busy} = 0;
  329. }
  330. sub IsBusy {
  331. my $self = shift;
  332. return $self->{_busy};
  333. }
  334. package Slic3r::GUI::Notifier;
  335. sub new {
  336. my $class = shift;
  337. my $self;
  338. $self->{icon} = "$Slic3r::var/Slic3r.png";
  339. if (eval 'use Growl::GNTP; 1') {
  340. # register with growl
  341. eval {
  342. $self->{growler} = Growl::GNTP->new(AppName => 'Slic3r', AppIcon => $self->{icon});
  343. $self->{growler}->register([{Name => 'SKEIN_DONE', DisplayName => 'Slicing Done'}]);
  344. };
  345. }
  346. bless $self, $class;
  347. return $self;
  348. }
  349. sub notify {
  350. my ($self, $message) = @_;
  351. my $title = 'Slicing Done!';
  352. eval {
  353. $self->{growler}->notify(Event => 'SKEIN_DONE', Title => $title, Message => $message)
  354. if $self->{growler};
  355. };
  356. if (eval 'use Net::DBus; 1') {
  357. eval {
  358. my $session = Net::DBus->session;
  359. my $serv = $session->get_service('org.freedesktop.Notifications');
  360. my $notifier = $serv->get_object('/org/freedesktop/Notifications',
  361. 'org.freedesktop.Notifications');
  362. $notifier->Notify('Slic3r', 0, $self->{icon}, $title, $message, [], {}, -1);
  363. undef $Net::DBus::bus_session;
  364. };
  365. }
  366. }
  367. 1;