| 1 | # Copyright 2008, 2009, 2010 Kevin Ryde | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | # This file is part of Gtk2-Ex-TiedListColumn. | ||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||
| 5 | # Gtk2-Ex-TiedListColumn 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 | # Gtk2-Ex-TiedListColumn is distributed in the hope that it will be useful, | ||||||||||||||||||||||
| 11 | # 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 Gtk2-Ex-TiedListColumn. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | package Gtk2::Ex::TiedListColumn; | ||||||||||||||||||||||
| 20 | 2 | ▶ | use 5.008; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 21 | 2 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 22 | 2 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 23 | 2 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 24 | 2 | ▶ | use List::Util qw(min max); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | 2 | our $VERSION = 5; | |||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | 2 | ▶ | use constant DEBUG => 0; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | 0 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 31 | 0 | my ($class, $model, $column) = @_; | |||||||||||||||||||||
| 32 | 0 | tie (my @array, $class, $model, $column); | |||||||||||||||||||||
| 33 | 0 | return \@array; | |||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||
| 35 | |||||||||||||||||||||||
| 36 | 0 | ▶ | sub TIEARRAY { | ||||||||||||||||||||
|
Subroutine
TIEARRAY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 37 | 0 | my ($class, $model, $column) = @_; | |||||||||||||||||||||
| 38 | 0 | ▶ | return bless { model => $model, | ||||||||||||||||||||
|
Condition0%0/2
$column || 0
MC/DC0%0/1
$column || 0
$column
Truth table0%0/2
A
A= $column
| |||||||||||||||||||||||
| 39 | column => ($column||0) | ||||||||||||||||||||||
| 40 | }, $class; | ||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||
| 42 | # optional, not needed | ||||||||||||||||||||||
| 43 | # sub UNTIE { } | ||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | # tied object funcs | ||||||||||||||||||||||
| 46 | 0 | ▶ | sub model { | ||||||||||||||||||||
|
Subroutine
model: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 47 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 48 | 0 | return $self->{'model'}; | |||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||
| 50 | 0 | ▶ | sub column { | ||||||||||||||||||||
|
Subroutine
column: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 51 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 52 | 0 | return $self->{'column'}; | |||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | # negative indices already normalized to >=0 by the time they get here | ||||||||||||||||||||||
| 56 | 0 | ▶ | sub FETCH { | ||||||||||||||||||||
|
Subroutine
FETCH: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 57 | 0 | my ($self, $index) = @_; | |||||||||||||||||||||
| 58 | 0 | if (DEBUG >= 2) { print "FETCH $index\n"; } | |||||||||||||||||||||
| 59 | 0 | my $model = $self->{'model'}; | |||||||||||||||||||||
| 60 | 0 | ▶ | my $iter = $model->get_iter (Gtk2::TreePath->new ($index)) | ||||||||||||||||||||
|
Condition0%0/2
$model->get_iter('Gtk2::TreePath'->new($index)) || (return undef)
MC/DC0%0/1
$model->get_iter('Gtk2::TreePath'->new($index)) || (return undef)
$model->get_iter('Gtk2::TreePath'->new($index))
Truth table0%0/2
A
A= $model->get_iter('Gtk2::TreePath'->new($index))
| |||||||||||||||||||||||
| 61 | || return undef; | ||||||||||||||||||||||
| 62 | 0 | return $model->get_value ($iter, $self->{'column'}); | |||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||
| 64 | |||||||||||||||||||||||
| 65 | # negative indices already normalized to >=0 by the time they get here | ||||||||||||||||||||||
| 66 | 0 | ▶ | sub STORE { | ||||||||||||||||||||
|
Subroutine
STORE: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||
| 67 | 0 | my ($self, $index, $value) = @_; | |||||||||||||||||||||
| 68 | 0 | if (DEBUG) { print "STORE $index $value\n"; } | |||||||||||||||||||||
| 69 | 0 | my $model = $self->{'model'}; | |||||||||||||||||||||
| 70 | 0 | my $iter = $model->get_iter (Gtk2::TreePath->new ($index)); | |||||||||||||||||||||
| 71 | 0 | ▶ | if (! $iter) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 72 | 0 | my $len = $model->iter_n_children (undef); | |||||||||||||||||||||
| 73 | 0 | ▶ | while ($len <= $index) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 74 | 0 | $iter = $model->insert ($len); | |||||||||||||||||||||
| 75 | 0 | $len++; | |||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||
| 78 | 0 | $model->set_value ($iter, $self->{'column'}, $value); | |||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | 0 | ▶ | sub FETCHSIZE { | ||||||||||||||||||||
|
Subroutine
FETCHSIZE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 82 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 83 | 0 | if (DEBUG) { print "FETCHSIZE\n"; } | |||||||||||||||||||||
| 84 | 0 | my $model = $self->{'model'}; | |||||||||||||||||||||
| 85 | 0 | return $model->iter_n_children (undef); | |||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | # big negatives already normalized to 0 by the time they get here | ||||||||||||||||||||||
| 89 | 0 | ▶ | sub STORESIZE { | ||||||||||||||||||||
|
Subroutine
STORESIZE: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||
| 90 | 0 | my ($self, $want_size) = @_; | |||||||||||||||||||||
| 91 | 0 | if (DEBUG) { print "STORESIZE $want_size, currently ", | |||||||||||||||||||||
| 92 | $self->{'model'}->iter_n_children (undef),"\n"; } | ||||||||||||||||||||||
| 93 | 0 | my $model = $self->{'model'}; | |||||||||||||||||||||
| 94 | 0 | my $got_size = $model->iter_n_children (undef); | |||||||||||||||||||||
| 95 | 0 | ▶ | while ($got_size < $want_size) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 96 | 0 | $model->append; | |||||||||||||||||||||
| 97 | 0 | $got_size++; | |||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||
| 99 | 0 | ▶ | while ($got_size > $want_size) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 100 | 0 | my $iter = $model->get_iter (Gtk2::TreePath->new($got_size-1)); | |||||||||||||||||||||
| 101 | 0 | $model->remove ($iter); | |||||||||||||||||||||
| 102 | 0 | $got_size--; | |||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||
| 105 | |||||||||||||||||||||||
| 106 | 0 | ▶ | sub EXTEND { | ||||||||||||||||||||
|
Subroutine
EXTEND: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||
| 108 | |||||||||||||||||||||||
| 109 | # negative indices already normalized to >=0 by the time they get here | ||||||||||||||||||||||
| 110 | 0 | ▶ | sub EXISTS { | ||||||||||||||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 111 | 0 | my ($self, $index) = @_; | |||||||||||||||||||||
| 112 | 0 | if (DEBUG) { print "EXISTS $index\n"; } | |||||||||||||||||||||
| 113 | 0 | my $model = $self->{'model'}; | |||||||||||||||||||||
| 114 | 0 | return $index < $model->iter_n_children(undef); | |||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | 0 | ▶ | sub DELETE { | ||||||||||||||||||||
|
Subroutine
DELETE: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||
| 118 | 0 | my ($self, $index) = @_; | |||||||||||||||||||||
| 119 | 0 | if (DEBUG) { print "DELETE $index\n"; } | |||||||||||||||||||||
| 120 | 0 | my $model = $self->{'model'}; | |||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | 0 | ▶ | my $iter = $model->get_iter (Gtk2::TreePath->new ($index)) | ||||||||||||||||||||
|
Condition0%0/2
$model->get_iter('Gtk2::TreePath'->new($index)) || (return undef)
MC/DC0%0/1
$model->get_iter('Gtk2::TreePath'->new($index)) || (return undef)
$model->get_iter('Gtk2::TreePath'->new($index))
Truth table0%0/2
A
A= $model->get_iter('Gtk2::TreePath'->new($index))
| |||||||||||||||||||||||
| 123 | || return undef; | ||||||||||||||||||||||
| 124 | 0 | my $ret = $model->get_value ($iter); | |||||||||||||||||||||
| 125 | 0 | my $len = $model->iter_n_children (undef); | |||||||||||||||||||||
| 126 | 0 | ▶ | if ($index == $len-1) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 127 | 0 | $model->remove ($iter); | |||||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||||
| 129 | 0 | $model->set ($iter, $self->{'column'}, undef); | |||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||
| 131 | 0 | return $ret; | |||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||
| 133 | |||||||||||||||||||||||
| 134 | 0 | ▶ | sub CLEAR { | ||||||||||||||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 135 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 136 | 0 | if (DEBUG) { print "CLEAR\n"; } | |||||||||||||||||||||
| 137 | 0 | my $model = $self->{'model'}; | |||||||||||||||||||||
| 138 | 0 | $model->clear; | |||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | 0 | ▶ | sub PUSH { | ||||||||||||||||||||
|
Subroutine
PUSH: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 142 | 0 | my $self = shift; | |||||||||||||||||||||
| 143 | 0 | my $model = $self->{'model'}; | |||||||||||||||||||||
| 144 | 0 | my $column = $self->{'column'}; | |||||||||||||||||||||
| 145 | 0 | my $pos = $model->iter_n_children (undef); | |||||||||||||||||||||
| 146 | 0 | foreach my $value (@_) { | |||||||||||||||||||||
| 147 | 0 | $model->insert_with_values ($pos++, $column, $value); | |||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||
| 150 | |||||||||||||||||||||||
| 151 | 0 | ▶ | sub POP { | ||||||||||||||||||||
|
Subroutine
POP: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 152 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 153 | 0 | if (DEBUG) { print "POP\n"; } | |||||||||||||||||||||
| 154 | 0 | my $model = $self->{'model'}; | |||||||||||||||||||||
| 155 | 0 | ▶ | my $len = $model->iter_n_children (undef) || return undef; # if empty | ||||||||||||||||||||
|
Condition0%0/2
$model->iter_n_children(undef) || (return undef)
MC/DC0%0/1
$model->iter_n_children(undef) || (return undef)
$model->iter_n_children(undef)
Truth table0%0/2
A
A= $model->iter_n_children(undef)
| |||||||||||||||||||||||
| 156 | 0 | my $iter = $model->iter_nth_child (undef, $len-1); | |||||||||||||||||||||
| 157 | 0 | my $value = $model->get_value ($iter, $self->{'column'}); | |||||||||||||||||||||
| 158 | 0 | $model->remove ($iter); | |||||||||||||||||||||
| 159 | 0 | return $value; | |||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||
| 161 | |||||||||||||||||||||||
| 162 | 0 | ▶ | sub SHIFT { | ||||||||||||||||||||
|
Subroutine
SHIFT: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 163 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 164 | 0 | my $model = $self->{'model'}; | |||||||||||||||||||||
| 165 | 0 | ▶ | my $iter = $model->get_iter_first || return undef; # if empty | ||||||||||||||||||||
|
Condition0%0/2
$model->get_iter_first || (return undef)
MC/DC0%0/1
$model->get_iter_first || (return undef)
$model->get_iter_first
Truth table0%0/2
A
A= $model->get_iter_first
| |||||||||||||||||||||||
| 166 | 0 | my $value = $model->get_value ($iter, $self->{'column'}); | |||||||||||||||||||||
| 167 | 0 | $model->remove ($iter); | |||||||||||||||||||||
| 168 | 0 | return $value; | |||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||
| 170 | |||||||||||||||||||||||
| 171 | # don't have to return the new size here, FETCHSIZE is called separately | ||||||||||||||||||||||
| 172 | 0 | ▶ | sub UNSHIFT { | ||||||||||||||||||||
|
Subroutine
UNSHIFT: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 173 | 0 | my $self = shift; | |||||||||||||||||||||
| 174 | 0 | if (DEBUG) { print "UNSHIFT\n"; } | |||||||||||||||||||||
| 175 | 0 | my $model = $self->{'model'}; | |||||||||||||||||||||
| 176 | 0 | my $column = $self->{'column'}; | |||||||||||||||||||||
| 177 | 0 | my $pos = 0; | |||||||||||||||||||||
| 178 | 0 | foreach my $value (@_) { | |||||||||||||||||||||
| 179 | 0 | $model->insert_with_values ($pos++, $column, $value); | |||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||
| 182 | |||||||||||||||||||||||
| 183 | 0 | ▶ | sub SPLICE { | ||||||||||||||||||||
|
Subroutine
SPLICE: not called CC=16 SCAR=56.1
| |||||||||||||||||||||||
| 184 | 0 | my $self = shift; | |||||||||||||||||||||
| 185 | 0 | my $offset = shift; | |||||||||||||||||||||
| 186 | 0 | my $length = shift; | |||||||||||||||||||||
| 187 | 0 | if (DEBUG) { print "SPLICE ",defined $offset ? $offset : 'undef', | |||||||||||||||||||||
| 188 | " ", defined $length ? $length : 'undef', "\n"; } | ||||||||||||||||||||||
| 189 | |||||||||||||||||||||||
| 190 | 0 | my $model = $self->{'model'}; | |||||||||||||||||||||
| 191 | 0 | my $column = $self->{'column'}; | |||||||||||||||||||||
| 192 | 0 | my $total = $model->iter_n_children (undef); | |||||||||||||||||||||
| 193 | |||||||||||||||||||||||
| 194 | # carp similar to "use warnings" on ordinary arrays | ||||||||||||||||||||||
| 195 | 0 | ▶ | if (! defined $offset) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 196 | 0 | $offset = 0; | |||||||||||||||||||||
| 197 | } elsif ($offset < -$total) { | ||||||||||||||||||||||
| 198 | 0 | carp "TiedListColumn: offset $offset before start of array"; | |||||||||||||||||||||
| 199 | 0 | $offset = 0; | |||||||||||||||||||||
| 200 | } elsif ($offset < 0) { | ||||||||||||||||||||||
| 201 | 0 | $offset += $total; | |||||||||||||||||||||
| 202 | } elsif ($offset > $total) { | ||||||||||||||||||||||
| 203 | 0 | carp "TiedListColumn: offset $offset past end of array"; | |||||||||||||||||||||
| 204 | 0 | $offset = $total; | |||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||
| 206 | |||||||||||||||||||||||
| 207 | 0 | ▶ | if (! defined $length) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 208 | 0 | $length = $total - $offset; | |||||||||||||||||||||
| 209 | } elsif ($length < 0) { | ||||||||||||||||||||||
| 210 | 0 | $length = max (0, $total + $length - $offset); | |||||||||||||||||||||
| 211 | } else { | ||||||||||||||||||||||
| 212 | 0 | $length = min ($length, $total - $offset); | |||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||
| 214 | |||||||||||||||||||||||
| 215 | 0 | if (DEBUG) { print " norm to $offset, $length\n"; } | |||||||||||||||||||||
| 216 | |||||||||||||||||||||||
| 217 | 0 | my @ret; | |||||||||||||||||||||
| 218 | 0 | ▶ | if ($length > 0) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 219 | 0 | my $iter = $model->iter_nth_child (undef, $offset); | |||||||||||||||||||||
| 220 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 221 | 0 | $#ret = $length-1; | |||||||||||||||||||||
| 222 | 0 | foreach my $i (0 .. $length-1) { | |||||||||||||||||||||
| 223 | 0 | $ret[$i] = $model->get_value ($iter, $column); | |||||||||||||||||||||
| 224 | 0 | ▶ | $model->remove ($iter) or last; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||
| 226 | |||||||||||||||||||||||
| 227 | } else { | ||||||||||||||||||||||
| 228 | 0 | $ret[0] = undef; | |||||||||||||||||||||
| 229 | 0 | foreach (0 .. $length-2) { | |||||||||||||||||||||
| 230 | 0 | ▶ | if (! $model->remove ($iter)) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 231 | 0 | $iter = undef; | |||||||||||||||||||||
| 232 | 0 | last; | |||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||
| 235 | 0 | ▶ | if ($iter) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 236 | 0 | $ret[0] = $model->get_value ($iter, $column); | |||||||||||||||||||||
| 237 | 0 | $model->remove ($iter); | |||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||
| 241 | |||||||||||||||||||||||
| 242 | 0 | foreach my $value (@_) { | |||||||||||||||||||||
| 243 | 0 | $model->insert_with_values ($offset++, $column, $value); | |||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||
| 245 | |||||||||||||||||||||||
| 246 | # here in scalar context $ret[0] is the last removed as per what splice() | ||||||||||||||||||||||
| 247 | # should return | ||||||||||||||||||||||
| 248 | 0 | ▶ | return (wantarray ? @ret : $ret[0]); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||
| 250 | |||||||||||||||||||||||
| 251 | 2 | 1; | |||||||||||||||||||||
| 252 | __END__ | ||||||||||||||||||||||