| 1 | package Gapp::TreeViewColumn; | ||
| 2 | { | ||
| 3 | $Gapp::TreeViewColumn::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | extends 'Gapp::Object'; | ||
| 8 | |||
| 9 | use Gapp::CellRenderer; | ||
| 10 | use Gapp::Util; | ||
| 11 | use Gapp::Types qw( GappCellRenderer GappTreeViewColumn ); | ||
| 12 | |||
| 13 | use Moose::Util; | ||
| 14 | use MooseX::Types::Moose qw( Str ArrayRef HashRef CodeRef Undef ); | ||
| 15 | |||
| 16 | has '+gclass' => ( | ||
| 17 | default => 'Gtk2::TreeViewColumn', | ||
| 18 | ); | ||
| 19 | |||
| 20 | has 'name' => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => 'Str', | ||
| 23 | default => '', | ||
| 24 | ); | ||
| 25 | |||
| 26 | has 'renderer' => ( | ||
| 27 | is => 'rw', | ||
| 28 | isa => GappCellRenderer|Undef, | ||
| 29 | default => sub { Gapp::CellRenderer->new( gclass => 'Gtk2::CellRendererText', property => 'markup' ) }, | ||
| 30 | coerce => 1, | ||
| 31 | ); | ||
| 32 | |||
| 33 | has 'data_column' => ( | ||
| 34 | is => 'rw', | ||
| 35 | isa => 'Int|Undef', | ||
| 36 | default => 0, | ||
| 37 | ); | ||
| 38 | |||
| 39 | has 'data_func' => ( | ||
| 40 | is => 'rw', | ||
| 41 | isa => 'Str|CodeRef|Undef', | ||
| 42 | ); | ||
| 43 | |||
| 44 | has 'sort_enabled' => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => 'Bool', | ||
| 47 | default => 0, | ||
| 48 | trigger => sub { | ||
| 49 | my ( $self ) = @_; | ||
| 50 | if ( $self->has_gobject ) { | ||
| 51 | $self->gobject->set_clickable( 1 ); | ||
| 52 | $self->gobject->signal_connect( 'clicked', sub { | ||
| 53 | $self->gobject->get_tree_view->get_model->set_default_sort_func( sub { | ||
| 54 | my ( $model, $itera, $iterb, $self ) = @_; | ||
| 55 | my $a = $model->get( $itera, $self->data_column ); | ||
| 56 | my $b = $model->get( $iterb, $self->data_column ); | ||
| 57 | $self->sort_func->( $self, $a, $b ); | ||
| 58 | }, $self) | ||
| 59 | } ); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | ); | ||
| 63 | |||
| 64 | has 'sort_func' => ( | ||
| 65 | is => 'rw', | ||
| 66 | isa => 'Maybe[CodeRef]', | ||
| 67 | default => sub { | ||
| 68 | sub { | ||
| 69 | my ( $self, $a, $b ) = @_; | ||
| 70 | lc $self->get_cell_value( $a ) cmp lc $self->get_cell_value( $b ); | ||
| 71 | }; | ||
| 72 | }, | ||
| 73 | ); | ||
| 74 | |||
| 75 | |||
| 76 | sub BUILDARGS { | ||
| 77 | my $class = shift; | ||
| 78 | my %args = @_ == 1 && is_HashRef( $_[0] ) ? %{$_[0]} : @_; | ||
| 79 | |||
| 80 | |||
| 81 | for my $att ( qw(alignment clickable expand fixed_width min_width reordable resizable sizing), | ||
| 82 | qw(sort_column_id sort_indicator sort_order spacing title visible width') ) { | ||
| 83 | $args{properties}{$att} = delete $args{$att} if exists $args{$att}; | ||
| 84 | } | ||
| 85 | |||
| 86 | __PACKAGE__->SUPER::BUILDARGS( %args ); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub get_cell_value { | ||
| 90 | my ( $self, $input ) = @_; | ||
| 91 | |||
| 92 | local $_ = $input; | ||
| 93 | |||
| 94 | my $value = $input; | ||
| 95 | if ( is_CodeRef( $self->data_func ) ) { | ||
| 96 | $value = &{ $self->data_func }( @_ ); | ||
| 97 | } | ||
| 98 | elsif ( is_Str( $self->data_func ) ) { | ||
| 99 | my $method = $self->data_func; | ||
| 100 | $value = $_ ? $_->$method : ''; | ||
| 101 | } | ||
| 102 | |||
| 103 | return $value; | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | 1; | ||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | __END__ |