Treex/Core/TredView/TreeLayout.pm

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