From db031a621045facbc916075ec7bddc16a857499e Mon Sep 17 00:00:00 2001
From: Alessandro Ranellucci <aar@cpan.org>
Date: Mon, 16 Jun 2014 20:11:52 +0200
Subject: [PATCH] Incomplete work for adapting wizard to bed_shape

---
 lib/Slic3r/GUI/BedShapeDialog.pm | 61 ++++++++++++++++++++++++--------
 lib/Slic3r/GUI/ConfigWizard.pm   | 38 +++++++++++++++-----
 lib/Slic3r/GUI/MainFrame.pm      |  4 +--
 3 files changed, 78 insertions(+), 25 deletions(-)

diff --git a/lib/Slic3r/GUI/BedShapeDialog.pm b/lib/Slic3r/GUI/BedShapeDialog.pm
index 2cc7998b7..7295d027d 100644
--- a/lib/Slic3r/GUI/BedShapeDialog.pm
+++ b/lib/Slic3r/GUI/BedShapeDialog.pm
@@ -9,6 +9,43 @@ use Wx qw(:dialog :id :misc :sizer :choicebook wxTAB_TRAVERSAL);
 use Wx::Event qw(EVT_CLOSE EVT_BUTTON EVT_CHOICE);
 use base 'Wx::Dialog';
 
+sub new {
+    my $class = shift;
+    my ($parent, $default) = @_;
+    my $self = $class->SUPER::new($parent, -1, "Bed Shape", wxDefaultPosition, [350,700], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
+    
+    $self->{panel} = my $panel = Slic3r::GUI::BedShapePanel->new($self, $default);
+    
+    my $main_sizer = Wx::BoxSizer->new(wxVERTICAL);
+    $main_sizer->Add($panel, 1, wxEXPAND);
+    $main_sizer->Add($self->CreateButtonSizer(wxOK | wxCANCEL), 0, wxEXPAND);
+    
+    $self->SetSizer($main_sizer);
+    $self->SetMinSize($self->GetSize);
+    $main_sizer->SetSizeHints($self);
+    
+    # needed to actually free memory
+    EVT_CLOSE($self, sub {
+        $self->EndModal(wxID_OK);
+        $self->Destroy;
+    });
+    
+    return $self;
+}
+
+sub GetValue {
+    my ($self) = @_;
+    return $self->{panel}->GetValue;
+}
+
+package Slic3r::GUI::BedShapePanel;
+
+use List::Util qw(min max);
+use Slic3r::Geometry qw(PI X Y unscale);
+use Wx qw(:dialog :id :misc :sizer :choicebook wxTAB_TRAVERSAL);
+use Wx::Event qw(EVT_CLOSE EVT_BUTTON EVT_CHOICE);
+use base 'Wx::Panel';
+
 use constant SHAPE_RECTANGULAR  => 0;
 use constant SHAPE_CIRCULAR     => 1;
 use constant SHAPE_CUSTOM       => 2;
@@ -16,7 +53,9 @@ use constant SHAPE_CUSTOM       => 2;
 sub new {
     my $class = shift;
     my ($parent, $default) = @_;
-    my $self = $class->SUPER::new($parent, -1, "Bed Shape", wxDefaultPosition, [350,700], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
+    my $self = $class->SUPER::new($parent, -1);
+    
+    $self->on_change(undef);
     
     my $box = Wx::StaticBox->new($self, -1, "Shape");
     my $sbsizer = Wx::StaticBoxSizer->new($box, wxVERTICAL);
@@ -53,19 +92,7 @@ sub new {
     $top_sizer->Add($sbsizer, 0, wxEXPAND | wxTOP | wxBOTTOM, 10);
     $top_sizer->Add($canvas, 1, wxEXPAND | wxALL, 0) if $canvas;
     
-    my $main_sizer = Wx::BoxSizer->new(wxVERTICAL);
-    $main_sizer->Add($top_sizer, 1, wxEXPAND);
-    $main_sizer->Add($self->CreateButtonSizer(wxOK | wxCANCEL), 0, wxEXPAND);
-    
-    $self->SetSizer($main_sizer);
-    $self->SetMinSize($self->GetSize);
-    $main_sizer->SetSizeHints($self);
-    
-    # needed to actually free memory
-    EVT_CLOSE($self, sub {
-        $self->EndModal(wxID_OK);
-        $self->Destroy;
-    });
+    $self->SetSizerAndFit($top_sizer);
     
     $self->_set_shape($default);
     $self->_update_preview;
@@ -73,6 +100,11 @@ sub new {
     return $self;
 }
 
+sub on_change {
+    my ($self, $cb) = @_;
+    $self->{on_change} = $cb // sub {};
+}
+
 sub _set_shape {
     my ($self, $points) = @_;
     
@@ -131,6 +163,7 @@ sub _update_shape {
         ];
     }
     
+    $self->{on_change}->();
     $self->_update_preview;
 }
 
diff --git a/lib/Slic3r/GUI/ConfigWizard.pm b/lib/Slic3r/GUI/ConfigWizard.pm
index efa1117f1..a5fa9c1cc 100644
--- a/lib/Slic3r/GUI/ConfigWizard.pm
+++ b/lib/Slic3r/GUI/ConfigWizard.pm
@@ -5,6 +5,7 @@ use utf8;
 
 use Wx;
 use base 'Wx::Wizard';
+use Slic3r::Geometry qw(unscale);
 
 # adhere to various human interface guidelines
 our $wizard = 'Wizard';
@@ -52,9 +53,13 @@ sub run {
         # it would be cleaner to have these defined inside each page class,
         # in some event getting called before leaving the page
         {
-            # set print_center to centre of bed_size
-            my $bed_size = $self->{config}->bed_size;
-            $self->{config}->set('print_center', [$bed_size->[0]/2, $bed_size->[1]/2]);
+            # set print_center to center of bed_shape
+            {
+                my $bed_shape = $self->{config}->bed_shape;
+                my $polygon = Slic3r::Polygon->new_scale(@$bed_shape);
+                my $center = $polygon->centroid;
+                $self->{config}->set('print_center', [ map unscale($_), @$center ]);
+            }
             
             # set first_layer_height + layer_height based on nozzle_diameter
             my $nozzle = $self->{config}->nozzle_diameter;
@@ -204,19 +209,24 @@ sub append_option {
     # populate repository with the factory default
     my $opt_key = $full_key;
     $opt_key =~ s/#.+//;
-    $self->GetParent->{config}->apply(Slic3r::Config->new_from_defaults($opt_key));
+    $self->config->apply(Slic3r::Config->new_from_defaults($opt_key));
     
     # draw the control
     my $optgroup = Slic3r::GUI::ConfigOptionsGroup->new(
         parent      => $self,
         title       => '',
-        config      => $self->GetParent->{config},
+        config      => $self->config,
         options     => [$full_key],
         full_labels => 1,
     );
     $self->{vsizer}->Add($optgroup->sizer, 0, wxEXPAND | wxTOP | wxBOTTOM, 10);
 }
 
+sub append_panel {
+    my ($self, $panel) = @_;
+    $self->{vsizer}->Add($panel, 0, wxEXPAND | wxTOP | wxBOTTOM, 10);
+}
+
 sub set_previous_page {
     my $self = shift;
     my ($previous_page) = @_;
@@ -253,6 +263,11 @@ sub build_index {
     $self->{index}->append_title($page->get_short_title) while ($page = $page->GetNext);
 }
 
+sub config {
+    my ($self) = @_;
+    return $self->GetParent->{config};
+}
+
 package Slic3r::GUI::ConfigWizard::Page::Welcome;
 use base 'Slic3r::GUI::ConfigWizard::Page';
 
@@ -290,9 +305,14 @@ sub new {
     my ($parent) = @_;
     my $self = $class->SUPER::new($parent, 'Bed Size');
 
-    $self->append_text('Enter the size of your printers bed, then click Next.');
-    $self->append_option('bed_size');
-
+    $self->append_text('Set the shape of your printer\'s bed, then click Next.');
+    
+    $self->config->apply(Slic3r::Config->new_from_defaults('bed_shape'));
+    $self->{bed_shape_panel} = my $panel = Slic3r::GUI::BedShapePanel->new($self, $self->config->bed_shape);
+    $self->{bed_shape_panel}->on_change(sub {
+        $self->config->set('bed_shape', $self->{bed_shape_panel}->GetValue);
+    });
+    $self->append_panel($self->{bed_shape_panel});
     return $self;
 }
 
@@ -304,7 +324,7 @@ sub new {
     my ($parent) = @_;
     my $self = $class->SUPER::new($parent, 'Nozzle Diameter');
 
-    $self->append_text('Enter the diameter of your printers hot end nozzle, then click Next.');
+    $self->append_text('Enter the diameter of your printer\'s hot end nozzle, then click Next.');
     $self->append_option('nozzle_diameter#0');
 
     return $self;
diff --git a/lib/Slic3r/GUI/MainFrame.pm b/lib/Slic3r/GUI/MainFrame.pm
index 49c154068..cb11e2f1f 100644
--- a/lib/Slic3r/GUI/MainFrame.pm
+++ b/lib/Slic3r/GUI/MainFrame.pm
@@ -551,13 +551,13 @@ sub config_wizard {
     my $self = shift;
 
     return unless $self->check_unsaved_changes;
-    if (my $config = Slic3r::GUI::ConfigWizard->new($self)->run) {
+    if (my $config = Slic3r::GUI::ConfigWizard->new($self)->run) {use XXX; YYY $config->bed_shape;
         if ($self->{mode} eq 'expert') {
             for my $tab (values %{$self->{options_tabs}}) {
                 $tab->select_default_preset;
             }
         }
-        $self->load_config($config);
+        $self->load_config($config);use XXX; YYY $self->config->bed_shape;
         if ($self->{mode} eq 'expert') {
             for my $tab (values %{$self->{options_tabs}}) {
                 $tab->save_preset('My Settings');