| 1 | # Copyright 2007, 2008, 2009, 2010, 2011, 2017 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 | |||
| 18 | package App::Chart::Gtk2::TickerModel; | ||
| 19 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 0 | ▶ | use Gtk2 1.200; # for working TreeModelFilter modify_func |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 23 | use Carp; | ||
| 24 | use Locale::TextDomain ('App-Chart'); | ||
| 25 | |||
| 26 | use Gtk2::Ex::TreeModelFilter::Draggable; | ||
| 27 | use App::Chart; | ||
| 28 | |||
| 29 | |||
| 30 | use Glib::Object::Subclass | ||
| 31 | 'Gtk2::Ex::TreeModelFilter::Draggable'; | ||
| 32 | |||
| 33 | # uncomment this to run the ### lines | ||
| 34 | # use Smart::Comments; | ||
| 35 | |||
| 36 | use constant { UP_SPAN => '<span foreground="green">', | ||
| 37 | |||
| 38 | # a brightish red, for contrast against a black background | ||
| 39 | DOWN_SPAN => '<span foreground="#FF7070">', | ||
| 40 | |||
| 41 | INPROGRESS_SPAN => '<span foreground="light blue">' }; | ||
| 42 | |||
| 43 | sub new { | ||
| 44 | my ($class, $symlist) = @_; | ||
| 45 | |||
| 46 | # FIXME: As of Gtk2-Perl 1.201 Gtk2::TreeModelFilter::new() leaks a | ||
| 47 | # reference (its returned object is never destroyed), so go through | ||
| 48 | # Glib::Object::new() instead. Can switch to SUPER::new when ready to | ||
| 49 | # depend on a fixed Gtk2-Perl. | ||
| 50 | # | ||
| 51 | my $self = Glib::Object::new ($class, child_model => $symlist); | ||
| 52 | |||
| 53 | $self->{'symlist'} = $symlist; | ||
| 54 | $self->set_modify_func ([ 'Glib::String' ], \&_model_filter_func); | ||
| 55 | App::Chart::chart_dirbroadcast()->connect_for_object | ||
| 56 | ('latest-changed', \&_do_latest_changed, $self); | ||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | # 'latest-changed' from DirBroadcast | ||
| 61 | sub _do_latest_changed { | ||
| 62 | my ($self, $changed) = @_; | ||
| 63 | ### Ticker: latest-changed: keys %$changed | ||
| 64 | |||
| 65 | my $symlist = $self->{'symlist'}; | ||
| 66 | $symlist->foreach (sub { | ||
| 67 | my ($self, $path, $iter) = @_; | ||
| 68 | my $symbol = $self->get_value($iter,0); | ||
| 69 | if (exists $changed->{$symbol}) { | ||
| 70 | ### Ticker: changed: $symbol, $path->to_string | ||
| 71 | $self->row_changed ($path, $iter); | ||
| 72 | } | ||
| 73 | return 0; # keep iterating | ||
| 74 | }); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _model_filter_func { | ||
| 78 | my ($self, $iter, $col) = @_; | ||
| 79 | |||
| 80 | my $child_model = $self->get_model; | ||
| 81 | my $child_iter = $self->convert_iter_to_child_iter ($iter); | ||
| 82 | my $symbol = $child_model->get_value ($child_iter, 0); | ||
| 83 | |||
| 84 | require App::Chart::Latest; | ||
| 85 | my $latest = App::Chart::Latest->get ($symbol); | ||
| 86 | return _form ($self, $latest); | ||
| 87 | } | ||
| 88 | |||
| 89 | # return a pango markup string to display for $latest | ||
| 90 | sub _form { | ||
| 91 | my ($self, $latest) = @_; | ||
| 92 | |||
| 93 | return ($latest->{__PACKAGE__.'.form'} ||= do { | ||
| 94 | my $symbol = $latest->{'symbol'}; | ||
| 95 | my $last = $latest->{'last'} | ||
| 96 | // return $symbol . ' ' . ($latest->{'note'} || __('no data')); | ||
| 97 | my $nf = App::Chart::number_formatter(); | ||
| 98 | my $str = $symbol . ' ' | ||
| 99 | . $nf->format_number ($last, App::Chart::count_decimals($last), 1); | ||
| 100 | |||
| 101 | my $change = $latest->{'change'}; | ||
| 102 | if ($latest->{'halt'}) { $str .= ' '.__('halt'); } | ||
| 103 | elsif ($latest->{'limit_up'}) { $str .= ' '.__('limit up'); } | ||
| 104 | elsif ($latest->{'limit_down'}) { $str .= ' '.__('limit down'); } | ||
| 105 | elsif (! defined $change) { } # nothing added for undef | ||
| 106 | elsif ($change == 0) { $str .= ' '.__('unch'); } | ||
| 107 | else { | ||
| 108 | $str .= ' ' . ($change > 0 ? '+' : '') | ||
| 109 | . $nf->format_number ($change,App::Chart::count_decimals($change), 1); | ||
| 110 | } | ||
| 111 | |||
| 112 | my $span; | ||
| 113 | if ($App::Chart::Gtk2::Job::Latest::inprogress{$symbol}) { | ||
| 114 | $span = INPROGRESS_SPAN; | ||
| 115 | } elsif ($latest->{'inprogress'}) { | ||
| 116 | $span = INPROGRESS_SPAN; | ||
| 117 | } elsif (defined $change && $change>0) { | ||
| 118 | $span = UP_SPAN; | ||
| 119 | } elsif (defined $change && $change<0) { | ||
| 120 | $span = DOWN_SPAN; | ||
| 121 | } | ||
| 122 | if ($span) { | ||
| 123 | $str = $span . $str . '</span>'; | ||
| 124 | } | ||
| 125 | $str; | ||
| 126 | }); | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; | ||
| 130 | __END__ | ||