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