App/Chart/Gtk2/IntradayImage.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« App/Chart/Gtk2/Ex/ListStoreDBISeq.pm ↑ Summary App/Chart/Gtk2/OpenDialog.pm »
1 # Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2014, 2016, 2018 Kevin Ryde
2  
3 # This file is part of Chart.
4 #
5 # Chart is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3, or (at your option) any later version.
8 #
9 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Chart. If not, see <http://www.gnu.org/licenses/>.
16  
17 package App::Chart::Gtk2::IntradayImage;
18 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 0 ▶use Gtk2 1.220;
Subroutine BEGIN: called CC=1 SCAR=2.6
23 use Encode;
24 use Encode::Locale; # for coding system "locale"
25 use List::Util qw(min max);
26 use Locale::TextDomain ('App-Chart');
27  
28 use Gtk2::Ex::Units;
29 use Gtk2::Ex::PixbufBits;
30 use App::Chart::Database;
31 use App::Chart::Gtk2::GUI;
32  
33 # uncomment this to run the ### lines
34 # use Smart::Comments;
35  
36  
37 use Glib::Object::Subclass
38 'Gtk2::DrawingArea',
39 signals => { expose_event => \&_do_expose_event,
40 size_request => \&_do_size_request },
41 properties => [Glib::ParamSpec->string
42 ('symbol',
43 __('Symbol'),
44 'The symbol of the stock or commodity to be shown.',
45 '', # default
46 Glib::G_PARAM_READWRITE),
47  
48 Glib::ParamSpec->string
49 ('mode',
50 'Mode',
51 'The intraday mode, such as "1d" for one day. The possible values here depend on the symbol\'s data source code.',
52 '', # default
53 Glib::G_PARAM_READWRITE)];
54  
55 sub INIT_INSTANCE {
56 my ($self) = @_;
57 $self->{'symbol'} = '';
58 $self->{'mode'} = '';
59  
60 # Single pixbuf draw operation doesn't need double buffering.
61 $self->set_double_buffered (0);
62 $self->set_app_paintable (1);
63  
64 App::Chart::chart_dirbroadcast()->connect_for_object
65 ('intraday-changed', \&_do_intraday_changed, $self);
66 }
67  
68 sub SET_PROPERTY {
69 my ($self, $pspec, $newval) = @_;
70 my $pname = $pspec->get_name;
71 ### IntradayImage SET_PROPERTY(): "$pname $newval"
72  
73 my $oldval = $self->{$pname};
74 $self->{$pname} = $newval; # per default GET_PROPERTY
75
76 ### stored to: ''.\$self->{$pname}
77  
78 if ($oldval eq $newval) {
79 return;
80 }
81  
82 if ($pname eq 'symbol' || $pname eq 'mode') {
83 # new image (or new no image)
84 delete $self->{'xor_background'}; # new colour scheme
85 $self->queue_resize;
86 $self->queue_draw;
87 }
88 }
89  
90 # 'size-request' class closure
91 sub _do_size_request {
92 my ($self, $req) = @_;
93 ### IntradayImage _do_size_request()
94 my $pixbuf = _load_pixbuf ($self);
95 if (ref $pixbuf) {
96 $req->width ($pixbuf->get_width);
97 $req->height ($pixbuf->get_height);
98 } else {
99 $req->width (35 * Gtk2::Ex::Units::em($self));
100 $req->height (6 * Gtk2::Ex::Units::line_height($self));
101 }
102 ### _do_size_request() decide: $req->width."x".$req->height
103 }
104  
105 # 'expose-event' class closure
106 sub _do_expose_event {
107 my ($self, $event) = @_;
108 ### IntradayImage _do_expose_event(): $self->get_name.' "'.($self->{'symbol'}||'[nosymbol]').'" "'.($self->{'mode'}||'[nomode]').'"'
109 my $win = $self->window;
110  
111 # Reading the database on every expose probably isn't fast, but we're not
112 # expecting to scroll or anything much, so leaving the data on disk might
113 # save a little memory.
114 #
115 my $pixbuf = _load_pixbuf ($self);
116 if (! ref $pixbuf) {
117 my $errmsg = $pixbuf;
118 ### pixbuf load error: $errmsg
119 $win->clear;
120 App::Chart::Gtk2::GUI::draw_text_centred ($self, undef, $errmsg);
121 return Gtk2::EVENT_PROPAGATE;
122 }
123  
124 my $pix_width = $pixbuf->get_width;
125 my $pix_height = $pixbuf->get_height;
126 ### pixbuf: "${pix_width}x${pix_height}"
127  
128 my ($x, $y, $alloc_width, $alloc_height) = $self->allocation->values;
129 ### alloc size: "${alloc_width}x${alloc_height} at $x,$y"
130  
131 # windowed
132 $x = 0; $y = 0;
133  
134 # align in allocated space, if alloc bigger than pixbuf
135 my $x_offset = max(0, ($alloc_width - $pix_width) / 2);
136 my $y_offset = max(0, ($alloc_height - $pix_height) / 2);
137  
138 # restrict to alloc width/height, in case pixbuf+pad is bigger than alloc
139 my $width = min ($alloc_width - $x_offset, $pix_width);
140 my $height = min ($alloc_height - $y_offset, $pix_height);
141  
142 my $gc = $self->get_style->bg_gc($self->state);
143 $gc->set_clip_region ($event->region);
144 $win->draw_pixbuf ($gc, $pixbuf,
145 0, 0, # source x,y
146 $x+$x_offset, $y+$y_offset, # dest x,y
147 $width, $height,
148 'normal', # dither
149 0, 0); # dither x,y
150  
151 my $region = $event->region->copy;
152 $region->subtract (Gtk2::Gdk::Region->rectangle
153 (Gtk2::Gdk::Rectangle->new
154 ($x+$x_offset, $y+$y_offset, $width, $height)));
155 $gc->set_clip_region ($region);
156 $win->draw_rectangle ($gc, 1, $event->area->values);
157  
158 $gc->set_clip_region (undef);
159 return Gtk2::EVENT_PROPAGATE;
160 }
161  
162 sub _load_pixbuf {
163 my ($self) = @_;
164 ### _load_pixbuf() ...
165  
166 my $symbol = $self->{'symbol'};
167 my $mode = $self->{'mode'};
168 if (! $symbol || ! $mode) { return __('(No data)'); }
169  
170 my $dbh = App::Chart::DBI->instance;
171 my $sth = $dbh->prepare_cached
172 ('SELECT image, error FROM intraday_image WHERE symbol=? AND mode=?');
173  
174 # Crib note: Some DBI 1.618 SQLite3 1.35 seems to hold a ref to the
175 # scalars passed to selectrow_array() until the next call. So use the
176 # local variables since holding onto $self->{'symbol'} looks like a leak.
177 #
178 my ($image, $error) = $dbh->selectrow_array ($sth, undef,
179 $symbol,
180 $mode);
181 $sth->finish();
182 if (! defined $image) { # error message in database
183 return $error || __('(No data)');
184 }
185  
186 my $loader = Gtk2::Gdk::PixbufLoader->new();
187 my $pixbuf;
188 if (eval {
189 $loader->write ($image);
190 $loader->close ();
191 $pixbuf = $loader->get_pixbuf;
192 1 }) {
193 return $pixbuf;
194 } else {
195 # Should be Glib::Error in $@ thrown by $loader, but allow for plain
196 # string too.
197 my $err = "$@";
198 unless (utf8::is_utf8($err)) { $err = Encode::decode('locale',$err); }
199 return $err;
200 }
201 }
202  
203 sub _do_intraday_changed {
204 my ($self, $symbol, $mode) = @_;
205 ### IntradayImage _do_intraday_changed(): "\"$symbol\" \"$mode\"\n"
206 if ($self->{'symbol'} eq $symbol && $self->{'mode'} eq $mode) {
207 # new image (or new no image)
208 delete $self->{'xor_background'}; # new colour scheme
209 $self->queue_resize;
210 $self->queue_draw;
211 }
212 }
213  
214 #-----------------------------------------------------------------------------
215 # pixbuf background
216  
217 sub Gtk2_Ex_Xor_background {
218 my ($self) = @_;
219 return ($self->{'xor_background'} ||= do {
220 require Gtk2::Ex::PixbufBits;
221 my $pixbuf = _load_pixbuf ($self);
222 if (! ref $pixbuf) {
223 # error loading, treat as widget background
224 return $self->get_style->bg($self->state);
225 }
226 my $rgbstr = Gtk2::Ex::PixbufBits::sampled_majority_color ($pixbuf);
227 ### IntradayImage xor background: $rgbstr
228 require App::Chart::Gtk2::Ex::GdkColorAlloc;
229 App::Chart::Gtk2::Ex::GdkColorAlloc->new (widget => $self,
230 color => $rgbstr);
231 });
232 }
233  
234 1;
235 __END__
« App/Chart/Gtk2/Ex/ListStoreDBISeq.pm ↑ Summary App/Chart/Gtk2/OpenDialog.pm »