App-GUI-Cellgraph

 view release on metacpan or  search on metacpan

lib/App/GUI/Cellgraph/Frame/Panel/ColorSetPicker.pm  view on Meta::CPAN

use v5.12;
use warnings;
use Wx;

package App::GUI::Cellgraph::Frame::Panel::ColorSetPicker;
use base qw/Wx::Panel/;

use App::GUI::Cellgraph::Widget::ColorDisplay;
use Graphics::Toolkit::Color qw/color/;

our $default_color = {red => 225, green => 225, blue => 225};

sub new {
    my ( $class, $parent, $color_sets) = @_;
    return unless ref $parent and ref $color_sets eq 'HASH';

    my $self = $class->SUPER::new( $parent, -1 );

    $self->{'sets'} = { %$color_sets };
    $self->{'set_names'} = [ sort keys %{$self->{'sets'}} ];
    $self->{'set_index'} = 1;

    my $btnw = 46; my $btnh = 17;# button width and height
    $self->{'select'} = Wx::ComboBox->new( $self, -1, $self->current_set_name, [-1,-1], [170, -1], $self->{'set_names'});
    $self->{'<'}    = Wx::Button->new( $self, -1, '<',       [-1,-1], [ 27, $btnh] );
    $self->{'>'}    = Wx::Button->new( $self, -1, '>',       [-1,-1], [ 27, $btnh] );
    $self->{'load'} = Wx::Button->new( $self, -1, 'Load',    [-1,-1], [$btnw, $btnh] );
    $self->{'del'}  = Wx::Button->new( $self, -1, 'Del',     [-1,-1], [$btnw, $btnh] );
    $self->{'save'} = Wx::Button->new( $self, -1, 'Save',    [-1,-1], [$btnw, $btnh] );
    $self->{'new'}  = Wx::Button->new( $self, -1, 'New',     [-1,-1], [$btnw, $btnh] );

    $self->{'display'}[$_] = App::GUI::Cellgraph::Widget::ColorDisplay->new( $self, 16, 8, $_, $default_color ) for 0 .. 8;

    $self->{'select'}->SetToolTip("select color set in list directly");
    $self->{'<'}->SetToolTip("go to previous color set name in list");
    $self->{'>'}->SetToolTip("go to next color set name in list");
    $self->{'load'}->SetToolTip("use displayed color on the right side as color of selected state");
    $self->{'save'}->SetToolTip("save currently used state colors under the displayed color set name");
    $self->{'del'}->SetToolTip("delete color set of displayed name from storage");
    $self->{'new'}->SetToolTip("save currently used state colors under a new set name");

    Wx::Event::EVT_COMBOBOX( $self, $self->{'select'}, sub {
        my ($win, $evt) = @_;                            $self->{'set_index'} = $evt->GetInt; $self->update_display });
    Wx::Event::EVT_BUTTON( $self, $self->{'<'},    sub { $self->{'set_index'}--; $self->update_display });
    Wx::Event::EVT_BUTTON( $self, $self->{'>'},    sub { $self->{'set_index'}++; $self->update_display });
    Wx::Event::EVT_BUTTON( $self, $self->{'load'}, sub { $parent->set_all_colors( $self->get_current_color_set )  });
    Wx::Event::EVT_BUTTON( $self, $self->{'del'},  sub {
        delete $self->{'sets'}{ $self->current_set_name };
        $self->{'set_index'}-- if $self->{'set_index'};
        $self->update_select();
    });
    Wx::Event::EVT_BUTTON( $self, $self->{'save'}, sub {
        $self->{'sets'}{ $self->current_set_name } = [map { $_->name ? $_->name : $_->values(as => 'hex') } $parent->get_all_colors];
        $self->update_display();
    });
    Wx::Event::EVT_BUTTON( $self, $self->{'new'}, sub {
        my $name;
        while (1){
            my $dialog = Wx::TextEntryDialog->new ( $self, "Please insert the color set name", 'Request Dialog');
            return if $dialog->ShowModal == &Wx::wxID_CANCEL;
            $name = $dialog->GetValue();
            last unless exists $self->{'sets'}{ $name };
        }
        $self->{'sets'}{ $name } = [ map { $_->name ? $_->name : $_->rgb_hex } $parent->get_all_colors ];
        $self->{'set_names'} = [ sort keys %{$self->{'sets'}} ];
        for (0 .. $#{$self->{'set_names'}}){
            $self->{'set_index'} = $_ if $name eq $self->{'set_names'}[$_];
        }
        $self->update_select();
    });

    my $std_attr = &Wx::wxALIGN_LEFT | &Wx::wxALIGN_CENTER_VERTICAL| &Wx::wxGROW;
    my $tb_attr = $std_attr | &Wx::wxTOP| &Wx::wxBOTTOM;
    my $button_attr  = &Wx::wxLEFT | $tb_attr;
    my $all_attr = $std_attr | &Wx::wxALL;
    my $row1 = Wx::BoxSizer->new(&Wx::wxHORIZONTAL);
    $row1->AddSpacer( 10 );



( run in 0.664 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )