| 1 | package Gtk2::Ex::MindMapView::Connection; | ||
| 2 | |||
| 3 | our $VERSION = '0.000001'; | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | use List::Util; | ||
| 9 | |||
| 10 | use Gnome2::Canvas; | ||
| 11 | |||
| 12 | use POSIX qw(DBL_MAX); | ||
| 13 | |||
| 14 | use Glib ':constants'; | ||
| 15 | use Glib::Object::Subclass | ||
| 16 | Gnome2::Canvas::Bpath::, | ||
| 17 | properties => [ | ||
| 18 | Glib::ParamSpec->string ('arrows', 'arrow-type', | ||
| 19 | 'Type of arrow to display', 'none', G_PARAM_READWRITE), | ||
| 20 | |||
| 21 | Glib::ParamSpec->scalar ('predecessor_item', 'predecessor_item', | ||
| 22 | 'Predecessor view item', G_PARAM_READWRITE), | ||
| 23 | |||
| 24 | Glib::ParamSpec->scalar ('item', 'item_item', | ||
| 25 | 'Item view_item', G_PARAM_READWRITE), | ||
| 26 | ] | ||
| 27 | ; | ||
| 28 | |||
| 29 | |||
| 30 | sub INIT_INSTANCE | ||
| 31 | { | ||
| 32 | my $self = shift(@_); | ||
| 33 | |||
| 34 | $self->{x1} = 0; | ||
| 35 | |||
| 36 | $self->{y1} = 0; | ||
| 37 | |||
| 38 | $self->{x2} = 0; | ||
| 39 | |||
| 40 | $self->{y2} = 0; | ||
| 41 | |||
| 42 | $self->{predecessor_item} = undef; | ||
| 43 | |||
| 44 | $self->{predecessor_signal_id} = 0; | ||
| 45 | |||
| 46 | $self->{item} = undef; | ||
| 47 | |||
| 48 | $self->{item_signal_id} = 0; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | sub SET_PROPERTY | ||
| 53 | { | ||
| 54 | my ($self, $pspec, $newval) = @_; | ||
| 55 | |||
| 56 | my $param_name = $pspec->get_name(); | ||
| 57 | |||
| 58 | if ($param_name eq 'predecessor_item') | ||
| 59 | { | ||
| 60 | if ((defined $self->{predecessor_item}) && ($self->{predecessor_item} != $newval)) | ||
| 61 | { | ||
| 62 | $self->{predecessor_item}->signal_handler_disconnect($self->{predecessor_signal_id}); | ||
| 63 | } | ||
| 64 | |||
| 65 | $self->{predecessor_item} = $newval; | ||
| 66 | |||
| 67 | $self->{predecessor_signal_id} = | ||
| 68 | $newval->signal_connect('connection_adjust'=> | ||
| 69 | sub { _predecessor_connection($self, @_); }); | ||
| 70 | } | ||
| 71 | |||
| 72 | if ($param_name eq 'item') | ||
| 73 | { | ||
| 74 | if ((defined $self->{item}) && ($self->{item} != $newval)) | ||
| 75 | { | ||
| 76 | $self->{item}->signal_handler_disconnect($self->{item_signal_id}); | ||
| 77 | } | ||
| 78 | |||
| 79 | $self->{item} = $newval; | ||
| 80 | |||
| 81 | $self->{item_signal_id} = | ||
| 82 | $newval->signal_connect('connection_adjust'=> | ||
| 83 | sub { _item_connection($self, @_); }); | ||
| 84 | } | ||
| 85 | |||
| 86 | $self->{$param_name} = $newval; | ||
| 87 | |||
| 88 | # print "Connection, SET_PROPERTY, name: $param_name value: $newval\n"; | ||
| 89 | |||
| 90 | if ((defined $self->{predecessor_item}) && (defined $self->{item})) | ||
| 91 | { | ||
| 92 | _set_connection_path($self); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub connect | ||
| 98 | { | ||
| 99 | my $self = shift(@_); | ||
| 100 | |||
| 101 | $self->{predecessor_signal_id} = | ||
| 102 | $self->{predecessor_item}->signal_connect('connection_adjust'=> | ||
| 103 | sub { _predecessor_connection($self, @_); }); | ||
| 104 | $self->{item_signal_id} = | ||
| 105 | $self->{item}->signal_connect('connection_adjust'=> | ||
| 106 | sub { _item_connection($self, @_); }); | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | sub disconnect | ||
| 111 | { | ||
| 112 | my $self = shift(@_); | ||
| 113 | |||
| 114 | $self->{predecessor_item}->signal_handler_disconnect($self->{predecessor_signal_id}); | ||
| 115 | |||
| 116 | $self->{item}->signal_handler_disconnect($self->{item_signal_id}); | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | sub _direction | ||
| 121 | { | ||
| 122 | my ($predecessor_item, $item) = @_; | ||
| 123 | |||
| 124 | my $predecessor_column = $predecessor_item->get('column'); | ||
| 125 | |||
| 126 | my $predecessor_column_no = | ||
| 127 | (defined $predecessor_column) ? $predecessor_column->get('column_no') : 0; | ||
| 128 | |||
| 129 | my $column = $item->get('column'); | ||
| 130 | |||
| 131 | my $item_column_no = (defined $column) ? $column->get('column_no') : 0; | ||
| 132 | |||
| 133 | if ($predecessor_column_no > $item_column_no) | ||
| 134 | { | ||
| 135 | return ('left', 'left'); | ||
| 136 | } | ||
| 137 | |||
| 138 | if ($predecessor_column_no < $item_column_no) | ||
| 139 | { | ||
| 140 | return ('right', 'right'); | ||
| 141 | } | ||
| 142 | |||
| 143 | if ($predecessor_column_no >= 0) | ||
| 144 | { | ||
| 145 | return ('right', 'left'); | ||
| 146 | } | ||
| 147 | |||
| 148 | return ('left', 'right'); | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | sub _item_connection | ||
| 153 | { | ||
| 154 | my $self = shift(@_); | ||
| 155 | |||
| 156 | my $direction = (_direction($self->get('predecessor_item'), $self->get('item')))[1]; | ||
| 157 | |||
| 158 | my $side = ($direction eq 'right') ? 'left' : 'right'; | ||
| 159 | |||
| 160 | my ($x2, $y2) = $self->get('item')->get_connection_point($side); | ||
| 161 | |||
| 162 | my @successors = $self->get('item')->successors($side); | ||
| 163 | |||
| 164 | my $offset = ($side eq 'left') ? -3 : 3; # FIXME: UGH should be radius of toggle. | ||
| 165 | |||
| 166 | $self->{x2} = (scalar @successors > 0) ? $x2 + $offset : $x2; | ||
| 167 | |||
| 168 | $self->{y2} = $y2; | ||
| 169 | |||
| 170 | _set_connection_path($self); | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | sub _predecessor_connection | ||
| 175 | { | ||
| 176 | my $self = shift(@_); | ||
| 177 | |||
| 178 | my $direction = (_direction($self->get('predecessor_item'), $self->get('item')))[0]; | ||
| 179 | |||
| 180 | my ($x1, $y1) = $self->get('predecessor_item')->get_connection_point($direction); | ||
| 181 | |||
| 182 | my @successors = $self->get('predecessor_item')->successors($direction); | ||
| 183 | |||
| 184 | my $offset = ($direction eq 'left') ? -3 : 3; # FIXME: UGH should be radius of toggle. | ||
| 185 | |||
| 186 | $self->{x1} = (scalar @successors > 0) ? $x1 + $offset : $x1; | ||
| 187 | |||
| 188 | $self->{y1} = $y1; | ||
| 189 | |||
| 190 | _set_connection_path($self); | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | sub _bpath | ||
| 195 | { | ||
| 196 | my $self = shift(@_); | ||
| 197 | |||
| 198 | my $x1 = $self->{x1}; | ||
| 199 | |||
| 200 | my $y1 = $self->{y1}; | ||
| 201 | |||
| 202 | my $x2 = $self->{x2}; | ||
| 203 | |||
| 204 | my $y2 = $self->{y2}; | ||
| 205 | |||
| 206 | my ($predecessor_direction, $item_direction) = | ||
| 207 | _direction($self->get('predecessor_item'), $self->get('item')); | ||
| 208 | |||
| 209 | my $c = List::Util::max(25, abs((($x2 - $x1) / 2))); | ||
| 210 | |||
| 211 | my $a = ($predecessor_direction eq 'right') ? $x1 + $c : $x1 - $c; | ||
| 212 | |||
| 213 | my $b = ($item_direction eq 'right') ? $x2 - $c : $x2 + $c; | ||
| 214 | |||
| 215 | my @p = ($x1, $y1, $a, $y1, $b, $y2, $x2, $y2); | ||
| 216 | |||
| 217 | my $pathdef = Gnome2::Canvas::PathDef->new(); | ||
| 218 | |||
| 219 | $pathdef->moveto ($p[0], $p[1]); | ||
| 220 | |||
| 221 | $pathdef->curveto ($p[2], $p[3], $p[4], $p[5], $p[6], $p[7]); | ||
| 222 | |||
| 223 | |||
| 224 | return $pathdef if ($self->get('arrows') eq 'none'); | ||
| 225 | |||
| 226 | |||
| 227 | my $h = 4 * $self->get('width-pixels'); # Height of arrow head. | ||
| 228 | |||
| 229 | my $v = $h / 2; | ||
| 230 | |||
| 231 | if ($item_direction eq 'right') | ||
| 232 | { | ||
| 233 | @p = ($x2-$h,$y2+$v, $x2-$h,$y2-$v, $x2,$y2); | ||
| 234 | } | ||
| 235 | else | ||
| 236 | { | ||
| 237 | @p = ($x2+$h,$y2+$v, $x2+$h,$y2-$v, $x2,$y2); | ||
| 238 | } | ||
| 239 | |||
| 240 | $pathdef->lineto ($p[0], $p[1]); | ||
| 241 | |||
| 242 | $pathdef->lineto ($p[2], $p[3]); | ||
| 243 | |||
| 244 | $pathdef->lineto ($p[4], $p[5]); | ||
| 245 | |||
| 246 | return $pathdef if ($self->get('arrows') eq 'one-way'); | ||
| 247 | |||
| 248 | |||
| 249 | my $o = 3; # offset. | ||
| 250 | |||
| 251 | $h = $h + $o; | ||
| 252 | |||
| 253 | $pathdef->moveto ($x1, $y1); | ||
| 254 | |||
| 255 | if ($item_direction eq 'left') | ||
| 256 | { | ||
| 257 | @p = ($x1-$h,$y1+$v, $x1-$h,$y1-$v, $x1,$y1); | ||
| 258 | } | ||
| 259 | else | ||
| 260 | { | ||
| 261 | @p = ($x1+$h,$y1+$v, $x1+$h,$y1-$v, $x1,$y1); | ||
| 262 | } | ||
| 263 | |||
| 264 | $pathdef->lineto ($p[0], $p[1]); | ||
| 265 | |||
| 266 | $pathdef->lineto ($p[2], $p[3]); | ||
| 267 | |||
| 268 | $pathdef->lineto ($p[4], $p[5]); | ||
| 269 | |||
| 270 | return $pathdef; | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | sub _set_connection_path | ||
| 275 | { | ||
| 276 | my $self = shift(@_); | ||
| 277 | |||
| 278 | $self->set_path_def(_bpath($self)); | ||
| 279 | |||
| 280 | if ($self->get('item')->is_visible() && $self->get('predecessor_item')->is_visible()) | ||
| 281 | { | ||
| 282 | $self->show(); | ||
| 283 | |||
| 284 | $self->lower_to_bottom(); | ||
| 285 | } | ||
| 286 | else | ||
| 287 | { | ||
| 288 | $self->hide(); | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | |||
| 294 | 1; # Magic true value required at end of module | ||
| 295 | __END__ |