| 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 | |||
| 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) { say " submodel $symlist_model subiter $subiter"; | ||
| 73 | say " iter_is_valid ", | ||
| 74 | $symlist_model->iter_is_valid($subiter)?"yes":"no"; | ||
| 75 | my $subpath = $symlist_model->get_path ($subiter); | ||
| 76 | say " subpath=", | ||
| 77 | (defined $subpath ? $subpath->to_string : 'undef'); | ||
| 78 | } | ||
| 79 | |||
| 80 | if ($col == COL_SYMLIST_OBJECT) { | ||
| 81 | my $key = $symlist_model->get_value ($subiter, $symlist_model->COL_KEY); | ||
| 82 | if (DEBUG) { print " key ",$key//'undef',"\n"; } | ||
| 83 | return App::Chart::Gtk2::Symlist->new_from_key ($key); | ||
| 84 | |||
| 85 | } elsif ($col == COL_SYMLIST_NAME) { | ||
| 86 | return $symlist_model->get_value ($subiter, $symlist_model->COL_NAME); | ||
| 87 | |||
| 88 | } else { | ||
| 89 | return undef; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub path_for_key { | ||
| 94 | my ($self, $key) = @_; | ||
| 95 | my $symlist = App::Chart::Gtk2::Symlist->new_from_key ($key); | ||
| 96 | return $self->path_for_model ($symlist); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub path_for_symbol_and_symlist { | ||
| 100 | my ($self, $symbol, $symlist) = @_; | ||
| 101 | my $path = $self->path_for_model ($symlist) || return undef; | ||
| 102 | my $sub_index = $symlist->find_symbol_pos ($symbol); | ||
| 103 | if (! defined $sub_index) { return undef; } | ||
| 104 | $path->append_index ($sub_index); | ||
| 105 | return $path; | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | 1; | ||
| 110 | __END__ | ||
| 111 | |||
| 112 | =for stopwords watchlist symlist ie | ||
| 113 | |||
| 114 | =head1 NAME | ||
| 115 | |||
| 116 | App::Chart::Gtk2::OpenModel -- watchlist data model object | ||
| 117 | |||
| 118 | =for test_synopsis my ($symlist) | ||
| 119 | |||
| 120 | =head1 SYNOPSIS | ||
| 121 | |||
| 122 | use App::Chart::Gtk2::OpenModel; | ||
| 123 | my $model = App::Chart::Gtk2::OpenModel->new ($symlist); | ||
| 124 | |||
| 125 | =head1 OBJECT HIERARCHY | ||
| 126 | |||
| 127 | C<App::Chart::Gtk2::OpenModel> is a subclass of C<Gtk2::TreeModelFilter>, | ||
| 128 | |||
| 129 | Glib::Object | ||
| 130 | Gtk2::TreeModelFilter | ||
| 131 | App::Chart::Gtk2::OpenModel | ||
| 132 | |||
| 133 | =head1 DESCRIPTION | ||
| 134 | |||
| 135 | A C<App::Chart::Gtk2::OpenModel> object presents the data from a given | ||
| 136 | C<App::Chart::Gtk2::Symlist> in a form suitable for C<App::Chart::Open> dialog. | ||
| 137 | Currently this is its sole use. | ||
| 138 | |||
| 139 | =head1 FUNCTIONS | ||
| 140 | |||
| 141 | =over 4 | ||
| 142 | |||
| 143 | =item C<< App::Chart::Gtk2::OpenModel->new ($symlist) >> | ||
| 144 | |||
| 145 | Create and return a C<App::Chart::Gtk2::OpenModel> object presenting the | ||
| 146 | symbols in C<$symlist>. | ||
| 147 | |||
| 148 | =back | ||
| 149 | |||
| 150 | =head1 PROPERTIES | ||
| 151 | |||
| 152 | =over 4 | ||
| 153 | |||
| 154 | =item C<symlist> (C<App::Chart::Gtk2::Symlist> object, read-only) | ||
| 155 | |||
| 156 | The symlist to track and get data from. The intention is that this is | ||
| 157 | "construct-only", ie. to be set only when first constructing the model. To | ||
| 158 | get a different symlist then create a new model. | ||
| 159 | |||
| 160 | =back | ||
| 161 | |||
| 162 | =head1 SEE ALSO | ||
| 163 | |||
| 164 | L<App::Chart::Gtk2::OpenDialog> | ||
| 165 | |||
| 166 | =cut |