App-Chart

 view release on metacpan or  search on metacpan

lib/App/Chart/Gtk2/TickerModel.pm  view on Meta::CPAN

# Copyright 2007, 2008, 2009, 2010, 2011, 2017 Kevin Ryde

# This file is part of Chart.
#
# Chart is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3, or (at your option) any later version.
#
# Chart is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Chart.  If not, see <http://www.gnu.org/licenses/>.


package App::Chart::Gtk2::TickerModel;
use 5.010;
use strict;
use warnings;
use Gtk2 1.200; # for working TreeModelFilter modify_func
use Carp;
use Locale::TextDomain ('App-Chart');

use Gtk2::Ex::TreeModelFilter::Draggable;
use App::Chart;


use Glib::Object::Subclass
  'Gtk2::Ex::TreeModelFilter::Draggable';

# uncomment this to run the ### lines
# use Smart::Comments;

use constant { UP_SPAN => '<span foreground="green">',

               # a brightish red, for contrast against a black background
               DOWN_SPAN => '<span foreground="#FF7070">',

               INPROGRESS_SPAN => '<span foreground="light blue">' };

sub new {
  my ($class, $symlist) = @_;

  # FIXME: As of Gtk2-Perl 1.201 Gtk2::TreeModelFilter::new() leaks a
  # reference (its returned object is never destroyed), so go through
  # Glib::Object::new() instead.  Can switch to SUPER::new when ready to
  # depend on a fixed Gtk2-Perl.
  #
  my $self = Glib::Object::new ($class, child_model => $symlist);

  $self->{'symlist'} = $symlist;
  $self->set_modify_func ([ 'Glib::String' ], \&_model_filter_func);
  App::Chart::chart_dirbroadcast()->connect_for_object
      ('latest-changed', \&_do_latest_changed, $self);
  return $self;
}

# 'latest-changed' from DirBroadcast
sub _do_latest_changed {
  my ($self, $changed) = @_;
  ### Ticker: latest-changed: keys %$changed

  my $symlist = $self->{'symlist'};
  $symlist->foreach (sub {
                       my ($self, $path, $iter) = @_;
                       my $symbol = $self->get_value($iter,0);
                       if (exists $changed->{$symbol}) {
                         ### Ticker: changed: $symbol, $path->to_string
                         $self->row_changed ($path, $iter);
                       }
                       return 0; # keep iterating
                     });
}

sub _model_filter_func {
  my ($self, $iter, $col) = @_;

  my $child_model = $self->get_model;
  my $child_iter = $self->convert_iter_to_child_iter ($iter);
  my $symbol = $child_model->get_value ($child_iter, 0);

  require App::Chart::Latest;
  my $latest = App::Chart::Latest->get ($symbol);
  return _form ($self, $latest);
}

# return a pango markup string to display for $latest
sub _form {
  my ($self, $latest) = @_;

  return ($latest->{__PACKAGE__.'.form'} ||= do {
    my $symbol = $latest->{'symbol'};
    my $last = $latest->{'last'}
      // return $symbol . ' ' . ($latest->{'note'} || __('no data'));
    my $nf = App::Chart::number_formatter();
    my $str = $symbol . ' '



( run in 0.633 second using v1.01-cache-2.11-cpan-f56aa216473 )