Geoinformatica  0.90
Symbols.pm
Go to the documentation of this file.
1 package Gtk2::Ex::Geo::Dialogs::Symbols;
2 
3 
4 use strict;
5 use warnings;
6 use Carp;
7 use Glib qw/TRUE FALSE/;
8 
9 ## @method open_symbols_dialog($gui)
10 # @brief Open the symbols dialog for this layer.
11 sub open {
12  my($self, $gui) = @_;
13 
14  my $dialog = $self->bootstrap_dialog
15  ($gui, 'symbols_dialog', "Symbols for ".$self->name,
16  {
17  symbols_dialog => [delete_event => \&cancel_symbols, [$self, $gui]],
18  symbols_scale_button => [clicked => \&fill_symbol_scale_fields, [$self, $gui]],
19  symbols_field_combobox => [changed=>\&symbol_field_changed, [$self, $gui]],
20  symbols_type_combobox => [changed=>\&symbol_field_changed, [$self, $gui]],
21  symbols_apply_button => [clicked => \&apply_symbols, [$self, $gui, 0]],
22  symbols_cancel_button => [clicked => \&cancel_symbols, [$self, $gui]],
23  symbols_ok_button => [clicked => \&apply_symbols, [$self, $gui, 1]],
24  });
25 
26  my $symbol_type_combo = $dialog->get_widget('symbols_type_combobox');
27  my $field_combo = $dialog->get_widget('symbols_field_combobox');
28  my $scale_min = $dialog->get_widget('symbols_scale_min_entry');
29  my $scale_max = $dialog->get_widget('symbols_scale_max_entry');
30  my $size_spin = $dialog->get_widget('symbols_size_spinbutton');
31 
32  # back up data
33 
34  my $symbol_type = $self->symbol_type();
35  my $size = $self->symbol_size();
36  my $field = $self->symbol_field();
37  my @scale = $self->symbol_scale();
38  $self->{backup}->{symbol_type} = $symbol_type;
39  $self->{backup}->{symbol_size} = $size;
40  $self->{backup}->{symbol_field} = $field;
41  $self->{backup}->{symbol_scale} = \@scale;
42 
43  # set up the controllers
44 
45  fill_symbol_type_combo($self, $symbol_type);
46  fill_symbol_field_combo($self, $field);
47  $scale_min->set_text($scale[0]);
48  $scale_max->set_text($scale[1]);
49  $size_spin->set_value($size);
50  return $self->{symbols_dialog}->get_widget('symbols_dialog');
51 }
52 
53 sub apply_symbols {
54  my($self, $gui, $close) = @{$_[1]};
55  my $dialog = $self->{symbols_dialog};
56 
57  my $symbol_type = get_selected_symbol_type($self);
58  $self->symbol_type($symbol_type);
59  my $field_combo = $dialog->get_widget('symbols_field_combobox');
60  my $field = $self->{index2symbol_field}{$field_combo->get_active()};
61  $self->symbol_field($field) if defined $field;
62  my $scale_min = $dialog->get_widget('symbols_scale_min_entry');
63  my $scale_max = $dialog->get_widget('symbols_scale_max_entry');
64  $self->symbol_scale($scale_min->get_text(), $scale_max->get_text());
65  my $size_spin = $dialog->get_widget('symbols_size_spinbutton');
66  my $size = $size_spin->get_value();
67  $self->symbol_size($size);
68 
69  $self->hide_dialog('symbols_dialog') if $close;
70  $gui->set_layer($self);
71  $gui->{overlay}->render;
72 }
73 
74 sub cancel_symbols {
75  my($self, $gui);
76  for (@_) {
77  next unless ref CORE::eq 'ARRAY';
78  ($self, $gui) = @{$_};
79  }
80 
81  $self->symbol_type($self->{backup}->{symbol_type});
82  $self->symbol_field($self->{backup}->{symbol_field}) if $self->{backup}->{symbol_field};
83  $self->symbol_scale(@{$self->{backup}->{symbol_scale}});
84  $self->symbol_size($self->{backup}->{symbol_size});
85 
86  $self->hide_dialog('symbols_dialog');
87  $gui->set_layer($self);
88  $gui->{overlay}->render;
89  1;
90 }
91 
92 sub fill_symbol_type_combo {
93  my($self, $symbol_type) = @_;
94  $symbol_type = '' unless defined $symbol_type;
95  my $combo = $self->{symbols_dialog}->get_widget('symbols_type_combobox');
96  my $model = $combo->get_model;
97  $model->clear;
98  my @symbol_types = $self->supported_symbol_types();
99  my $i = 0;
100  my $active = 0;
101  for (@symbol_types) {
102  $model->set ($model->append, 0, $_);
103  $self->{index2symbol_type}{$i} = $_;
104  $self->{symbol_type2index}{$_} = $i;
105  $active = $i if $_ eq $symbol_type;
106  $i++;
107  }
108  $combo->set_active($active);
109 }
110 
111 sub get_selected_symbol_type {
112  my $self = shift;
113  my $combo = $self->{symbols_dialog}->get_widget('symbols_type_combobox');
114  ($self->{index2symbol_type}{$combo->get_active()} or '');
115 }
116 
117 sub fill_symbol_field_combo {
118  my($self, $symbol_field) = @_;
119  my $combo = $self->{symbols_dialog}->get_widget('symbols_field_combobox');
120  my $model = $combo->get_model;
121  $model->clear;
122  delete $self->{index2symbol_field};
123  my $active = 0;
124  my $i = 0;
125 
126  my $name = 'Fixed size';
127  $model->set($model->append, 0, $name);
128  $active = $i if $name eq $self->symbol_field();
129  $self->{index2symbol_field}{$i} = $name;
130  $i++;
131  for my $field ($self->schema()->fields) {
132  next unless $field->{Type};
133  next unless $field->{Type} eq 'Integer' or $field->{Type} eq 'Real';
134  $model->set($model->append, 0, $field->{Name});
135  $active = $i if $field->{Name} eq $symbol_field;
136  $self->{index2symbol_field}{$i} = $field->{Name};
137  $i++;
138  }
139  $combo->set_active($active);
140 }
141 
142 sub get_selected_symbol_field {
143  my $self = shift;
144  my $combo = $self->{symbols_dialog}->get_widget('symbols_field_combobox');
145  ($self->{index2symbol_field}{$combo->get_active()} or '');
146 }
147 
148 sub fill_symbol_scale_fields {
149  my($self, $gui) = @{$_[1]};
150  my @range;
151  my $field = get_selected_symbol_field($self);
152  return if $field eq 'Fixed size';
153  my @r = $gui->{overlay}->get_viewport_of_selection;
154  @r = $gui->{overlay}->get_viewport unless @r;
155  eval {
156  @range = $self->value_range(field_name => $field, filter_rect => \@r);
157  };
158  if ($@) {
159  $gui->message("$@");
160  return;
161  }
162  $self->{symbols_dialog}->get_widget('symbols_scale_min_entry')->set_text($range[0]);
163  $self->{symbols_dialog}->get_widget('symbols_scale_max_entry')->set_text($range[1]);
164 }
165 
166 sub symbol_field_changed {
167  my($self, $gui) = @{$_[1]};
168  my $type = get_selected_symbol_type($self);
169  my $field = get_selected_symbol_field($self);
170  my $dialog = $self->{symbols_dialog};
171  if ($type eq 'No symbol') {
172  $dialog->get_widget('symbols_size_spinbutton')->set_sensitive(0);
173  $dialog->get_widget('symbols_field_combobox')->set_sensitive(0);
174  } else {
175  $dialog->get_widget('symbols_size_spinbutton')->set_sensitive(1);
176  $dialog->get_widget('symbols_field_combobox')->set_sensitive(1);
177  }
178  if (!$field or $field eq 'Fixed size') {
179  $dialog->get_widget('symbols_scale_min_entry')->set_sensitive(0);
180  $dialog->get_widget('symbols_scale_max_entry')->set_sensitive(0);
181  $dialog->get_widget('symbols_size_label')->set_text('Size: ');
182  } else {
183  $dialog->get_widget('symbols_scale_min_entry')->set_sensitive(1);
184  $dialog->get_widget('symbols_scale_max_entry')->set_sensitive(1);
185  $dialog->get_widget('symbols_size_label')->set_text('Maximum size: ');
186  }
187 }
188 
189 1;