PrusaSlicer-NonPlainar/lib/Slic3r/GUI.pm

415 lines
15 KiB
Perl
Raw Normal View History

2011-10-03 09:55:32 +00:00
package Slic3r::GUI;
use strict;
use warnings;
use utf8;
2011-10-03 09:55:32 +00:00
use File::Basename qw(basename);
2012-04-07 18:45:02 +00:00
use FindBin;
use List::Util qw(first);
2015-11-02 19:16:37 +00:00
use Slic3r::GUI::2DBed;
2012-07-13 21:46:39 +00:00
use Slic3r::GUI::AboutDialog;
2014-06-15 23:49:49 +00:00
use Slic3r::GUI::BedShapeDialog;
use Slic3r::GUI::BonjourBrowser;
2012-06-26 15:42:29 +00:00
use Slic3r::GUI::ConfigWizard;
use Slic3r::GUI::Controller;
2015-11-02 19:16:37 +00:00
use Slic3r::GUI::Controller::ManualControlDialog;
2014-12-31 18:10:46 +00:00
use Slic3r::GUI::Controller::PrinterPanel;
use Slic3r::GUI::MainFrame;
2014-06-14 17:54:18 +00:00
use Slic3r::GUI::Notifier;
use Slic3r::GUI::Plater;
use Slic3r::GUI::Plater::2D;
2014-07-03 07:24:19 +00:00
use Slic3r::GUI::Plater::2DToolpaths;
2014-07-13 10:10:34 +00:00
use Slic3r::GUI::Plater::3D;
2015-01-18 18:36:47 +00:00
use Slic3r::GUI::Plater::3DPreview;
use Slic3r::GUI::Plater::ObjectPartsPanel;
use Slic3r::GUI::Plater::ObjectCutDialog;
use Slic3r::GUI::Plater::ObjectSettingsDialog;
use Slic3r::GUI::Plater::LambdaObjectDialog;
use Slic3r::GUI::Plater::OverrideSettingsPanel;
2013-01-02 19:30:48 +00:00
use Slic3r::GUI::Preferences;
2014-06-14 17:54:18 +00:00
use Slic3r::GUI::ProgressStatusBar;
2015-11-15 20:08:14 +00:00
use Slic3r::GUI::Projector;
2011-10-03 09:55:32 +00:00
use Slic3r::GUI::OptionsGroup;
use Slic3r::GUI::OptionsGroup::Field;
use Slic3r::GUI::SimpleTab;
use Slic3r::GUI::SystemInfo;
2012-06-17 20:27:05 +00:00
use Slic3r::GUI::Tab;
2011-10-03 09:55:32 +00:00
2015-01-09 00:30:04 +00:00
our $have_OpenGL = eval "use Slic3r::GUI::3DScene; 1";
2014-12-28 00:30:05 +00:00
our $have_LWP = eval "use LWP::UserAgent; 1";
use Wx 0.9901 qw(:bitmap :dialog :icon :id :misc :systemsettings :toplevelwindow
:filedialog :font);
use Wx::Event qw(EVT_IDLE EVT_COMMAND);
2011-10-03 09:55:32 +00:00
use base 'Wx::App';
2014-06-14 17:54:18 +00:00
use constant FILE_WILDCARDS => {
known => 'Known files (*.stl, *.obj, *.amf, *.xml, *.prusa)|*.stl;*.STL;*.obj;*.OBJ;*.amf;*.AMF;*.xml;*.XML;*.prusa;*.PRUSA',
2014-06-14 17:54:18 +00:00
stl => 'STL files (*.stl)|*.stl;*.STL',
obj => 'OBJ files (*.obj)|*.obj;*.OBJ',
amf => 'AMF files (*.amf)|*.amf;*.AMF;*.xml;*.XML',
prusa => 'Prusa Control files (*.prusa)|*.prusa;*.PRUSA',
2014-06-14 17:54:18 +00:00
ini => 'INI files *.ini|*.ini;*.INI',
gcode => 'G-code files (*.gcode, *.gco, *.g, *.ngc)|*.gcode;*.GCODE;*.gco;*.GCO;*.g;*.G;*.ngc;*.NGC',
svg => 'SVG files *.svg|*.svg;*.SVG',
};
use constant MODEL_WILDCARD => join '|', @{&FILE_WILDCARDS}{qw(known stl obj amf prusa)};
2014-06-14 17:54:18 +00:00
2012-06-18 20:27:57 +00:00
our $datadir;
# If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
our $no_controller;
our $no_plater;
our $mode;
our $autosave;
our @cb;
2013-01-03 14:49:20 +00:00
our $Settings = {
_ => {
# Simple mode is very limited, rather start with the expert mode.
mode => 'expert',
2013-04-27 18:55:43 +00:00
version_check => 1,
autocenter => 1,
# Disable background processing by default as it is not stable.
background_processing => 0,
# If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
# By default, Prusa has the controller hidden.
no_controller => 1,
# If set, the "- default -" selections of print/filament/printer are suppressed, if there is a valid preset available.
no_defaults => 1,
2013-01-03 14:49:20 +00:00
},
};
our $have_button_icons = &Wx::wxVERSION_STRING =~ m" (?:2\.9\.[1-9]|3\.)";
2012-07-01 17:24:06 +00:00
our $small_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
$small_font->SetPointSize(11) if &Wx::wxMAC;
our $small_bold_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
$small_bold_font->SetPointSize(11) if &Wx::wxMAC;
$small_bold_font->SetWeight(wxFONTWEIGHT_BOLD);
2012-07-01 17:24:06 +00:00
our $medium_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
2012-06-19 15:23:10 +00:00
$medium_font->SetPointSize(12);
2015-01-04 17:17:15 +00:00
our $grey = Wx::Colour->new(200,200,200);
2012-06-19 15:23:10 +00:00
#our $VERSION_CHECK_EVENT : shared = Wx::NewEventType;
2015-11-15 20:08:14 +00:00
our $DLP_projection_screen;
2011-10-03 09:55:32 +00:00
sub OnInit {
2014-06-14 17:54:18 +00:00
my ($self) = @_;
2011-10-03 09:55:32 +00:00
2012-02-23 20:51:45 +00:00
$self->SetAppName('Slic3r');
$self->SetAppDisplayName('Slic3r Prusa Edition') if (Wx::wxVERSION >= 3.000000);
Slic3r::debugf "wxWidgets version %s, Wx version %s\n", &Wx::wxVERSION_STRING, $Wx::VERSION;
2011-10-03 09:55:32 +00:00
$self->{notifier} = Slic3r::GUI::Notifier->new;
2012-06-18 20:27:57 +00:00
# locate or create data directory
2016-09-13 09:24:55 +00:00
# Unix: ~/.Slic3r
# Windows: "C:\Users\username\AppData\Roaming\Slic3r" or "C:\Documents and Settings\username\Application Data\Slic3r"
# Mac: "~/Library/Application Support/Slic3r"
$datadir ||= Slic3r::decode_path(Wx::StandardPaths::Get->GetUserDataDir);
2014-11-22 20:55:45 +00:00
my $enc_datadir = Slic3r::encode_path($datadir);
Slic3r::debugf "Data directory: %s\n", $datadir;
# just checking for existence of $datadir is not enough: it may be an empty directory
# supplied as argument to --datadir; in that case we should still run the wizard
2014-11-22 20:55:45 +00:00
my $run_wizard = (-d $enc_datadir && -e "$enc_datadir/slic3r.ini") ? 0 : 1;
2015-01-09 13:50:42 +00:00
foreach my $dir ($enc_datadir, "$enc_datadir/print", "$enc_datadir/filament", "$enc_datadir/printer") {
next if -d $dir;
if (!mkdir $dir) {
my $error = "Slic3r was unable to create its data directory at $dir ($!).";
warn "$error\n";
fatal_error(undef, $error);
}
2012-06-18 20:27:57 +00:00
}
2012-06-19 16:11:51 +00:00
# load settings
my $last_version;
2014-11-22 20:55:45 +00:00
if (-f "$enc_datadir/slic3r.ini") {
2012-06-19 16:11:51 +00:00
my $ini = eval { Slic3r::Config->read_ini("$datadir/slic3r.ini") };
$Settings = $ini if $ini;
$last_version = $Settings->{_}{version};
2013-01-03 14:49:20 +00:00
$Settings->{_}{mode} ||= 'expert';
$Settings->{_}{autocenter} //= 1;
$Settings->{_}{background_processing} //= 1;
# If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
$Settings->{_}{no_controller} //= 1;
# If set, the "- default -" selections of print/filament/printer are suppressed, if there is a valid preset available.
$Settings->{_}{no_defaults} //= 1;
2012-06-19 16:11:51 +00:00
}
2013-11-18 17:48:06 +00:00
$Settings->{_}{version} = $Slic3r::VERSION;
2014-06-14 17:59:59 +00:00
$self->save_settings;
2012-06-19 16:11:51 +00:00
2012-06-18 20:27:57 +00:00
# application frame
Wx::Image::AddHandler(Wx::PNGHandler->new);
2014-06-14 17:54:18 +00:00
$self->{mainframe} = my $frame = Slic3r::GUI::MainFrame->new(
mode => $mode // $Settings->{_}{mode},
# If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
no_controller => $no_controller // $Settings->{_}{no_controller},
no_plater => $no_plater,
);
$self->SetTopWindow($frame);
2011-10-03 09:55:32 +00:00
# load init bundle
{
my @dirs = ($FindBin::Bin);
if (&Wx::wxMAC) {
push @dirs, qw();
} elsif (&Wx::wxMSW) {
push @dirs, qw();
}
my $init_bundle = first { -e $_ } map "$_/.init_bundle.ini", @dirs;
if ($init_bundle) {
Slic3r::debugf "Loading config bundle from %s\n", $init_bundle;
$self->{mainframe}->load_configbundle($init_bundle, 1);
$run_wizard = 0;
}
}
if (!$run_wizard && (!defined $last_version || $last_version ne $Slic3r::VERSION)) {
# user was running another Slic3r version on this computer
if (!defined $last_version || $last_version =~ /^0\./) {
2014-06-14 17:54:18 +00:00
show_info($self->{mainframe}, "Hello! Support material was improved since the "
. "last version of Slic3r you used. It is strongly recommended to revert "
. "your support material settings to the factory defaults and start from "
. "those. Enjoy and provide feedback!", "Support Material");
}
2014-06-16 20:56:28 +00:00
if (!defined $last_version || $last_version =~ /^(?:0|1\.[01])\./) {
show_info($self->{mainframe}, "Hello! In this version a new Bed Shape option was "
2014-06-16 21:36:31 +00:00
. "added. If the bed coordinates in the plater preview screen look wrong, go "
2014-06-16 20:56:28 +00:00
. "to Print Settings and click the \"Set\" button next to \"Bed Shape\".", "Bed Shape");
}
}
2014-06-14 17:54:18 +00:00
$self->{mainframe}->config_wizard if $run_wizard;
# $self->check_version
# if $self->have_version_check
# && ($Settings->{_}{version_check} // 1)
# && (!$Settings->{_}{last_version_check} || (time - $Settings->{_}{last_version_check}) >= 86400);
2013-04-27 18:55:43 +00:00
EVT_IDLE($frame, sub {
while (my $cb = shift @cb) {
$cb->();
}
});
# EVT_COMMAND($self, -1, $VERSION_CHECK_EVENT, sub {
# my ($self, $event) = @_;
# my ($success, $response, $manual_check) = @{$event->GetData};
#
# if ($success) {
# if ($response =~ /^obsolete ?= ?([a-z0-9.-]+,)*\Q$Slic3r::VERSION\E(?:,|$)/) {
# my $res = Wx::MessageDialog->new(undef, "A new version is available. Do you want to open the Slic3r website now?",
# 'Update', wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_INFORMATION | wxICON_ERROR)->ShowModal;
# Wx::LaunchDefaultBrowser('http://slic3r.org/') if $res == wxID_YES;
# } else {
# Slic3r::GUI::show_info(undef, "You're using the latest version. No updates are available.") if $manual_check;
# }
# $Settings->{_}{last_version_check} = time();
# $self->save_settings;
# } else {
# Slic3r::GUI::show_error(undef, "Failed to check for updates. Try later.") if $manual_check;
# }
# });
2011-10-03 11:08:43 +00:00
return 1;
2011-10-03 09:55:32 +00:00
}
sub about {
2014-06-14 17:54:18 +00:00
my ($self) = @_;
2011-10-03 09:55:32 +00:00
2014-06-14 17:54:18 +00:00
my $about = Slic3r::GUI::AboutDialog->new(undef);
2012-07-13 21:46:39 +00:00
$about->ShowModal;
$about->Destroy;
2011-10-03 09:55:32 +00:00
}
sub system_info {
my ($self) = @_;
my $slic3r_info = Slic3r::slic3r_info(format => 'html');
my $copyright_info = Slic3r::copyright_info(format => 'html');
my $system_info = Slic3r::system_info(format => 'html');
my $opengl_info;
my $opengl_info_txt = '';
if (defined($self->{mainframe}) && defined($self->{mainframe}->{plater}) &&
defined($self->{mainframe}->{plater}->{canvas3D})) {
$opengl_info = $self->{mainframe}->{plater}->{canvas3D}->opengl_info(format => 'html');
$opengl_info_txt = $self->{mainframe}->{plater}->{canvas3D}->opengl_info;
}
my $about = Slic3r::GUI::SystemInfo->new(
parent => undef,
slic3r_info => $slic3r_info,
# copyright_info => $copyright_info,
system_info => $system_info,
opengl_info => $opengl_info,
text_info => Slic3r::slic3r_info . Slic3r::system_info . $opengl_info_txt,
);
$about->ShowModal;
$about->Destroy;
}
2014-06-14 17:54:18 +00:00
# static method accepting a wxWindow object as first parameter
2012-04-30 12:56:01 +00:00
sub catch_error {
my ($self, $cb, $message_dialog) = @_;
2012-04-30 12:56:01 +00:00
if (my $err = $@) {
$cb->() if $cb;
$message_dialog
2014-06-13 12:27:55 +00:00
? $message_dialog->($err, 'Error', wxOK | wxICON_ERROR)
: Slic3r::GUI::show_error($self, $err);
2012-04-30 12:56:01 +00:00
return 1;
}
return 0;
}
2014-06-14 17:54:18 +00:00
# static method accepting a wxWindow object as first parameter
2012-06-18 20:27:57 +00:00
sub show_error {
2015-01-09 13:50:42 +00:00
my ($parent, $message) = @_;
Wx::MessageDialog->new($parent, $message, 'Error', wxOK | wxICON_ERROR)->ShowModal;
2012-06-18 20:27:57 +00:00
}
2014-06-14 17:54:18 +00:00
# static method accepting a wxWindow object as first parameter
2013-04-27 18:55:43 +00:00
sub show_info {
2015-01-09 13:50:42 +00:00
my ($parent, $message, $title) = @_;
Wx::MessageDialog->new($parent, $message, $title || 'Notice', wxOK | wxICON_INFORMATION)->ShowModal;
2013-04-27 18:55:43 +00:00
}
2014-06-14 17:54:18 +00:00
# static method accepting a wxWindow object as first parameter
2012-06-18 20:27:57 +00:00
sub fatal_error {
2015-01-09 13:50:42 +00:00
show_error(@_);
2012-06-18 20:27:57 +00:00
exit 1;
}
2014-06-14 17:54:18 +00:00
# static method accepting a wxWindow object as first parameter
2012-04-30 12:56:01 +00:00
sub warning_catcher {
my ($self, $message_dialog) = @_;
2012-04-30 12:56:01 +00:00
return sub {
my $message = shift;
return if $message =~ /GLUquadricObjPtr|Attempt to free unreferenced scalar/;
2012-07-01 17:24:06 +00:00
my @params = ($message, 'Warning', wxOK | wxICON_WARNING);
$message_dialog
? $message_dialog->(@params)
: Wx::MessageDialog->new($self, @params)->ShowModal;
2012-04-30 12:56:01 +00:00
};
}
sub notify {
2014-06-14 17:54:18 +00:00
my ($self, $message) = @_;
my $frame = $self->GetTopWindow;
# try harder to attract user attention on OS X
$frame->RequestUserAttention(&Wx::wxMAC ? wxUSER_ATTENTION_ERROR : wxUSER_ATTENTION_INFO)
unless ($frame->IsActive);
$self->{notifier}->notify($message);
}
sub save_settings {
2014-06-14 17:54:18 +00:00
my ($self) = @_;
Slic3r::Config->write_ini("$datadir/slic3r.ini", $Settings);
}
# Called after the Preferences dialog is closed and the program settings are saved.
# Update the UI based on the current preferences.
sub update_ui_from_settings {
my ($self) = @_;
$self->{mainframe}->update_ui_from_settings;
}
sub presets {
2014-06-14 17:54:18 +00:00
my ($self, $section) = @_;
my %presets = ();
2014-11-22 20:55:45 +00:00
opendir my $dh, Slic3r::encode_path("$Slic3r::GUI::datadir/$section")
or die "Failed to read directory $Slic3r::GUI::datadir/$section (errno: $!)\n";
foreach my $file (grep /\.ini$/i, readdir $dh) {
$file = Slic3r::decode_path($file);
my $name = basename($file);
$name =~ s/\.ini$//;
$presets{$name} = "$Slic3r::GUI::datadir/$section/$file";
}
closedir $dh;
return %presets;
}
#sub have_version_check {
# my ($self) = @_;
#
# # return an explicit 0
# return ($Slic3r::have_threads && $Slic3r::build && $have_LWP) || 0;
#}
2013-04-27 18:55:43 +00:00
#sub check_version {
# my ($self, $manual_check) = @_;
#
# Slic3r::debugf "Checking for updates...\n";
#
# @_ = ();
# threads->create(sub {
# my $ua = LWP::UserAgent->new;
# $ua->timeout(10);
# my $response = $ua->get('http://slic3r.org/updatecheck');
# Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $VERSION_CHECK_EVENT,
# threads::shared::shared_clone([ $response->is_success, $response->decoded_content, $manual_check ])));
# Slic3r::thread_cleanup();
# })->detach;
#}
2013-04-27 18:55:43 +00:00
sub output_path {
2014-06-14 17:54:18 +00:00
my ($self, $dir) = @_;
return ($Settings->{_}{last_output_path} && $Settings->{_}{remember_output_path})
? $Settings->{_}{last_output_path}
: $dir;
}
2014-01-18 17:43:55 +00:00
sub open_model {
2014-06-14 17:54:18 +00:00
my ($self, $window) = @_;
2014-01-18 17:43:55 +00:00
my $dir = $Slic3r::GUI::Settings->{recent}{skein_directory}
|| $Slic3r::GUI::Settings->{recent}{config_directory}
|| '';
my $dialog = Wx::FileDialog->new($window // $self->GetTopWindow, 'Choose one or more files (STL/OBJ/AMF/PRUSA):', $dir, "",
2014-06-14 17:54:18 +00:00
MODEL_WILDCARD, wxFD_OPEN | wxFD_MULTIPLE | wxFD_FILE_MUST_EXIST);
2014-01-18 17:43:55 +00:00
if ($dialog->ShowModal != wxID_OK) {
$dialog->Destroy;
return;
}
my @input_files = map Slic3r::decode_path($_), $dialog->GetPaths;
2014-01-18 17:43:55 +00:00
$dialog->Destroy;
return @input_files;
}
sub CallAfter {
2014-06-14 17:54:18 +00:00
my ($self, $cb) = @_;
push @cb, $cb;
}
2015-01-03 22:25:55 +00:00
sub scan_serial_ports {
my ($self) = @_;
my @ports = ();
2015-01-04 22:53:59 +00:00
if ($^O eq 'MSWin32') {
# Windows
if (eval "use Win32::TieRegistry; 1") {
my $ts = Win32::TieRegistry->new("HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM",
{ Access => 'KEY_READ' });
if ($ts) {
# when no serial ports are available, the registry key doesn't exist and
# TieRegistry->new returns undef
$ts->Tie(\my %reg);
push @ports, sort values %reg;
}
2015-01-04 22:53:59 +00:00
}
} else {
# UNIX and OS X
push @ports, glob '/dev/{ttyUSB,ttyACM,tty.,cu.,rfcomm}*';
}
2015-01-03 22:25:55 +00:00
2015-11-06 15:25:51 +00:00
return grep !/Bluetooth|FireFly/, @ports;
2015-01-03 22:25:55 +00:00
}
2011-10-03 09:55:32 +00:00
1;