aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMatteo Pasotti <matteo.pasotti@gmail.com>2016-03-21 23:29:29 +0100
committerMatteo Pasotti <matteo.pasotti@gmail.com>2016-03-21 23:29:29 +0100
commit8956708f921cc7e5997988f30939637fe9d8bd6b (patch)
tree9dc739d27c554512c609aa045cf8cf580c3dddc4 /lib
parenta51162cca4303a8a92158285b1ac9c1e6ef26623 (diff)
parent3bb8ad08e4b8d5757ac7cf3bf7411b0b5e1684d9 (diff)
downloadmanatools-8956708f921cc7e5997988f30939637fe9d8bd6b.tar
manatools-8956708f921cc7e5997988f30939637fe9d8bd6b.tar.gz
manatools-8956708f921cc7e5997988f30939637fe9d8bd6b.tar.bz2
manatools-8956708f921cc7e5997988f30939637fe9d8bd6b.tar.xz
manatools-8956708f921cc7e5997988f30939637fe9d8bd6b.zip
Merge branch 'master' of ssh://git.mageia.org/software/manatools
Diffstat (limited to 'lib')
-rw-r--r--lib/ManaTools/Shared/Action.pm111
-rw-r--r--lib/ManaTools/Shared/ActionsRole.pm58
-rw-r--r--lib/ManaTools/Shared/GUI/ActionList.pm166
3 files changed, 317 insertions, 18 deletions
diff --git a/lib/ManaTools/Shared/Action.pm b/lib/ManaTools/Shared/Action.pm
new file mode 100644
index 00000000..34e968cf
--- /dev/null
+++ b/lib/ManaTools/Shared/Action.pm
@@ -0,0 +1,111 @@
+# vim: set et ts=4 sw=4:
+package ManaTools::Shared::Action;
+
+#============================================================= -*-perl-*-
+
+=head1 NAME
+
+ ManaTools::Shared::Action - an Action helper class
+
+=head1 SYNOPSIS
+
+ use ManaTools::Shared::Action;
+
+ my $action = ManaTools::Shared::Action->new(name => 'addPartition', label => 'Add a partition', item => $foo, action => sub {
+ my $self = shift;
+ my $item = $self->item();
+ my @args = @_;
+ ...
+ return 'return value';
+ }, valid => sub {
+ my $self = shift;
+ my $item = $self->item();
+ ...
+ return 0;
+ });
+ my $res = $action->act(@_);
+ $action->is_valid();
+
+
+=head1 DESCRIPTION
+
+ This helper class is used to abstract an action
+
+=head1 SUPPORT
+
+ You can find documentation for this plugin with the perldoc command:
+
+ perldoc ManaTools::Shared::Action
+
+
+=head1 AUTHOR
+
+ Maarten Vanraes <alien@rmail.be>
+
+=head1 COPYRIGHT and LICENSE
+
+Copyright (c) 2015 Maarten Vanraes <alien@rmail.be>
+
+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 METHODS
+
+=cut
+
+use Moose;
+
+has 'name' => (
+ is => 'ro',
+ isa => 'Str',
+ required => 1
+);
+
+has 'label' => (
+ is => 'rw',
+ isa => 'Str',
+ required => 1
+);
+
+has 'item' => (
+ is => 'rw',
+ isa => 'Item',
+ required => 0,
+ default => undef
+);
+
+has 'action' => (
+ traits => ['Code'],
+ is => 'ro',
+ isa => 'CodeRef',
+ required => 1,
+ handles => {
+ act => 'execute'
+ }
+);
+
+has 'valid' => (
+ traits => ['Code'],
+ is => 'ro',
+ isa => 'CodeRef',
+ required => 0,
+ default => sub {
+ return sub { return 1; };
+ },
+ handles => {
+ is_valid => 'execute'
+ }
+);
+
+1;
+
diff --git a/lib/ManaTools/Shared/ActionsRole.pm b/lib/ManaTools/Shared/ActionsRole.pm
index 7b6829d7..888750b3 100644
--- a/lib/ManaTools/Shared/ActionsRole.pm
+++ b/lib/ManaTools/Shared/ActionsRole.pm
@@ -18,9 +18,9 @@ package ManaTools::Shared::ActionsRole;
my $f = Foo->new();
my @actionnames = $f->get_actions();
- $f->add('aname', sub { my @params = @_; ... ; return 'foo'; });
+ $f->add_action('aname', 'a label', $item, sub { my $self = shift; my @params = @_; ... ; return 'foo'; }, sub { return 1; });
my $res = $f->act('aname', 'param1', 'param2', @params);
- $f->remove('aname');
+ $f->remove_action('aname');
=head1 DESCRIPTION
@@ -61,13 +61,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
use Moose::Role;
-## Class DATA
-has 'actions' => (
+use MooseX::ClassAttribute;
+use ManaTools::Shared::Action;
+
+class_has 'acts' => (
is => 'ro',
init_arg => undef,
lazy => 1,
- isa => 'HashRef[CodeRef]',
- default => sub {return {};}
+ isa => 'ArrayRef[ManaTools::Shared::Action]',
+ default => sub {return [];}
);
#=============================================================
@@ -88,7 +90,7 @@ has 'actions' => (
sub get_actions {
my $self = shift;
- return keys %{$self->actions};
+ return map { return $_->name()} @{$self->acts()};
}
#=============================================================
@@ -114,17 +116,26 @@ sub get_actions {
sub act {
my $self = shift;
my $key = shift;
- return $self->actions->{$key}(@_);
+ my $acts = $self->acts();
+ for my $action (@{$self->acts()}) {
+ if ($key == $action->name()) {
+ return $action->act($self, @_);
+ }
+ }
+
+ return undef;
}
#=============================================================
-=head2 add
+=head2 add_action
=head3 INPUT
- $key: string
- $action: sub
+ $name: Str
+ $label: Str
+ $action: CodeRef
+ $valid: CodeRef
=head3 DESCRIPTION
@@ -133,17 +144,22 @@ sub act {
=cut
#=============================================================
-sub add {
+sub add_action {
my $self = shift;
- my $key = shift;
+ my $name = shift;
+ my $label = shift;
+ my $item = shift;
my $action = shift;
+ my $valid = shift;
+ my $options = {name => $name, label => $label, item => $item, action => $action};
+ $options->{valid} = $valid if defined $valid;
- $self->actions->{$key} = $action;
+ push @{$self->acts()}, ManaTools::Shared::Action->new($options);
}
#=============================================================
-=head2 remove
+=head2 remove_action
=head3 INPUT
@@ -156,11 +172,17 @@ sub add {
=cut
#=============================================================
-sub remove {
+sub remove_action {
my $self = shift;
my $key = shift;
-
- delete $self->actions->{$key};
+ my $acts = $self->acts();
+ my $index = scalar(@{$acts});
+ while ($index > 0) {
+ $index = $index - 1;
+ if ($acts->[$index]->name() == $key) {
+ delete $acts->[$index];
+ }
+ }
}
1;
diff --git a/lib/ManaTools/Shared/GUI/ActionList.pm b/lib/ManaTools/Shared/GUI/ActionList.pm
new file mode 100644
index 00000000..14aea013
--- /dev/null
+++ b/lib/ManaTools/Shared/GUI/ActionList.pm
@@ -0,0 +1,166 @@
+# vim: set et ts=4 sw=4:
+package ManaTools::Shared::GUI::ActionList;
+#============================================================= -*-perl-*-
+
+=head1 NAME
+
+ManaTools::Shared::GUI::ActionList - Class to visualize ActionsRole
+
+=head1 SYNOPSIS
+
+package FooActions;
+
+with 'ManaTools::Shared::ActionsRole';
+
+...
+
+
+use ManaTools::Shared::GUI::ActionList;
+
+my $hbox = ...
+my $foo = FooActions->new();
+my $actionlist = ManaTools::Shared::GUI::ActionList->new(parentWidget => $hbox, actions => $foo);
+$foo->add_action('bar', sub {...});
+$actionlist->refresh();
+...
+
+=head1 DESCRIPTION
+
+This class is a GUI helper for ActionsRole classes
+
+
+=head1 SUPPORT
+
+You can find documentation for this module with the perldoc command:
+
+perldoc ManaTools::Shared::GUI::ActionList
+
+=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;
+
+use ManaTools::Shared::GUI::ReplacePoint;
+
+#=============================================================
+
+=head2 new
+
+=head3 INPUT
+
+ hash ref containing
+ parentWidget: the parent widget
+ properties: the properties object
+
+=head3 DESCRIPTION
+
+ new is inherited from Moose, to create a ActionList object
+
+=cut
+
+#=============================================================
+
+has 'parentDialog' => (
+ is => 'ro',
+ isa => 'ManaTools::Shared::GUI::Dialog',
+ required => 1,
+);
+
+has 'parentWidget' => (
+ is => 'ro',
+ isa => 'yui::YWidget',
+ required => 1,
+);
+
+has 'actions' => (
+ is => 'rw',
+ does => 'ManaTools::Shared::ActionsRole',
+ trigger => \&refresh,
+ default => undef,
+);
+
+has 'replacepoint' => (
+ is => 'ro',
+ isa => 'ManaTools::Shared::GUI::ReplacePoint',
+ init_arg => undef,
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ my $rpl = ManaTools::Shared::GUI::ReplacePoint->new(eventHandler => $self->parentDialog(), parentWidget => $self->parentWidget());
+ $rpl->finished();
+ return $rpl;
+ },
+);
+
+#=============================================================
+
+=head2 refresh
+
+=head3 INPUT
+
+ $self: this object
+
+=head3 DESCRIPTION
+
+ rebuilds the properties
+
+=cut
+
+#=============================================================
+sub refresh {
+ my $self = shift;
+ my $dialog = $self->parentDialog();
+ my $factory = $dialog->factory();
+ my $parentWidget = $self->parentWidget();
+ my $replacepoint = $self->replacepoint();
+ my $actions = $self->actions();
+
+ # clear and start new changes on replacepoint
+ $replacepoint->clear();
+ if (defined $actions) {
+ my $vbox = $factory->createVBox($replacepoint->container());
+ # rebuild for all actions a Button
+ for my $key (sort $actions->get_actions()) {
+ $replacepoint->addWidget($key, $factory->createPushButton($vbox, $key), sub {
+ my $self = shift;
+ my $actions = shift;
+ my $key = shift;
+ my @args = @_;
+ return $actions->act($key, @args);
+ }, $actions, $key);
+ }
+ }
+ # finished
+ $replacepoint->finished();
+}
+
+#=============================================================
+
+no Moose;
+__PACKAGE__->meta->make_immutable;
+
+
+1;