~ubuntu-branches/ubuntu/utopic/slic3r/utopic

« back to all changes in this revision

Viewing changes to lib/Slic3r/GUI/Plater/ObjectSettingsDialog.pm

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2014-06-17 01:27:26 UTC
  • Revision ID: package-import@ubuntu.com-20140617012726-2wrs4zdo251nr4vg
Tags: upstream-1.1.4+dfsg
ImportĀ upstreamĀ versionĀ 1.1.4+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Slic3r::GUI::Plater::ObjectSettingsDialog;
 
2
use strict;
 
3
use warnings;
 
4
use utf8;
 
5
 
 
6
use Wx qw(:dialog :id :misc :sizer :systemsettings :notebook wxTAB_TRAVERSAL);
 
7
use Wx::Event qw(EVT_BUTTON);
 
8
use base 'Wx::Dialog';
 
9
 
 
10
sub new {
 
11
    my $class = shift;
 
12
    my ($parent, %params) = @_;
 
13
    my $self = $class->SUPER::new($parent, -1, "Settings for " . $params{object}->name, wxDefaultPosition, [700,500], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
 
14
    $self->{$_} = $params{$_} for keys %params;
 
15
    
 
16
    $self->{tabpanel} = Wx::Notebook->new($self, -1, wxDefaultPosition, wxDefaultSize, wxNB_TOP | wxTAB_TRAVERSAL);
 
17
    $self->{tabpanel}->AddPage($self->{parts} = Slic3r::GUI::Plater::ObjectPartsPanel->new($self->{tabpanel}, model_object => $params{model_object}), "Parts");
 
18
    $self->{tabpanel}->AddPage($self->{layers} = Slic3r::GUI::Plater::ObjectDialog::LayersTab->new($self->{tabpanel}), "Layers");
 
19
    
 
20
    my $buttons = $self->CreateStdDialogButtonSizer(wxOK);
 
21
    EVT_BUTTON($self, wxID_OK, sub {
 
22
        # validate user input
 
23
        return if !$self->{parts}->CanClose;
 
24
        return if !$self->{layers}->CanClose;
 
25
        
 
26
        # notify tabs
 
27
        $self->{layers}->Closing;
 
28
        
 
29
        $self->EndModal(wxID_OK);
 
30
        $self->Destroy;
 
31
    });
 
32
    
 
33
    my $sizer = Wx::BoxSizer->new(wxVERTICAL);
 
34
    $sizer->Add($self->{tabpanel}, 1, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 10);
 
35
    $sizer->Add($buttons, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
 
36
    
 
37
    $self->SetSizer($sizer);
 
38
    $self->SetMinSize($self->GetSize);
 
39
    
 
40
    return $self;
 
41
}
 
42
 
 
43
sub PartsChanged {
 
44
    my ($self) = @_;
 
45
    return $self->{parts}->PartsChanged;
 
46
}
 
47
 
 
48
sub PartSettingsChanged {
 
49
    my ($self) = @_;
 
50
    return $self->{parts}->PartSettingsChanged;
 
51
}
 
52
 
 
53
package Slic3r::GUI::Plater::ObjectDialog::BaseTab;
 
54
use base 'Wx::Panel';
 
55
 
 
56
sub model_object {
 
57
    my ($self) = @_;
 
58
    return $self->GetParent->GetParent->{model_object};
 
59
}
 
60
 
 
61
package Slic3r::GUI::Plater::ObjectDialog::LayersTab;
 
62
use Wx qw(:dialog :id :misc :sizer :systemsettings);
 
63
use Wx::Grid;
 
64
use Wx::Event qw(EVT_GRID_CELL_CHANGED);
 
65
use base 'Slic3r::GUI::Plater::ObjectDialog::BaseTab';
 
66
 
 
67
sub new {
 
68
    my $class = shift;
 
69
    my ($parent, %params) = @_;
 
70
    my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize);
 
71
    
 
72
    my $sizer = Wx::BoxSizer->new(wxVERTICAL);
 
73
    
 
74
    {
 
75
        my $label = Wx::StaticText->new($self, -1, "You can use this section to override the default layer height for parts of this object. Set layer height to zero to skip portions of the input file.",
 
76
            wxDefaultPosition, [-1, 40]);
 
77
        $label->SetFont(Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
 
78
        $sizer->Add($label, 0, wxEXPAND | wxALL, 10);
 
79
    }
 
80
    
 
81
    my $grid = $self->{grid} = Wx::Grid->new($self, -1, wxDefaultPosition, wxDefaultSize);
 
82
    $sizer->Add($grid, 1, wxEXPAND | wxALL, 10);
 
83
    $grid->CreateGrid(0, 3);
 
84
    $grid->DisableDragRowSize;
 
85
    $grid->HideRowLabels if &Wx::wxVERSION_STRING !~ / 2\.8\./;
 
86
    $grid->SetColLabelValue(0, "Min Z (mm)");
 
87
    $grid->SetColLabelValue(1, "Max Z (mm)");
 
88
    $grid->SetColLabelValue(2, "Layer height (mm)");
 
89
    $grid->SetColSize($_, 135) for 0..2;
 
90
    $grid->SetDefaultCellAlignment(wxALIGN_CENTRE, wxALIGN_CENTRE);
 
91
    
 
92
    # load data
 
93
    foreach my $range (@{ $self->model_object->layer_height_ranges }) {
 
94
        $grid->AppendRows(1);
 
95
        my $i = $grid->GetNumberRows-1;
 
96
        $grid->SetCellValue($i, $_, $range->[$_]) for 0..2;
 
97
    }
 
98
    $grid->AppendRows(1); # append one empty row
 
99
    
 
100
    EVT_GRID_CELL_CHANGED($grid, sub {
 
101
        my ($grid, $event) = @_;
 
102
        
 
103
        # remove any non-numeric character
 
104
        my $value = $grid->GetCellValue($event->GetRow, $event->GetCol);
 
105
        $value =~ s/,/./g;
 
106
        $value =~ s/[^0-9.]//g;
 
107
        $grid->SetCellValue($event->GetRow, $event->GetCol, $value);
 
108
        
 
109
        # if there's no empty row, let's append one
 
110
        for my $i (0 .. $grid->GetNumberRows-1) {
 
111
            if (!grep $grid->GetCellValue($i, $_), 0..2) {
 
112
                return;
 
113
            }
 
114
        }
 
115
        $grid->AppendRows(1);
 
116
    });
 
117
    
 
118
    $self->SetSizer($sizer);
 
119
    $sizer->SetSizeHints($self);
 
120
    
 
121
    return $self;
 
122
}
 
123
 
 
124
sub CanClose {
 
125
    my $self = shift;
 
126
    
 
127
    # validate ranges before allowing user to dismiss the dialog
 
128
    
 
129
    foreach my $range ($self->_get_ranges) {
 
130
        my ($min, $max, $height) = @$range;
 
131
        if ($max <= $min) {
 
132
            Slic3r::GUI::show_error($self, "Invalid Z range $min-$max.");
 
133
            return 0;
 
134
        }
 
135
        if ($min < 0 || $max < 0) {
 
136
            Slic3r::GUI::show_error($self, "Invalid Z range $min-$max.");
 
137
            return 0;
 
138
        }
 
139
        if ($height < 0) {
 
140
            Slic3r::GUI::show_error($self, "Invalid layer height $height.");
 
141
            return 0;
 
142
        }
 
143
        # TODO: check for overlapping ranges
 
144
    }
 
145
    
 
146
    return 1;
 
147
}
 
148
 
 
149
sub Closing {
 
150
    my $self = shift;
 
151
    
 
152
    # save ranges into the plater object
 
153
    $self->model_object->set_layer_height_ranges([ $self->_get_ranges ]);
 
154
}
 
155
 
 
156
sub _get_ranges {
 
157
    my $self = shift;
 
158
    
 
159
    my @ranges = ();
 
160
    for my $i (0 .. $self->{grid}->GetNumberRows-1) {
 
161
        my ($min, $max, $height) = map $self->{grid}->GetCellValue($i, $_), 0..2;
 
162
        next if $min eq '' || $max eq '' || $height eq '';
 
163
        push @ranges, [ $min, $max, $height ];
 
164
    }
 
165
    return sort { $a->[0] <=> $b->[0] } @ranges;
 
166
}
 
167
 
 
168
1;