| 1 | package Gapp::TreeView; | ||
| 2 | { | ||
| 3 | $Gapp::TreeView::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | extends 'Gapp::Container'; | ||
| 8 | |||
| 9 | use Gapp::Util; | ||
| 10 | use Gapp::Types qw( GappTreeViewColumn ); | ||
| 11 | |||
| 12 | use Moose::Util; | ||
| 13 | use MooseX::Types::Moose qw( ArrayRef HashRef ); | ||
| 14 | |||
| 15 | has '+gclass' => ( | ||
| 16 | default => 'Gtk2::TreeView', | ||
| 17 | ); | ||
| 18 | |||
| 19 | has 'model' => ( | ||
| 20 | is => 'rw', | ||
| 21 | isa => 'Maybe[Object]', | ||
| 22 | trigger => sub { | ||
| 23 | my ( $self, $newval, $oldval ) = @_; | ||
| 24 | |||
| 25 | if ( $self->has_gobject ) { | ||
| 26 | $self->gobject->set_model( $newval->gobject ); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | ); | ||
| 30 | |||
| 31 | has 'columns' => ( | ||
| 32 | is => 'rw', | ||
| 33 | isa => 'ArrayRef', | ||
| 34 | default => sub { [ ] }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | has 'data_column' => ( | ||
| 38 | is => 'rw', | ||
| 39 | isa => 'Int', | ||
| 40 | default => 0, | ||
| 41 | ); | ||
| 42 | |||
| 43 | sub BUILDARGS { | ||
| 44 | my $class = shift; | ||
| 45 | my %args = @_ == 1 && is_HashRef( $_[0] ) ? %{$_[0]} : @_; | ||
| 46 | |||
| 47 | # coerce column values if they are not Gapp::TreViewColumn objects | ||
| 48 | if ( exists $args{columns} ) { | ||
| 49 | |||
| 50 | my @columns; | ||
| 51 | |||
| 52 | for my $c ( @{$args{columns}} ) { | ||
| 53 | $c = to_GappTreeViewColumn( $c ) if ! is_GappTreeViewColumn( $c ); | ||
| 54 | push @columns, $c if $c; | ||
| 55 | } | ||
| 56 | |||
| 57 | $args{columns} = \@columns; | ||
| 58 | } | ||
| 59 | |||
| 60 | # headers visible | ||
| 61 | for my $att ( qw(headers_visible headers_clickable) ) { | ||
| 62 | $args{properties}{$att} = delete $args{$att} if exists $args{$att}; | ||
| 63 | } | ||
| 64 | |||
| 65 | __PACKAGE__->SUPER::BUILDARGS( %args ); | ||
| 66 | } | ||
| 67 | |||
| 68 | after '_build_gobject' => sub { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | for my $c ( @{ $self->columns } ) { | ||
| 72 | $c = to_GappTreeViewColumn( $c ) if ! is_GappTreeViewColumn( $c ); | ||
| 73 | $self->gobject->append_column( $c->gobject ); | ||
| 74 | $self->gobject->{columns}{$c->name} = $c->gobject; | ||
| 75 | } | ||
| 76 | }; | ||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | sub find_column { | ||
| 81 | my ( $self, $cname ) = @_; | ||
| 82 | |||
| 83 | for my $c ( @{ $self->columns } ) { | ||
| 84 | if ( $c->name eq $cname ) { | ||
| 85 | return $c; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | sub get_selected { | ||
| 91 | my ( $self ) = @_; | ||
| 92 | |||
| 93 | my @records; | ||
| 94 | $self->gobject->get_selection->selected_foreach( sub{ | ||
| 95 | my ( $model, $path, $iter ) = @_; | ||
| 96 | push @records, $model->get( $iter, $self->data_column ); | ||
| 97 | return; | ||
| 98 | }); | ||
| 99 | |||
| 100 | |||
| 101 | return wantarray ? @records : $records[0]; | ||
| 102 | } | ||
| 103 | |||
| 104 | 1; | ||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | __END__ |