| 1 | # Copyright 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 | |||
| 18 | package App::Chart::Gtk2::SymlistTreeModel; | ||
| 19 | use 5.008; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Glib; | ||
| 23 | use Gtk2; | ||
| 24 | use Carp; | ||
| 25 | use Gtk2::Ex::TreeModel::ImplBits; | ||
| 26 | |||
| 27 | use App::Chart::Database; | ||
| 28 | |||
| 29 | use Glib::Object::Subclass | ||
| 30 | 'Glib::Object', | ||
| 31 | interfaces => [ Gtk2::TreeModel:: ]; | ||
| 32 | |||
| 33 | use constant DEBUG => 0; | ||
| 34 | |||
| 35 | use constant { COL_ITEM_SYMBOL => 0, | ||
| 36 | COL_ITEM_COMMENT => 1, | ||
| 37 | COL_SYMLIST_KEY => 0, | ||
| 38 | COL_SYMLIST_NAME => 1, | ||
| 39 | COL_SYMLIST_CONDITION => 2, | ||
| 40 | NUM_COLUMNS => 3 | ||
| 41 | }; | ||
| 42 | |||
| 43 | # created on first use, then kept while still in use | ||
| 44 | use base 'Class::WeakSingleton'; | ||
| 45 | *_new_instance = \&Glib::Object::new; | ||
| 46 | |||
| 47 | |||
| 48 | #------------------------------------------------------------------------------ | ||
| 49 | sub symlist_pos_to_key { | ||
| 50 | my ($listnum) = @_; | ||
| 51 | return App::Chart::Database::read_notes_single | ||
| 52 | ('SELECT key FROM symlist WHERE seq=?', $listnum); | ||
| 53 | } | ||
| 54 | sub symlist_key_to_pos { | ||
| 55 | my ($key) = @_; | ||
| 56 | return App::Chart::Database::read_notes_single | ||
| 57 | ('SELECT seq FROM symlist WHERE key=?', $key); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub symlist_content_pos_exists { | ||
| 61 | my ($key, $pos) = @_; | ||
| 62 | return App::Chart::Database::read_notes_single | ||
| 63 | ('SELECT symbol FROM symlist_content WHERE key=? AND seq=?', | ||
| 64 | $key, $pos); | ||
| 65 | } | ||
| 66 | |||
| 67 | #------------------------------------------------------------------------------ | ||
| 68 | |||
| 69 | # 'symlist-list-inserted' broadcast handler | ||
| 70 | sub _do_symlist_inserted { | ||
| 71 | my ($self, $pos, $key) = @_; | ||
| 72 | if (DEBUG) { print "SymlistTreeModel list row added\n"; } | ||
| 73 | my $path = Gtk2::TreePath->new_from_indices ($pos); | ||
| 74 | $self->row_inserted ($path, $self->get_iter($path)); | ||
| 75 | } | ||
| 76 | # 'symlist-list-deleted' broadcast handler | ||
| 77 | sub _do_symlist_deleted { | ||
| 78 | my ($self, $key, $pos) = @_; | ||
| 79 | if (DEBUG) { print "SymlistTreeModel list row deleted\n"; } | ||
| 80 | my $path = Gtk2::TreePath->new_from_indices ($pos); | ||
| 81 | $self->row_deleted ($path); | ||
| 82 | } | ||
| 83 | |||
| 84 | # 'symlist-content-inserted' broadcast handler | ||
| 85 | sub _do_symlist_content_inserted { | ||
| 86 | my ($self, $key, $pos) = @_; | ||
| 87 | if (DEBUG) { print "SymlistTreeModel symbol row added\n"; } | ||
| 88 | my $listnum = symlist_key_to_pos ($key); | ||
| 89 | my $path = Gtk2::TreePath->new_from_indices ($listnum, $pos); | ||
| 90 | my $iter = $self->get_iter ($path); | ||
| 91 | $self->row_inserted ($path, $iter); | ||
| 92 | |||
| 93 | # if this symlist now has 1 entry then has_child toggled | ||
| 94 | if (! symlist_content_pos_exists ($key, 1)) { | ||
| 95 | my $parent_path = Gtk2::TreePath->new_from_indices ($listnum); | ||
| 96 | my $parent_iter = $self->get_iter ($path); | ||
| 97 | $self->row_has_child_toggled ($parent_path, $parent_iter); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | # 'symlist-content-deleted' broadcast handler | ||
| 102 | sub _do_symlist_content_deleted { | ||
| 103 | my ($self, $key, $pos) = @_; | ||
| 104 | if (DEBUG) { print "SymlistTreeModel symbol row deleted\n"; } | ||
| 105 | my $path = Gtk2::TreePath->new_from_indices (symlist_key_to_pos($key), $pos); | ||
| 106 | $self->row_deleted ($path); | ||
| 107 | |||
| 108 | # if this symlist now has 0 entries then has_child toggled | ||
| 109 | if (! symlist_content_pos_exists ($key, 0)) { | ||
| 110 | my $listnum = symlist_key_to_pos ($key); | ||
| 111 | my $parent_path = Gtk2::TreePath->new_from_indices ($listnum); | ||
| 112 | my $parent_iter = $self->get_iter ($path); | ||
| 113 | $self->row_has_child_toggled ($parent_path, $parent_iter); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | #------------------------------------------------------------------------------ | ||
| 118 | |||
| 119 | use constant ITEM_SHIFT => 10; | ||
| 120 | use constant LIST_MASK => (1 << ITEM_SHIFT) - 1; | ||
| 121 | sub _iter_to_coords { | ||
| 122 | my ($self, $iter) = @_; | ||
| 123 | if (! defined $iter) { return (undef, undef); } | ||
| 124 | if ($iter->[0] != $self->{'stamp'}) { | ||
| 125 | croak "iter is not for this ".ref($self)." (id ", | ||
| 126 | $iter->[0]," want ",$self->{'stamp'},")\n"; | ||
| 127 | } | ||
| 128 | use integer; | ||
| 129 | my $listnum = $iter->[1] & LIST_MASK; | ||
| 130 | my $itemnum = $iter->[1] >> ITEM_SHIFT; | ||
| 131 | if (DEBUG) { printf " _iter_to_coords %x -> %d,%d\n", | ||
| 132 | $iter->[1], $listnum, $itemnum; } | ||
| 133 | if ($itemnum == -1) { $itemnum = undef; } | ||
| 134 | return ($listnum, $itemnum); | ||
| 135 | } | ||
| 136 | sub _coords_to_iter { | ||
| 137 | my ($self, $listnum, $itemnum) = @_; | ||
| 138 | if (! defined $itemnum) { $itemnum = -1; } | ||
| 139 | if (DEBUG) { printf " _coords_to iter %d,%d -> %x\n", | ||
| 140 | $listnum, $itemnum, ($itemnum << ITEM_SHIFT) + $listnum; } | ||
| 141 | return [ $self->{'stamp'}, ($itemnum << ITEM_SHIFT) + $listnum, undef, undef ]; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub INIT_INSTANCE { | ||
| 145 | my ($self) = @_; | ||
| 146 | Gtk2::Ex::TreeModel::ImplBits::random_stamp ($self); | ||
| 147 | |||
| 148 | App::Chart::chart_dirbroadcast()->connect_for_object | ||
| 149 | ('symlist-list-inserted', \&_do_symlist_inserted, $self); | ||
| 150 | App::Chart::chart_dirbroadcast()->connect_for_object | ||
| 151 | ('symlist-list-deleted', \&_do_symlist_deleted, $self); | ||
| 152 | App::Chart::chart_dirbroadcast()->connect_for_object | ||
| 153 | ('symlist-content-inserted', \&_do_symlist_content_inserted, $self); | ||
| 154 | App::Chart::chart_dirbroadcast()->connect_for_object | ||
| 155 | ('symlist-content-deleted', \&_do_symlist_content_deleted, $self); | ||
| 156 | } | ||
| 157 | |||
| 158 | # gtk_tree_model_get_flags | ||
| 159 | # | ||
| 160 | sub GET_FLAGS { | ||
| 161 | return [ ]; | ||
| 162 | } | ||
| 163 | |||
| 164 | # gtk_tree_model_get_n_columns | ||
| 165 | # | ||
| 166 | sub GET_N_COLUMNS { | ||
| 167 | return NUM_COLUMNS; | ||
| 168 | } | ||
| 169 | |||
| 170 | # gtk_tree_model_get_column_type | ||
| 171 | # | ||
| 172 | sub GET_COLUMN_TYPE { | ||
| 173 | return 'Glib::String'; | ||
| 174 | } | ||
| 175 | |||
| 176 | # gtk_tree_model_get_iter | ||
| 177 | # | ||
| 178 | sub GET_ITER { | ||
| 179 | my ($self, $path) = @_; | ||
| 180 | if (DEBUG) { print "get_iter\n"; } | ||
| 181 | if ($path->get_depth > 2) { return undef; } | ||
| 182 | my ($listnum, $itemnum) = $path->get_indices; | ||
| 183 | return _coords_to_iter ($self, $listnum, $itemnum); | ||
| 184 | } | ||
| 185 | |||
| 186 | # gtk_tree_model_get_path | ||
| 187 | # | ||
| 188 | sub GET_PATH { | ||
| 189 | my ($self, $iter) = @_; | ||
| 190 | if (DEBUG) { print "get_path\n"; } | ||
| 191 | my ($listnum, $itemnum) = _iter_to_coords ($self, $iter); | ||
| 192 | if (defined $itemnum) { | ||
| 193 | return Gtk2::TreePath->new_from_indices ($listnum, $itemnum); | ||
| 194 | } else { | ||
| 195 | return Gtk2::TreePath->new_from_indices ($listnum); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | # gtk_tree_model_get_value | ||
| 200 | # | ||
| 201 | sub GET_VALUE { | ||
| 202 | my ($self, $iter, $col) = @_; | ||
| 203 | if (DEBUG) { print "get_value\n"; } | ||
| 204 | my ($listnum, $itemnum) = _iter_to_coords ($self, $iter); | ||
| 205 | |||
| 206 | if (! defined $itemnum) { | ||
| 207 | if ($col == COL_SYMLIST_NAME) { | ||
| 208 | return App::Chart::Database::read_notes_single | ||
| 209 | ('SELECT name FROM symlist WHERE seq=?', $listnum); | ||
| 210 | } elsif ($col == COL_SYMLIST_KEY) { | ||
| 211 | return App::Chart::Database::read_notes_single | ||
| 212 | ('SELECT key FROM symlist WHERE seq=?', $listnum); | ||
| 213 | } elsif ($col == COL_SYMLIST_CONDITION) { | ||
| 214 | return App::Chart::Database::read_notes_single | ||
| 215 | ('SELECT condition FROM symlist WHERE seq=?', $listnum); | ||
| 216 | } | ||
| 217 | } else { | ||
| 218 | my $listkey = symlist_pos_to_key ($listnum); | ||
| 219 | if ($col == COL_ITEM_SYMBOL) { | ||
| 220 | return App::Chart::Database::read_notes_single | ||
| 221 | ('SELECT symbol FROM symlist_content WHERE key=? AND seq=?', | ||
| 222 | $listkey, $itemnum); | ||
| 223 | } elsif ($col == COL_ITEM_COMMENT) { | ||
| 224 | return App::Chart::Database::read_notes_single | ||
| 225 | ('SELECT comment FROM symlist_content WHERE key=? AND seq=?', | ||
| 226 | $listkey, $itemnum); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | return undef; | ||
| 230 | } | ||
| 231 | |||
| 232 | # gtk_tree_model_iter_next | ||
| 233 | # | ||
| 234 | sub ITER_NEXT { | ||
| 235 | my ($self, $iter) = @_; | ||
| 236 | if (DEBUG) { print "iter_next\n"; } | ||
| 237 | my ($listnum, $itemnum) = _iter_to_coords ($self, $iter); | ||
| 238 | |||
| 239 | if (defined $itemnum) { | ||
| 240 | return $self->ITER_NTH_CHILD ($self->ITER_PARENT($iter), $itemnum+1); | ||
| 241 | } else { | ||
| 242 | return $self->ITER_NTH_CHILD (undef, $listnum+1); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | # gtk_tree_model_iter_children | ||
| 247 | # | ||
| 248 | sub ITER_CHILDREN { | ||
| 249 | my ($self, $iter) = @_; | ||
| 250 | return $self->ITER_NTH_CHILD ($iter, 0); | ||
| 251 | } | ||
| 252 | |||
| 253 | # gtk_tree_model_iter_has_child | ||
| 254 | # Note Gtk2 prior to 1.183 demands numeric return (zero or non-zero). | ||
| 255 | # | ||
| 256 | sub ITER_HAS_CHILD { | ||
| 257 | my ($self, $iter) = @_; | ||
| 258 | return $self->ITER_NTH_CHILD ($iter,0) ? 1 : 0; | ||
| 259 | } | ||
| 260 | |||
| 261 | # gtk_tree_model_iter_n_children | ||
| 262 | # | ||
| 263 | sub ITER_N_CHILDREN { | ||
| 264 | my ($self, $iter) = @_; | ||
| 265 | if (DEBUG) { print "iter_n_children\n"; } | ||
| 266 | my ($listnum, $itemnum) = _iter_to_coords ($self, $iter); | ||
| 267 | |||
| 268 | if (! defined $listnum) { | ||
| 269 | # toplevel, how many symlists | ||
| 270 | return App::Chart::Gtk2::Symlist::symlist_num(); | ||
| 271 | } | ||
| 272 | if (! defined $itemnum) { | ||
| 273 | # symlist, how many elements | ||
| 274 | return App::Chart::Gtk2::Symlist::symlist_length (symlist_pos_to_key ($listnum)); | ||
| 275 | } | ||
| 276 | # items have no children | ||
| 277 | return 0; | ||
| 278 | } | ||
| 279 | |||
| 280 | # gtk_tree_model_iter_nth_child | ||
| 281 | # | ||
| 282 | sub ITER_NTH_CHILD { | ||
| 283 | my ($self, $iter, $n) = @_; | ||
| 284 | if (DEBUG) { print "iter_nth_child $n\n"; } | ||
| 285 | my ($listnum, $itemnum) = _iter_to_coords ($self, $iter); | ||
| 286 | |||
| 287 | if (! defined $listnum) { | ||
| 288 | if (DEBUG) { print " toplevel\n"; } | ||
| 289 | if (App::Chart::Database::read_notes_single | ||
| 290 | ('SELECT key FROM symlist WHERE seq=?', $n)) { | ||
| 291 | return _coords_to_iter ($self, $n, undef); | ||
| 292 | } else { | ||
| 293 | return undef; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | |||
| 297 | if (! defined $itemnum) { | ||
| 298 | my $listkey = symlist_pos_to_key ($listnum); | ||
| 299 | if (DEBUG) { print " of list $listkey\n"; } | ||
| 300 | if (symlist_content_pos_exists ($listkey, $n)) { | ||
| 301 | return _coords_to_iter ($self, $listnum, $n); | ||
| 302 | } else { | ||
| 303 | if (DEBUG) { print " not available\n"; } | ||
| 304 | return undef; | ||
| 305 | } | ||
| 306 | } | ||
| 307 | # items have no children | ||
| 308 | if (DEBUG) { print " no children of items\n"; } | ||
| 309 | return undef; | ||
| 310 | } | ||
| 311 | |||
| 312 | # gtk_tree_model_iter_parent | ||
| 313 | # | ||
| 314 | sub ITER_PARENT { | ||
| 315 | my ($self, $iter) = @_; | ||
| 316 | my ($listnum, $itemnum) = _iter_to_coords ($self, $iter); | ||
| 317 | if (! defined $itemnum) { | ||
| 318 | return undef; | ||
| 319 | } else { | ||
| 320 | return _coords_to_iter ($self, $listnum, undef); | ||
| 321 | } | ||
| 322 | } | ||
| 323 | |||
| 324 | #------------------------------------------------------------------------------ | ||
| 325 | |||
| 326 | sub path_for_key { | ||
| 327 | my ($model, $key) = @_; | ||
| 328 | my $seq = App::Chart::Database::read_notes_single | ||
| 329 | ('SELECT seq FROM symlist WHERE key=?', $key); | ||
| 330 | if (! defined $seq) { return undef; } | ||
| 331 | return Gtk2::TreePath->new_from_indices ($seq); | ||
| 332 | } | ||
| 333 | |||
| 334 | sub path_for_symbol_and_symlist { | ||
| 335 | my ($self, $symbol, $symlist) = @_; | ||
| 336 | my $listnum = App::Chart::Database::read_notes_single | ||
| 337 | ('SELECT seq FROM symlist WHERE key=?', $symlist->key); | ||
| 338 | if (! defined $listnum) { return undef; } | ||
| 339 | |||
| 340 | my $itemnum = App::Chart::Database::read_notes_single | ||
| 341 | ('SELECT seq FROM symlist_content WHERE key=? AND symbol=?', | ||
| 342 | $symlist->key, $symbol); | ||
| 343 | if (! defined $itemnum) { return undef; } | ||
| 344 | |||
| 345 | return Gtk2::TreePath->new_from_indices ($listnum, $itemnum); | ||
| 346 | } | ||
| 347 | |||
| 348 | 1; | ||
| 349 | __END__ |