From d0588c2ef805b735c77bd8f468dbbd03af6953ad Mon Sep 17 00:00:00 2001 From: Henrik Brix Andersen Date: Sat, 23 Jun 2012 17:39:20 +0200 Subject: [PATCH] Ask for confirmation before closing if there are unsaved presets. --- lib/Slic3r/GUI.pm | 26 ++++++++++++++++---------- lib/Slic3r/GUI/SkeinPanel.pm | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/Slic3r/GUI.pm b/lib/Slic3r/GUI.pm index f4e08f41..c1d5c2d5 100644 --- a/lib/Slic3r/GUI.pm +++ b/lib/Slic3r/GUI.pm @@ -10,7 +10,7 @@ use Slic3r::GUI::SkeinPanel; use Slic3r::GUI::Tab; use Wx 0.9901 qw(:sizer :frame wxID_EXIT wxID_ABOUT); -use Wx::Event qw(EVT_MENU); +use Wx::Event qw(EVT_MENU EVT_CLOSE); use base 'Wx::App'; my $growler; @@ -53,8 +53,7 @@ sub OnInit { Wx::Image::AddHandler(Wx::PNGHandler->new); my $frame = Wx::Frame->new(undef, -1, 'Slic3r', [-1, -1], [760,520], wxDEFAULT_FRAME_STYLE); $frame->SetIcon(Wx::Icon->new("$Slic3r::var/Slic3r_128px.png", &Wx::wxBITMAP_TYPE_PNG) ); - my $panel = Slic3r::GUI::SkeinPanel->new($frame); - + $frame->{skeinpanel} = Slic3r::GUI::SkeinPanel->new($frame); # status bar $frame->{statusbar} = Slic3r::GUI::ProgressStatusBar->new($frame, -1); @@ -72,13 +71,13 @@ sub OnInit { $fileMenu->Append(6, "Export SVG…"); $fileMenu->AppendSeparator(); $fileMenu->Append(wxID_EXIT, "&Quit"); - EVT_MENU($frame, 1, sub { $panel->save_config }); - EVT_MENU($frame, 2, sub { $panel->load_config }); - EVT_MENU($frame, 3, sub { $panel->do_slice }); - EVT_MENU($frame, 4, sub { $panel->do_slice(reslice => 1) }); - EVT_MENU($frame, 5, sub { $panel->do_slice(save_as => 1) }); - EVT_MENU($frame, 6, sub { $panel->do_slice(save_as => 1, export_svg => 1) }); - EVT_MENU($frame, wxID_EXIT, sub {$_[0]->Close(1)}); + EVT_MENU($frame, 1, sub { $frame->{skeinpanel}->save_config }); + EVT_MENU($frame, 2, sub { $frame->{skeinpanel}->load_config }); + EVT_MENU($frame, 3, sub { $frame->{skeinpanel}->do_slice }); + EVT_MENU($frame, 4, sub { $frame->{skeinpanel}->do_slice(reslice => 1) }); + EVT_MENU($frame, 5, sub { $frame->{skeinpanel}->do_slice(save_as => 1) }); + EVT_MENU($frame, 6, sub { $frame->{skeinpanel}->do_slice(save_as => 1, export_svg => 1) }); + EVT_MENU($frame, wxID_EXIT, sub {$_[0]->Close(0)}); } # Help menu @@ -98,6 +97,8 @@ sub OnInit { $frame->SetMenuBar($menubar); } + EVT_CLOSE($frame, \&on_close); + $frame->SetMinSize($frame->GetSize); $frame->Show; $frame->Layout; @@ -117,6 +118,11 @@ sub About { Wx::AboutBox($info); } +sub on_close { + my ($frame, $event) = @_; + $event->CanVeto ? $event->Skip($frame->{skeinpanel}->on_close) : $event->Skip(1); +} + sub catch_error { my ($self, $cb, $message_dialog) = @_; if (my $err = $@) { diff --git a/lib/Slic3r/GUI/SkeinPanel.pm b/lib/Slic3r/GUI/SkeinPanel.pm index 2e4c4bce..f67d64f9 100644 --- a/lib/Slic3r/GUI/SkeinPanel.pm +++ b/lib/Slic3r/GUI/SkeinPanel.pm @@ -223,4 +223,22 @@ sub load_config { $dlg->Destroy; } +sub on_close { + my $self = shift; + + my @dirty; + foreach (keys %{$self->{options_tabs}}) { + push (@dirty, $_) if $self->{options_tabs}{$_}->is_dirty; + } + + if (@dirty) { + my $titles = join ', ', @dirty; + my $confirm = Wx::MessageDialog->new($self, "You have unsaved changes ($titles). Exit anyway?", + 'Unsaved Presets', wxICON_QUESTION | wxOK | wxCANCEL); + return ($confirm->ShowModal == wxID_OK); + } + + return 1; +} + 1;