| 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::SeriesModel; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Gtk2; | ||
| 22 | use Carp; | ||
| 23 | use Gtk2::Ex::TreeModel::ImplBits; | ||
| 24 | |||
| 25 | |||
| 26 | use Glib::Object::Subclass | ||
| 27 | 'Glib::Object', | ||
| 28 | interfaces => [ 'Gtk2::TreeModel' ], | ||
| 29 | properties => [ Glib::ParamSpec->scalar | ||
| 30 | ('series', | ||
| 31 | 'series', | ||
| 32 | 'The perl App::Chart::Series object to present.', | ||
| 33 | Glib::G_PARAM_READWRITE) | ||
| 34 | ]; | ||
| 35 | |||
| 36 | use constant { COL_DATE => 0, | ||
| 37 | COL_OPEN => 1, | ||
| 38 | COL_HIGH => 2, | ||
| 39 | COL_LOW => 3, | ||
| 40 | COL_CLOSE => 4, | ||
| 41 | COL_VOLUME => 5, | ||
| 42 | COL_OPENINT => 6, | ||
| 43 | NUM_COLUMNS => 7 }; | ||
| 44 | |||
| 45 | sub _validate_iter { | ||
| 46 | my ($self, $iter) = @_; | ||
| 47 | if (! defined $iter) { return; } | ||
| 48 | if ($iter->[0] != $self->{'stamp'}) { | ||
| 49 | croak "iter is not for this ".ref($self)." (id ", | ||
| 50 | $iter->[0]," want ",$self->{'stamp'},")\n"; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | sub INIT_INSTANCE { | ||
| 55 | my ($self) = @_; | ||
| 56 | Gtk2::Ex::TreeModel::ImplBits::random_stamp ($self); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub SET_PROPERTY { | ||
| 60 | my ($self, $pspec, $newval) = @_; | ||
| 61 | my $pname = $pspec->get_name; | ||
| 62 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 63 | |||
| 64 | if ($pname eq 'series') { | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | # gtk_tree_model_get_flags | ||
| 69 | # | ||
| 70 | sub GET_FLAGS { | ||
| 71 | return [ 'list-only' ]; | ||
| 72 | } | ||
| 73 | |||
| 74 | # gtk_tree_model_get_n_columns | ||
| 75 | # | ||
| 76 | sub GET_N_COLUMNS { | ||
| 77 | my ($self) = @_; | ||
| 78 | my $series = $self->{'series'}; | ||
| 79 | if ($series && $series->isa ('Series::OHLCVI')) { | ||
| 80 | return 7; | ||
| 81 | } else { | ||
| 82 | return 2; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | # gtk_tree_model_get_column_type | ||
| 87 | # | ||
| 88 | use constant GET_COLUMN_TYPE => 'Glib::String'; | ||
| 89 | |||
| 90 | # gtk_tree_model_get_iter | ||
| 91 | # | ||
| 92 | sub GET_ITER { | ||
| 93 | my ($self, $path) = @_; | ||
| 94 | $path->get_depth == 1 or return undef; | ||
| 95 | my ($n) = $path->get_indices; | ||
| 96 | return [ $self->{'stamp'}, $n, undef, undef ]; | ||
| 97 | } | ||
| 98 | |||
| 99 | # gtk_tree_model_get_path | ||
| 100 | # | ||
| 101 | sub GET_PATH { | ||
| 102 | my ($self, $iter) = @_; | ||
| 103 | _validate_iter ($self, $iter); | ||
| 104 | my $n = $iter->[1]; | ||
| 105 | return Gtk2::TreePath->new_from_indices ($n); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub data { | ||
| 109 | my ($self) = @_; | ||
| 110 | if (exists $self->{'data'}) { return $self->{'data'}; } | ||
| 111 | my $symbol = $self->{'symbol'}; | ||
| 112 | if (! defined $symbol) { return ($self->{'data'} = []); } | ||
| 113 | |||
| 114 | ### raw read: $symbol | ||
| 115 | require App::Chart::DBI; | ||
| 116 | my $dbh = App::Chart::DBI->instance; | ||
| 117 | my $sth = $dbh->prepare_cached | ||
| 118 | ('SELECT date, open, high, low, close, volume, openint | ||
| 119 | FROM daily WHERE symbol=? ORDER BY date ASC'); | ||
| 120 | my $data = $dbh->selectall_arrayref ($sth, undef, $symbol); | ||
| 121 | return ($self->{'data'} = $data); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub length { | ||
| 125 | my ($self) = @_; | ||
| 126 | my $series = $self->{'series'}; | ||
| 127 | if (! $series) { return 0; } | ||
| 128 | return $series->hi + 1; | ||
| 129 | } | ||
| 130 | |||
| 131 | # gtk_tree_model_get_value | ||
| 132 | # | ||
| 133 | my %col_to_array = (COL_OPEN, 'opens', | ||
| 134 | COL_HIGH, 'highs', | ||
| 135 | COL_LOW, 'lows', | ||
| 136 | COL_CLOSE, 'closes', | ||
| 137 | COL_VOLUME, 'volumes', | ||
| 138 | COL_OPENINT, 'openints'); | ||
| 139 | sub GET_VALUE { | ||
| 140 | my ($self, $iter, $col) = @_; | ||
| 141 | _validate_iter ($self, $iter); | ||
| 142 | my $n = $iter->[1]; | ||
| 143 | ### GET_VALUE: "$n,$col" | ||
| 144 | if ($n < 0) { return ''; } | ||
| 145 | |||
| 146 | my $series = $self->{'series'}; | ||
| 147 | if (! $series) { return ''; } | ||
| 148 | |||
| 149 | if ($col == 0) { | ||
| 150 | return $series->timebase->to_iso ($n); | ||
| 151 | } | ||
| 152 | $series->fill ($n, $n); | ||
| 153 | if (! $series->isa ('App::Chart::Series::OHLCVI')) { | ||
| 154 | return $series->values_array->[$n]; | ||
| 155 | } | ||
| 156 | my $array = $col_to_array{$col} // return ''; | ||
| 157 | return $series->array($array)->[$n]; | ||
| 158 | } | ||
| 159 | |||
| 160 | # gtk_tree_model_iter_next | ||
| 161 | # | ||
| 162 | sub ITER_NEXT { | ||
| 163 | my ($self, $iter) = @_; | ||
| 164 | _validate_iter ($self, $iter); | ||
| 165 | |||
| 166 | my $n = $iter->[1] + 1; | ||
| 167 | if ($n >= $self->length) { | ||
| 168 | # past last record | ||
| 169 | return undef; | ||
| 170 | } | ||
| 171 | return [ $self->{'stamp'}, $n, undef, undef ]; | ||
| 172 | } | ||
| 173 | |||
| 174 | # gtk_tree_model_iter_children | ||
| 175 | # | ||
| 176 | sub ITER_CHILDREN { | ||
| 177 | my ($self, $iter) = @_; | ||
| 178 | _validate_iter ($self, $iter); | ||
| 179 | |||
| 180 | if ($iter) { | ||
| 181 | # no children of any nodes | ||
| 182 | return undef; | ||
| 183 | } else { | ||
| 184 | # $iter==NULL means first toplevel | ||
| 185 | return [ $self->{'stamp'}, 0, undef, undef ]; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | # gtk_tree_model_iter_has_child | ||
| 190 | # Note Gtk2 prior to 1.183 demands numeric return (zero or non-zero). | ||
| 191 | # | ||
| 192 | use constant ITER_HAS_CHILD => 0; | ||
| 193 | |||
| 194 | # gtk_tree_model_iter_n_children | ||
| 195 | # | ||
| 196 | sub ITER_N_CHILDREN { | ||
| 197 | my ($self, $iter) = @_; | ||
| 198 | _validate_iter ($self, $iter); | ||
| 199 | |||
| 200 | if ($iter) { | ||
| 201 | # nothing under actual rows | ||
| 202 | return 0; | ||
| 203 | } else { | ||
| 204 | # $iter==NULL asks about toplevel | ||
| 205 | return $self->length; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | # gtk_tree_model_iter_nth_child | ||
| 210 | # | ||
| 211 | sub ITER_NTH_CHILD { | ||
| 212 | my ($self, $iter, $n) = @_; | ||
| 213 | _validate_iter ($self, $iter); | ||
| 214 | |||
| 215 | if ($iter) { | ||
| 216 | # nothing unde actual rows | ||
| 217 | return undef; | ||
| 218 | } else { | ||
| 219 | # $iter==NULL means nth toplevel | ||
| 220 | if ($n < 0 || $n >= $self->length) { | ||
| 221 | # out of range | ||
| 222 | return undef; | ||
| 223 | } | ||
| 224 | return [ $self->{'stamp'}, $n, undef, undef ]; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | # gtk_tree_model_iter_parent | ||
| 229 | # | ||
| 230 | sub ITER_PARENT { | ||
| 231 | my ($self, $iter) = @_; | ||
| 232 | _validate_iter ($self, $iter); | ||
| 233 | return undef; | ||
| 234 | } | ||
| 235 | |||
| 236 | my @field_name = qw(date open high low close volume openint); | ||
| 237 | |||
| 238 | sub set_value { | ||
| 239 | (@_ == 4) or croak 'SeriesModel::set_value(): wrong number of arguments'; | ||
| 240 | my ($self, $iterobj, $col, $value) = @_; | ||
| 241 | my $iter = $iterobj->to_arrayref ($self->{'stamp'}); | ||
| 242 | ### set_value: "$self $iter, $col, $value" | ||
| 243 | _validate_iter ($self, $iter); | ||
| 244 | my $n = $iter->[1]; | ||
| 245 | |||
| 246 | my $series = $self->{'series'}; | ||
| 247 | if (! $series->isa ('App::Chart::Series::Database')) { | ||
| 248 | croak "Can only change Database series"; | ||
| 249 | } | ||
| 250 | my $symbol = $series->symbol || croak "No symbol in series"; | ||
| 251 | |||
| 252 | my $date = $series->timebase->to_iso ($n); | ||
| 253 | my $field = $field_name[$col]; | ||
| 254 | |||
| 255 | require App::Chart::Database; | ||
| 256 | require App::Chart::DBI; | ||
| 257 | my $dbh = App::Chart::DBI->instance; | ||
| 258 | ### write: "$symbol $date $field $value" | ||
| 259 | |||
| 260 | App::Chart::Database::call_with_transaction | ||
| 261 | ($dbh, sub { | ||
| 262 | if (! App::Chart::DBI->read_single | ||
| 263 | ('SELECT symbol FROM daily WHERE symbol=? AND date=?', | ||
| 264 | $symbol, $date)) { | ||
| 265 | $dbh->do ('INSERT INTO daily (symbol, date) VALUES (?,?)', | ||
| 266 | undef, # attrs | ||
| 267 | $symbol, $date); | ||
| 268 | } | ||
| 269 | my $sth = $dbh->prepare_cached | ||
| 270 | ("UPDATE daily SET $field=? WHERE symbol=? AND date=?"); | ||
| 271 | $sth->execute ($value, $symbol, $date); | ||
| 272 | $sth->finish; | ||
| 273 | }); | ||
| 274 | $series->{'fill_set'}->remove ($n); | ||
| 275 | |||
| 276 | App::Chart::chart_dirbroadcast()->send ('data-changed', { $symbol => 1 }); | ||
| 277 | $self->row_changed ($self->get_path($iterobj), $iterobj); | ||
| 278 | } | ||
| 279 | |||
| 280 | 1; | ||
| 281 | __END__ |