File Coverage

blib/lib/Graphics/Primitive/Container.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Graphics::Primitive::Container;
2 1     1   23630 use Moose;
  0            
  0            
3             use MooseX::Storage;
4              
5             with Storage (format => 'JSON', io => 'File');
6              
7             use Graphics::Primitive::ComponentList;
8              
9             use Forest::Tree;
10              
11             extends 'Graphics::Primitive::Component';
12              
13             with 'MooseX::Clone';
14              
15             has 'component_list' => (
16             is => 'rw',
17             isa => 'Graphics::Primitive::ComponentList',
18             default => sub { Graphics::Primitive::ComponentList->new },
19             handles => [qw(component_count components constraints each find find_component get_component get_constraint)],
20             trigger => sub { my ($self) = @_; $self->prepared(0); }
21             );
22             has 'layout_manager' => (
23             is => 'rw',
24             isa => 'Layout::Manager',
25             handles => [ 'do_layout' ],
26             trigger => sub { my ($self) = @_; $self->prepared(0); },
27             );
28              
29             sub add_component {
30             my ($self, $component, $args) = @_;
31              
32             return 0 unless $self->validate_component($component, $args);
33              
34             $component->parent($self);
35             $self->component_list->add_component($component, $args);
36              
37             $self->prepared(0);
38              
39             return 1;
40             }
41              
42             sub clear_components {
43             my ($self) = @_;
44              
45             # Clear all the component's parent attributes just in case some
46             # outside thingie is holding a reference to it
47             foreach my $c (@{ $self->components }) {
48             next unless(defined($c));
49             $c->parent(undef);
50             }
51             $self->component_list->clear;
52             $self->prepared(0);
53             }
54              
55             sub get_tree {
56             my ($self) = @_;
57              
58             my $tree = Forest::Tree->new(node => $self);
59              
60             foreach my $c (@{ $self->components }) {
61             $tree->add_child($c->get_tree);
62             }
63              
64             return $tree;
65             }
66              
67             sub prepare {
68             my ($self, $driver) = @_;
69              
70             return if $self->prepared;
71              
72             unless($self->minimum_width) {
73             $self->minimum_width($self->outside_width);
74             }
75             unless($self->minimum_height) {
76             $self->minimum_height($self->outside_height);
77             }
78             }
79              
80             sub remove_component {
81             my ($self, $component) = @_;
82              
83             my $removed = $self->component_list->remove_component($component);
84             if(scalar(@{ $removed })) {
85             foreach my $r (@{ $removed }) {
86             $r->parent(undef);
87             }
88             }
89              
90             return $removed;
91             }
92              
93             sub validate_component {
94             my ($self, $c, $a) = @_;
95              
96             return 1;
97             }
98              
99             __PACKAGE__->meta->make_immutable;
100              
101             no Moose;
102             1;
103             __END__
104              
105             =head1 NAME
106              
107             Graphics::Primitive::Container - Component that holds other Components
108              
109             =head1 DESCRIPTION
110              
111             Containers are components that contain other components. They can also hold
112             an instance of a L<Layout::Manager> for automatic layout of their internal
113             components. See the
114             L<Component's Lifecycle Section|Graphics::Primitive::Component#LIFECYCLE> for
115             more information.
116              
117             =head1 SYNOPSIS
118              
119             my $c = Graphics::Primitive::Container->new(
120             width => 500, height => 350,
121             layout_manager => Layout::Manager::Compass->new
122             );
123             $c->add_component($comp, { meta => 'data' });
124              
125             =head1 METHODS
126              
127             =head2 Constructor
128              
129             =over 4
130              
131             =item I<new>
132              
133             Creates a new Container.
134              
135             =back
136              
137             =head2 Instance Methods
138              
139             =over 4
140              
141             =item I<add_component ($component, [ $constraint ])>
142              
143             Add a component to the container. Returns a true value if the component
144             was added successfully. A second argument may be required, please consult the
145             POD for your specific layout manager implementation.
146              
147             Before the component is added, it is passed to the validate_component method.
148             If validate_component does not return a true value, then the component is not
149             added.
150              
151             =item I<clear_components>
152              
153             Remove all components from the layout manager.
154              
155             =item I<component_count>
156              
157             Returns the number of components in this container.
158              
159             =item I<component_list>
160              
161             Returns this Container's L<ComponentList|Graphics::Primitive::ComponentList>.
162              
163             =item I<find_component>
164              
165             Returns the index of the first component with the supplied name. Returns
166             undef if no component with that name is found.
167              
168             =item I<get_component>
169              
170             Get the component at the specified index.
171              
172             =item I<get_constraint>
173              
174             Get the constraint at the specified index.
175              
176             =item I<get_tree>
177              
178             Returns a Forest::Tree object with this component at the root and all child
179             components as children. Calling this from your root container will result
180             in a tree representation of the entire scene.
181              
182             =item I<prepare>
183              
184             Prepares this container. Does not mark as prepared, as that's done by the
185             layout manager.
186              
187             =item I<remove_component>
188              
189             Removes a component. B<Components must have names to be removed.> Returns an
190             arrayref of removed components.
191              
192             =item I<validate_component>
193              
194             Optionally overridden by an implementation, allows it to deem a component as
195             invalid. If this sub returns false, the component won't be added.
196              
197             =back
198              
199             =head1 AUTHOR
200              
201             Cory Watson, C<< <gphat@cpan.org> >>
202              
203             =head1 BUGS
204              
205             Please report any bugs or feature requests to C<bug-geometry-primitive at rt.cpan.org>, or through
206             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Geometry-Primitive>. I will be notified, and then you'll
207             automatically be notified of progress on your bug as I make changes.
208              
209             =head1 COPYRIGHT & LICENSE
210              
211             Copyright 2008-2009 by Cory G Watson.
212              
213             This program is free software; you can redistribute it and/or modify it
214             under the same terms as Perl itself.