| 1 | # Copyright 2007, 2008, 2009, 2010, 2011 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::SeriesTreeView; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Gtk2; | ||
| 22 | use Locale::TextDomain ('App-Chart'); | ||
| 23 | use List::Util; | ||
| 24 | |||
| 25 | use Gtk2::Ex::Units; | ||
| 26 | use App::Chart::Database; | ||
| 27 | use App::Chart::Intraday; | ||
| 28 | use App::Chart::Gtk2::SeriesModel; | ||
| 29 | use App::Chart::Gtk2::Ex::CellRendererTextBits; | ||
| 30 | |||
| 31 | use constant DEBUG => 0; | ||
| 32 | |||
| 33 | use Glib::Object::Subclass | ||
| 34 | 'Gtk2::TreeView', | ||
| 35 | properties => [Glib::ParamSpec->scalar | ||
| 36 | ('series', | ||
| 37 | 'series', | ||
| 38 | 'Blurb.', | ||
| 39 | Glib::G_PARAM_READWRITE) | ||
| 40 | ]; | ||
| 41 | |||
| 42 | |||
| 43 | sub INIT_INSTANCE { | ||
| 44 | my ($self) = @_; | ||
| 45 | App::Chart::chart_dirbroadcast()->connect_for_object | ||
| 46 | ('data-changed', \&_do_data_changed, $self); | ||
| 47 | |||
| 48 | $self->set (fixed_height_mode => 1); | ||
| 49 | |||
| 50 | my $em = Gtk2::Ex::Units::em($self); | ||
| 51 | { | ||
| 52 | my $renderer = Gtk2::CellRendererText->new; | ||
| 53 | $renderer->set (xalign => 0, | ||
| 54 | ypad => 0); | ||
| 55 | $renderer->set_fixed_height_from_font (1); | ||
| 56 | |||
| 57 | my $column = Gtk2::TreeViewColumn->new_with_attributes | ||
| 58 | (__('Date'), $renderer, text => App::Chart::Gtk2::SeriesModel::COL_DATE); | ||
| 59 | $column->set (sizing => 'fixed', | ||
| 60 | fixed_width => 10*$em, # eg. "2007-12-31" | ||
| 61 | resizable => 1); | ||
| 62 | $self->append_column ($column); | ||
| 63 | } | ||
| 64 | |||
| 65 | foreach my $elem ([ App::Chart::Gtk2::SeriesModel::COL_OPEN, __('Open'), 7 ], | ||
| 66 | [ App::Chart::Gtk2::SeriesModel::COL_HIGH, __('High'), 7 ], | ||
| 67 | [ App::Chart::Gtk2::SeriesModel::COL_LOW, __('Low'), 7 ], | ||
| 68 | [ App::Chart::Gtk2::SeriesModel::COL_CLOSE, __('Close'), 7 ], | ||
| 69 | [ App::Chart::Gtk2::SeriesModel::COL_VOLUME, __('Volume'), 10 ], | ||
| 70 | [ App::Chart::Gtk2::SeriesModel::COL_OPENINT, __('Openint'), 9 ], | ||
| 71 | ) { | ||
| 72 | my ($colnum, $heading, $width) = @$elem; | ||
| 73 | my $renderer = Gtk2::CellRendererText->new; | ||
| 74 | $renderer->set (xalign => 1, | ||
| 75 | ypad => 0, | ||
| 76 | editable => 1); | ||
| 77 | $renderer->set_fixed_height_from_font (1); | ||
| 78 | |||
| 79 | my $column = Gtk2::TreeViewColumn->new_with_attributes | ||
| 80 | ($heading, $renderer, text => $colnum); | ||
| 81 | $column->set (sizing => 'fixed', | ||
| 82 | fixed_width => $width * $em, | ||
| 83 | resizable => 1); | ||
| 84 | App::Chart::Gtk2::Ex::CellRendererTextBits::renderer_edited_set_value ($renderer, $column, $colnum); | ||
| 85 | $self->append_column ($column); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub SET_PROPERTY { | ||
| 90 | my ($self, $pspec, $newval) = @_; | ||
| 91 | my $pname = $pspec->get_name; | ||
| 92 | if (DEBUG) { print "SeriesTreeView: set $pname ",$newval//'undef',"\n"; } | ||
| 93 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 94 | |||
| 95 | if ($pname eq 'series') { | ||
| 96 | $self->refresh; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | sub refresh { | ||
| 101 | my ($self) = @_; | ||
| 102 | my $series = $self->{'series'}; | ||
| 103 | if (DEBUG) { print "SeriesTreeView refresh ",$series//'undef',"\n"; } | ||
| 104 | |||
| 105 | require Gtk2::Ex::WidgetCursor; | ||
| 106 | Gtk2::Ex::WidgetCursor->busy; | ||
| 107 | my $model = $self->{'model'} | ||
| 108 | = App::Chart::Gtk2::SeriesModel->new (series => $series); | ||
| 109 | $self->set_model ($model); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub _do_entry_activate { | ||
| 113 | my ($entry, $self) = @_; | ||
| 114 | $self->set_symbol ($entry->get_text); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _do_data_changed { | ||
| 118 | my ($self, $symbol_hash) = @_; | ||
| 119 | my $symbol = $self->{'symbol'} // return; | ||
| 120 | if (exists $symbol_hash->{$symbol}) { | ||
| 121 | $self->refresh; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | 1; | ||
| 126 | __END__ |