Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GUI.pm « Slic3r « lib - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 52c4828133c68a497ecc66038fe82d82cbd665a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package Slic3r::GUI;
use strict;
use warnings;
use utf8;

use File::Basename qw(basename);
use FindBin;
use List::Util qw(first);
use Slic3r::GUI::2DBed;
use Slic3r::GUI::BedShapeDialog;
use Slic3r::GUI::Controller;
use Slic3r::GUI::Controller::ManualControlDialog;
use Slic3r::GUI::Controller::PrinterPanel;
use Slic3r::GUI::MainFrame;
use Slic3r::GUI::Plater;
use Slic3r::GUI::Plater::2D;
use Slic3r::GUI::Plater::2DToolpaths;
use Slic3r::GUI::Plater::3D;
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;
use Slic3r::GUI::ProgressStatusBar;
use Slic3r::GUI::OptionsGroup;
use Slic3r::GUI::OptionsGroup::Field;
use Slic3r::GUI::SystemInfo;

use Wx::Locale gettext => 'L';

our $have_OpenGL = eval "use Slic3r::GUI::3DScene; 1";

use Wx 0.9901 qw(:bitmap :dialog :icon :id :misc :systemsettings :toplevelwindow :filedialog :font);
use Wx::Event qw(EVT_IDLE EVT_COMMAND EVT_MENU);
use base 'Wx::App';

use constant FILE_WILDCARDS => {
    known   => 'Known files (*.stl, *.obj, *.amf, *.xml, *.3mf, *.prusa)|*.stl;*.STL;*.obj;*.OBJ;*.zip.amf;*.amf;*.AMF;*.xml;*.XML;*.3mf;*.3MF;*.prusa;*.PRUSA',
    stl     => 'STL files (*.stl)|*.stl;*.STL',
    obj     => 'OBJ files (*.obj)|*.obj;*.OBJ',
    amf     => 'AMF files (*.amf)|*.zip.amf;*.amf;*.AMF;*.xml;*.XML',
    threemf => '3MF files (*.3mf)|*.3mf;*.3MF',
    prusa   => 'Prusa Control files (*.prusa)|*.prusa;*.PRUSA',
    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 threemf prusa)};

# Datadir provided on the command line.
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_plater;
our @cb;

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);
our $medium_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
$medium_font->SetPointSize(12);
our $grey = Wx::Colour->new(200,200,200);

# Events to be sent from a C++ menu implementation:
# 1) To inform about a change of the application language.
our $LANGUAGE_CHANGE_EVENT  = Wx::NewEventType;
# 2) To inform about a change of Preferences.
our $PREFERENCES_EVENT      = Wx::NewEventType;
# To inform AppConfig about Slic3r version available online
our $VERSION_ONLINE_EVENT   = Wx::NewEventType;

sub OnInit {
    my ($self) = @_;
    
    $self->SetAppName('Slic3rPE');
    $self->SetAppDisplayName('Slic3r Prusa Edition');
    Slic3r::debugf "wxWidgets version %s, Wx version %s\n", &Wx::wxVERSION_STRING, $Wx::VERSION;

    # Set the Slic3r data directory at the Slic3r XS module.
    # Unix: ~/.Slic3r
    # Windows: "C:\Users\username\AppData\Roaming\Slic3r" or "C:\Documents and Settings\username\Application Data\Slic3r"
    # Mac: "~/Library/Application Support/Slic3r"
    Slic3r::set_data_dir($datadir || Wx::StandardPaths::Get->GetUserDataDir);
    Slic3r::GUI::set_wxapp($self);

    $self->{app_config} = Slic3r::GUI::AppConfig->new;
    Slic3r::GUI::set_app_config($self->{app_config});
    $self->{preset_bundle} = Slic3r::GUI::PresetBundle->new;
    Slic3r::GUI::set_preset_bundle($self->{preset_bundle});

    # just checking for existence of Slic3r::data_dir is not enough: it may be an empty directory
    # supplied as argument to --datadir; in that case we should still run the wizard
    eval { $self->{preset_bundle}->setup_directories() };
    if ($@) {
        warn $@ . "\n";
        fatal_error(undef, $@);
    }
    my $app_conf_exists = $self->{app_config}->exists;
    # load settings
    $self->{app_config}->load if $app_conf_exists;
    $self->{app_config}->set('version', $Slic3r::VERSION);
    $self->{app_config}->save;

    $self->{preset_updater} = Slic3r::PresetUpdater->new($VERSION_ONLINE_EVENT);
    Slic3r::GUI::set_preset_updater($self->{preset_updater});

    Slic3r::GUI::load_language();

    # Suppress the '- default -' presets.
    $self->{preset_bundle}->set_default_suppressed($self->{app_config}->get('no_defaults') ? 1 : 0);
    eval { $self->{preset_bundle}->load_presets($self->{app_config}); };
    if ($@) {
        warn $@ . "\n";
        show_error(undef, $@);
    }

    # application frame
    print STDERR "Creating main frame...\n";
    Wx::Image::FindHandlerType(wxBITMAP_TYPE_PNG) || Wx::Image::AddHandler(Wx::PNGHandler->new);
    $self->{mainframe} = my $frame = Slic3r::GUI::MainFrame->new(
        # If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
        no_controller   => $self->{app_config}->get('no_controller'),
        no_plater       => $no_plater,
        lang_ch_event   => $LANGUAGE_CHANGE_EVENT,
        preferences_event => $PREFERENCES_EVENT,
    );
    $self->SetTopWindow($frame);

    # This makes CallAfter() work
    EVT_IDLE($self->{mainframe}, sub {
        while (my $cb = shift @cb) {
            $cb->();
        }
        $self->{app_config}->save if $self->{app_config}->dirty;
    });

    # On OS X the UI tends to freeze in weird ways if modal dialogs (config wizard, update notifications, ...)
    # are shown before or in the same event callback with the main frame creation.
    # Therefore we schedule them for later using CallAfter.
    $self->CallAfter(sub {
        eval {
            if (! $self->{preset_updater}->config_update()) {
                exit 0;
            }
        };
        if ($@) {
            warn $@ . "\n";
            fatal_error(undef, $@);
        }
    });

    $self->CallAfter(sub {
        if (! Slic3r::GUI::config_wizard_startup($app_conf_exists)) {
            # Only notify if there was not wizard so as not to bother too much ...
            $self->{preset_updater}->slic3r_update_notify();
        }
        $self->{preset_updater}->sync($self->{preset_bundle});
    });

    # The following event is emited by the C++ menu implementation of application language change.
    EVT_COMMAND($self, -1, $LANGUAGE_CHANGE_EVENT, sub{
        print STDERR "LANGUAGE_CHANGE_EVENT\n";
        $self->recreate_GUI;
    });

    # The following event is emited by the C++ menu implementation of preferences change.
    EVT_COMMAND($self, -1, $PREFERENCES_EVENT, sub{
        $self->update_ui_from_settings;
    });
    
    # The following event is emited by PresetUpdater (C++)
    EVT_COMMAND($self, -1, $VERSION_ONLINE_EVENT, sub {
        my ($self, $event) = @_;
        my $version = $event->GetString;
        $self->{app_config}->set('version_online', $version);
        $self->{app_config}->save;
    });

    return 1;
}

sub recreate_GUI{
    print STDERR "recreate_GUI\n";
    my ($self) = @_;
    my $topwindow = $self->GetTopWindow();
    $self->{mainframe} = my $frame = Slic3r::GUI::MainFrame->new(
        # If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
        no_controller   => $self->{app_config}->get('no_controller'),
        no_plater       => $no_plater,
        lang_ch_event   => $LANGUAGE_CHANGE_EVENT,
        preferences_event => $PREFERENCES_EVENT,
    );

    if($topwindow)
    {
        $self->SetTopWindow($frame);
        $topwindow->Destroy;
    }

    EVT_IDLE($self->{mainframe}, sub {
        while (my $cb = shift @cb) {
            $cb->();
        }
        $self->{app_config}->save if $self->{app_config}->dirty;
    });

    # On OSX the UI was not initialized correctly if the wizard was called
    # before the UI was up and running.
    $self->CallAfter(sub {
        # Run the config wizard, don't offer the "reset user profile" checkbox.
        Slic3r::GUI::config_wizard_startup(1);
    });
}

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 = Slic3r::GUI::_3DScene::get_gl_info(1, 1);
        $opengl_info_txt = Slic3r::GUI::_3DScene::get_gl_info(0, 1);
    }
    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;
}

# static method accepting a wxWindow object as first parameter
sub catch_error {
    my ($self, $cb, $message_dialog) = @_;
    if (my $err = $@) {
        $cb->() if $cb;
        $message_dialog
            ? $message_dialog->($err, 'Error', wxOK | wxICON_ERROR)
            : Slic3r::GUI::show_error($self, $err);
        return 1;
    }
    return 0;
}

# static method accepting a wxWindow object as first parameter
sub show_error {
    my ($parent, $message) = @_;
    Slic3r::GUI::show_error_id($parent ? $parent->GetId() : 0, $message);
}

# static method accepting a wxWindow object as first parameter
sub show_info {
    my ($parent, $message, $title) = @_;
    Wx::MessageDialog->new($parent, $message, $title || 'Notice', wxOK | wxICON_INFORMATION)->ShowModal;
}

# static method accepting a wxWindow object as first parameter
sub fatal_error {
    show_error(@_);
    exit 1;
}

# static method accepting a wxWindow object as first parameter
sub warning_catcher {
    my ($self, $message_dialog) = @_;
    return sub {
        my $message = shift;
        return if $message =~ /GLUquadricObjPtr|Attempt to free unreferenced scalar/;
        my @params = ($message, 'Warning', wxOK | wxICON_WARNING);
        $message_dialog
            ? $message_dialog->(@params)
            : Wx::MessageDialog->new($self, @params)->ShowModal;
    };
}

sub notify {
    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);

    # There used to be notifier using a Growl application for OSX, but Growl is dead.
    # The notifier also supported the Linux X D-bus notifications, but that support was broken.
    #TODO use wxNotificationMessage?
}

# 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 open_model {
    my ($self, $window) = @_;

    my $dlg_title = L('Choose one or more files (STL/OBJ/AMF/3MF/PRUSA):');   
    my $dialog = Wx::FileDialog->new($window // $self->GetTopWindow, $dlg_title, 
        $self->{app_config}->get_last_dir, "",
        MODEL_WILDCARD, wxFD_OPEN | wxFD_MULTIPLE | wxFD_FILE_MUST_EXIST);
    if ($dialog->ShowModal != wxID_OK) {
        $dialog->Destroy;
        return;
    }
    my @input_files = $dialog->GetPaths;
    $dialog->Destroy;
    return @input_files;
}

sub CallAfter {
    my ($self, $cb) = @_;
    push @cb, $cb;
}

sub append_menu_item {
    my ($self, $menu, $string, $description, $cb, $id, $icon, $kind) = @_;
    
    $id //= &Wx::NewId();
    my $item = Wx::MenuItem->new($menu, $id, $string, $description // '', $kind // 0);
    $self->set_menu_item_icon($item, $icon);
    $menu->Append($item);
    
    EVT_MENU($self, $id, $cb);
    return $item;
}

sub append_submenu {
    my ($self, $menu, $string, $description, $submenu, $id, $icon) = @_;
    
    $id //= &Wx::NewId();
    my $item = Wx::MenuItem->new($menu, $id, $string, $description // '');
    $self->set_menu_item_icon($item, $icon);
    $item->SetSubMenu($submenu);
    $menu->Append($item);
    
    return $item;
}

sub set_menu_item_icon {
    my ($self, $menuItem, $icon) = @_;
    
    # SetBitmap was not available on OS X before Wx 0.9927
    if ($icon && $menuItem->can('SetBitmap')) {
        $menuItem->SetBitmap(Wx::Bitmap->new(Slic3r::var($icon), wxBITMAP_TYPE_PNG));
    }
}

sub save_window_pos {
    my ($self, $window, $name) = @_;
    
    $self->{app_config}->set("${name}_pos", join ',', $window->GetScreenPositionXY);
    $self->{app_config}->set("${name}_size", join ',', $window->GetSizeWH);
    $self->{app_config}->set("${name}_maximized", $window->IsMaximized);
    $self->{app_config}->save;
}

sub restore_window_pos {
    my ($self, $window, $name) = @_;
    if ($self->{app_config}->has("${name}_pos")) {
        my $size = [ split ',', $self->{app_config}->get("${name}_size"), 2 ];
        $window->SetSize($size);
        
        my $display = Wx::Display->new->GetClientArea();
        my $pos = [ split ',', $self->{app_config}->get("${name}_pos"), 2 ];
        if (($pos->[0] + $size->[0]/2) < $display->GetRight && ($pos->[1] + $size->[1]/2) < $display->GetBottom) {
            $window->Move($pos);
        }
        $window->Maximize(1) if $self->{app_config}->get("${name}_maximized");
    }
}

1;