App-Chart

 view release on metacpan or  search on metacpan

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

# Copyright 2007, 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::IndicatorModel;
use 5.008;
use strict;
use warnings;
use Gtk2;
use List::MoreUtils;
use Locale::TextDomain ('App-Chart');

use constant DEBUG => 0;

use Glib::Object::Subclass
  'Gtk2::TreeStore';

use Class::Singleton 1.03; # 1.03 for _new_instance()
use base 'Class::Singleton';
*_new_instance = \&Glib::Object::new;

my %columns;
BEGIN {
  %columns = (COL_KEY      => 0,   # string
              COL_NAME     => 1,   # string
              COL_TYPE     => 2,   # string
              COL_PRIORITY => 3);  # string
}
use constant ({%columns});
use constant NUM_COLS => 4;

our $MODEL;
use constant::defer INIT_INSTANCE => sub {
  my ($self) = @_;

  $self->set_column_types (('Glib::String') x NUM_COLS);
  @{$self}{keys %columns} = values %columns;

  $self->set ($self->append(undef),
              COL_KEY,     'None',
              COL_NAME,    __('None'));

  my $aref = require App::Chart::Gtk2::IndicatorModelGenerated;

  # add anything not in IndicatorModelGenerated.pm
  {
    require Module::Find;
    require Gtk2::Ex::TreeModelBits;
    my %extra;
    # hash slice, everything on disk
    @extra{map {s/^App::Chart::Series::Derived:://;$_}
             Module::Find::findsubmod ('App::Chart::Series::Derived')} = ();
    # hash slice, drop keys already in the model
    delete @extra{map {$_->{'key'}} @$aref};

    # could load each extra to get name,type,priority ...
    foreach my $key (sort keys %extra) {
      push @$aref, { key      => $key,
                     name     => $key,
                     priority => 0 };
    }
  }

  # sort by translated name, case-insensitive
  @$aref = sort {$b->{'priority'} <=> $a->{'priority'}
                   || lc($a->{'name'}) cmp lc($b->{'name'})
                     || $a->{'name'} cmp $b->{'name'}
                   } @$aref;
  my ($top, $low)
    = List::MoreUtils::part {$_->{'priority'} >= 0 ? 0 : 1} @$aref;
  foreach my $elem (@$top) {
    $self->set($self->append(undef),
               COL_KEY,      $elem->{'key'},
               COL_NAME,     $elem->{'name'},
               COL_TYPE,     $elem->{'type'},
               COL_PRIORITY, $elem->{'priority'});
  }
  if (@$low) {
    my $low_iter = $self->append(undef);
    $self->set ($low_iter,
                COL_KEY,     'low-priority',
                COL_NAME,    __('Low Priority'));
    foreach my $elem (@$low) {
      $self->set($self->append($low_iter),
                 COL_KEY,      $elem->{'key'},
                 COL_NAME,     $elem->{'name'},
                 COL_TYPE,     $elem->{'type'},
                 COL_PRIORITY, $elem->{'priority'});
    }
  }
  if (DEBUG) {
    require Scalar::Util;
    Scalar::Util::weaken ($aref);
    if ($aref) {
      die "Oops, IndicatorModelGenerated array not destroyed";
    } else {
      print "IndicatorModelGenerated array destroyed\n";
    }
  }

  #--------------
  # TA



( run in 0.387 second using v1.01-cache-2.11-cpan-ceb78f64989 )