aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ManaTools/Shared/GUI/Dialog.pm
blob: 520fc30d73d286636c3e0d3059bcc080f12f1610 (plain)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# vim: set et ts=4 sw=4:
package ManaTools::Shared::GUI::Dialog;
#============================================================= -*-perl-*-

=head1 NAME

ManaTools::Shared::GUI::Dialog - Class to manage a yui YDumbTab properly

=head1 SYNOPSIS

use ManaTools::Shared::GUI::Dialog;

my $dlg = ManaTools::Shared::GUI::Dialog->new(
    dialogType => ManaTools::Shared::GUI::Dialog->mainDialog, ## or popupDialog
    title => "New Title",
    icon => $icon,
    optFields => [
        ManaTools::Shared::GUI::Dialog->TimeField,
        ManaTools::Shared::GUI::Dialog->DateField,
        ManaTools::Shared::GUI::Dialog->TabField,
    ],
    buttons => [
        ManaTools::Shared::GUI::Dialog->cancelButton,
        ManaTools::Shared::GUI::Dialog->okButton,
    ],
    event_timeout => 0, # event timeout in msec during the waitForEvent()
    layout => sub { my $self = shift; my $layoutstart = shift; my $dlg = $self->dialog(); my $info = $self->info(); ... $self->addWidget('button1', $button, sub{...}, $backendItem1); },
    restoreValues => sub { my $self = shift; $info = {}; ...; return $info },
    result => sub { my $self = shift; ... },
);

my $result = $dlg->call();

## you should call this from the Module, and not directly from here...

=head1 DESCRIPTION

This class wraps the most common dialog functionality


=head1 SUPPORT

You can find documentation for this module with the perldoc command:

perldoc ManaTools::Shared::GUI::Dialog

=head1 AUTHOR

Maarten Vanraes <alien@rmail.be>

=head1 COPYRIGHT and LICENSE

Copyright (C) 2015-2016, Maarten Vanraes.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA

=head1 FUNCTIONS

=cut


use Moose;
with 'ManaTools::Shared::GUI::EventHandlerRole';

use diagnostics;
use utf8;

use Moose::Util::TypeConstraints;
use MooseX::ClassAttribute;

use yui;
use ManaTools::Shared::GUI::Event;
#=============================================================

=head2 new

=head3 INPUT

    hash ref containing
        module:             the parent ManaTools::Module
        dialogType:         ManaTools::Shared::GUI::Dialog->mainDialog, or popupDialog
        title:              a title
        icon:               an icon
        layout:             a callback that builds the layout of the dialog
        restoreValues:      an optional callback that restore the values to an $info HashRef
        event_timeout:      an optional and rw timeout in msec during the waitForEvent()
                            needs a ManaTools::Shared::GUI::Event to manage the $yui::YEvent::TimeoutEvent
        buttons:            an optional hashref containing
                                ManaTools::Shared::GUI::Dialog->cancelButton and/or
                                ManaTools::Shared::GUI::Dialog->okButton
        result:             an optional callback for returning a result (mostly for popupDialogs)

=head3 DESCRIPTION

    new is inherited from Moose, to create a Dialog object

=cut

#=============================================================

has 'module' => (
    is => 'ro',
    isa => 'ManaTools::Module',
    required => 1,
);

has 'factory' => (
    is => 'ro',
    isa => 'Maybe[yui::YWidgetFactory]',
    lazy => 1,
    init_arg => undef,
    default => sub {
       return yui::YUI::widgetFactory;
    },
);

has 'optFactory' => (
    is => 'ro',
    isa => 'Maybe[yui::YOptionalWidgetFactory]',
    lazy => 1,
    init_arg => undef,
    default => sub {
        return yui::YUI::optionalWidgetFactory;
    },
);

has 'mgaExternalFactory' => (
    is => 'ro',
    isa => 'Maybe[yui::YMGAWidgetFactory]',
    lazy => 1,
    init_arg => undef,
     builder => '_MGAFactoryInitialize'
);

sub _MGAFactoryInitialize {
    my $self = shift();

    $self->factory();  # just to be sure default factory is initialized first

    my $mageiaPlugin = "mga";
    my $mgaFactory   = yui::YExternalWidgets::externalWidgetFactory($mageiaPlugin);
    return yui::YMGAWidgetFactory::getYMGAWidgetFactory($mgaFactory);
}

has 'dialog' => (
    is => 'rw',
    isa => 'Maybe[yui::YDialog]',
    lazy => 1,
    init_arg => undef,
    default => sub {
        return undef;
    },
);

subtype 'TimeoutType'
    => as Int
    => where {($_ >= 0)};

has 'event_timeout' => (
    is => 'rw',
    isa => 'TimeoutType',
    lazy => 1,
    default => 0,
);

subtype 'DialogType'
    => as Int
    => where {($_ > 0 && $_<=2)};

has 'dialogType' => (
    is => 'ro',
    isa => 'DialogType',
    required => 1,
);

class_has 'mainDialog' => (
    is => 'ro',
    isa => 'DialogType',
    init_arg => undef,
    default => sub {return 1;},
);

class_has 'popupDialog' => (
    is => 'ro',
    isa => 'DialogType',
    init_arg => undef,
    default => sub {return 2;},
);

has 'title' => (
    is => 'ro',
    isa => 'Str',
    required => 1,
);

has 'icon' => (
    is => 'ro',
    isa => 'Str',
    required => 1,
);

class_has 'cancelButton' => (
    is => 'ro',
    init_arg => undef,
    default => sub {return 'cancelButton';},
);

class_has 'okButton' => (
    is => 'ro',
    init_arg => undef,
    default => sub {return 'okButton';},
);

class_has 'closeButton' => (
    is => 'ro',
    init_arg => undef,
    default => sub {return 'closeButton';},
);

class_has 'resetButton' => (
    is => 'ro',
    init_arg => undef,
    default => sub {return 'resetButton';},
);

class_has 'aboutButton' => (
    is => 'ro',
    init_arg => undef,
    default => sub {return 'aboutButton';},
);

has 'buttons' => (
    is => 'ro',
    isa => 'HashRef[CodeRef]',
    lazy => 1,
    default => sub {
        return {};
    },
);

class_has 'DateField' => (
    is => 'ro',
    isa => 'Int',
    init_arg => undef,
    default => sub {return 1;},
);
class_has 'TimeField' => (
    is => 'ro',
    isa => 'Int',
    init_arg => undef,
    default => sub {return 2;},
);
class_has 'TabField' => (
    is => 'ro',
    isa => 'Int',
    init_arg => undef,
    default => sub {return 3;},
);

has 'optFields' => (
    is => 'ro',
    isa => 'ArrayRef[Int]',
    lazy => 1,
    default => sub {
        return [];
    },
);

has 'layoutDirty' => (
    is => 'rw',
    isa => 'Bool',
    default => 0,
);

has 'info' => (
    is => 'rw',
    isa => 'HashRef',
    default => sub {
        return {};
    }
);

has 'layout' => (
    is => 'ro',
    isa => 'CodeRef',
    required => 1,
);

has 'restoreValues' => (
    is => 'ro',
    isa => 'Maybe[CodeRef]',
    lazy => 1,
    default => sub {
        return undef;
    },
);

has 'result' => (
    is => 'ro',
    isa => 'Maybe[CodeRef]',
    lazy => 1,
    default => sub {
        return undef;
    }
);

sub loc {
    my $self = shift;
    return $self->module->loc(@_);
}

#=============================================================

=head2 checkFields

=head3 INPUT

    $self: this object

=head3 OUTPUT

    returns true or false depending on success

=head3 DESCRIPTION

    helper function to check if the optional factory has the optFields

=cut

#=============================================================
sub checkFields {
    my $self = shift;
    my $fields = $self->optFields();
    my $optFactory = $self->optFactory();
    for my $field (@{$fields}) {
        return 0 if ($field == ManaTools::Shared::GUI::Dialog->TimeField && !$optFactory->hasTimeField());
        return 0 if ($field == ManaTools::Shared::GUI::Dialog->DateField && !$optFactory->hasDateField());
        return 0 if ($field == ManaTools::Shared::GUI::Dialog->TabField && !$optFactory->hasDumbTab());
    }
    return 1;
}

#=============================================================

=head2 getButton

=head3 INPUT

    $self: this object
    $buttonType: a buttonType

=head3 OUTPUT

    returns the CodeRef associated with the buttonType, or undef

=head3 DESCRIPTION

    gets the CodeRef associated with a buttontype

=cut

#=============================================================
sub getButton {
    my $self = shift;
    my $buttonType = shift;
    my $buttons = $self->buttons;

    return $buttons->{$buttonType} if (defined($buttons->{$buttonType}));
    return undef;
}

#=============================================================

=head2 addButtons

=head3 INPUT

    $self: this object
    $ylayout: a layout widget to base the buttons on

=head3 DESCRIPTION

    add buttons to the layout according to $self->buttons()

=cut

#=============================================================
sub addButtons {
    my $self = shift;
    my $layout = shift;
    my $buttons = $self->buttons;
    return if scalar(keys %{$buttons}) == 0;
    my $factory = $self->factory();

    ### buttons on the last line
    my $buttonbox = $factory->createHBox($layout);

    ## Left side
    my $align = $factory->createLeft($buttonbox);
    my $hbox = $factory->createHBox($align);

    $self->addWidget('aboutButton',
                     $factory->createPushButton($hbox, $self->loc->N("&About")),
                     $self->getButton(ManaTools::Shared::GUI::Dialog->aboutButton)) if $self->getButton(ManaTools::Shared::GUI::Dialog->aboutButton);
    $self->addWidget('resetButton', $factory->createPushButton($hbox, $self->loc->N("&Reset")), $self->getButton(ManaTools::Shared::GUI::Dialog->resetButton)) if $self->getButton(ManaTools::Shared::GUI::Dialog->resetButton);

    ## Right side
    $align = $factory->createRight($buttonbox);
    $hbox = $factory->createHBox($align);
    $self->addWidget('cancelButton', $factory->createPushButton($hbox, $self->loc->N("&Cancel")), $self->getButton(ManaTools::Shared::GUI::Dialog->cancelButton)) if $self->getButton(ManaTools::Shared::GUI::Dialog->cancelButton);
    $self->addWidget('okButton', $factory->createPushButton($hbox, $self->loc->N("&Ok")), $self->getButton(ManaTools::Shared::GUI::Dialog->okButton)) if $self->getButton(ManaTools::Shared::GUI::Dialog->okButton);
    $self->addWidget('closeButton', $factory->createPushButton($hbox, $self->loc->N("&Close")), $self->getButton(ManaTools::Shared::GUI::Dialog->closeButton)) if $self->getButton(ManaTools::Shared::GUI::Dialog->closeButton);
    $factory->createHSpacing($hbox, 1.0);

    ## no changes by default
    my $ydialog = $self->dialog();
    if (defined($self->widget('closeButton'))) {
        $ydialog->setDefaultButton($self->widget('closeButton'));
    }
    elsif (defined($self->widget('cancelButton'))) {
        $ydialog->setDefaultButton($self->widget('cancelButton'));
    }
    elsif (defined($self->widget('okButton'))) {
        $ydialog->setDefaultButton($self->widget('okButton'));
    }
}

#=============================================================

=head2 multipleChanges

=head3 INPUT

    $self: this object

=head3 DESCRIPTION

    Start multiple changes (if required)

=cut

#=============================================================
sub multipleChanges {
    my $self = shift;
    my $ydialog = $self->dialog();
    return if ($self->layoutDirty());
    $ydialog->startMultipleChanges();
    $self->layoutDirty(1);
}

#=============================================================

=head2 recalcLayout

=head3 INPUT

    $self: this object
    $force: bool

=head3 DESCRIPTION

    Recalculates the layout and ends multiple changes (if required or if forced)

=cut

#=============================================================
sub recalcLayout {
    my $self = shift;
    my $force = shift || 0;
    my $ydialog = $self->dialog();
    return if (!$self->layoutDirty() || $force);
    $ydialog->recalcLayout();
    $ydialog->doneMultipleChanges();
    $self->layoutDirty(0);
}

#=============================================================

=head2 call

=head3 INPUT

    $self: this object

=head3 OUTPUT

    the result from the dialog

=head3 DESCRIPTION

    Creates a dialog (either popup or main) and handles it's events and resturns the proper result.

=cut

#=============================================================
sub call {
    my $self = shift;
    my $oldAppTitle = yui::YUI::app()->applicationTitle();
    my $oldAppIcon  = yui::YUI::app()->applicationIcon();

    ## set new title to get it in dialog
    yui::YUI::app()->setApplicationTitle($self->title());
    ## set icon if not already set by external launcher
    yui::YUI::app()->setApplicationIcon($self->icon());

    ## required fields
    die "required widgets missing from YOptionalFactory" if (!$self->checkFields());

    ## restore values
    my $restoreValues = $self->restoreValues();
    $self->info($restoreValues->($self)) if defined($restoreValues);

    ## create the dialog
    my $factory = $self->factory();
    $self->dialog($factory->createMainDialog()) if ($self->dialogType() == ManaTools::Shared::GUI::Dialog->mainDialog);
    $self->dialog($factory->createPopupDialog()) if ($self->dialogType() == ManaTools::Shared::GUI::Dialog->popupDialog);
    my $ydialog = $self->dialog();
    my $layoutstart = $ydialog;
    my $vbox = undef;
    if (scalar(keys %{$self->buttons()}) > 0) {
        # we have buttons, so, we need a vbox + hbox basic layout to start with
        $vbox = $factory->createVBox($ydialog);
        $layoutstart = $factory->createHBox($vbox);
    }
    ## if layout returns a YWidget, we can define buttons on it
    $self->addButtons($vbox) if defined($vbox);

    ## build the whole layout
    my $layout = $self->layout->($self, $layoutstart);

    ## add a cancelEvent
    ManaTools::Shared::GUI::Event->new(name => 'cancelEvent', eventHandler => $self, eventType => $yui::YEvent::CancelEvent, event => sub { return 0; });

    # main loop
    while(1) {
        my $yevent = $ydialog->pollEvent();
        if (!$yevent) {
            # only recalc layout after all pending events have run
            $self->recalcLayout();
            # wait for a new event
            $yevent = $ydialog->waitForEvent($self->event_timeout);
        }
        # if we have an event, process it (and possibly exit)
        last if (!$self->processEvents($yevent));
    }

    my $resultcb = $self->result();
    my $result = 1;
    $result = $resultcb->($self) if defined ($resultcb);

    # end dialog
    $ydialog->destroy();

    # restore old application title and icon
    yui::YUI::app()->setApplicationTitle($oldAppTitle) if $oldAppTitle;
    yui::YUI::app()->setApplicationIcon($self->icon()) if $oldAppIcon;

    return $result;
}

no Moose;
__PACKAGE__->meta->make_immutable;


1;