1 | package Uploader::GUI::wxImagePreview; |
---|
2 | use strict; |
---|
3 | use base qw/Wx::Dialog Class::Accessor::Fast/; |
---|
4 | use Wx qw/ |
---|
5 | wxDefaultPosition |
---|
6 | wxDefaultSize |
---|
7 | wxBG_STYLE_CUSTOM |
---|
8 | |
---|
9 | wxIMAGE_QUALITY_HIGH |
---|
10 | |
---|
11 | wxRESIZE_BORDER |
---|
12 | |
---|
13 | wxDIALOG_EX_METAL |
---|
14 | |
---|
15 | wxDIALOG_NO_PARENT |
---|
16 | wxDEFAULT_DIALOG_STYLE |
---|
17 | |
---|
18 | wxMAXIMIZE_BOX |
---|
19 | wxMINIMIZE_BOX |
---|
20 | |
---|
21 | wxSTAY_ON_TOP |
---|
22 | /; |
---|
23 | |
---|
24 | __PACKAGE__->mk_accessors( |
---|
25 | qw/ |
---|
26 | image |
---|
27 | image_size |
---|
28 | caption |
---|
29 | / |
---|
30 | ); |
---|
31 | use Data::Dumper; |
---|
32 | use Wx::Event qw/ |
---|
33 | EVT_PAINT |
---|
34 | EVT_SIZE |
---|
35 | /; |
---|
36 | |
---|
37 | sub new { |
---|
38 | my( $class, $params ) = @_; |
---|
39 | my( $self ) = $class->SUPER::new( |
---|
40 | undef, |
---|
41 | -1, |
---|
42 | $params->{caption}, |
---|
43 | wxDefaultPosition, |
---|
44 | [400,300], |
---|
45 | wxMAXIMIZE_BOX| |
---|
46 | wxMINIMIZE_BOX| |
---|
47 | wxRESIZE_BORDER| |
---|
48 | wxDEFAULT_DIALOG_STYLE| |
---|
49 | wxSTAY_ON_TOP| |
---|
50 | wxDIALOG_NO_PARENT |
---|
51 | ); |
---|
52 | |
---|
53 | $self->SetBackgroundStyle(wxBG_STYLE_CUSTOM); |
---|
54 | |
---|
55 | Wx::InitAllImageHandlers(); |
---|
56 | |
---|
57 | EVT_SIZE($self, sub { |
---|
58 | my ( $this, $event ) = @_; |
---|
59 | |
---|
60 | $this->Refresh; |
---|
61 | $event->Skip(); |
---|
62 | } |
---|
63 | ); |
---|
64 | |
---|
65 | |
---|
66 | EVT_PAINT( |
---|
67 | $self, |
---|
68 | sub { |
---|
69 | my ( $this, $event ) = @_; |
---|
70 | |
---|
71 | my $dc = Wx::AutoBufferedPaintDC->new($this); |
---|
72 | if($this->image){ |
---|
73 | |
---|
74 | my ($w, $h) = $this->GetSizeWH; |
---|
75 | my $p_prop = $w / $h; |
---|
76 | |
---|
77 | # calculation made to keep aspect |
---|
78 | # ratio and maximize image size |
---|
79 | |
---|
80 | my $scale; |
---|
81 | my ($img_w, $img_h) = @{$this->image_size}; |
---|
82 | return unless $img_w; |
---|
83 | return unless $img_h; |
---|
84 | |
---|
85 | my $img_prop = $img_w / $img_h; |
---|
86 | |
---|
87 | $scale = ($p_prop > $img_prop) |
---|
88 | ? ($h / $img_h) |
---|
89 | : ($w / $img_w); |
---|
90 | |
---|
91 | $scale = 1 if($scale > 1); |
---|
92 | |
---|
93 | my ($sw, $sh) = map({$_ * $scale} $img_w, $img_h); |
---|
94 | my $bmp = Wx::Bitmap->new($this->image->Scale($sw, $sh, wxIMAGE_QUALITY_HIGH)); |
---|
95 | my $xoff = ($w - $sw) / 2; |
---|
96 | my $yoff = ($h - $sh) / 2; |
---|
97 | $dc->DrawBitmap($bmp, $xoff, $yoff, $dc->Clear ? 1:1); |
---|
98 | } |
---|
99 | else{ |
---|
100 | $dc->Clear; |
---|
101 | } |
---|
102 | } |
---|
103 | ); |
---|
104 | |
---|
105 | |
---|
106 | $self; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | 1; |
---|