Class: CanvasMap

Inherits:
Gtk::VBox
  • Object
show all
Defined in:
lib/gui/canvas_map.rb

Instance Method Summary collapse

Constructor Details

#initializeCanvasMap

Returns a new instance of CanvasMap.



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/gui/canvas_map.rb', line 260

def initialize()
super()
@bg_image_enable=false
@dragging = false
@cursor_x=nil
@cursor_y=nil
@drawing_selection=false
@selection=nil
@fullscreen=false
show()

# Create the canvas
@canvas = Goo::Canvas.new

#here we set the background color
set_rgb($config.rgb[0].to_f, $config.rgb[1].to_f, $config.rgb[2].to_f)

# Setup canvas items
root = @canvas.root_item
   
# Layout the stuff
table = Gtk::Table.new(2, 2, false)
pack_start(table,true)
   
table.show()
table.attach(@canvas,
0, 1, 0, 1,
Gtk::EXPAND | Gtk::FILL | Gtk::SHRINK,
Gtk::EXPAND | Gtk::FILL | Gtk::SHRINK,
0, 0)

@canvas.show()        
@canvas.signal_connect_after("key_press_event") do |item, event|    
  if !busy?()
     key_press(item, event)
  end
end

@canvas.signal_connect("scroll_event") do  |w, event| 
  if !busy?()
	 if event.state & Gdk::Window::CONTROL_MASK == Gdk::Window::CONTROL_MASK
       case event.direction
       when Gdk::EventScroll::UP
         node_view_zoom_in()
       when Gdk::EventScroll::DOWN
         node_view_zoom_out()
       end
	 end
  end
  #use update to cute scroll_event forwarding @canvas.root_item
  @canvas.update()
end

@canvas.signal_connect("button_press_event") do |w, event|
  if busy?()
     next
  end
  #puts "in canvas button press #{w}"

  xw, yw = w.convert_to_pixels(event.x,event.y)
  cvitem=item_at(xw, yw)
  if (cvitem == nil) || (cvitem.parent == @bg_img if defined?(@bg_img))      
    if event.state & Gdk::Window::CONTROL_MASK != Gdk::Window::CONTROL_MASK
      if $map == nil
        $network[ROOTMAPADDR].node_view.unselect()
      else
        $canvas.get_selected_node().each {|n|
          n.node_view.unselect()
        }
      end
    end

    if (event.event_type == Gdk::Event::BUTTON_PRESS)
	#puts "event button #{event.button}"
      if event.button == 8
        set_map($network[$map].map) unless $map == nil
      end
      if event.button == 9
        if $map == nil
          set_map(ROOTMAPADDR)
        else
          if $network[get_map()].nb_network == 1
            $network[get_map()].network.each_key {|k| set_map(k)}
          end
        end
      end

      if event.button == 3 #right click
        #show popup menu
        b = event.button
        t = event.time
        popup_menu = Gtk::Menu.new
        add_node=Gtk::ImageMenuItem.new("Add node")
        image_add_node = Gtk::Image.new()
        image_add_node.set_stock(Gtk::Stock::ADD)
        add_node.set_image(image_add_node)
        popup_menu.append(add_node)
        if get_map() != nil
          add_sub_menu=Gtk::Menu.new
          add_sub_menu.append(add_host=Gtk::MenuItem.new("Add host"))
          add_host.signal_connect('button-press-event') {
            addnodewin
            popup_menu.destroy
          }
          add_sub_menu.append(add_host_range_item=Gtk::MenuItem.new("Add host range"))
          add_host_range_item.signal_connect('button-press-event') {
            add_host_range
            popup_menu.destroy
          }
          add_sub_menu.append(add_network=Gtk::MenuItem.new("Add network"))
          add_network.signal_connect('button-press-event') {
            addnetworkwin
            popup_menu.destroy
          }
        end
        add_node.set_submenu(add_sub_menu)

        find_node=Gtk::ImageMenuItem.new("Find node")
        image_find_node = Gtk::Image.new()
        image_find_node.set_stock(Gtk::Stock::FIND)
        find_node.set_image(image_find_node)
        popup_menu.append(find_node)

        if $event_win.visible?
          popup_menu.append(ewin_hide=Gtk::MenuItem.new("Hide event log"))
        else
          popup_menu.append(ewin_show=Gtk::MenuItem.new("Show event log"))
        end
        popup_menu.append(layout_grid=Gtk::MenuItem.new("Grid layout"))

        map_up=Gtk::ImageMenuItem.new("Go up")
        image_map_up = Gtk::Image.new()
        image_map_up.set_stock(Gtk::Stock::GO_UP)
        map_up.set_image(image_map_up)
        popup_menu.append(map_up)

        if $canvas.fullscreen?()
          popup_menu.append(Gtk::SeparatorMenuItem.new)
          popup_menu.append(exit_fullscreen=Gtk::MenuItem.new("Unfullscreen"))
          exit_fullscreen.signal_connect('button-press-event') {
            toggle_fullscreen()
            popup_menu.destroy
          }
        end

        if $map == nil
          layout_grid.set_sensitive(false)
          map_up.set_sensitive(false)
          add_node.set_sensitive(false)
        end
        popup_menu.show_all
        popup_menu.popup(nil,nil,b,t)

        find_node.signal_connect('button-press-event') {
          findnodewin
          popup_menu.destroy
        }
        if (ewin_hide)
          ewin_hide.signal_connect('button-press-event') {
            $event_win.hide()
            popup_menu.destroy
          }
        end
        if (ewin_show)
          ewin_show.signal_connect('button-press-event') {
            $event_win.show()
            popup_menu.destroy
          }
        end
        layout_grid.signal_connect('button-press-event') {
          layout_grid($map)
          popup_menu.destroy
        }
        map_up.signal_connect('button-press-event') {
          if $map != nil
            set_map($network[$map].map)
          end
          popup_menu.destroy
        }
      else	  
        #draw selection with other button
        @drawing_selection=true
        @cursor_x=event.x
        @cursor_y=event.y
      end
    end
  end
end

@canvas.signal_connect_after("button_release_event") do |w, event|
  if busy?()
    next
  end
  #puts "here canvas signal connect button release"
  if @drawing_selection
    #we select node in rectangle selection
    cursor_x, cursor_y = w.convert_to_pixels(@cursor_x,@cursor_y)
    x2, y2 = w.convert_to_pixels(event.x,event.y)

    if cursor_x>x2
      xpos=x2.to_i
      x2=cursor_x
    else
      xpos=cursor_x.to_i
    end
    while xpos < x2.to_i
      if cursor_y>y2
        ypos=y2.to_i
        y2=cursor_y
      else
        ypos=cursor_y.to_i
      end
      while ypos < y2.to_i
        if $map == nil
          node=$network[ROOTMAPADDR]
          if xpos<node.x && x2>node.x+$config.pixmap_size && ypos<node.y && y2>node.y+$config.pixmap_size+10
            #pos point is in node zone
            node.node_view.select()
          end
        else
          $network[get_map()].get_node().each_value {|node|
            if xpos<node.x && x2>node.x+$config.pixmap_size && ypos<node.y && y2>node.y+$config.pixmap_size+10
              #pos point is in node zone
              node.node_view.select()
            end
          }
        end
        ypos+=$config.pixmap_size+10
      end
      xpos+=$config.pixmap_size
    end
    @drawing_selection=false
  end
  #hide rectangle selection
  @selection.visibility = Goo::CanvasItem::HIDDEN unless @selection == nil

  #redraw links cause nodes have moved
  node_tmp=nil
  get_selected_node().each {|n|
    node_tmp=n
    if n.nb_links > 0
      n.get_links().each {|lnk|
        lnk.remove_link
        lnk.display()
      }
    end
  }
end

@canvas.signal_connect("motion_notify_event") do |w, event|
  if busy?()
     next
  end
      
  if @drawing_selection
    @selection.visibility = Goo::CanvasItem::HIDDEN unless @selection == nil
		
    cursor_x, cursor_y = w.convert_to_pixels(@cursor_x,@cursor_y)
    x2, y2 = w.convert_to_pixels(event.x,event.y)
    @selection=Goo::CanvasRect.new(root, cursor_x, cursor_y, x2 - cursor_x, y2 - cursor_y,
      :stroke_color => "red",
      :line_width => 1.0)
    end
  end
end

Instance Method Details

#background_image_isset?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/gui/canvas_map.rb', line 62

def background_image_isset?()
  return @bg_image_enable
end

#boundsObject



24
25
26
# File 'lib/gui/canvas_map.rb', line 24

def bounds()
  return @canvas.bounds
end

#fullscreen?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/gui/canvas_map.rb', line 8

def fullscreen?()
  return @fullscreen==true
end

#get_rootObject



235
236
237
# File 'lib/gui/canvas_map.rb', line 235

def get_root()
  @canvas.root_item
end

#get_selected_nodeObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gui/canvas_map.rb', line 76

def get_selected_node()
  nodeselected=[]
  if $map != nil
    tmp_node=$network[get_map()]
    if tmp_node
      tmp_node.get_node().each_value {|n|
        if n.node_view and n.node_view.selected?()
          nodeselected.push(n)
        end
      }
    end
  else
    if $network[ROOTMAPADDR] and $network[ROOTMAPADDR].node_view.selected?()
      nodeselected.push($network[ROOTMAPADDR])
    end
  end
  return nodeselected
end

#grab_focusObject



231
232
233
# File 'lib/gui/canvas_map.rb', line 231

def grab_focus()
  @canvas.grab_focus(get_root())
end

#item_at(x, y) ⇒ Object

return the visible item at position (x,y) or nil



52
53
54
55
56
57
58
59
60
# File 'lib/gui/canvas_map.rb', line 52

def item_at(x, y)
  cvitem = @canvas.get_item_at(x, y, true)
  if cvitem
    if !(cvitem.visibility == Goo::CanvasItem::VISIBLE)
      cvitem = nil
    end
  end
  return cvitem
end

#key_press(canvas, event) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/gui/canvas_map.rb', line 135

def key_press(canvas, event)
  estring=Gdk::Keyval.to_name(event.keyval)
  #print "canvas key code: ",event.keyval," = ",estring,"\n"

  x, y = [canvas.x1,canvas.y1]
  case event.keyval
  when Gdk::Keyval::GDK_KP_Add
    node_view_zoom_in()
  when Gdk::Keyval::GDK_plus
    node_view_zoom_in()
  when Gdk::Keyval::GDK_KP_Subtract
    node_view_zoom_out()
  when Gdk::Keyval::GDK_minus
    node_view_zoom_out()
  when Gdk::Keyval::GDK_Delete
    del_selected_node()
  when Gdk::Keyval::GDK_d
    del_selected_node()
  when Gdk::Keyval::GDK_i
    #up
    get_selected_node().each {|n|
      n.node_view.move_up()
    }
  when Gdk::Keyval::GDK_k
    #down
    get_selected_node().each {|n|
      n.node_view.move_down()
    }
  when Gdk::Keyval::GDK_j
    #left
    get_selected_node().each {|n|
      n.node_view.move_left()
    }
  when Gdk::Keyval::GDK_l
    #right
    get_selected_node().each {|n|
      n.node_view.move_right()
    }
  when Gdk::Keyval::GDK_Up
    #canvas root group scroll_to or translate are pain in the ass to handle
    #so we are just here moving each node one by one ...
    if $map.nil?
      $network[ROOTMAPADDR].node_view.move_up()
    else
	$network[$map].get_node().each_value {|node|
        node.node_view.move_up()
	}
    end
  when Gdk::Keyval::GDK_Down    
    if $map.nil?
      $network[ROOTMAPADDR].node_view.move_down()
    else
	$network[$map].get_node().each_value {|node|
        node.node_view.move_down()
	}
    end
  when Gdk::Keyval::GDK_Left
    if $map.nil?
      $network[ROOTMAPADDR].node_view.move_left()
    else
	$network[$map].get_node().each_value {|node|
        node.node_view.move_left()
	}
    end
  when Gdk::Keyval::GDK_Right
    if $map.nil?
      $network[ROOTMAPADDR].node_view.move_right()
    else
	$network[$map].get_node().each_value {|node|
        node.node_view.move_right()
	}
    end
  when Gdk::Keyval::GDK_Page_Up
    set_map($network[$map].map) unless $map == nil
  when Gdk::Keyval::GDK_Page_Down
    if $map == nil
      set_map(ROOTMAPADDR)
    else
      if $network[get_map()].nb_network == 1
        $network[get_map()].network.each_key {|k| set_map(k)}
      else
        #$canvas.get_selected_node().each {|n|
        #if n.class == Network
        #$network[get_map()].network.each_key {|k| set_map(n)}
        #break
        #end
        #}
      end
    end
  when Gdk::Keyval::GDK_f
    toggle_fullscreen()
  when Gdk::Keyval::GDK_F7
    DebugWindow.new
  end
end

#nb_selectedObject



127
128
129
130
131
132
133
# File 'lib/gui/canvas_map.rb', line 127

def nb_selected()
  tmp=get_selected_node()
  if tmp
    return get_selected_node().size()
  end
  return 0
end

#node_view_zoom_inObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gui/canvas_map.rb', line 95

def node_view_zoom_in()
  if $map == nil
    $config.pixmap_size+=5
    $network[ROOTMAPADDR].node_view.resize()
  else
    if $network[get_map()].node_size == nil
      $network[get_map()].node_size=$config.pixmap_size+5
    else
      $network[get_map()].node_size+=5
    end
    $network[get_map()].get_node().each_value {|n|
      n.node_view.resize() if n.node_view
    }
  end
end

#node_view_zoom_outObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gui/canvas_map.rb', line 111

def node_view_zoom_out()
  if $map == nil
    $config.pixmap_size-=5 unless $config.pixmap_size < 10
    $network[ROOTMAPADDR].node_view.resize()
  else
    if $network[get_map()].node_size == nil
      $network[get_map()].node_size=$config.pixmap_size-5 unless $config.pixmap_size < 10
    else
      $network[get_map()].node_size-=5 unless $network[get_map()].node_size < 10
    end
    $network[get_map()].get_node().each_value {|n|
      n.node_view.resize() if n.node_view
    }     
  end
end

#redraw_allObject



239
240
241
# File 'lib/gui/canvas_map.rb', line 239

def redraw_all()
  @canvas.update()
end

#set_background_image(image_file) ⇒ Object

set the background image



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gui/canvas_map.rb', line 31

def set_background_image(image_file)
  if !defined?(@bg_img) || (@bg_img == nil)
    @bg_img=Goo::CanvasGroup.new(@canvas.root_item,{:x => 0.0,:y => 0.0})
  end
  if (image_file != nil) && FileTest.exist?(image_file)
    im=Gdk::Pixbuf.new(image_file)
    @bg_img.lower(nil)
    if defined?(@img) && (@img != nil)
      @img.visibility = Goo::CanvasItem::HIDDEN
	@img.parent.remove_child(@img)
    end
    @img=Goo::CanvasImage.new(@bg_img, im, 0, 0,:width => @canvas.x2 - @canvas.x1, :height=> @canvas.y2 - @canvas.y1, :scale_to_fit => true)
    @img.lower(nil)
    @bg_img.visibility = Goo::CanvasItem::VISIBLE
    @bg_image_enable=true
  end
end

#set_rgb(r, g, b) ⇒ Object

set canvas background color



246
247
248
249
250
# File 'lib/gui/canvas_map.rb', line 246

def set_rgb(r, g, b)
  #convert the r g b color in a gdk color object,
  #then transform it to a rgb hex value then an int
  @canvas.background_color_rgb = Integer("0x#{rgb_to_hex_color(Gdk::Color.new(r, g, b))}")    
end

#set_size(w, h) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/gui/canvas_map.rb', line 252

def set_size(w, h)
  if @bg_image_enable && (@img != nil)
    @img.width = @canvas.x2 - @canvas.x1
    @img.height = @canvas.y2 - @canvas.y1      
  end
  @canvas.set_bounds(0, 0, get_map_width(), get_map_height()) if $win
end

#toggle_fullscreenObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gui/canvas_map.rb', line 12

def toggle_fullscreen()
  if @fullscreen == false
    @fullscreen=true
    $win.hide_menu()
    $win.fullscreen()
  else
    @fullscreen=false
    $win.show_menu()
    $win.unfullscreen()
  end
end

#unset_background_imageObject

unset the background image



69
70
71
72
73
74
# File 'lib/gui/canvas_map.rb', line 69

def unset_background_image()
  if defined?(@bg_img)
    @bg_img.visibility = Goo::CanvasItem::HIDDEN unless @bg_img==nil
    @bg_image_enable = false
  end
end