| 1 | package Gtk2::Ex::DBITableFilter::FilterWidget; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Glib qw(TRUE FALSE); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Data::Dumper; | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my ($class, $parent) = @_; | ||
| 10 | my $self = {}; | ||
| 11 | bless ($self, $class); | ||
| 12 | $self->{parent} = $parent; | ||
| 13 | return $self; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub add_search_box { | ||
| 17 | my ($self, $columnnumber) = @_; | ||
| 18 | my $parent = $self->{parent}; | ||
| 19 | my $slist = $parent->{slist}; | ||
| 20 | $slist->set_headers_clickable(TRUE); | ||
| 21 | my $col = $slist->get_column($columnnumber); | ||
| 22 | my $title = $col->get_title; | ||
| 23 | my $label = Gtk2::Label->new ($title); | ||
| 24 | my $labelbox = _add_arrow($label); | ||
| 25 | $col->set_widget ($labelbox); | ||
| 26 | $labelbox->show_all; | ||
| 27 | my $button = $col->get_widget; # not a button | ||
| 28 | do { | ||
| 29 | $button = $button->get_parent; | ||
| 30 | } until ($button->isa ('Gtk2::Button')); | ||
| 31 | my $popupwindow = Gtk2::Ex::PopupWindow->new($labelbox); | ||
| 32 | my $frame = Gtk2::Frame->new; | ||
| 33 | my $entry = Gtk2::Entry->new; | ||
| 34 | $entry->signal_connect( 'changed' => | ||
| 35 | sub { | ||
| 36 | my $pattern_string; | ||
| 37 | if ($entry->get_text) { | ||
| 38 | $pattern_string = '\'%'.$entry->get_text.'%\''; | ||
| 39 | } else { | ||
| 40 | $pattern_string = undef; | ||
| 41 | } | ||
| 42 | $parent->{params}->{columnfilter}->{$columnnumber} = $pattern_string; | ||
| 43 | } | ||
| 44 | ); | ||
| 45 | $frame->add($entry); | ||
| 46 | $frame->set_size_request(0, 0); | ||
| 47 | $popupwindow->{window}->add($frame); | ||
| 48 | $button->signal_connect ('button-release-event' => | ||
| 49 | sub { | ||
| 50 | my ($self, $event) = @_; | ||
| 51 | $popupwindow->show; | ||
| 52 | } | ||
| 53 | ); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub add_choice { | ||
| 57 | my ($self, $columnnumber, $list) = @_; | ||
| 58 | my $parent = $self->{parent}; | ||
| 59 | my $slist = $parent->{slist}; | ||
| 60 | $slist->set_headers_clickable(TRUE); | ||
| 61 | my $col = $slist->get_column($columnnumber); | ||
| 62 | my $title = $col->get_title; | ||
| 63 | my $label = Gtk2::Label->new ($title); | ||
| 64 | my $labelbox = _add_arrow($label); | ||
| 65 | $col->set_widget ($labelbox); | ||
| 66 | $labelbox->show_all; | ||
| 67 | my $button = $col->get_widget; # not a button | ||
| 68 | do { | ||
| 69 | $button = $button->get_parent; | ||
| 70 | } until ($button->isa ('Gtk2::Button')); | ||
| 71 | my $combobox = Gtk2::Ex::ComboBox->new($labelbox, 'with-buttons'); | ||
| 72 | $combobox->set_list_preselected($list); | ||
| 73 | $button->signal_connect ('button-release-event' => | ||
| 74 | sub { | ||
| 75 | my ($self, $event) = @_; | ||
| 76 | $combobox->show; | ||
| 77 | } | ||
| 78 | ); | ||
| 79 | $parent->{params}->{columnfilter}->{$columnnumber} | ||
| 80 | = combine($combobox->get_selected_values->{'selected-values'}); | ||
| 81 | $combobox->signal_connect( 'changed' => | ||
| 82 | sub { | ||
| 83 | $parent->{params}->{columnfilter}->{$columnnumber} | ||
| 84 | = combine($combobox->get_selected_values->{'selected-values'}); | ||
| 85 | } | ||
| 86 | ); | ||
| 87 | $parent->{combobox}->{$columnnumber} = $combobox; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub combine { | ||
| 91 | my ($list) = @_; | ||
| 92 | my $str = join '\',\'', @$list; | ||
| 93 | $str = '(\''.$str.'\')'; | ||
| 94 | return $str; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub add_date_filter { | ||
| 98 | my ($self, $columnnumber, $preselected) = @_; | ||
| 99 | my ($command1, $date1, $joiner1, $command2, $date2) = ('', '', '', '', ''); | ||
| 100 | |||
| 101 | ($command1, $date1, $joiner1, $command2, $date2) = @$preselected if $preselected; | ||
| 102 | #print "$date1, $command1, $joiner1, $date2, $command2\n"; | ||
| 103 | |||
| 104 | my $parent = $self->{parent}; | ||
| 105 | my $button = $self->_get_column_header_button($columnnumber); | ||
| 106 | my $popupwindow = Gtk2::Ex::PopupWindow->new($button); | ||
| 107 | $popupwindow->set_move_with_parent(TRUE); | ||
| 108 | |||
| 109 | my $choices = ['before', 'after']; | ||
| 110 | my $joiner = [ '', 'and', 'or']; | ||
| 111 | |||
| 112 | my $datelabel1 = Gtk2::Label->new; | ||
| 113 | my $datelabelbox1 = _calendar_popup($datelabel1, $date1); | ||
| 114 | my $combobox1 = Gtk2::ComboBox->new_text; | ||
| 115 | $combobox1->append_text($choices->[0]); | ||
| 116 | $combobox1->append_text($choices->[1]); | ||
| 117 | my $index1 = 1; | ||
| 118 | if ($command1) { | ||
| 119 | my $i = 0; | ||
| 120 | $index1 = { map { $_ => $i++ } @$choices }->{$command1}; | ||
| 121 | } | ||
| 122 | $combobox1->set_active($index1); | ||
| 123 | |||
| 124 | my $datelabel2 = Gtk2::Label->new; | ||
| 125 | my $datelabelbox2 = _calendar_popup($datelabel2, $date2); | ||
| 126 | my $combobox2 = Gtk2::ComboBox->new_text; | ||
| 127 | $combobox2->append_text($choices->[0]); | ||
| 128 | $combobox2->append_text($choices->[1]); | ||
| 129 | my $index2 = 0; | ||
| 130 | if ($command1) { | ||
| 131 | my $i = 0; | ||
| 132 | $index2 = { map { $_ => $i++ } @$choices }->{$command2}; | ||
| 133 | } | ||
| 134 | $combobox2->set_active($index2); | ||
| 135 | |||
| 136 | my $and_or_combobox = Gtk2::ComboBox->new_text; | ||
| 137 | $and_or_combobox->append_text($joiner->[0]); | ||
| 138 | $and_or_combobox->append_text($joiner->[1]); | ||
| 139 | $and_or_combobox->append_text($joiner->[2]); | ||
| 140 | my $index = 0; | ||
| 141 | if ($joiner1) { | ||
| 142 | my $i = 0; | ||
| 143 | $index = { map { $_ => $i++ } @$joiner }->{$joiner1}; | ||
| 144 | } | ||
| 145 | $and_or_combobox->set_active($index); | ||
| 146 | $and_or_combobox->signal_connect ( 'changed' => | ||
| 147 | sub { | ||
| 148 | my $choice = $and_or_combobox->get_active(); | ||
| 149 | if ($choice == 0) { | ||
| 150 | $combobox2->hide_all; | ||
| 151 | $datelabelbox2->hide_all; | ||
| 152 | } else { | ||
| 153 | $combobox2->show; | ||
| 154 | $datelabelbox2->show_all; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | ); | ||
| 158 | |||
| 159 | my $refreshparams = sub { | ||
| 160 | my $params; | ||
| 161 | $params->[0]->{command} = $choices->[$combobox1->get_active()]; | ||
| 162 | $params->[0]->{date} = $datelabel1->get_text(); | ||
| 163 | return $params unless $and_or_combobox->get_active();; | ||
| 164 | $params->[1]->{joiner} = $joiner->[$and_or_combobox->get_active()]; | ||
| 165 | $params->[1]->{command} = $choices->[$combobox2->get_active()]; | ||
| 166 | $params->[1]->{date} = $datelabel2->get_text(); | ||
| 167 | return $params; | ||
| 168 | }; | ||
| 169 | |||
| 170 | my $table = Gtk2::Table->new(3,3,FALSE); | ||
| 171 | $table->attach($combobox1,0,1,0,1, 'expand', 'expand', 0, 0); | ||
| 172 | $table->attach($datelabelbox1,1,2,0,1, 'expand', 'expand', 0, 0); | ||
| 173 | $table->attach($and_or_combobox,2,3,0,1, 'expand', 'expand', 0, 0); | ||
| 174 | $table->attach($combobox2,0,1,1,2, 'expand', 'expand', 0, 0); | ||
| 175 | $table->attach($datelabelbox2,1,2,1,2, 'expand', 'expand', 0, 0); | ||
| 176 | my $apply = Gtk2::Button->new_from_stock('gtk-apply'); | ||
| 177 | $parent->{params}->{columnfilter}->{$columnnumber} = &$refreshparams if $preselected; | ||
| 178 | $apply->signal_connect ('button-release-event' => | ||
| 179 | sub { | ||
| 180 | $parent->{params}->{columnfilter}->{$columnnumber} | ||
| 181 | = &$refreshparams; | ||
| 182 | $popupwindow->hide; | ||
| 183 | } | ||
| 184 | ); | ||
| 185 | my $clear = Gtk2::Button->new_from_stock('gtk-clear'); | ||
| 186 | $clear->signal_connect ('button-release-event' => | ||
| 187 | sub { | ||
| 188 | $parent->{params}->{columnfilter}->{$columnnumber} = undef; | ||
| 189 | $and_or_combobox->set_active(0); | ||
| 190 | $datelabel1->set_text('none'); | ||
| 191 | $popupwindow->hide; | ||
| 192 | } | ||
| 193 | ); | ||
| 194 | my $hbox = Gtk2::HBox->new (TRUE, 0); | ||
| 195 | $hbox->pack_start ($apply, TRUE, TRUE, 0); | ||
| 196 | $hbox->pack_start ($clear, TRUE, TRUE, 0); | ||
| 197 | my $vbox = Gtk2::VBox->new (FALSE, 0); | ||
| 198 | $vbox->pack_start ($table, TRUE, TRUE, 0); | ||
| 199 | $vbox->pack_start ($hbox, FALSE, FALSE, 0); | ||
| 200 | $popupwindow->{window}->add($vbox); | ||
| 201 | $button->signal_connect ('button-release-event' => | ||
| 202 | sub { | ||
| 203 | my ($self, $event) = @_; | ||
| 204 | $popupwindow->show; | ||
| 205 | unless ($and_or_combobox->get_active()) { | ||
| 206 | $combobox2->hide; | ||
| 207 | $datelabelbox2->hide; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | ); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub process_dates { | ||
| 214 | my ($self, $fieldname, $list) = @_; | ||
| 215 | print Dumper $list; | ||
| 216 | my $str = ''; | ||
| 217 | return $str unless $list; | ||
| 218 | return $str unless $list->[0]; | ||
| 219 | if ($list->[0]->{command} eq 'after') { | ||
| 220 | $str .= ' '.$fieldname.' > \''.$list->[0]->{date}.'\''; | ||
| 221 | } elsif ($list->[0]->{command} eq 'before') { | ||
| 222 | $str .= ' '.$fieldname.' < \''.$list->[0]->{date}.'\''; | ||
| 223 | } | ||
| 224 | return ' and '.$str unless $list->[1]->{joiner}; | ||
| 225 | $str .=' '.$list->[1]->{joiner}.' '; | ||
| 226 | if ($list->[1]->{command} eq 'after') { | ||
| 227 | $str .= ''.$fieldname.' > \''.$list->[1]->{date}.'\''; | ||
| 228 | } elsif ($list->[1]->{command} eq 'before') { | ||
| 229 | $str .= ''.$fieldname.' < \''.$list->[1]->{date}.'\''; | ||
| 230 | } | ||
| 231 | $str = ' and ( '.$str.' ) '; | ||
| 232 | return $str; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub _get_column_header_button { | ||
| 236 | my ($self, $columnnumber) = @_; | ||
| 237 | my $parent = $self->{parent}; | ||
| 238 | my $slist = $parent->{slist}; | ||
| 239 | $slist->set_headers_clickable(TRUE); | ||
| 240 | my $col = $slist->get_column($columnnumber); | ||
| 241 | my $title = $col->get_title; | ||
| 242 | my $label = Gtk2::Label->new ($title); | ||
| 243 | my $labelbox = _add_arrow($label); | ||
| 244 | $col->set_widget ($labelbox); | ||
| 245 | $labelbox->show_all; | ||
| 246 | my $button = $col->get_widget; # not a button | ||
| 247 | do { | ||
| 248 | $button = $button->get_parent; | ||
| 249 | } until ($button->isa ('Gtk2::Button')); | ||
| 250 | return $button; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub _calendar_popup { | ||
| 254 | my ($datelabel, $datestr) = @_; | ||
| 255 | my $datelabelbox = _add_button_press(_add_arrow($datelabel)); | ||
| 256 | my $datepopup = Gtk2::Ex::PopupWindow->new($datelabelbox); | ||
| 257 | my $date = Gtk2::Calendar->new; | ||
| 258 | if ($datestr) { | ||
| 259 | my ($year, $mon, $day) = split '-', $datestr; | ||
| 260 | $date->select_month($mon-1, $year); | ||
| 261 | $date->select_day($day); | ||
| 262 | $datelabel->set_text("$year-$mon-$day"); | ||
| 263 | } | ||
| 264 | my $apply = Gtk2::Button->new_from_stock('gtk-apply'); | ||
| 265 | my $vbox = Gtk2::VBox->new (FALSE, 0); | ||
| 266 | $vbox->pack_start ($date, FALSE, FALSE, 0); | ||
| 267 | $vbox->pack_start ($apply, FALSE, FALSE, 0); | ||
| 268 | $apply->signal_connect ('button-release-event' => | ||
| 269 | sub { | ||
| 270 | my ($year, $mon, $day) = $date->get_date(); | ||
| 271 | $datelabel->set_text("$year-$mon-$day"); | ||
| 272 | $datepopup->hide; | ||
| 273 | } | ||
| 274 | ); | ||
| 275 | $datepopup->{window}->add($vbox); | ||
| 276 | $datelabelbox->signal_connect ('button-release-event' => | ||
| 277 | sub { | ||
| 278 | $datepopup->show; | ||
| 279 | } | ||
| 280 | ); | ||
| 281 | return $datelabelbox; | ||
| 282 | } | ||
| 283 | |||
| 284 | sub month { | ||
| 285 | return [ | ||
| 286 | 'January', | ||
| 287 | 'February', | ||
| 288 | 'March', | ||
| 289 | 'April', | ||
| 290 | 'May', | ||
| 291 | 'June', | ||
| 292 | 'July', | ||
| 293 | 'August', | ||
| 294 | 'September', | ||
| 295 | 'October', | ||
| 296 | 'November', | ||
| 297 | 'December', | ||
| 298 | ]; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub _add_arrow { | ||
| 302 | my ($label) = @_; | ||
| 303 | my $arrow = Gtk2::Arrow -> new('down', 'none'); | ||
| 304 | my $labelbox = Gtk2::HBox->new (FALSE, 0); | ||
| 305 | $labelbox->pack_start ($label, FALSE, FALSE, 0); | ||
| 306 | $labelbox->pack_start ($arrow, FALSE, FALSE, 0); | ||
| 307 | return $labelbox; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub _add_button_press { | ||
| 311 | my ($widget) = @_; | ||
| 312 | my $eventbox = Gtk2::EventBox->new; | ||
| 313 | $eventbox->add ($widget); | ||
| 314 | $eventbox->add_events (['button-release-mask']); | ||
| 315 | return $eventbox; | ||
| 316 | } | ||
| 317 | |||
| 318 | 1; | ||
| 319 | |||
| 320 | __END__ | ||