AppWindow/Ext/Settings.pm untested

filter: Statementstmt 0.0% 0 / 197 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 325 CC 22 SCAR 29.5
CC
22
Cov
0%
dialogCreate
72.0
LoadSettings
30.0
externalRemove
12.0
CRAP 19.2
1 package Tk::AppWindow::Ext::Settings;
2  
3 =head1 NAME
4  
5 Tk::AppWindow::Ext::Settings - allow your user to configure settings
6  
7 =cut
8  
9 use strict;
10 use warnings;
11 use Tk;
12 use vars qw($VERSION);
13 $VERSION="0.21";
14  
15 use base qw( Tk::AppWindow::BaseClasses::Extension );
16  
17 require Tk::YADialog;
18 require Tk::QuickForm;
19  
20  
21 =head1 SYNOPSIS
22  
23 my $app = new Tk::AppWindow(@options,
24 -extensions => ['Settings'],
25 );
26 $app->MainLoop;
27  
28 =head1 DESCRIPTION
29  
30 Add a settings feature to your application and allow the end user to configure the application.
31  
32 Creates a menu item in the main menu.
33  
34 Loads settings file at startup.
35  
36 =head1 CONFIG VARIABLES
37  
38 =over 4
39  
40 =item B<-settingsfile> I<hookable>
41  
42 Name of the settings file. Default is I<settingsrc>.
43  
44 =item B<-useroptions> I<hookable>
45  
46 Name of the settings file. Default is I<settingsrc>. A typical setup might look
47 like this:
48  
49 -useroptions => [
50 '*page' => 'Editing',
51 '*section' => 'User interface',
52 -contentforeground => ['color', 'Foreground'],
53 -contentbackground => ['color', 'Background'],
54 -contentfont => ['font', 'Font'],
55 '*end',
56 '*section' => 'Editor settings',
57 -contenttabs => ['text', 'Tab size'],
58 -contentwrap => ['radio', 'Wrap', [qw[none char word]]],
59 '*end',
60 '*page' => 'Icons',
61 -icontheme => ['list', 'Icon theme', 'available_icon_themes'],
62 -iconsize => ['list', 'Icon size', 'available_icon_sizes'],
63 '*page' => 'Bars',
64 '*section' => 'Menubar',
65 -menuiconsize => ['list', 'Icon size', 'available_icon_sizes'],
66 '*end',
67 '*section' => 'Toolbar',
68 -toolbarvisible => ['boolean', 'Visible at launch'],
69 -tooliconsize => ['list', 'Icon size', 'available_icon_sizes'],
70 -tooltextposition => ['radio', 'Text position', [qw[none left right top bottom]]],
71 '*end',
72 '*section' => 'Statusbar',
73 -statusbarvisible => ['boolean', 'Visible at launch'],
74 '*end',
75 ],
76  
77 It uses L<Tk::QuickForm> in the popup. See there for details of this option.
78  
79 =back
80  
81 =head1 COMMANDS
82  
83 =over 4
84  
85 =item B<settings>
86  
87 Launches the settings dialog.
88  
89 =back
90  
91 =cut
92  
93 sub new {
94 my $class = shift;
95 my $self = $class->SUPER::new(@_);
96 my $args = $self->GetArgsRef;
97  
98 $self->Require( 'ConfigFolder');
99 $self->{SETTINGSFILE} = undef;
100 $self->{USEROPTIONS} = undef;
101 $self->{EXTERNALS} = {};
102 $self->{DIALOG} = undef;
103  
104
105 $self->configInit(
106 -settingsfile => ['SettingsFile', $self, 'settingsrc'],
107 -useroptions => ['UserOptions', $self, []],
108 );
109  
110 $self->cmdConfig(
111 settings => ['CmdSettings', $self],
112 );
113  
114 return $self;
115 }
116  
117 =head1 METHODS
118  
119 =over 4
120  
121 =cut
122  
123 sub CmdSettings {
124 my $self = shift;
125
126 #create dialog if necessary
127 unless (defined $self->dialog) {
128 $self->dialogCreate;
129 }
130  
131 $self->dialogPop;
132 }
133  
134 sub dialog {
135 my $self = shift;
136 $self->{DIALOG} = shift if @_;
137 return $self->{DIALOG}
138 }
139  
140 sub dialogCreate {
141 my $self = shift;
142 my $m = $self->GetAppWindow->YADialog(
143 -buttons => ['Close'],
144 -title => 'Configure settings',
145 );
146
147 my $f;
148 my $nb;
149 my $b = $m->Subwidget('buttonframe')->Button(
150 -text => 'Apply',
151 -command => sub {
152 my %options = $f->get;
153 my @opts = sort keys %options;
154 my @save = ();
155 my $restart = 0;
156 for (@opts) {
157 my $val = $options{$_};
158 if ($val ne '') {
159 $self->configPut($_, $val);
160 push @save, $_;
161 } else {
162 $restart = 1
163 }
164 }
165 $self->ReConfigureAll;
166 $self->SaveSettings(@save);
167 # if ($restart) {
168 # my $name = $self->configGet('-appname');
169 # $self->popMessage("Please restart $name", 'dialog-warning');
170 # }
171 if (defined $nb) {
172 my $externals = $self->_externals;
173 for (keys %$externals) {
174 my $w = $externals->{$_};
175 $w->Apply if Exists($w) and $w->can('Apply');
176 }
177 }
178 }
179 );
180  
181 my %qopts = ();
182 my @images = (
183 ['-fileimage', 'text-x-plain', 16],
184 ['-folderimage', 'folder', 16],
185 ['-fontimage', 'gtk-select-font', 16],
186 ['-msgimage', 'dialog-question', 32],
187 ['-newfolderimage', 'folder-new', 16],
188 ['-reloadimage', 'appointment-recurring', 16],
189 ['-warnimage', 'dialog-warning', 32],
190 );
191 for (@images) {
192 my ($opt, $icon, $size) = @$_;
193 my $img = $self->getArt($icon, $size);
194 $qopts{$opt} = $img if defined $img;
195 }
196  
197 my $historyfile;
198 my $cf = $self->extGet('ConfigFolder');
199 $historyfile = $cf->ConfigFolder . '/color_history' if defined $cf;
200 $f = $m->QuickForm(%qopts,
201 -acceptempty => 1,
202 -colorhistoryfile => $historyfile,
203 # -listcall => ['cmdExecute', $self],
204 -structure => $self->configGet('-useroptions'),
205 -postvalidatecall => sub {
206 my $flag = shift;
207 if ($flag) {
208 $b->configure('-state', 'normal')
209 } else {
210 $b->configure('-state', 'disabled')
211 }
212 },
213 )->pack(-expand => 1, -fill => 'both');
214 $nb = $f->createForm;
215 if (defined $nb) {
216 $self->{NB} = $nb;
217 $self->externalAdd($self->GetSettingsPages);
218 }
219 $f->put($self->GetUserOptions);
220
221 $m->ButtonPack($b);
222 $self->dialog($m);
223 }
224  
225 sub dialogPop {
226 my $self = shift;
227 $self->dialog->Show(-popover => $self->GetAppWindow);
228 }
229  
230 sub _externals { return $_[0]->{EXTERNALS} }
231  
232 sub externalAdd {
233 my $self = shift;
234 my $nb = $self->NBWidget;
235 return unless defined $nb;
236 while (@_) {
237 my $title = shift @_;
238 my $opt = shift @_;
239 my $class = shift @$opt;
240 my $page = $nb->add($title, -label => $title);
241 $self->_externals->{$title} = $page->$class(@$opt)->pack(-fill => 'both', -expand => 1);
242 }
243 }
244  
245 sub externalExists {
246 my ($self, $page) = @_;
247 return exists $self->_externals->{$page}
248 }
249  
250 sub externalRemove {
251 my ($self, $page) = @_;
252 my $nb = $self->NBWidget;
253 return unless defined $nb;
254 if ($self->externalExists($page)) {
255 $nb->delete($page);
256 delete $self->_externals->{$page};
257 }
258 }
259  
260 sub GetExternalPages { return $_[0]->{EXTERNALS} }
261  
262 sub GetUserOptions {
263 my $self = shift;
264 my $uo = $self->configGet('-useroptions');
265 my @options = @$uo;
266 my %usopt = ();
267 while (@options) {
268 my $key = shift @options;
269 if (($key eq '*page') or ($key eq '*section')) {
270 shift @options;
271 next;
272 }
273 next if (($key eq '*end') or ($key eq '*column') or ($key eq '*expand') or ($key eq '*frame'));
274 shift @options;
275 $usopt{$key} = $self->configGet($key);
276 }
277 return %usopt
278 }
279  
280 sub GetSettingsPages {
281 my $self = shift;
282 my @p = $self->extList;
283 my @u = ();
284 my @l = ();
285 for (@p) { push @l, $self->extGet($_) }
286 for (@l) {
287 push @u, $_->SettingsPage;
288 }
289 return @u;
290 }
291  
292 sub LoadSettings {
293 my $self = shift;
294 my $cff = $self->extGet('ConfigFolder');
295 my $file = $self->configGet('-settingsfile');
296 return () unless $cff->confExists($file);
297 my $uo = $self->configGet('-useroptions');
298 my %useroptions = ();
299 my @temp = (@$uo);
300 while (@temp) {
301 my $key = shift @temp;
302 if (($key eq '*page') or ($key eq '*section')) {
303 shift @temp;
304 next;
305 }
306 next if (($key eq '*end') or ($key eq '*column') or ($key eq '*expand') or ($key eq '*frame'));
307 shift @temp;
308 $useroptions{$key} = 1;
309 }
310 my %hash = $cff->loadHash($file, 'aw settings');
311 my @output = ();
312 for (keys %hash) {
313 my $key = $_;
314 if (exists $useroptions{$key}) {
315 push @output, $key, $hash{$key}
316 } else {
317 warn "Ignoring invalid option: $key"
318 }
319 }
320 return @output;
321 }
322  
323 sub MenuItems {
324 my $self = shift;
325 return (
326 #This table is best viewed with tabsize 3.
327 # type menupath label cmd icon keyb
328 [ 'menu_normal', 'appname::Quit', '~Settings', 'settings', 'configure', 'F9', ],
329 [ 'menu_separator', 'appname::Quit', 'h2'],
330 )
331 }
332  
333 =item B<NBWidget>
334  
335 Returns a reference to the notebook widget in the settingsdialog when this dialog is active.
336 Otherwise returns undef.
337  
338 =cut
339  
340 sub NBWidget {
341 return $_[0]->{NB}
342 }
343  
344 sub ReConfigureAll {
345 my $self = shift;
346 my @list = $self->extList;
347 my %hash = ();
348 for (@list) {
349 $hash{$_} = $self->extGet($_);
350 }
351 my $kb = delete $hash{'Keyboard'};
352 $kb->ReConfigure if defined $kb;
353 for (keys %hash) {
354 $hash{$_}->ReConfigure;
355 }
356 }
357  
358 sub SaveSettings {
359 my $self = shift;
360 my %hash = ();
361 my $file = $self->configGet('-settingsfile');
362 for (@_) {
363 my $option = $_;
364 my $value = $self->configGet($_);
365 $hash{$option} = $value;
366 }
367 my $cff = $self->extGet('ConfigFolder');
368 $cff->saveHash($file, 'aw settings', %hash);
369 }
370  
371 sub SettingsFile {
372 my $self = shift;
373 if (@_) { $self->{SETTINGSFILE} = shift }
374 return $self->{SETTINGSFILE}
375 }
376  
377 sub UserOptions {
378 my $self = shift;
379 if (@_) { $self->{USEROPTIONS} = shift }
380 return $self->{USEROPTIONS}
381 }
382  
383 =back
384  
385 =head1 AUTHOR
386  
387 Hans Jeuken (hanje at cpan dot org)
388  
389 =head1 BUGS
390  
391 Unknown. If you find any, please contact the author.
392  
393 =head1 SEE ALSO
394  
395 =over 4
396  
397 =item L<Tk::AppWindow>
398  
399 =item L<Tk::AppWindow::BaseClasses::Extension>
400  
401 =item L<Tk::AppWindow::BaseClasses::PanelExtension>
402  
403 =back
404  
405 =cut
406  
407 1;
408  
409  
410  
411  
412  
413  
414