| 1 | package Gtk2::Ex::DBITableFilter; | ||
| 2 | |||
| 3 | our $VERSION = '0.03'; | ||
| 4 | |||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use Gtk2::Ex::Simple::List; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Glib qw(TRUE FALSE); | ||
| 10 | use Data::Dumper; | ||
| 11 | use Gtk2::Ex::ComboBox; | ||
| 12 | use Gtk2::Ex::Simple::Menu; | ||
| 13 | use Gtk2::Ex::DBITableFilter::BrowserBar; | ||
| 14 | use Gtk2::Ex::DBITableFilter::FilterWidget; | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my ($class, $slist) = @_; | ||
| 18 | my $self = {}; | ||
| 19 | bless ($self, $class); | ||
| 20 | $self->set_increment(500); | ||
| 21 | $self->{slist} = $slist; | ||
| 22 | $self->{dumpedcachedparams} = undef; | ||
| 23 | $self->{params} = undef; | ||
| 24 | $self->{browserbar} = undef; | ||
| 25 | $self->{filterwidget} = Gtk2::Ex::DBITableFilter::FilterWidget->new($self); | ||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub process_dates { | ||
| 30 | my ($self, $fieldname, $list) = @_; | ||
| 31 | return $self->{filterwidget}->process_dates($fieldname, $list); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub set_simple_sql { | ||
| 35 | my ($self, $callback) = @_; | ||
| 36 | $self->{methods}->{count} = sub { | ||
| 37 | my ($dbh, $params) = @_; | ||
| 38 | my $sql = &$callback($params); | ||
| 39 | my $countsql = $sql; | ||
| 40 | $countsql = 'select count(*) from ('.$sql.') DBITableFilterTemp'; | ||
| 41 | print Dumper $countsql; | ||
| 42 | my $sth = $dbh->prepare($countsql); | ||
| 43 | $sth->execute(); | ||
| 44 | my @result_array; | ||
| 45 | while (my @ary = $sth->fetchrow_array()) { | ||
| 46 | push @result_array, $ary[0]; | ||
| 47 | } | ||
| 48 | return \@result_array; | ||
| 49 | }; | ||
| 50 | $self->{methods}->{fetch} = sub { | ||
| 51 | my ($dbh, $params) = @_; | ||
| 52 | my $sql = &$callback($params); | ||
| 53 | my $fetchsql = $sql; | ||
| 54 | $fetchsql = 'select * from ('.$sql.') DBITableFilterTemp limit ' | ||
| 55 | .$params->{limit}->{start} | ||
| 56 | .' , ' | ||
| 57 | .$params->{limit}->{step}; | ||
| 58 | print Dumper $fetchsql; | ||
| 59 | my $sth = $dbh->prepare($fetchsql); | ||
| 60 | $sth->execute(); | ||
| 61 | my @result_array; | ||
| 62 | while (my @ary = $sth->fetchrow_array()) { | ||
| 63 | push @result_array, \@ary; | ||
| 64 | } | ||
| 65 | return \@result_array; | ||
| 66 | }; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub set_increment { | ||
| 70 | my ($self, $increment) = @_; | ||
| 71 | my $limit = $self->{limit}; | ||
| 72 | $limit->{increment} = $increment; | ||
| 73 | $limit->{start} = 0; | ||
| 74 | $limit->{end} = $limit->{increment}; | ||
| 75 | $limit->{total} = $limit->{increment}; | ||
| 76 | $self->{limit} = $limit; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub count_using { | ||
| 80 | my ($self, $method) = @_; | ||
| 81 | $self->{methods}->{count} = $method; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub fetch_using { | ||
| 85 | my ($self, $method) = @_; | ||
| 86 | $self->{methods}->{fetch} = $method; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub set_thread { | ||
| 90 | my ($self, $thread) = @_; | ||
| 91 | $self->{thread} = $thread; | ||
| 92 | my $fetch_records_query = $thread->register_query ( | ||
| 93 | $self, $self->{methods}->{fetch}, \&_post_fetch_records | ||
| 94 | ); | ||
| 95 | $self->{fetch_records_query} = $fetch_records_query; | ||
| 96 | my $count_records_query = $thread->register_query ( | ||
| 97 | $self, $self->{methods}->{count}, \&_post_count_records | ||
| 98 | ); | ||
| 99 | $self->{count_records_query} = $count_records_query; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub get_widget { | ||
| 103 | my ($self) = @_; | ||
| 104 | my $scrolledwindow= Gtk2::ScrolledWindow->new (undef, undef); | ||
| 105 | $scrolledwindow->set_policy ('automatic', 'automatic'); | ||
| 106 | $scrolledwindow->add($self->{slist}); | ||
| 107 | my $bbar = Gtk2::Ex::DBITableFilter::BrowserBar->new($self); | ||
| 108 | $self->{browserbar} = $bbar; | ||
| 109 | my $hboxbottom = $bbar->get_widget; | ||
| 110 | my $vbox = Gtk2::VBox->new (FALSE, 0); | ||
| 111 | $vbox->pack_start ($scrolledwindow, TRUE, TRUE, 0); | ||
| 112 | $vbox->pack_start ($hboxbottom, FALSE, TRUE, 0); | ||
| 113 | return $vbox; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub _update_count { | ||
| 117 | my ($self, $params) = @_; | ||
| 118 | $self->{browserbar}->update_progress_label('Counting'); | ||
| 119 | $self->{browserbar}->start_progress(0, 0.4); | ||
| 120 | $self->{count_records_query}->execute($params); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub refresh { | ||
| 124 | my ($self) = @_; | ||
| 125 | my $str1 = $self->{dumpedcachedparams} || ''; | ||
| 126 | my $str2 = Dumper $self->{params}; | ||
| 127 | if ($str1 eq $str2) { | ||
| 128 | # No need to update the count | ||
| 129 | print "No need to count\n"; | ||
| 130 | $self->{browserbar}->end_progress(0.4); | ||
| 131 | $self->{browserbar}->start_progress(0.5, 1.0); | ||
| 132 | $self->{browserbar}->update_progress_label('Fetching'); | ||
| 133 | $self->{fetch_records_query}->execute( | ||
| 134 | {params => $self->{params}, limit => $self->{limit}} | ||
| 135 | ); | ||
| 136 | } else { | ||
| 137 | # Must update the count | ||
| 138 | print "Must re-count\n"; | ||
| 139 | $self->{dumpedcachedparams} = Dumper $self->{params}; | ||
| 140 | $self->_update_count({params => $self->{params}}); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | sub _post_count_records { | ||
| 145 | my ($self, $result_array) = @_; | ||
| 146 | my $count = $result_array->[0]; | ||
| 147 | $self->{limit}->{total} = $count; | ||
| 148 | $self->{browserbar}->_go_to_first; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub _post_fetch_records { | ||
| 152 | my ($self, $result_array) = @_; | ||
| 153 | $self->{browserbar}->update_progress_label('Rendering'); | ||
| 154 | @{$self->{slist}->{data}} = (); | ||
| 155 | foreach my $x (@$result_array) { | ||
| 156 | push @{$self->{slist}->{data}}, $x; | ||
| 157 | } | ||
| 158 | $self->{browserbar}->update_progress_label('Showing'); | ||
| 159 | $self->{browserbar}->end_progress(1); | ||
| 160 | $self->{browserbar}->end_progress(0); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub make_checkable { | ||
| 164 | my ($self) = @_; | ||
| 165 | my ($slist) = $self->{slist}; | ||
| 166 | # Add a checkbutton to select all | ||
| 167 | $slist->set_headers_clickable(TRUE); | ||
| 168 | my $col = $slist->get_column(0); | ||
| 169 | my $check = Gtk2::CheckButton->new; | ||
| 170 | $col->set_widget($check); | ||
| 171 | $check->set_active(TRUE); | ||
| 172 | $check->show; # very important, show_all doesn't get this widget! | ||
| 173 | my $button = $col->get_widget; # not a button | ||
| 174 | do { | ||
| 175 | $button = $button->get_parent; | ||
| 176 | } until ($button->isa ('Gtk2::Button')); | ||
| 177 | |||
| 178 | $button->signal_connect (button_release_event => | ||
| 179 | sub { | ||
| 180 | if ($check->get_active()) { | ||
| 181 | $check->set_active(FALSE); | ||
| 182 | } else { | ||
| 183 | $check->set_active(TRUE); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | ); | ||
| 187 | $check->signal_connect (toggled => | ||
| 188 | sub { | ||
| 189 | my $flag = 0; | ||
| 190 | $flag = 1 if $check->get_active(); | ||
| 191 | foreach my $line (@{$slist->{data}}) { | ||
| 192 | $line->[0] = $flag; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | ); | ||
| 196 | } | ||
| 197 | |||
| 198 | sub add_choice { | ||
| 199 | my ($self, $columnnumber, $list) = @_; | ||
| 200 | $self->{filterwidget}->add_choice($columnnumber, $list); | ||
| 201 | } | ||
| 202 | |||
| 203 | sub add_search_box { | ||
| 204 | my ($self, $columnnumber) = @_; | ||
| 205 | $self->{filterwidget}->add_search_box($columnnumber); | ||
| 206 | } | ||
| 207 | |||
| 208 | sub add_date_filter { | ||
| 209 | my ($self, $columnnumber, $preselected) = @_; | ||
| 210 | $self->{filterwidget}->add_date_filter($columnnumber, $preselected); | ||
| 211 | } | ||
| 212 | |||
| 213 | 1; | ||
| 214 | |||
| 215 | __END__ | ||