File: tkview.pm

package info (click to toggle)
libpdl-io-hdf5-perl 1:0.73-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 596 kB
  • sloc: perl: 1,544; makefile: 14
file content (315 lines) | stat: -rwxr-xr-x 6,877 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package PDL::IO::HDF5::tkview;

# Experimental module to view HDF5 using perl/tk and PDL::IO::HDF5 modules

use Tk 800;
use Tk::Tree;
use IO::File;

=head1 NAME

PDL::IO::HDF5::tkview - View HDF5 files using perl/tk and PDL::IO::HDF5 modules

=head1 DESCRIPTION

This is a experimental object to view HDF5 files the PDL::IO::HDF5 module.
The HDF files are displayed in a tree structure using Tk::Tree

=head1 SYNOPSIS

 use Tk;
 use PDL::IO::HDF5::tkview
 use PDL::IO::HDF5;

 my $mw = MainWindow->new;

 
 my $h5 = new PDL::IO::HDF5('datafile.h5');  # open HDF5 file object

 my $tkview = new PDL::IO::HDF5::tkview( $mw, $h5);
 
 MainLoop;

=head1 MEMBER DATA

=over 1

=item mw

Tk window where the file structure is displayed.

=item H5obj

PDL::IO::HDF5 Object 

=item hl

Tk Hlist object 

=item dataDisplaySub

Sub ref to execute when a dataset is double-clicked. This defaults to a print of the dataset. See 
L<dataDisplaySubSet> for details.

Tk Hlist object 

=back

=head1 METHODS

####---------------------------------------------------------

=head2 new

=for ref

PDL::IO::HDF5::tkview Constructor - creates new object

B<Usage:>

=for usage

 $tkview = new PDL::IO::HDF5::tkview( $mw, $H5obj);
 
    Where:
	$mw     Tk window 
	$H5obj	PDL::IO::HDF5::Object

=cut

# Cube Image Pixmap (ppm) format. raw data string
$cubeImage = 
'/* XPM */
static char * cube_xpm[] = {
"12 12 3 1",
" 	c #FFFFFFFFFFFF",
".	c #000000000000",
"X	c #FFFFFFFF0000",
"    ........",
"   .XXXXXX..",
"  .XXXXXX.X.",
" ........XX.",
" .XXXXXX.XX.",
" .XXXXXX.XX.",
" .XXXXXX.XX.",
" .XXXXXX.XX.",
" .XXXXXX.X. ",
" .XXXXXX..  ",
" ........   ",
"            "};';
# -----------------------------------------------
#   Routine to create the array_display window
sub new{


	my $type = shift; # get the class type
	my $mw = $_[0];
	my $H5obj = $_[1];
	
	my $self = {};
	
	
	# setup member variables:
	$self->{mw} = $mw;
	$self->{H5obj} = $H5obj;
	
	bless $self, $type;
		
	# setup the window
	if (defined $H5obj){
	
		
		  my $hl = $mw->Scrolled('Tree',-separator => $;,-drawbranch => 1, -width => '15', -bg => 'white');
		  $hl->configure(-opencmd => [\&More,$self, $hl]); 
		  $hl->configure(-command => [\&activateCmd,$self]);  # command to called when entry double-clicked
		  my $name = $H5obj->filename;
		  $hl->add($name, -text => $name, -data => $H5obj, -itemtype => 'imagetext');
		  $hl->setmode($name => 'close');
		  
 		   # Get Images for display
		   $self->{groupImage} = $mw->Pixmap(-file => Tk->findINC('winfolder.xpm') );
		   $self->{cubeImage} = $mw->Pixmap(-data => $cubeImage );
		
		  
		  AddChildren($self,$hl,$name,$H5obj);
		
		  $hl->pack(-expand=> 1, -fill => 'both');

		  $self->{hl} = $hl;
		
		  # Set Default dataDisplaySub
		  $self->{dataDisplaySub} = sub{ print $_[0]};


	 }
	

	return $self;

}




#  sub to add elements to the hlist after an element in the list has been expanded (i.e. clicked-on)
sub AddChildren
{ 

	 my $self = shift;
	
	 my ($hl,$path,$data) = @_;  # hl list object, location, data
	 my $w;
	 my $name;
	 my $text;
	  
	 if( ref($data) =~ /Group/ || !($path =~ /$;/ ) ){ # Current Item to expand is a group or top level of file
	
       		# Display any Attributes First:
		my @attrs;  # attributes stored
		my %attrs;  

		@attrs = sort $data->attrs;  

		if( @attrs){  # set attribute hash if there are attributes
			@attrs{@attrs} = $data->attrGet(@attrs); # attrget not defined yet
		}
		my ($attr, $attrValue);
		foreach $attr(@attrs){  # add each attribute to the display
			$attrValue = $attrs{$attr};
			$text = "$attr: $attrValue";
			$hl->add("$path$;"."_Attr$attr",  -text => $text, -data => $attrValue);

		}

		# Display Datasets next:
		my @datasets;  # dataset names stored
		@datasets = sort $data->datasets; # get list of datasets in the current group/file

		my ($dataset, @dims);
		foreach $dataset(@datasets){  # add each attribute to the display
			my $datasetData = $data->dataset($dataset);
			@dims = $datasetData->dims;  # get the dims of the dataset
			if( @dims){  # > 0-dimensional dataset
				$text = "$dataset: Dims ".join(", ",@dims);
			}
			else{ # zero-dimensional dataset
				$text = "$dataset: ".$datasetData->get;
			}
			$hl->add("$path$;"."_Dset$dataset", -image => $self->{cubeImage}, -text => $text, -data => $data);

		}


		# Display Groups Next

		my @groups;  # groups stored

		@groups = sort $data->groups;  

		my ($group, $groupName);
		foreach $groupName(@groups){  # Add each group to the display

			# data element is the parent object and the group name.
			$hl->add("$path$;"."_Group$groupName", -image => $self->{groupImage}, -text => $groupName, -data => [ $data,$groupName] );
			$hl->setmode( "$path$;"."_Group$groupName",  "open");
		}





	 }
	
	
}
#   This Sub called when a element of the H-list is expanded/collapsed. (i.e. clicked-on)
sub More
{
	 my $self = shift;   
	 my ($w,$item) = @_;  # hl list object, hlist item name
	 
	 if( defined $w->info('children',$item) > 0){  #get rid of old elements if it has already been opened
		# print "Has children\n";
		$w->delete('offsprings',$item);
	 }


	 # print "item = $item\n";
	 my $data = $w->entrycget($item,'-data');  #get the data ref for this entry
	 
	 my @levels = split($;,$item);
	
	 if( @levels && ( $levels[-1] =~ /^_Group/) ){ # if this is a group then get the group object
	
		 my ($obj, $groupName) = @$data;
		 $data = $obj->group($groupName);
	 }
	
	 $self->AddChildren($w,$item,$data);

}


=head2 dataDisplaySubSet

=for ref

Set the dataDisplaySub data member.

B<Usage:>

=for usage

 # Data Display sub to call when a dataset is double-clicked
 my $dataDisplay = sub{ my $data = $_[0]; print "I'm Displaying This $data\n";};
 $tkview->dataDisplaySubSet($dataDisplay);


The dataDisplaySub data member is a perl sub ref that is called when a dataset is double-clicked. 
This data member is initially set to just print the dataset's data to the command line. Using the L<dataDisplaySubSet>
method, different actions for displaying the data can be "plugged-in".

=cut

sub dataDisplaySubSet {
	my ($self, $subref) = @_;

	$self->{dataDisplaySub} = $subref;
}

#-------------------------------------------------------------------

=head2 activateCmd

=for ref

Internal Display method invoked whenever a tree element is activated (i.e.
double-clicked). This method does nothing unless a dataset element has been
selected. It that cases it calls $self->dataDisplaySub with the data.


=cut

sub activateCmd{

	my $self = shift;   

	my ($name) = (@_);  # Name of the hlist element that was selected
	

	return unless($name =~ /$;_Dset(.+)$/);  # only process datasets
	my $datasetName = $1;
	my $hlist = $self->{hl};

	
	my $group = $hlist->entrycget($name,'-data');
	my $PDL = $group->dataset($datasetName)->get;

	my $dataDisplaySub = $self->{dataDisplaySub};
	&$dataDisplaySub($PDL)

}



1;