| 1 | # Copyright 2007, 2008, 2009, 2010, 2011, 2024 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::OpenModel; | ||
| 19 | use 5.010; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Gtk2 1.190; # for working TreeModelFilter modify_func | ||
| 23 | use Carp; | ||
| 24 | |||
| 25 | use App::Chart; | ||
| 26 | use App::Chart::Gtk2::Symlist; | ||
| 27 | |||
| 28 | use constant DEBUG => 0; | ||
| 29 | |||
| 30 | use App::Chart::Gtk2::Ex::ListOfListsModel; | ||
| 31 | use Glib::Object::Subclass | ||
| 32 | 'App::Chart::Gtk2::Ex::ListOfListsModel'; | ||
| 33 | |||
| 34 | use base 'Class::WeakSingleton'; | ||
| 35 | *_new_instance = \&Glib::Object::new; | ||
| 36 | |||
| 37 | use constant { | ||
| 38 | COL_SYMLIST_OBJECT => 0, | ||
| 39 | COL_SYMLIST_NAME => 1, | ||
| 40 | COL_ITEM_SYMBOL => 2 + App::Chart::Gtk2::Symlist->COL_SYMBOL, | ||
| 41 | }; | ||
| 42 | |||
| 43 | sub INIT_INSTANCE { | ||
| 44 | my ($self) = @_; | ||
| 45 | |||
| 46 | require App::Chart::Gtk2::SymlistListModel; | ||
| 47 | my $symlist_model = App::Chart::Gtk2::SymlistListModel->instance; | ||
| 48 | |||
| 49 | require Gtk2::Ex::TreeModelFilter::Draggable; | ||
| 50 | my $symlist_filter | ||
| 51 | = Gtk2::Ex::TreeModelFilter::Draggable->new ($symlist_model); | ||
| 52 | $symlist_filter->set_modify_func (['App::Chart::Gtk2::Symlist','Glib::String'], | ||
| 53 | \&_symlist_filter_func); | ||
| 54 | |||
| 55 | $self->set (list_model => $symlist_filter); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _symlist_filter_func { | ||
| 59 | my ($symlist_filter, $iter, $col) = @_; | ||
| 60 | if (DEBUG) { print "_symlist_filter_func iter=$iter col=$col\n"; | ||
| 61 | my $path = $symlist_filter->get_path ($iter); | ||
| 62 | print " path=", defined $path ? $path->to_string : 'undef', | ||
| 63 | "\n"; | ||
| 64 | } | ||
| 65 | my $symlist_model = $symlist_filter->get_model; | ||
| 66 | # my $subiter = $symlist_filter->convert_iter_to_child_iter ($iter); | ||
| 67 | |||
| 68 | # avoid strange trouble with convert_iter .... | ||
| 69 | my $path = $symlist_filter->get_path ($iter); | ||
| 70 | my $subiter = $symlist_model->get_iter ($path) || return undef; | ||
| 71 | |||
| 72 | if (DEBUG) { print " submodel $symlist_model subiter $subiter\n"; | ||
| 73 | print " iter_is_valid ", | ||
| 74 | $symlist_model->iter_is_valid($subiter)?"yes":"no", | ||
| 75 | "\n"; | ||
| 76 | my $subpath = $symlist_model->get_path ($subiter); | ||
| 77 | print " subpath=", | ||
| 78 | (defined $subpath ? $subpath->to_string : 'undef'), | ||
| 79 | "\n"; | ||
| 80 | } | ||
| 81 | |||
| 82 | if ($col == COL_SYMLIST_OBJECT) { | ||
| 83 | my $key = $symlist_model->get_value ($subiter, $symlist_model->COL_KEY); | ||
| 84 | if (DEBUG) { print " key ",$key//'undef',"\n"; } | ||
| 85 | return App::Chart::Gtk2::Symlist->new_from_key ($key); | ||
| 86 | |||
| 87 | } elsif ($col == COL_SYMLIST_NAME) { | ||
| 88 | return $symlist_model->get_value ($subiter, $symlist_model->COL_NAME); | ||
| 89 | |||
| 90 | } else { | ||
| 91 | return undef; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | sub path_for_key { | ||
| 96 | my ($self, $key) = @_; | ||
| 97 | my $symlist = App::Chart::Gtk2::Symlist->new_from_key ($key); | ||
| 98 | return $self->path_for_model ($symlist); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub path_for_symbol_and_symlist { | ||
| 102 | my ($self, $symbol, $symlist) = @_; | ||
| 103 | my $path = $self->path_for_model ($symlist) || return undef; | ||
| 104 | my $sub_index = $symlist->find_symbol_pos ($symbol); | ||
| 105 | if (! defined $sub_index) { return undef; } | ||
| 106 | $path->append_index ($sub_index); | ||
| 107 | return $path; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | 1; | ||
| 112 | __END__ |