Gtk3-Ex-DBI

 view release on metacpan or  search on metacpan

lib/Gtk3/Ex/DBI/Datasheet.pm  view on Meta::CPAN

# (C) Daniel Kasak: d.j.kasak.dk@gmail.com
# See COPYRIGHT file for full license

package Gtk3::Ex::DBI::Datasheet;

use parent 'Gtk3::Ex::DBI';

use strict;

# Perl throws up some pretty stupid warnings. If you want to hear all about them,
# go ahead and 'use warnings'. I've checked all the warnings produced, and they're
# all harmless ...

#use warnings;
no warnings;

use Carp;

use Glib qw/TRUE FALSE/;
#use Pango;
use Time::HiRes;

# Record Status Indicators
use constant {
                                        UNCHANGED               => 0,
                                        CHANGED                 => 1,
                                        INSERTED                => 2,
                                        DELETED                 => 3,
                                        LOCKED                  => 4
};

# Record Status column
use constant {
                                        STATUS_COLUMN           => 0
};

BEGIN {
    $Gtk3::Ex::DBI::Datasheet::VERSION                          = '3.3';
}

sub new {
    
    my ( $class, $req ) = @_;
    
    # Assemble object from request
    my $self = {
        dbh                  => $$req{dbh}                           # A database handle
      , schema               => $$req{schema}                        # Database schema ( not required for MySQL )
      , search_path          => $$req{search_path}                   # Schema search paths ( not required for MySQL )
      , sql                  => $$req{sql}                           # A hash of SQL related stuff
      , force_upper_case_fields => $$req{force_upper_case_fields}    # Forces fieldnames to be upper-case ( set this to match upper-case glade object names )
      , treeview             => $$req{treeview}                      # A Gtk2::Treeview to connect to
      , footer_treeview      => $$req{footer_treeview}               # A Gtk2::Treeview to connect to ( for the footer )
      , vbox                 => $$req{vbox}                          # A vbox to create treeview(s) in
      , footer               => $$req{footer}                        # A boolean to activate the footer treeview
      , sw_footer_no_scroll  => $$req{sw_footer_no_scroll}           # A boolean to de-activate the automatic ( vertical ) scrollbar for footers
      , fields               => $$req{fields}                        # Field definitions
      , column_info          => $$req{column_info} || undef          # 'Faked' column_info
      , multi_select         => $$req{multi_select}                  # Boolean to enable multi selection mode
      , column_sorting       => $$req{column_sorting} || 0           # Boolean to activate ( incomplete ) column sorting
      , default_font_size    => $$req{default_font_size} || undef    # Default font size
      , fixed_row_height     => $$req{fixed_row_height} || 0         # Boolean to activate fixed-height mode 
      , read_only            => $$req{read_only}                     # Boolean to indicate read-only mode
      , force_editable       => $$req{force_editable}                # Boolean to force cells to be editable ( eg so you can copy from them ) even if read-only
      , before_insert        => $$req{before_insert}                 # Code that runs *before* each record is inserted
      , before_query         => $$req{before_query}                  # Code that runs *before* query() is run
      , on_insert            => $$req{on_insert}                     # Code that runs *after* each record is inserted
      , before_apply         => $$req{before_apply}                  # Code that runs *before* each record is applied
      , on_apply             => $$req{on_apply}                      # Code that runs *after* each record is applied
      , on_row_select        => $$req{on_row_select}                 # Code that runs when a row is selected
      , on_changed           => $$req{on_changed}                    # Code that runs when a record is changed ( any column )
      , auto_apply           => $$req{auto_apply}                    # Boolean to force all records to be applied automatically when querying, closing, etc
      , after_size_allocate  => $$req{after_size_allocate} || undef  # Code that runs after the columns have responded to a size_allocate
      , dump_on_error        => $$req{dump_on_error}                 # Boolean to dump SQL command on DBI error
      , friendly_table_name  => $$req{friendly_table_name}           # Table name to use when issuing GUI errors
      , custom_changed_text  => $$req{custom_changed_text} || undef  # Text ( including markup ) to use in GUI questions when changes need to be applied
      , data_lock_field      => $$req{data_lock_field} || undef      # A field ( sql fieldname ) to use as a data-driven lock ( positive values will lock the record )
      , quick_renderers      => $$req{quick_renderers} || 0          # Boolean to force all renderers to quick, default text renderers
      , primary_keys         => $$req{primary_keys}                  # An array of primary keys ( overrides the default detected ones )
      , dont_update_keys     => $$req{dont_update_keys} || 0         # Boolean to allow primary key updates
      , auto_incrementing    => defined $$req{auto_incrementing}
                                   ? $$req{auto_incrementing} : 1    # A flag to indicate whether we should try to poll the last inserted ID after an insert
      , quiet                => $$req{quiet} || 0                    # Boolean to supress non-fatal warnings
      , recordset_tools_box  => $$req{recordset_tools_box}           # A box to create recordset tools in ( add / insert / update / delete / undo / spinnner )
      , recordset_tool_items => $$req{recordset_tool_items}          # An array of item names to add to the recordset tools box
      , auto_tools_box       => $$req{auto_tools_box}                # Boolean to turn on creation of the tools box ( requires vbox )
      , no_autosizing        => $$req{no_autosizing}                 # Disable column auto-sizing
      , recordset_extra_tools => $$req{recordset_extra_tools}        # Extra buttons to add to the recordset tools box
    };
    
    $self->{months_array} = qw/ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec /;
    
    # Sanity checks ...
    if ( ! $self->{dbh} ) {
        croak( "Gtk3::Ex::DBI::Datasheet constructor missing a dbh!" );
    }
    
    if ( ! $self->{treeview} && ! $self->{vbox} ) {
        croak( "Gtk3::Ex::DBI::Datasheet constructor requires either a treeview or a vbox!" );
    }
    
    if ( $self->{treeview} && $self->{vbox} ) {
        croak( "You passed BOTH a treeview AND a vbox. Use one or the other!" );
    }
    
    if ( $self->{sql} ) {
        if ( exists $self->{sql}->{pass_through} ) {
            $self->{read_only} = TRUE;
        } elsif ( ! ( exists $self->{sql}->{select} && exists $self->{sql}->{from} ) ) {
            croak( "Gtk3::Ex::DBI::Datasheet constructor missing a complete sql definition!\n"
                . "You either need to specify a pass_through key\n"
                . "or BOTH a 'select' AND and a 'from' key" );
        }



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