blib/lib/Gtk2/Ex/NumberRange.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Gtk2::Ex::NumberRange;
2  
3 our $VERSION = '0.02';
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 0 ▶use Glib qw(TRUE FALSE);
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Data::Dumper;
9 use Gtk2::Ex::PopupWindow;
10 use Carp;
11  
12 sub new {
13 my ($class, $preselected) = @_;
14 my $self = {};
15 bless ($self, $class);
16 $self->{widget} = $self->_get_widget;
17 return $self;
18 }
19  
20 sub get_model {
21 my ($self) = @_;
22 return $self->{model};
23 }
24  
25 sub set_model {
26 my ($self, $model) = @_;
27 if (!$model) {
28 $self->_clear;
29 return;
30 }
31 $self->{freezesignals} = TRUE;
32 my $commandchoices = $self->{commandchoices};
33 my $joinerchoices = $self->{joinerchoices};
34 my $i = 0; my $commandhash = { map { $_ => $i++ } @$commandchoices };
35 $i = 0; my $joinerhash = { map { $_ => $i++ } @$joinerchoices };
36 _model_error_check($commandhash, $joinerhash, $model);
37
38 $self->{commandcombo1}->set_active($commandhash->{$model->[0]});
39 $self->{entry1}->set_text($model->[1]);
40 if ($#{@$model} == 1) {
41 $self->{freezesignals} = TRUE;
42 $self->{joinercombo}->set_active(0);
43 $self->{commandcombo2}->set_active(-1);
44 $self->{freezesignals} = FALSE;
45 &{ $self->{signals}->{'changed'} } if $self->{signals}->{'changed'};
46 return;
47 }
48
49 $self->{joinercombo}->set_active($joinerhash->{$model->[2]});
50 $self->{commandcombo2}->set_active($commandhash->{$model->[3]});
51 $self->{entry2}->set_text($model->[4]);
52
53 $self->{freezesignals} = FALSE;
54 &{ $self->{signals}->{'changed'} } if $self->{signals}->{'changed'};
55 }
56  
57 sub to_sql_condition {
58 my ($self, $fieldname, $model) = @_;
59 return undef if $#{@$model} < 1;
60 if ($#{@$model} >= 1 and $#{@$model} < 4) {
61 my $str =
62 $fieldname
63 .' '.$model->[0]
64 .' '.$model->[1];
65 return $str;
66 }
67 if ($#{@$model} == 4) {
68 my $str1 =
69 $fieldname
70 .' '.$model->[0]
71 .' '.$model->[1];
72 my $str2 = $model->[2];
73 my $str3 =
74 $fieldname
75 .' '.$model->[3]
76 .' '.$model->[4];
77 my $str = "( $str1 $str2 $str3 )";
78 return $str;
79 }
80 }
81  
82  
83 sub _clear {
84 my ($self) = @_;
85 $self->{freezesignals} = TRUE;
86 $self->{commandcombo1}->set_active(-1);
87 $self->{entry1}->set_text('');
88 $self->{entry1}->set_sensitive(FALSE);
89 $self->{joinercombo}->set_active(0);
90 $self->{joinercombo}->set_sensitive(FALSE);
91 $self->{commandcombo2}->set_active(-1);
92 $self->{entry2}->set_text('');
93 $self->{model} = undef;
94 $self->{freezesignals} = FALSE;
95 &{ $self->{signals}->{'changed'} } if $self->{signals}->{'changed'};
96 }
97  
98 sub attach_popup_to {
99 my ($self, $parent) = @_;
100 my $popupwindow = Gtk2::Ex::PopupWindow->new($parent);
101 $popupwindow->set_move_with_parent(TRUE);
102 my $okbutton = Gtk2::Button->new_from_stock('gtk-ok');
103 $okbutton->signal_connect ('button-release-event' =>
104 sub {
105 $popupwindow->hide;
106 }
107 );
108 my $clearbutton = Gtk2::Button->new_from_stock('gtk-clear');
109 $clearbutton->signal_connect ('button-release-event' =>
110 sub {
111 $self->_clear;
112 }
113 );
114 my $hbox = Gtk2::HBox->new(TRUE, 0);
115 $hbox->pack_start ($clearbutton, TRUE, TRUE, 0);
116 $hbox->pack_start ($okbutton, TRUE, TRUE, 0);
117 my $vbox = Gtk2::VBox->new (FALSE, 0);
118 $vbox->pack_start ($self->{widget}, TRUE, TRUE, 0);
119 $vbox->pack_start ($hbox, FALSE, FALSE, 0);
120 my $frame = Gtk2::Frame->new;
121 $frame->add($vbox);
122 $popupwindow->{window}->add($frame);
123 return $popupwindow;
124 }
125  
126 sub signal_connect {
127 my ($self, $signal, $callback) = @_;
128 $self->{signals}->{$signal} = $callback;
129 }
130  
131 sub _model_error_check {
132 my ($commandhash, $joinerhash, $model) = @_;
133 return warn "Model should contain 2 or 5 parameters"
134 unless ($#{@$model} == 1 or $#{@$model} == 4);
135 return warn "Unknown command in model [@$model]"
136 unless exists($commandhash->{$model->[0]});
137 return if ($#{@$model} == 1);
138 return warn "Unknown joiner command in model [@$model]"
139 unless exists($joinerhash->{$model->[2]});
140 }
141  
142 sub _get_widget {
143 my ($self) = @_;
144
145 my $commandchoices = ['>', '>=', '=', '<=', '<'];
146 my $joinerchoices = ['', 'and', 'or'];
147
148 my $commandcombo1 = Gtk2::ComboBox->new_text;
149 my $entry1 = Gtk2::Entry->new;
150 my $joinercombo = Gtk2::ComboBox->new_text;
151 my $commandcombo2 = Gtk2::ComboBox->new_text;
152 my $entry2 = Gtk2::Entry->new;
153  
154 foreach my $x (@$commandchoices) {
155 $commandcombo1->append_text($x);
156 }
157 foreach my $x (@$joinerchoices) {
158 $joinercombo->append_text($x);
159 }
160 foreach my $x (@$commandchoices) {
161 $commandcombo2->append_text($x);
162 }
163 $commandcombo1->signal_connect ( 'changed' =>
164 sub {
165 $self->{model}->[0] = $commandchoices->[$commandcombo1->get_active()]
166 unless $commandcombo1->get_active() < 0;
167 $entry1->set_sensitive(TRUE);
168 &{ $self->{signals}->{'changed'} }
169 if $self->{signals}->{'changed'} and !$self->{freezesignals};
170 }
171 );
172 $entry1->signal_connect ( 'changed' =>
173 sub {
174 if ($entry1->get_text) {
175 $self->{model}->[1] = $entry1->get_text;
176 $joinercombo->set_sensitive(TRUE);
177 } else {
178 $self->{model}->[1] = undef;
179 $joinercombo->set_sensitive(FALSE);
180 }
181 &{ $self->{signals}->{'changed'} }
182 if $self->{signals}->{'changed'} and !$self->{freezesignals};
183 }
184 );
185 $commandcombo2->signal_connect ( 'changed' =>
186 sub {
187 $self->{model}->[3] = $commandchoices->[$commandcombo2->get_active()]
188 unless $commandcombo2->get_active() < 0;
189 $entry2->set_sensitive(TRUE);
190 &{ $self->{signals}->{'changed'} }
191 if $self->{signals}->{'changed'} and !$self->{freezesignals};
192 }
193 );
194 $entry2->signal_connect ( 'changed' =>
195 sub {
196 if ($entry2->get_text) {
197 $self->{model}->[4] = $entry2->get_text;
198 $joinercombo->set_sensitive(TRUE);
199 } else {
200 $self->{model}->[4] = undef;
201 $joinercombo->set_sensitive(FALSE);
202 }
203 &{ $self->{signals}->{'changed'} }
204 if $self->{signals}->{'changed'} and !$self->{freezesignals};
205 }
206 );
207 $commandcombo2->set_no_show_all(TRUE);
208 $entry2->set_no_show_all(TRUE);
209 $joinercombo->signal_connect ( 'changed' =>
210 sub {
211 if ($joinercombo->get_active == 0) {
212 $commandcombo2->hide_all;
213 $entry2->hide_all;
214 $commandcombo2->set_no_show_all(TRUE);
215 $entry2->set_no_show_all(TRUE);
216 $self->{model} = [$self->{model}->[0], $self->{model}->[1]];
217 } else {
218 $commandcombo2->set_no_show_all(FALSE);
219 $entry2->set_no_show_all(FALSE);
220 $commandcombo2->show_all;
221 $entry2->show_all;
222 $self->{model}->[2] = $joinerchoices->[$joinercombo->get_active()];
223 if ($commandcombo2->get_active > 0) {
224 $self->{model}->[4] = $entry2->get_text if $entry2->get_text;
225 $self->{model}->[3] = $commandchoices->[$commandcombo2->get_active()]
226 }
227 }
228 &{ $self->{signals}->{'changed'} }
229 if $self->{signals}->{'changed'} and !$self->{freezesignals};
230 }
231 );
232  
233 $self->{commandchoices}= $commandchoices;
234 $self->{joinerchoices} = $joinerchoices;
235
236 $self->{commandcombo1} = $commandcombo1;
237 $self->{entry1} = $entry1;
238  
239 $self->{commandcombo2} = $commandcombo2;
240 $self->{entry2} = $entry2;
241  
242 $self->{joinercombo} = $joinercombo;
243  
244 $self->{entry1}->set_sensitive(FALSE);
245 $self->{joinercombo}->set_sensitive(FALSE);
246 $self->{entry2}->set_sensitive(FALSE);
247
248 $commandcombo1->set_wrap_width(1);
249 $joinercombo->set_wrap_width(1);
250 $commandcombo2->set_wrap_width(1);
251  
252 my $table = Gtk2::Table->new(3,3,FALSE);
253 $table->set_col_spacings(5);
254 $table->attach($commandcombo1,0,1,0,1, 'expand', 'expand', 0, 0);
255 $table->attach($entry1,1,2,0,1, 'expand', 'expand', 0, 0);
256 $table->attach($joinercombo ,2,3,0,1, 'expand', 'expand', 0, 0);
257 $table->attach($commandcombo2,0,1,1,2, 'expand', 'expand', 0, 0);
258 $table->attach($entry2,1,2,1,2, 'expand', 'expand', 0, 0);
259 return $table;
260 }
261  
262 1;
263  
264 __END__
↑ Summary