2016-09-14 09:22:41 +00:00
|
|
|
|
# The "Controller" tab to control the printer using serial / USB.
|
|
|
|
|
# This feature is rarely used. Much more often, the firmware reads the G-codes from a SD card.
|
|
|
|
|
# May there be multiple subtabs per each printer connected?
|
|
|
|
|
|
2015-01-04 22:18:23 +00:00
|
|
|
|
package Slic3r::GUI::Controller;
|
2014-12-31 18:10:46 +00:00
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
|
|
|
|
use utf8;
|
|
|
|
|
|
2015-05-28 16:31:53 +00:00
|
|
|
|
use Wx qw(wxTheApp :frame :id :misc :sizer :bitmap :button :icon :dialog);
|
2015-01-03 22:25:55 +00:00
|
|
|
|
use Wx::Event qw(EVT_CLOSE EVT_LEFT_DOWN EVT_MENU);
|
2015-11-08 10:17:55 +00:00
|
|
|
|
use base qw(Wx::ScrolledWindow Class::Accessor);
|
2017-11-02 20:51:06 +00:00
|
|
|
|
use List::Util qw(first);
|
2015-11-08 10:17:55 +00:00
|
|
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw(_selected_printer_preset));
|
|
|
|
|
|
|
|
|
|
our @ConfigOptions = qw(bed_shape serial_port serial_speed);
|
2014-12-31 18:10:46 +00:00
|
|
|
|
|
|
|
|
|
sub new {
|
2015-01-04 22:18:23 +00:00
|
|
|
|
my ($class, $parent) = @_;
|
|
|
|
|
my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, [600,350]);
|
2014-12-31 18:10:46 +00:00
|
|
|
|
|
2015-01-04 22:18:23 +00:00
|
|
|
|
$self->SetScrollbars(0, 1, 0, 1);
|
2014-12-31 18:10:46 +00:00
|
|
|
|
$self->{sizer} = my $sizer = Wx::BoxSizer->new(wxVERTICAL);
|
2015-01-03 22:25:55 +00:00
|
|
|
|
|
2015-11-03 22:00:59 +00:00
|
|
|
|
# warning to show when there are no printers configured
|
2015-01-03 22:25:55 +00:00
|
|
|
|
{
|
2015-11-03 22:00:59 +00:00
|
|
|
|
$self->{text_no_printers} = Wx::StaticText->new($self, -1,
|
|
|
|
|
"No printers were configured for USB/serial control.",
|
|
|
|
|
wxDefaultPosition, wxDefaultSize);
|
|
|
|
|
$self->{sizer}->Add($self->{text_no_printers}, 0, wxTOP | wxLEFT, 30);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# button for adding new printer panels
|
|
|
|
|
{
|
2017-10-25 10:53:31 +00:00
|
|
|
|
my $btn = $self->{btn_add} = Wx::BitmapButton->new($self, -1, Wx::Bitmap->new(Slic3r::var("add.png"), wxBITMAP_TYPE_PNG),
|
2015-01-03 22:25:55 +00:00
|
|
|
|
wxDefaultPosition, wxDefaultSize, Wx::wxBORDER_NONE);
|
|
|
|
|
$btn->SetToolTipString("Add printer…")
|
|
|
|
|
if $btn->can('SetToolTipString');
|
|
|
|
|
|
|
|
|
|
EVT_LEFT_DOWN($btn, sub {
|
2017-11-02 20:51:06 +00:00
|
|
|
|
my $menu = Wx::Menu->new;
|
|
|
|
|
my @panels = $self->print_panels;
|
2015-11-03 20:55:17 +00:00
|
|
|
|
# remove printers that already exist
|
2017-11-02 20:51:06 +00:00
|
|
|
|
# update configs of currently loaded print panels
|
|
|
|
|
foreach my $preset (@{wxTheApp->{preset_bundle}->printer}) {
|
|
|
|
|
my $preset_name = $preset->name;
|
|
|
|
|
next if ! $preset->config->serial_port ||
|
|
|
|
|
defined first { defined $_ && $_->printer_name eq $preset_name } @panels;
|
|
|
|
|
my $myconfig = $preset->config->clone_only(\@ConfigOptions);
|
2015-01-03 22:25:55 +00:00
|
|
|
|
my $id = &Wx::NewId();
|
|
|
|
|
$menu->Append($id, $preset_name);
|
|
|
|
|
EVT_MENU($menu, $id, sub {
|
2017-11-02 20:51:06 +00:00
|
|
|
|
$self->add_printer($preset_name, $myconfig);
|
2015-01-03 22:25:55 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
$self->PopupMenu($menu, $btn->GetPosition);
|
|
|
|
|
$menu->Destroy;
|
|
|
|
|
});
|
|
|
|
|
$self->{sizer}->Add($btn, 0, wxTOP | wxLEFT, 10);
|
|
|
|
|
}
|
2014-12-31 18:10:46 +00:00
|
|
|
|
|
|
|
|
|
$self->SetSizer($sizer);
|
|
|
|
|
$self->SetMinSize($self->GetSize);
|
2015-01-03 22:25:55 +00:00
|
|
|
|
#$sizer->SetSizeHints($self);
|
2014-12-31 18:10:46 +00:00
|
|
|
|
|
|
|
|
|
EVT_CLOSE($self, sub {
|
|
|
|
|
my (undef, $event) = @_;
|
|
|
|
|
|
2015-05-28 16:31:53 +00:00
|
|
|
|
if ($event->CanVeto) {
|
|
|
|
|
foreach my $panel ($self->print_panels) {
|
|
|
|
|
if ($panel->printing) {
|
|
|
|
|
my $confirm = Wx::MessageDialog->new(
|
|
|
|
|
$self, "Printer '" . $panel->printer_name . "' is printing.\n\nDo you want to stop printing?",
|
|
|
|
|
'Unfinished Print', wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION,
|
|
|
|
|
);
|
|
|
|
|
if ($confirm->ShowModal == wxID_NO) {
|
|
|
|
|
$event->Veto;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-03 22:25:55 +00:00
|
|
|
|
foreach my $panel ($self->print_panels) {
|
|
|
|
|
$panel->disconnect;
|
|
|
|
|
}
|
2014-12-31 18:10:46 +00:00
|
|
|
|
|
|
|
|
|
$event->Skip;
|
|
|
|
|
});
|
|
|
|
|
|
2015-11-03 20:55:17 +00:00
|
|
|
|
$self->Layout;
|
|
|
|
|
|
|
|
|
|
return $self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub OnActivate {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
# get all available presets
|
2017-11-02 20:51:06 +00:00
|
|
|
|
my %presets = map { $_->name => $_->config->clone_only(\@ConfigOptions) }
|
|
|
|
|
grep { $_->config->serial_port } @{wxTheApp->{preset_bundle}->printer};
|
2015-11-03 20:55:17 +00:00
|
|
|
|
|
|
|
|
|
# decide which ones we want to keep
|
|
|
|
|
my %active = ();
|
|
|
|
|
|
|
|
|
|
# keep the ones that are currently connected or have jobs in queue
|
|
|
|
|
$active{$_} = 1 for map $_->printer_name,
|
|
|
|
|
grep { $_->is_connected || @{$_->jobs} > 0 }
|
|
|
|
|
$self->print_panels;
|
|
|
|
|
|
2015-11-08 10:17:55 +00:00
|
|
|
|
if (%presets) {
|
|
|
|
|
# if there are no active panels, use sensible defaults
|
|
|
|
|
if (!%active && keys %presets <= 2) {
|
2015-11-03 20:55:17 +00:00
|
|
|
|
# if only one or two presets exist, load them
|
|
|
|
|
$active{$_} = 1 for keys %presets;
|
2015-11-08 10:17:55 +00:00
|
|
|
|
}
|
|
|
|
|
if (!%active) {
|
2015-11-03 20:55:17 +00:00
|
|
|
|
# enable printers whose port is available
|
2017-11-02 15:21:34 +00:00
|
|
|
|
my %ports = map { $_ => 1 } Slic3r::GUI::scan_serial_ports;
|
2015-11-03 20:55:17 +00:00
|
|
|
|
$active{$_} = 1
|
|
|
|
|
for grep exists $ports{$presets{$_}->serial_port}, keys %presets;
|
2015-01-03 22:25:55 +00:00
|
|
|
|
}
|
2015-11-08 10:17:55 +00:00
|
|
|
|
if (!%active && $self->_selected_printer_preset) {
|
|
|
|
|
# enable currently selected printer if it is configured
|
|
|
|
|
$active{$self->_selected_printer_preset} = 1
|
|
|
|
|
if $presets{$self->_selected_printer_preset};
|
|
|
|
|
}
|
2015-01-03 22:25:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 20:55:17 +00:00
|
|
|
|
# apply changes
|
|
|
|
|
for my $panel ($self->print_panels) {
|
|
|
|
|
next if $active{$panel->printer_name};
|
|
|
|
|
|
|
|
|
|
$self->{sizer}->DetachWindow($panel);
|
|
|
|
|
$panel->Destroy;
|
|
|
|
|
}
|
|
|
|
|
$self->add_printer($_, $presets{$_}) for sort keys %active;
|
|
|
|
|
|
2015-11-03 22:00:59 +00:00
|
|
|
|
# show/hide the warning about no printers
|
|
|
|
|
$self->{text_no_printers}->Show(!%presets);
|
|
|
|
|
|
|
|
|
|
# show/hide the Add button
|
|
|
|
|
$self->{btn_add}->Show(keys %presets != keys %active);
|
|
|
|
|
|
2015-01-03 22:25:55 +00:00
|
|
|
|
$self->Layout;
|
|
|
|
|
|
2015-11-03 20:55:17 +00:00
|
|
|
|
# we need this in order to trigger the OnSize event of wxScrolledWindow which
|
|
|
|
|
# recalculates the virtual size
|
|
|
|
|
Wx::GetTopLevelParent($self)->SendSizeEvent;
|
2014-12-31 18:10:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 22:25:55 +00:00
|
|
|
|
sub add_printer {
|
|
|
|
|
my ($self, $printer_name, $config) = @_;
|
|
|
|
|
|
|
|
|
|
# check that printer doesn't exist already
|
|
|
|
|
foreach my $panel ($self->print_panels) {
|
|
|
|
|
if ($panel->printer_name eq $printer_name) {
|
|
|
|
|
return $panel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $printer_panel = Slic3r::GUI::Controller::PrinterPanel->new($self, $printer_name, $config);
|
|
|
|
|
$self->{sizer}->Prepend($printer_panel, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 10);
|
|
|
|
|
$self->Layout;
|
|
|
|
|
|
|
|
|
|
return $printer_panel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub print_panels {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
return grep $_->isa('Slic3r::GUI::Controller::PrinterPanel'),
|
|
|
|
|
map $_->GetWindow, $self->{sizer}->GetChildren;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 20:51:06 +00:00
|
|
|
|
# Called by Slic3r::GUI::Tab::Printer::_on_presets_changed
|
|
|
|
|
# when the presets are loaded or the user selects another preset.
|
2015-11-08 10:17:55 +00:00
|
|
|
|
sub update_presets {
|
2017-11-02 20:51:06 +00:00
|
|
|
|
my ($self, $presets) = @_;
|
2015-11-08 10:17:55 +00:00
|
|
|
|
# update configs of currently loaded print panels
|
2017-11-02 20:51:06 +00:00
|
|
|
|
my @presets = @$presets;
|
2015-11-08 10:17:55 +00:00
|
|
|
|
foreach my $panel ($self->print_panels) {
|
2017-11-02 20:51:06 +00:00
|
|
|
|
my $preset = $presets->find_preset($panel->printer_name, 0);
|
|
|
|
|
$panel->config($preset->config->clone_only(\@ConfigOptions))
|
|
|
|
|
if defined $preset;
|
2015-11-08 10:17:55 +00:00
|
|
|
|
}
|
2017-11-02 20:51:06 +00:00
|
|
|
|
|
|
|
|
|
$self->_selected_printer_preset($presets->get_selected_preset->name);
|
2015-11-08 10:17:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-31 18:10:46 +00:00
|
|
|
|
1;
|