~ubuntu-branches/ubuntu/utopic/padre/utopic

« back to all changes in this revision

Viewing changes to lib/Padre/Wx/Role/Config.pm

  • Committer: Package Import Robot
  • Author(s): Dominique Dumont
  • Date: 2012-01-25 16:16:07 UTC
  • mfrom: (1.3.4)
  • Revision ID: package-import@ubuntu.com-20120125161607-ydc0rgxlfqk2ctrb
Tags: 0.94+dfsg1-1
* new upstream version
* watch: updated to match dfsg tag
* debian/not-real-manual.list: updated lib/Padre/Document/Perl.pm entry
* debian/copyright:
  * fixed DEP-5 syntax errors
  * updated copyright years.
  * reformatted with cme
* control: updated dependency list

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
use 5.008;
7
7
use strict;
8
8
use warnings;
9
 
use Padre::Wx ();
 
9
use Params::Util    ();
 
10
use Padre::Constant ();
 
11
use Padre::Wx       ();
10
12
 
11
 
our $VERSION = '0.92';
 
13
our $VERSION = '0.94';
12
14
 
13
15
sub config_load {
14
16
        my $self   = shift;
15
17
        my $config = shift;
16
18
 
17
 
        # Iterate over the specified config elements
18
19
        foreach my $name (@_) {
19
 
                next unless $self->can($name);
20
 
 
21
 
                # Get the Wx element for this option
22
 
                my $setting = $config->meta($name);
23
 
                my $value   = $config->$name();
24
 
                my $ctrl    = $self->$name();
25
 
 
26
 
                # Apply this one setting to this one widget
27
 
                if ( $ctrl->can('config_load') ) {
28
 
                        # Allow specialised widgets to load their own setting
29
 
                        $ctrl->config_load( $setting, $value );
30
 
 
31
 
                } elsif ( $ctrl->isa('Wx::CheckBox') ) {
32
 
                        $ctrl->SetValue($value);
33
 
 
34
 
                } elsif ( $ctrl->isa('Wx::TextCtrl') ) {
35
 
                        $ctrl->SetValue($value);
36
 
 
37
 
                } elsif ( $ctrl->isa('Wx::SpinCtrl') ) {
38
 
                        $ctrl->SetValue($value);
39
 
 
40
 
                } elsif ( $ctrl->isa('Wx::FilePickerCtrl') ) {
41
 
                        $ctrl->SetPath($value);
42
 
 
43
 
                } elsif ( $ctrl->isa('Wx::DirPickerCtrl') ) {
44
 
                        $ctrl->SetPath($value);
45
 
 
46
 
                } elsif ( $ctrl->isa('Wx::ColourPickerCtrl') ) {
47
 
                        $ctrl->SetColour( 
48
 
                                Padre::Wx::color($value)
49
 
                        );
50
 
 
51
 
                } elsif ( $ctrl->isa('Wx::FontPickerCtrl') ) {
52
 
                        my $font = Padre::Wx::native_font($value);
53
 
                        $ctrl->SetSelectedFont( $font ) if $font->IsOk;
54
 
 
55
 
                } elsif ( $ctrl->isa('Wx::Choice') ) {
56
 
                        my $options = $setting->options;
57
 
                        if ($options) {
58
 
                                $ctrl->Clear;
59
 
 
60
 
                                # NOTE: This assumes that the list will not be
61
 
                                # sorted in Wx via a style flag and that the
62
 
                                # order of the fields should be that of the key
63
 
                                # and not of the translated label.
64
 
                                # Doing sort in Wx will probably break this.
65
 
                                foreach my $option ( sort keys %$options ) {
66
 
                                        my $label = $options->{$option};
67
 
                                        $ctrl->Append(
68
 
                                                Wx::gettext($label),
69
 
                                                $option,
70
 
                                        );
71
 
                                        next unless $option eq $value;
72
 
                                        $ctrl->SetSelection( $ctrl->GetCount - 1 );
73
 
                                }
 
20
                my $meta  = $config->meta($name);
 
21
                my $value = $config->$name();
 
22
                $self->config_set( $meta => $value );
 
23
        }
 
24
 
 
25
        return 1;
 
26
}
 
27
 
 
28
sub config_set {
 
29
        my $self  = shift;
 
30
        my $meta  = shift;
 
31
        my $value = shift;
 
32
        my $name  = $meta->name;
 
33
 
 
34
        # Ignore config elements we don't have
 
35
        unless ( $self->can($name) ) {
 
36
                return undef;
 
37
        }
 
38
 
 
39
        # Apply to the relevant element
 
40
        my $ctrl = $self->$name();
 
41
        if ( $ctrl->can('config_set') ) {
 
42
                # Allow specialised widgets to load their own setting
 
43
                $ctrl->config_set( $meta, $value );
 
44
 
 
45
        } elsif ( $ctrl->isa('Wx::CheckBox') ) {
 
46
                $ctrl->SetValue($value);
 
47
 
 
48
        } elsif ( $ctrl->isa('Wx::TextCtrl') ) {
 
49
                $ctrl->SetValue($value);
 
50
 
 
51
        } elsif ( $ctrl->isa('Wx::SpinCtrl') ) {
 
52
                $ctrl->SetValue($value);
 
53
 
 
54
        } elsif ( $ctrl->isa('Wx::FilePickerCtrl') ) {
 
55
                $ctrl->SetPath($value);
 
56
 
 
57
        } elsif ( $ctrl->isa('Wx::DirPickerCtrl') ) {
 
58
                $ctrl->SetPath($value);
 
59
 
 
60
        } elsif ( $ctrl->isa('Wx::ColourPickerCtrl') ) {
 
61
                $ctrl->SetColour( 
 
62
                        Padre::Wx::color($value)
 
63
                );
 
64
 
 
65
        } elsif ( $ctrl->isa('Wx::FontPickerCtrl') ) {
 
66
                my $font = Padre::Wx::native_font($value);
 
67
                $ctrl->SetSelectedFont($font) if $font->IsOk;
 
68
 
 
69
        } elsif ( $ctrl->isa('Wx::Choice') ) {
 
70
                my $options = $meta->options;
 
71
                if ($options) {
 
72
                        $ctrl->Clear;
 
73
 
 
74
                        # NOTE: This assumes that the list will not be
 
75
                        # sorted in Wx via a style flag and that the
 
76
                        # order of the fields should be that of the key
 
77
                        # and not of the translated label.
 
78
                        # Doing sort in Wx will probably break this.
 
79
                        foreach my $option ( sort keys %$options ) {
 
80
                                my $label = $options->{$option};
 
81
                                $ctrl->Append(
 
82
                                        Wx::gettext($label),
 
83
                                        $option,
 
84
                                );
 
85
                                next unless $option eq $value;
 
86
                                $ctrl->SetSelection( $ctrl->GetCount - 1 );
74
87
                        }
75
 
 
76
 
                } else {
77
 
                        next;
78
88
                }
 
89
        } else {
 
90
                return 0;
79
91
        }
80
92
 
81
93
        return 1;
90
102
        my $diff = $self->config_diff( $config, @_ ) or return;
91
103
 
92
104
        # Lock most of Padre so any apply handlers run quickly
93
 
        my $lock = $self->main->lock( 'UPDATE', 'REFRESH', 'DB' );
 
105
        my $lock = $self->main->lock( qw{ UPDATE REFRESH AUI CONFIG DB } );
94
106
 
95
107
        # Apply the changes to the configuration
96
108
        foreach my $name ( sort keys %$diff ) {
97
109
                $config->apply( $name, $diff->{$name}, $current );
98
110
        }
99
111
 
100
 
        # Save the config file
101
 
        $config->write;
102
 
 
103
112
        return;
104
113
}
105
114
 
109
118
        my %diff   = ();
110
119
 
111
120
        foreach my $name (@_) {
112
 
                next unless $self->can($name);
113
 
 
114
 
                # Get the Wx element for this option
115
 
                my $setting = $config->meta($name);
116
 
                my $old     = $config->$name();
117
 
                my $ctrl    = $self->$name();
118
 
 
119
 
                # Don't capture options that are not shown,
120
 
                # as this may result in falsely clearing them.
121
 
                next unless $ctrl->IsEnabled;
122
 
 
123
 
                # Extract the value from the control
124
 
                my $value = undef;
125
 
                if ( $ctrl->isa('Wx::CheckBox') ) {
126
 
                        $value = $ctrl->GetValue ? 1 : 0;
127
 
 
128
 
                } elsif ( $ctrl->isa('Wx::TextCtrl') ) {
129
 
                        $value = $ctrl->GetValue;
130
 
 
131
 
                } elsif ( $ctrl->isa('Wx::SpinCtrl') ) {
132
 
                        $value = $ctrl->GetValue;
133
 
 
134
 
                } elsif ( $ctrl->isa('Wx::FilePickerCtrl') ) {
135
 
                        $value = $ctrl->GetPath;
136
 
 
137
 
                } elsif ( $ctrl->isa('Wx::DirPickerCtrl') ) {
138
 
                        $value = $ctrl->GetPath;
139
 
 
140
 
                } elsif ( $ctrl->isa('Wx::ColourPickerCtrl') ) {
141
 
                        $value = $ctrl->GetColour->GetAsString(Wx::C2S_HTML_SYNTAX);
142
 
                        $value =~ s/^#// if defined $value;
143
 
 
144
 
                } elsif ( $ctrl->isa('Wx::FontPickerCtrl') ) {
145
 
                        $value = $ctrl->GetSelectedFont->GetNativeFontInfoUserDesc;
146
 
 
147
 
                } elsif ( $ctrl->isa('Wx::Choice') ) {
148
 
                        my $options = $setting->options;
149
 
                        if ($options) {
150
 
                                my @k = sort keys %$options;
151
 
                                my $i = $ctrl->GetSelection;
152
 
                                $value = $k[$i];
153
 
                        }
154
 
                } else {
155
 
 
156
 
                        # To be completed
 
121
                my $meta = $config->meta($name);
 
122
 
 
123
                # Can we get a value from the control
 
124
                my $new = $self->config_get($meta);
 
125
                next unless defined $new;
 
126
 
 
127
                # Change the setting if different
 
128
                unless ( $new eq $config->$name() ) {
 
129
                        $diff{$name} = $new;
157
130
                }
158
 
 
159
 
                # Skip if null
160
 
                next unless defined $value;
161
 
                next if $value eq $old;
162
 
                $diff{$name} = $value;
163
131
        }
164
132
 
165
133
        return unless %diff;
166
134
        return \%diff;
167
135
}
168
136
 
 
137
sub config_get {
 
138
        my $self = shift;
 
139
        my $meta = shift;
 
140
        my $name = $meta->name;
 
141
 
 
142
        # Ignore config elements we don't have
 
143
        unless ( $self->can($name) ) {
 
144
                return undef;
 
145
        }
 
146
 
 
147
        # Ignore controls that are disabled
 
148
        my $ctrl = $self->$name();
 
149
        unless ( $ctrl->IsEnabled ) {
 
150
                return undef;
 
151
        }
 
152
 
 
153
        # Extract the value from the control
 
154
        my $value = undef;
 
155
        if ( $ctrl->isa('Wx::CheckBox') ) {
 
156
                $value = $ctrl->GetValue ? 1 : 0;
 
157
 
 
158
        } elsif ( $ctrl->isa('Wx::TextCtrl') ) {
 
159
                $value = $ctrl->GetValue;
 
160
 
 
161
        } elsif ( $ctrl->isa('Wx::SpinCtrl') ) {
 
162
                $value = $ctrl->GetValue;
 
163
 
 
164
        } elsif ( $ctrl->isa('Wx::FilePickerCtrl') ) {
 
165
                $value = $ctrl->GetPath;
 
166
 
 
167
        } elsif ( $ctrl->isa('Wx::DirPickerCtrl') ) {
 
168
                $value = $ctrl->GetPath;
 
169
 
 
170
        } elsif ( $ctrl->isa('Wx::ColourPickerCtrl') ) {
 
171
                $value = $ctrl->GetColour->GetAsString(Wx::C2S_HTML_SYNTAX);
 
172
                $value =~ s/^#// if defined $value;
 
173
 
 
174
        } elsif ( $ctrl->isa('Wx::FontPickerCtrl') ) {
 
175
                $value = $ctrl->GetSelectedFont->GetNativeFontInfoUserDesc;
 
176
 
 
177
        } elsif ( $ctrl->isa('Wx::Choice') ) {
 
178
                my $options = $meta->options;
 
179
                if ($options) {
 
180
                        my @k = sort keys %$options;
 
181
                        my $i = $ctrl->GetSelection;
 
182
                        $value = $k[$i];
 
183
                }
 
184
        }
 
185
        unless ( defined $value ) {
 
186
                return undef;
 
187
        }
 
188
 
 
189
        # For various strictly formatted configuration values,
 
190
        # attempt to determine a clean version.
 
191
        my $type = $meta->type;
 
192
        if ( $type == Padre::Constant::POSINT ) {
 
193
                $value =~ s/[^0-9]//g;
 
194
                $value =~ s/^0+//;
 
195
                if ( Params::Util::_POSINT($value) ) {
 
196
                        return $value;
 
197
                }
 
198
 
 
199
                # Fall back to the setting default
 
200
                return $meta->default;
 
201
 
 
202
        } else {
 
203
                # Implement cleaning for many more data types
 
204
 
 
205
        }
 
206
 
 
207
        return $value;
 
208
}
 
209
 
169
210
1;
170
211
 
171
 
# Copyright 2008-2011 The Padre development team as listed in Padre.pm.
 
212
# Copyright 2008-2012 The Padre development team as listed in Padre.pm.
172
213
# LICENSE
173
214
# This program is free software; you can redistribute it and/or
174
215
# modify it under the same terms as Perl 5 itself.