App-Chart

 view release on metacpan or  search on metacpan

lib/App/Chart/Gtk2/PreferencesDialog.pm  view on Meta::CPAN

# update entries if database changes



# Copyright 2008, 2009, 2010, 2011 Kevin Ryde

# This file is part of Chart.
#
# Chart is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3, or (at your option) any later version.
#
# Chart 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 Chart.  If not, see <http://www.gnu.org/licenses/>.

package App::Chart::Gtk2::PreferencesDialog;
use 5.010;
use strict;
use warnings;
use Gtk2;
use Gtk2::Ex::Units;
use Locale::TextDomain ('App-Chart');

use App::Chart;
use App::Chart::Database;
use App::Chart::DBI;

# use App::Chart::Gtk2::Ex::ToplevelSingleton hide_on_delete => 1;
# use base 'App::Chart::Gtk2::Ex::ToplevelSingleton';
# sub popup {
#   my ($class, $parent) = @_;
#   my $self = $class->instance;
#   $self->present;
#   return $self;
# }

use Glib::Object::Subclass
  'Gtk2::Dialog',
  signals => { show => \&_do_show },
  properties => [];


my @preferences = ( { key     => 'lme-username',
                      name    => __('LME Username'),
                      type    => 'string',
                      default => '' },
                    { key     => 'lme-password',
                      name    => __('LME Password'),
                      type    => 'string',
                      default => ''  },
                  );

use constant RESPONSE_SAVE => 0;

sub INIT_INSTANCE {
  my ($self) = @_;

  $self->set_title (__('Chart: Preferences'));
  $self->add_buttons ('gtk-save'   => RESPONSE_SAVE,
                      'gtk-ok'     => 'ok',
                      'gtk-cancel' => 'cancel',
                      'gtk-help'   => 'help');
  $self->signal_connect (response => \&_do_response);

  my $vbox = $self->vbox;

  my @elements;
  $self->{'elements'} = \@elements;

  foreach my $pref (@preferences) {
    my $key = $pref->{'key'};
    my $name = $pref->{'name'};
    my $type = $pref->{'type'};

    my $hbox = Gtk2::HBox->new (0, 0);
    $hbox->{'key'} = $key;
    $hbox->{'type'} = $type;
    push @elements, $hbox;
    $vbox->pack_start ($hbox, 0,0,
                       0.25 * Gtk2::Ex::Units::line_height($hbox));

    my $label = Gtk2::Label->new ($name . " ");
    $label->set (xalign => 0); # left
    $hbox->pack_start ($label, 0,0,0);

    if ($type eq 'string') {
      my $entry = $hbox->{'entry'} = Gtk2::Entry->new ();
      $hbox->pack_start ($entry, 1,1,0);
      $entry->signal_connect
        (changed => sub { $self->set_response_sensitive (RESPONSE_SAVE, 1); });

    } elsif ($type eq 'boolean') {
      my $checkbox = $hbox->{'checkbox'} = Gtk2::CheckButton->new;
      $hbox->pack_start ($checkbox, 0,0,0);
      $checkbox->signal_connect
        (toggled => sub { $self->set_response_sensitive (RESPONSE_SAVE, 1); });
    }
  }
  $vbox->show_all;
}

# 'show' class closure
sub _do_show {
  my ($self) = @_;
  $self->load;
  return shift->signal_chain_from_overridden(@_);
}



( run in 1.079 second using v1.01-cache-2.11-cpan-39bf76dae61 )