| 1 | # Copyright 2010, 2011, 2012, 2013, 2014 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Glib-Ex-ConnectProperties. | ||
| 4 | # | ||
| 5 | # Glib-Ex-ConnectProperties is free software; you can redistribute it and/or | ||
| 6 | # modify it under the terms of the GNU General Public License as published | ||
| 7 | # by the Free Software Foundation; either version 3, or (at your option) any | ||
| 8 | # later version. | ||
| 9 | # | ||
| 10 | # Glib-Ex-ConnectProperties is distributed in the hope that it will be | ||
| 11 | # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
| 13 | # Public License for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Glib-Ex-ConnectProperties. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | package Glib::Ex::ConnectProperties::Element::model_rows; | ||
| 19 | use 5.008; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Carp; | ||
| 23 | use Glib; | ||
| 24 | use base 'Glib::Ex::ConnectProperties::Element'; | ||
| 25 | |||
| 26 | our $VERSION = 20; | ||
| 27 | |||
| 28 | # uncomment this to run the ### lines | ||
| 29 | #use Smart::Comments; | ||
| 30 | |||
| 31 | |||
| 32 | use constant pspec_hash => | ||
| 33 | do { | ||
| 34 | # dummy name as paramspec name cannot be empty | ||
| 35 | my $pspec = Glib::ParamSpec->boolean ('empty', # name, unused | ||
| 36 | '', # nick, unused | ||
| 37 | '', # blurb, unused | ||
| 38 | 0, # default, unused | ||
| 39 | 'readable'); | ||
| 40 | ({ | ||
| 41 | 'empty' => $pspec, | ||
| 42 | 'not-empty' => $pspec, | ||
| 43 | }) | ||
| 44 | }; | ||
| 45 | |||
| 46 | sub connect_signals { | ||
| 47 | my ($self) = @_; | ||
| 48 | ### model_rows connect_signals() | ||
| 49 | my $model = $self->{'object'}; | ||
| 50 | |||
| 51 | # row-deleted permanent | ||
| 52 | $self->{'ids'} = Glib::Ex::SignalIds->new | ||
| 53 | ($model, | ||
| 54 | $model->signal_connect ('row-deleted', \&_do_deleted, $self)); | ||
| 55 | |||
| 56 | # row-inserted while not empty | ||
| 57 | unless ($model->get_iter_first) { | ||
| 58 | $self->{'ids2'} = Glib::Ex::SignalIds->new | ||
| 59 | ($model, | ||
| 60 | $model->signal_connect ('row-inserted', \&_do_inserted, $self)); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | sub disconnect { | ||
| 64 | my ($self) = @_; | ||
| 65 | delete $self->{'ids'}; | ||
| 66 | delete $self->{'ids2'}; | ||
| 67 | } | ||
| 68 | |||
| 69 | # row-deleted signal handler | ||
| 70 | sub _do_deleted { | ||
| 71 | my $self = $_[-1]; | ||
| 72 | ### model_rows _do_deleted() | ||
| 73 | my $model = $self->{'object'}; | ||
| 74 | if (! $self->{'object'}->get_iter_first) { | ||
| 75 | ### has become empty | ||
| 76 | $self->{'ids2'} = Glib::Ex::SignalIds->new | ||
| 77 | ($model, | ||
| 78 | $model->signal_connect ('row-inserted', \&_do_inserted, $self)); | ||
| 79 | Glib::Ex::ConnectProperties::_do_read_handler($self); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | # row-inserted signal handler, connected only while model is empty | ||
| 83 | sub _do_inserted { | ||
| 84 | my $self = $_[-1]; | ||
| 85 | ### model_rows _do_inserted() | ||
| 86 | ### has become not-empty | ||
| 87 | delete $self->{'ids2'}; | ||
| 88 | Glib::Ex::ConnectProperties::_do_read_handler($self); | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | sub get_value { | ||
| 93 | my ($self) = @_; | ||
| 94 | ### model_rows get_value() | ||
| 95 | ### is: (defined($self->{'object'}->get_iter_first) ^ ($self->{'pname'} eq 'not-empty')) | ||
| 96 | |||
| 97 | return (defined($self->{'object'}->get_iter_first) | ||
| 98 | ^ ($self->{'pname'} eq 'empty')); | ||
| 99 | } | ||
| 100 | |||
| 101 | 1; | ||
| 102 | __END__ |