lib/Treex/Core/TredView/TreeLayout.pm

filter: Statementstmt 11.6% 31 / 266 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 19.0% 4 / 21 Podpod 100% 6 / 6 totaltotal 7.9% 41 / 516 CC 86 SCAR 44.5
CC
86
Cov
7%
_mouse_move
240.0
_normalize_layout
132.0
save_layouts
90.0
CRAP 85.6
« lib/Treex/Core/TredView/Labels.pm ↑ Summary lib/Treex/Core/Resource.pm »
1 package Treex::Core::TredView::TreeLayout;
2 {
3 2 $Treex::Core::TredView::TreeLayout::VERSION = '0.08633_1';
4 }
5  
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 subtype 'TreeLayout' => as 'ArrayRef[ArrayRef[Int]]';
10  
11 2 has '_layouts_loaded' => ( is => 'rw', isa => 'Int', default => 0 );
12 2 has '_layouts_saved' => ( is => 'rw', isa => 'Int', default => 0 );
13 0 ▶has '_layouts' => ( is => 'rw', isa => 'HashRef[TreeLayout]', default => sub { {} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
14  
15 2 has '_tag_text' => ( is => 'ro', isa => 'Str', default => 'text' );
16 2 has '_tag_tree' => ( is => 'ro', isa => 'Str', default => 'tree' );
17 2 has '_tag_wrap' => ( is => 'ro', isa => 'Str', default => 'wrap' );
18 2 has '_tag_none' => ( is => 'ro', isa => 'Str', default => 'none' );
19  
20 2 has '_margin' => ( is => 'ro', isa => 'Int', default => 20 );
21 2 has '_width' => ( is => 'ro', isa => 'Int', default => 80 );
22 2 has '_height' => ( is => 'ro', isa => 'Int', default => 45 );
23  
24 2 has '_drag_tree' => ( is => 'rw', isa => 'Maybe[HashRef[Str]]', default => undef );
25 2 has '_drag_x' => ( is => 'rw', isa => 'Int', default => -1 );
26 2 has '_drag_y' => ( is => 'rw', isa => 'Int', default => -1 );
27 2 has '_cur_tree' => ( is => 'rw', isa => 'Maybe[HashRef[Str]]', default => undef );
28  
29 2 has '_cur_layout' => ( is => 'rw', isa => 'TreeLayout' );
30  
31 sub get_tree_label {
32 0 ▶ my ( $self, $tree ) = @_;
Subroutine get_tree_label: not called CC=2 SCAR=17.9
Pod documented
33 0 my $label = $tree->language . '-' . $tree->get_layer;
34 0 my $sel = $tree->selector;
35 0 ▶ $label .= '-' . $sel if $sel;
Branch: 1 branch
BranchTrueFalseTotal
if $sel 0 0 0
36 0 return $label;
37 }
38  
39 sub get_layout_label {
40 0 ▶ my ( $self, $bundle ) = @_;
Subroutine get_layout_label: not called CC=4 SCAR=26.3
Pod documented
41 {
42 2 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.2
43 0 ▶ $bundle = $TredMacro::root if not $bundle;
Branch: 1 branch
BranchTrueFalseTotal
unless $bundle 0 0 0
44 }
45 0 ▶ return unless ref($bundle) eq 'Treex::Core::Bundle';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $bundle eq "Treex::Core::Bundle" 0 0 0
46  
47 0 my @label;
48 ###!!! DZ: Occasionally we get a 'zone' of the type Treex::PML::Struct (instead of Treex::Core::BundleZone).
49 ###!!! Ttred then complains "Can't locate object method "get_all_trees" via package..."
50 ###!!! I am grepping the zones for real ones but one may want to find the cause within get_all_zones() instead.
51 0 my @zones = grep {ref($_) ne 'Treex::PML::Struct'} $bundle->get_all_zones();
52 0 foreach my $zone ( sort { $a->language cmp $b->language } @zones ) {
53 0 push @label, map { $self->get_tree_label($_) } sort { $a->get_layer cmp $b->get_layer } $zone->get_all_trees();
54 }
55 0 return join ',', @label;
56 }
57  
58 sub get_layout {
59 0 ▶ my $self = shift;
Subroutine get_layout: not called CC=3 SCAR=24.8
Pod documented
60 0 my $label = $self->get_layout_label(@_);
61 0 ▶ if ( exists $self->_layouts->{$label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_layouts->{$label}) { } 0 0 0
62 0 return $self->_layouts->{$label};
63 }
64 else {
65 0 my $cols = [];
66 0 my @trees = split ',', $label;
67 0 ▶ for ( my $i = 0; $i <= $#trees; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#trees) 0 0 0
68 0 $cols->[$i]->[0] = { 'label' => $trees[$i], 'visible' => 1 };
69 }
70  
71 0 $self->_layouts->{$label} = $cols;
72 0 return $cols;
73 }
74 }
75  
76 sub load_layouts {
77 0 ▶ my $self = shift;
Subroutine load_layouts: not called CC=6 SCAR=37.4
Pod documented
78 0 ▶ return if $self->_layouts_loaded;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_layouts_loaded 0 0 0
79 0 $self->{_layouts_loaded} = 1;
80  
81 0 my $filename = TredMacro::FindMacroDir('treex') . '/.layouts.cfg';
82 0 ▶ open my $CFG, '<:utf8', $filename or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $CFG, "<:utf8", $filename or return 0 0 0
83  
84 0 ▶ while (<$CFG>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $CFG)) 0 0 0
85 0 chomp;
86 0 my ( $label, $coords ) = split '=';
87 0 my @label = split ',', $label;
88 0 my @coords = split ',', $coords;
89 0 my $cols = [];
90  
91 0 ▶ for ( my $i = 0; $i <= $#label; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#label) 0 0 0
92 0 my ( $col, $row, $visibility ) = split '-', $coords[$i];
93 0 ▶ $visibility = 1 if not defined $visibility;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $visibility 0 0 0
94 0 $cols->[$col]->[$row] = { 'label' => $label[$i], 'visible' => $visibility };
95 }
96  
97 0 $self->_layouts->{$label} = $cols;
98 }
99  
100 0 close $CFG;
101 0 return;
102 }
103  
104 sub save_layouts {
105 0 ▶ my $self = shift;
Subroutine save_layouts: not called CC=9 SCAR=45.0
Pod documented
106 0 ▶ return if $self->_layouts_saved;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_layouts_saved 0 0 0
107 # There's no point in saving layouts when GUI is not running (e.q. btred processing)
108 0 ▶ return unless ref(TredMacro::GUI()) eq 'TrEd::Window';
Branch: 1 branch
BranchTrueFalseTotal
unless ref TredMacro::GUI() eq "TrEd::Window" 0 0 0
109  
110 0 my $filename = TredMacro::FindMacroDir('treex') . '/.layouts.cfg';
111 0 ▶ open my $CFG, '>:utf8', $filename or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $CFG, ">:utf8", $filename or die $! 0 0 0
112  
113 0 ▶ while ( my ( $label, $cols ) = each %{ $self->_layouts } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->_layouts;}) 0 0 0
114 0 my %coords = ();
115 0 ▶ for ( my $col = 0; $col < scalar(@$cols); $col++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < scalar @$cols) 0 0 0
116 0 ▶ for ( my $row = 0; $row < scalar( @{ $cols->[$col] } ); $row++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < scalar @{$cols->[$col];}) 0 0 0
117 0 my $tree = $cols->[$col]->[$row];
118 0 ▶ $coords{$tree->{'label'}} = "$col-$row-".$tree->{'visible'} if $tree;
Branch: 1 branch
BranchTrueFalseTotal
if $tree 0 0 0
119 }
120 }
121  
122 0 my @coords = ();
123 0 for my $tree ( split ',', $label ) {
124 0 push @coords, $coords{$tree};
125 }
126  
127 0 print $CFG $label . '=' . join( ',', @coords ) . "\n";
128 }
129  
130 0 close $CFG;
131 0 $self->{_layouts_saved} = 1;
132 0 return;
133 }
134  
135 sub _move_layout {
136 0 ▶ my ( $self, $x, $y ) = @_;
Subroutine _move_layout: not called CC=7 SCAR=40.3
137 0 my $layout = $self->_cur_layout;
138 0 my $new_layout = [];
139  
140 0 ▶ for ( my $i = 0; $i < $x; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $x) 0 0 0
141 0 $new_layout->[$i] = [];
142 }
143 0 ▶ for ( my $i = 0; $i < scalar @$layout; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$layout) 0 0 0
144 0 ▶ for ( my $j = 0; $j < scalar @{ $layout->[$i] }; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @{$layout->[$i];}) 0 0 0
145 0 ▶ if ( $layout->[$i]->[$j] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout->[$i][$j]) 0 0 0
146 0 ▶ if ( $i + $x < 0 or $j + $y < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + $x < 0 or $j + $y < 0) 0 0 0
Condition0%0/3
$i + $x < 0 or $j + $y < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$i + $x < 0 or $j + $y < 0
$i + $x < 0 $j + $y < 0
Truth table0%0/3
A or B
A= $i + $x < 0B= $j + $y < 0
ABresult
1X1
011
000
147 0 print STDERR "Error: layout is moving out of bounds.\n";
148 0 return;
149 }
150 0 $new_layout->[ $i + $x ]->[ $j + $y ] = $layout->[$i]->[$j];
151 }
152 }
153 }
154  
155 0 $self->{_cur_layout} = $new_layout;
156 0 return;
157 }
158  
159 sub _wrap_layout {
160 0 ▶ my $self = shift;
Subroutine _wrap_layout: not called CC=9 SCAR=45.0
161 0 my $layout = $self->_cur_layout;
162 0 my $new_layout = [];
163  
164 # 8 pairs of coords lying around the center point (0, 0)
165 0 my @c = (
166 -1, -1,
167 0, -1,
168 1, -1,
169 -1, 0,
170 1, 0,
171 -1, 1,
172 0, 1,
173 1, 1
174 );
175  
176 0 ▶ for ( my $i = 0; $i < scalar @$layout; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$layout) 0 0 0
177 0 ▶ for ( my $j = 0; $j < scalar @{ $layout->[$i] }; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @{$layout->[$i];}) 0 0 0
178 0 ▶ if ( $layout->[$i]->[$j] and $layout->[$i]->[$j]->{'label'} ne $self->_tag_wrap ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout->[$i][$j] and $layout->[$i][$j]{"label"} ne $self->_tag_wrap) 0 0 0
Condition0%0/3
$layout->[$i][$j] and $layout->[$i][$j]{"label"} ne $self->_tag_wrap
!ll&&!rl&&r
000
MC/DC0%0/2
$layout->[$i][$j] and $layout->[$i][$j]{"label"} ne $self->_tag_wrap
$layout->[$i][$j] $layout->[$i][$j]{"label"} ne $self->_tag_wrap
Truth table0%0/3
A and B
A= $layout->[$i][$j]B= $layout->[$i][$j]{"label"} ne $self->_tag_wrap
ABresult
0X0
100
111
179 0 $new_layout->[$i]->[$j] = $layout->[$i]->[$j];
180 0 ▶ for ( my $k = 0; $k < scalar @c; $k += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < scalar @c) 0 0 0
181 0 my ( $x, $y ) = ( $i + $c[$k], $j + $c[ $k + 1 ] );
182 0 ▶ if ($x >= 0 and $y >= 0 and not $new_layout->[$x]->[$y]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= 0 and $y >= 0 and not $new_layout->[$x][$y]) 0 0 0
Condition0%0/6
$x >= 0 and $y >= 0 and not $new_layout->[$x][$y]
!ll&&!rl&&r
000
$x >= 0 and $y >= 0
!ll&&!rl&&r
000
MC/DC0%0/3
$x >= 0 and $y >= 0 and not $new_layout->[$x][$y]
$x >= 0 $y >= 0 not $new_layout->[$x][$y]
Truth table0%0/4
A and B and not(C)
A= $x >= 0B= $y >= 0C= not $new_layout->[$x][$y]
ABCresult
0XX0
10X0
1100
1111
183 0 $new_layout->[$x]->[$y] = { 'label' => $self->_tag_wrap, 'visible' => 0 };
184 }
185 }
186 }
187 }
188 }
189  
190 0 $self->{_cur_layout} = $new_layout;
191 0 return;
192 }
193  
194 sub _normalize_layout {
195 0 ▶ my $self = shift;
Subroutine _normalize_layout: not called CC=11 SCAR=48.8
196 0 my $layout = $self->_cur_layout;
197 0 my %filled_cols = ();
198 0 my %filled_rows = ();
199 0 my $gap_x = 0;
200 0 my $gap_y = 0;
201 0 my $new_layout = [];
202  
203 0 ▶ for ( my $i = 0; $i < scalar @$layout; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$layout) 0 0 0
204 0 ▶ for ( my $j = 0; $j < scalar @{ $layout->[$i] }; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @{$layout->[$i];}) 0 0 0
205 0 ▶ if ( $layout->[$i]->[$j] and $layout->[$i]->[$j]->{'label'} ne $self->_tag_wrap ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout->[$i][$j] and $layout->[$i][$j]{"label"} ne $self->_tag_wrap) 0 0 0
Condition0%0/3
$layout->[$i][$j] and $layout->[$i][$j]{"label"} ne $self->_tag_wrap
!ll&&!rl&&r
000
MC/DC0%0/2
$layout->[$i][$j] and $layout->[$i][$j]{"label"} ne $self->_tag_wrap
$layout->[$i][$j] $layout->[$i][$j]{"label"} ne $self->_tag_wrap
Truth table0%0/3
A and B
A= $layout->[$i][$j]B= $layout->[$i][$j]{"label"} ne $self->_tag_wrap
ABresult
0X0
100
111
206 0 $filled_cols{$i} = 1;
207 0 $filled_rows{$j} = 1;
208 }
209 }
210 }
211  
212 0 ▶ for ( my $i = 0; $i < scalar @$layout; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$layout) 0 0 0
213 0 $gap_y = 0;
214 0 ▶ $gap_x++ if not exists $filled_cols{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $filled_cols{$i} 0 0 0
215 0 ▶ for ( my $j = 0; $j < scalar @{ $layout->[$i] }; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @{$layout->[$i];}) 0 0 0
216 0 ▶ $gap_y++ if not exists $filled_rows{$j};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $filled_rows{$j} 0 0 0
217 0 ▶ if ( $layout->[$i]->[$j] and $layout->[$i]->[$j]->{'label'} ne $self->_tag_wrap ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout->[$i][$j] and $layout->[$i][$j]{"label"} ne $self->_tag_wrap) 0 0 0
Condition0%0/3
$layout->[$i][$j] and $layout->[$i][$j]{"label"} ne $self->_tag_wrap
!ll&&!rl&&r
000
MC/DC0%0/2
$layout->[$i][$j] and $layout->[$i][$j]{"label"} ne $self->_tag_wrap
$layout->[$i][$j] $layout->[$i][$j]{"label"} ne $self->_tag_wrap
Truth table0%0/3
A and B
A= $layout->[$i][$j]B= $layout->[$i][$j]{"label"} ne $self->_tag_wrap
ABresult
0X0
100
111
218 0 $new_layout->[ $i - $gap_x ]->[ $j - $gap_y ] = $layout->[$i]->[$j];
219 }
220 }
221 }
222  
223 0 $self->{_cur_layout} = $new_layout;
224 0 return;
225 }
226  
227 sub _get_layout_coords {
228 0 ▶ my ( $self, $x, $y ) = @_;
Subroutine _get_layout_coords: not called CC=1 SCAR=6.9
229 0 $x -= $self->_margin;
230 0 $y -= $self->_margin;
231 0 $x /= $self->_width + $self->_margin;
232 0 $y /= $self->_height + $self->_margin;
233 0 return ( $x, $y );
234 }
235  
236 sub _get_pos {
237 0 ▶ my ( $self, $x, $y ) = @_;
Subroutine _get_pos: not called CC=6 SCAR=35.0
238 0 my $a = $x - $self->_margin;
239 0 my $b = $y - $self->_margin;
240  
241 0 my $wm = $self->_width + $self->_margin;
242 0 my $hm = $self->_height + $self->_margin;
243  
244 0 my ( $i, $j );
245 {
246 2 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.2
247 0 $i = $a / $wm;
248 0 $j = $b / $hm;
249 }
250 0 $a %= $wm;
251 0 $b %= $hm;
252  
253 0 ▶ return if $a >= $self->_width or $b >= $self->_height;
Branch: 1 branch
BranchTrueFalseTotal
if $a >= $self->_width or $b >= $self->_height 0 0 0
Condition0%0/3
$a >= $self->_width or $b >= $self->_height
l!l&&r!l&&!r
000
MC/DC0%0/2
$a >= $self->_width or $b >= $self->_height
$a >= $self->_width $b >= $self->_height
Truth table0%0/3
A or B
A= $a >= $self->_widthB= $b >= $self->_height
ABresult
1X1
011
000
254  
255 0 my $tree;
256 0 ▶ if ( $i < 0 or $j < 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < 0 or $j < 0) { } 0 0 0
elsif (defined $self->_cur_layout->[$i][$j]) { } 0 0 0
Condition0%0/3
$i < 0 or $j < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$i < 0 or $j < 0
$i < 0 $j < 0
Truth table0%0/3
A or B
A= $i < 0B= $j < 0
ABresult
1X1
011
000
257 0 $tree = { 'label' => $self->_tag_none, 'visible' => 0 };
258 }
259 elsif ( defined $self->_cur_layout->[$i]->[$j] ) {
260 0 $tree = $self->_cur_layout->[$i]->[$j];
261 }
262 else {
263 0 $tree = { 'label' => $self->_tag_none, 'visible' => 0 };
264 }
265 0 return ( $i * $wm + $self->_margin, $j * $hm + $self->_margin, $tree );
266 }
267  
268 sub _mouse_move {
269 0 ▶ my ( $self, $canvas ) = @_;
Subroutine _mouse_move: not called CC=15 SCAR=54.8
270 0 my ( $x, $y, $tree ) = $self->_get_pos( $Tk::event->x, $Tk::event->y );
271  
272 0 ▶ if ( $self->_cur_tree and (not $tree or $tree->{'label'} ne $self->_cur_tree->{'label'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cur_tree and !$tree || $tree->{"label"} ne $self->_cur_tree->{"label"}) 0 0 0
Condition0%0/6
$self->_cur_tree and !$tree || $tree->{"label"} ne $self->_cur_tree->{"label"}
!ll&&!rl&&r
000
not $tree or $tree->{"label"} ne $self->_cur_tree->{"label"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->_cur_tree and !$tree || $tree->{"label"} ne $self->_cur_tree->{"label"}
$self->_cur_tree not $tree $tree->{"label"} ne $self->_cur_tree->{"label"}
Truth table0%0/4
A and not(B) or C
A= $self->_cur_treeB= not $treeC= $tree->{"label"} ne $self->_cur_tree->{"label"}
ABCresult
0XX0
1000
11X1
1011
273 0 ▶ if ( $self->_cur_tree->{'label'} ne $self->_tag_wrap and $self->_cur_tree->{'label'} ne $self->_tag_none ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cur_tree->{'label'} ne $self->_tag_wrap and $self->_cur_tree->{'label'} ne $self->_tag_none) { } 0 0 0
Condition0%0/3
$self->_cur_tree->{"label"} ne $self->_tag_wrap and $self->_cur_tree->{"label"} ne $self->_tag_none
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_cur_tree->{"label"} ne $self->_tag_wrap and $self->_cur_tree->{"label"} ne $self->_tag_none
$self->_cur_tree->{"label"} ne $self->_tag_wrap $self->_cur_tree->{"label"} ne $self->_tag_none
Truth table0%0/3
A and B
A= $self->_cur_tree->{"label"} ne $self->_tag_wrapB= $self->_cur_tree->{"label"} ne $self->_tag_none
ABresult
0X0
100
111
274 0 $canvas->itemconfigure( $self->_cur_tree->{'label'} . '&&' . $self->_tag_tree, -outline => 'black', -width => 1 );
275 }
276 else {
277 0 $canvas->delete( $self->_cur_tree->{'label'} );
278 }
279 0 $self->{_cur_tree} = undef;
280 }
281 0 ▶ if ( $tree and (not $self->_cur_tree or $tree->{'label'} ne $self->_cur_tree->{'label'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree and !$self->_cur_tree || $tree->{"label"} ne $self->_cur_tree->{"label"}) 0 0 0
Condition0%0/6
$tree and !$self->_cur_tree || $tree->{"label"} ne $self->_cur_tree->{"label"}
!ll&&!rl&&r
000
not $self->_cur_tree or $tree->{"label"} ne $self->_cur_tree->{"label"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$tree and !$self->_cur_tree || $tree->{"label"} ne $self->_cur_tree->{"label"}
$tree not $self->_cur_tree $tree->{"label"} ne $self->_cur_tree->{"label"}
Truth table0%0/4
A and not(B) or C
A= $treeB= not $self->_cur_treeC= $tree->{"label"} ne $self->_cur_tree->{"label"}
ABCresult
0XX0
1000
11X1
1011
282 0 ▶ if ( $tree->{'label'} ne $self->_tag_wrap and $tree->{'label'} ne $self->_tag_none ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tree->{'label'} ne $self->_tag_wrap and $tree->{'label'} ne $self->_tag_none) { } 0 0 0
elsif ($self->_drag_tree) { } 0 0 0
Condition0%0/3
$tree->{"label"} ne $self->_tag_wrap and $tree->{"label"} ne $self->_tag_none
!ll&&!rl&&r
000
MC/DC0%0/2
$tree->{"label"} ne $self->_tag_wrap and $tree->{"label"} ne $self->_tag_none
$tree->{"label"} ne $self->_tag_wrap $tree->{"label"} ne $self->_tag_none
Truth table0%0/3
A and B
A= $tree->{"label"} ne $self->_tag_wrapB= $tree->{"label"} ne $self->_tag_none
ABresult
0X0
100
111
283 0 ▶ my $color = $self->_drag_tree ? ( $tree->{'label'} eq $self->_drag_tree->{'label'} ? 'red' : 'green' ) : 'blue';
Branch: 2 branches
BranchTrueFalseTotal
$self->_drag_tree ? : 0 0 0
$tree->{'label'} eq $self->_drag_tree->{'label'} ? : 0 0 0
284 0 $canvas->itemconfigure( $tree->{'label'}.'&&'.$self->_tag_tree, -outline => $color, -width => 2 );
285 }
286 elsif ( $self->_drag_tree ) {
287 0 ▶ my $color = $tree->{'label'} eq $self->_tag_wrap ? 'green' : 'red';
Branch: 1 branch
BranchTrueFalseTotal
$tree->{'label'} eq $self->_tag_wrap ? : 0 0 0
288 $canvas->create(
289 'rectangle',
290 $x, $y, $x + $self->_width, $y + $self->_height,
291 0 -tags => [$tree->{'label'}], -outline => $color, -width => 2
292 );
293 }
294 0 $self->{_cur_tree} = $tree;
295 }
296 0 return;
297 }
298  
299 sub _mouse_drag {
300 0 ▶ my ( $self, $canvas ) = @_;
Subroutine _mouse_drag: not called CC=4 SCAR=30.0
301 0 my ( $x, $y, $tree ) = $self->_get_pos( $Tk::event->x, $Tk::event->y );
302 0 ▶ return if ( not $tree ) or $tree->{'label'} eq $self->_tag_wrap or $tree->{'label'} eq $self->_tag_none;
Branch: 1 branch
BranchTrueFalseTotal
if not $tree or $tree->{"label"} eq $self->_tag_wrap or $tree->{"label"} eq $self->_tag_none 0 0 0
Condition0%0/6
not $tree or $tree->{"label"} eq $self->_tag_wrap or $tree->{"label"} eq $self->_tag_none
l!l&&r!l&&!r
000
not $tree or $tree->{"label"} eq $self->_tag_wrap
l!l&&r!l&&!r
000
MC/DC0%0/3
not $tree or $tree->{"label"} eq $self->_tag_wrap or $tree->{"label"} eq $self->_tag_none
not $tree $tree->{"label"} eq $self->_tag_wrap $tree->{"label"} eq $self->_tag_none
Truth table0%0/4
not(A) or B or C
A= not $treeB= $tree->{"label"} eq $self->_tag_wrapC= $tree->{"label"} eq $self->_tag_none
ABCresult
1XX1
01X1
0011
0000
303  
304 0 $self->{_drag_tree} = $tree;
305 0 ( $self->{_drag_x}, $self->{_drag_y} ) = $self->_get_layout_coords( $x, $y );
306 0 $canvas->itemconfigure( $tree->{'label'} . '&&' . $self->_tag_tree, -outline => 'red', -width => 2, -fill => 'yellow' );
307 0 return;
308 }
309  
310 sub _mouse_drop {
311 0 ▶ my ( $self, $canvas ) = @_;
Subroutine _mouse_drop: not called CC=8 SCAR=42.8
312 0 ▶ return unless $self->_drag_tree;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_drag_tree 0 0 0
313  
314 0 my ( $x, $y, $tree ) = $self->_get_pos( $Tk::event->x, $Tk::event->y );
315  
316 0 $canvas->itemconfigure( $self->_drag_tree->{'label'} . '&&' . $self->_tag_tree, -fill => 'white' );
317  
318 0 ▶ if ( ( not $tree ) or $tree->{'label'} eq $self->_tag_none ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $tree or $tree->{"label"} eq $self->_tag_none) 0 0 0
Condition0%0/3
not $tree or $tree->{"label"} eq $self->_tag_none
l!l&&r!l&&!r
000
MC/DC0%0/2
not $tree or $tree->{"label"} eq $self->_tag_none
not $tree $tree->{"label"} eq $self->_tag_none
Truth table0%0/3
not(A) or B
A= not $treeB= $tree->{"label"} eq $self->_tag_none
ABresult
1X1
011
000
319 0 ▶ $canvas->delete( $self->_tag_none ) if $tree;
Branch: 1 branch
BranchTrueFalseTotal
if $tree 0 0 0
320 0 $self->{_drag_tree} = undef;
321 0 $self->{_drag_x} = $self->{_drag_y} = -1;
322 0 return;
323 }
324  
325 0 my $layout = $self->_cur_layout;
326  
327 0 $layout->[ $self->_drag_x ]->[ $self->_drag_y ] = undef;
328 0 ( $x, $y ) = $self->_get_layout_coords( $x, $y );
329 0 ▶ if ( $tree->{'label'} ne $self->_tag_wrap ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->{"label"} ne $self->_tag_wrap) 0 0 0
330 0 ▶ for ( my $i = scalar @$layout; $i > $x; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $x) 0 0 0
331 0 ▶ if ( defined $layout->[ $i - 1 ]->[$y] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$layout[$i - 1][$y]) 0 0 0
332 0 $layout->[$i]->[$y] = $layout->[ $i - 1 ]->[$y];
333 0 $layout->[ $i - 1 ]->[$y] = undef;
334 }
335 }
336 }
337 0 $layout->[$x]->[$y] = $self->_drag_tree;
338  
339 0 $self->{_cur_layout} = $layout;
340 0 $self->_normalize_layout();
341 0 $self->_move_layout( 1, 1 );
342 0 $self->_wrap_layout();
343 0 $self->_draw_layout($canvas);
344  
345 0 $self->{_drag_tree} = undef;
346 0 $self->{_drag_x} = $self->{_drag_y} = -1;
347 0 return;
348 }
349  
350 sub _mouse_right {
351 0 ▶ my ( $self, $canvas ) = @_;
Subroutine _mouse_right: not called CC=7 SCAR=40.3
352 0 my ( $x, $y, $tree ) = $self->_get_pos( $Tk::event->x, $Tk::event->y );
353 0 ▶ return if ( not $tree ) or $tree->{'label'} eq $self->_tag_wrap or $tree->{'label'} eq $self->_tag_none or $self->{_drag_tree};
Branch: 1 branch
BranchTrueFalseTotal
if not $tree or $tree->{"label"} eq $self->_tag_wrap or $tree->{"label"} eq $self->_tag_none or $self->{"_drag_tree"} 0 0 0
Condition0%0/9
not $tree or $tree->{"label"} eq $self->_tag_wrap or $tree->{"label"} eq $self->_tag_none or $self->{"_drag_tree"}
l!l&&r!l&&!r
000
not $tree or $tree->{"label"} eq $self->_tag_wrap or $tree->{"label"} eq $self->_tag_none
l!l&&r!l&&!r
000
not $tree or $tree->{"label"} eq $self->_tag_wrap
l!l&&r!l&&!r
000
MC/DC0%0/4
not $tree or $tree->{"label"} eq $self->_tag_wrap or $tree->{"label"} eq $self->_tag_none or $self->{"_drag_tree"}
not $tree $tree->{"label"} eq $self->_tag_wrap $tree->{"label"} eq $self->_tag_none $self->{"_drag_tree"}
Truth table0%0/5
not(A) or B or C or D
A= not $treeB= $tree->{"label"} eq $self->_tag_wrapC= $tree->{"label"} eq $self->_tag_noneD= $self->{"_drag_tree"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
354  
355 0 ( $x, $y ) = $self->_get_layout_coords( $x, $y );
356 0 my $layout = $self->_cur_layout;
357 0 ▶ my $visibility = $layout->[$x]->[$y]->{'visible'} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$layout->[$x][$y]{'visible'} ? : 0 0 0
358  
359 0 $layout->[$x]->[$y]->{'visible'} = $visibility;
360 0 ▶ $canvas->itemconfigure( $tree->{'label'} . '&&' . $self->_tag_tree, -fill => $visibility ? 'white' : 'grey' );
Branch: 1 branch
BranchTrueFalseTotal
$visibility ? : 0 0 0
361 0 $self->{_cur_layout} = $layout;
362 0 return;
363 }
364  
365 sub _draw_layout {
366 0 ▶ my ( $self, $canvas ) = @_;
Subroutine _draw_layout: not called CC=7 SCAR=40.3
367 0 my $layout = $self->_cur_layout;
368  
369 0 $canvas->delete('all');
370 0 ▶ for ( my $i = 0; $i < scalar @$layout; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$layout) 0 0 0
371 0 ▶ for ( my $j = 0; $j < scalar @{ $layout->[$i] }; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @{$layout->[$i];}) 0 0 0
372 0 my $tree = $layout->[$i]->[$j];
373 0 ▶ if ( $tree and $tree->{'label'} ne $self->_tag_wrap ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree and $tree->{"label"} ne $self->_tag_wrap) 0 0 0
Condition0%0/3
$tree and $tree->{"label"} ne $self->_tag_wrap
!ll&&!rl&&r
000
MC/DC0%0/2
$tree and $tree->{"label"} ne $self->_tag_wrap
$tree $tree->{"label"} ne $self->_tag_wrap
Truth table0%0/3
A and B
A= $treeB= $tree->{"label"} ne $self->_tag_wrap
ABresult
0X0
100
111
374 0 my ( $lang, $layer, $selector ) = split '-', $tree->{'label'}, 3;
375 0 $lang = Treex::Core::Types::get_lang_name($lang);
376 $canvas->create(
377 'rectangle',
378 $i * ( $self->_width + $self->_margin ) + $self->_margin,
379 $j * ( $self->_height + $self->_margin ) + $self->_margin,
380 ( $i + 1 ) * ( $self->_width + $self->_margin ),
381 ( $j + 1 ) * ( $self->_height + $self->_margin ),
382 -tags => [ $self->_tag_tree, $tree->{'label'} ],
383 0 ▶ -fill => $tree->{'visible'} ? 'white' : 'grey'
Branch: 1 branch
BranchTrueFalseTotal
$tree->{'visible'} ? : 0 0 0
384 );
385 $canvas->create(
386 $self->_tag_text,
387 ( $i + 1 ) * ( $self->_width + $self->_margin ) - 0.5 * $self->_width,
388 ( $j + 1 ) * ( $self->_height + $self->_margin ) - 0.5 * $self->_height,
389 -anchor => 'center',
390 -justify => 'center',
391 0 ▶ -tags => [$tree->{'label'}],
Branch: 1 branch
BranchTrueFalseTotal
$selector ? : 0 0 0
392 -text => "$lang\n" . uc($layer) . ( $selector ? "\n$selector" : '' )
393 );
394 }
395 }
396 }
397  
398 0 $canvas->CanvasBind( '<Motion>' => [ $self => '_mouse_move', $canvas ] );
399 0 $canvas->CanvasBind( '<ButtonPress-1>' => [ $self => '_mouse_drag', $canvas ] );
400 0 $canvas->CanvasBind( '<ButtonRelease-1>' => [ $self => '_mouse_drop', $canvas ] );
401 0 $canvas->CanvasBind( '<ButtonRelease-3>' => [ $self => '_mouse_right', $canvas ] );
402 0 return;
403 }
404  
405 sub conf_dialog {
406 0 ▶ my $self = shift;
Subroutine conf_dialog: not called CC=2 SCAR=17.9
Pod documented
407  
408 0 $self->{_cur_layout} = $self->get_layout();
409 0 $self->_normalize_layout();
410 0 $self->_move_layout( 1, 1 );
411 0 $self->_wrap_layout();
412  
413 0 my $top = TredMacro::ToplevelFrame();
414 0 my $dialog = $top->DialogBox( -title => "Trees layout configuration", -buttons => [ "OK", "Cancel" ] );
415 0 my $canvas = $dialog->add(
416 'Canvas',
417 -width => 7 * $self->_width + 8 * $self->_margin,
418 -height => 5 * $self->_height + 6 * $self->_margin
419 );
420  
421 0 $self->_draw_layout($canvas);
422 0 $canvas->pack( -expand => 1, -fill => 'both' );
423  
424 0 my $button = $dialog->Show();
425 0 ▶ if ( $button eq 'OK' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($button eq 'OK') { } 0 0 0
426 0 $self->_normalize_layout();
427 0 $self->_layouts->{ $self->get_layout_label() } = $self->_cur_layout;
428 0 return 1;
429 }
430 else {
431 0 return 0;
432 }
433 }
434  
435 2 1;
436  
437 __END__
438  
439 =head1 NAME
440  
441 Treex::Core::TredView::TreeLayout - Layout of trees in Tred
442  
443 =head1 VERSION
444  
445 version 0.08633_1
446  
447 =head1 DESCRIPTION
448  
449 This package supports the main Tred visualization package Treex::Core::TredView.
450 It's purpose is to allow an user friendly configuration of the placement of
451 the trees stored in a bundle.
452  
453 The mechanism works only with bundles. Each bundle gets a label that describes
454 it's content (combination of layers, languages and selectors). When there is a
455 bundle to be displayed, the package constructs its label and tries to find its
456 layout. If the label is unknown (no layout found), default is provided.
457  
458 Therefore it's perfectly legal to have different sets of trees in each bundle in
459 a single file. Also, when a layout is configured for some set of trees, it will
460 be used each time the same set is displayed even if these sets occur in
461 completely different and unrelated files.
462  
463 The configuration is persistent - it is saved along with the tred extension in
464 a special file.
465  
466 =head1 METHODS
467  
468 =head2 Public methods
469  
470 =over 4
471  
472 =item get_tree_label
473  
474 =item get_layout_label
475  
476 =item get_layout
477  
478 =item load_layouts
479  
480 =item save_layouts
481  
482 =item conf_dialog
483  
484 =back
485  
486 =head2 Private methods
487  
488 =over 4
489  
490 =item _move_layout
491  
492 =item _wrap_layout
493  
494 =item _normalize_layout
495  
496 =item _get_layout_coords
497  
498 =item _get_pos
499  
500 =item _mouse_move
501  
502 =item _mouse_drag
503  
504 =item _mouse_drop
505  
506 =item _draw_layout
507  
508 =back
509  
510 =head1 AUTHOR
511  
512 Josef Toman <toman@ufal.mff.cuni.cz>
513  
514 =head1 COPYRIGHT AND LICENSE
515  
516 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
517  
518 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« lib/Treex/Core/TredView/Labels.pm ↑ Summary lib/Treex/Core/Resource.pm »