Class: Wx::SF::Diagram
- Inherits:
-
Object
- Object
- Wx::SF::Diagram
- Includes:
- FIRM::Serializable
- Defined in:
- lib/wx/shapes/diagram.rb
Constant Summary collapse
- SEARCHMODE =
Search mode flags for get_shape_at_position method
ShapeCanvas::SEARCHMODE
Instance Method Summary collapse
-
#accept_shape(type) ⇒ Object
Add given shape type to an acceptance list.
-
#accept_top_shape(type) ⇒ Object
Note: Constant value ACCEPT_ALL behaves like any class.
-
#add_shape(shape, parent, pos, initialize, save_state = true) ⇒ Wx::SF::ERRCODE
Add an existing shape to the canvas.
-
#clear ⇒ Object
Remove all shapes from canvas.
-
#clear_accepted_shapes ⇒ Object
Clear shape object acceptance list.
-
#clear_accepted_top_shapes ⇒ Object
Clear top shapes acceptance list.
-
#contains_shape(shape) ⇒ Boolean
(also: #contains_shape?, #contains?)
Returns true if the given shape is part of the diagram, false otherwise.
-
#create_connection(src, trg, *rest) ⇒ Object
Create new direct connection between two shapes.
-
#create_shape(shape_info, *rest) ⇒ Object
Create new shape and add it to the shape canvas.
- #get_accepted_shapes ⇒ Set<String> (also: #accepted_shapes)
- #get_accepted_top_shapes ⇒ Set<String> (also: #accepted_top_shapes)
-
#get_all_shapes ⇒ Object
Get all shapes recursively.
- #get_assigned_connections(parent, shape_info, mode, lines = []) ⇒ Object
-
#get_neighbours(parent, shape_info, condir, direct = true, neighbours = []) ⇒ Array<Wx::SF::Shape>
otherwise also shapes connected by forked lines will be found (also constants DIRECT and INDIRECT can be used).
- #get_shape_at_position(pos, zorder = 1, mode = SEARCHMODE::BOTH) ⇒ Object
-
#get_shape_canvas ⇒ Wx::SF::ShapeCanvas?
(also: #shape_canvas)
Returns the shape canvas.
- #get_shapes(shape_info = Wx::SF::Shape, mode = Shape::SEARCHMODE::BFS, shapes = []) ⇒ Object
-
#get_shapes_at_position(pos, shapes = []) ⇒ Array<Wx::SF::Shape>
Shape list.
-
#get_shapes_inside(rct, shapes = []) ⇒ Array<Wx::SF::Shape>
Shape list.
-
#get_top_shapes ⇒ Object
Returns the list of top level shapes.
-
#has_children(parent) ⇒ Boolean
(also: #has_children?)
Function finds out whether given shape has some children.
-
#initialize ⇒ Diagram
constructor
A new instance of Diagram.
-
#insert_shape(shape, *rest) ⇒ Object
Insert new shape to the shape canvas.
- #inspect ⇒ Object
-
#is_empty ⇒ Object
(also: #empty?)
Determines whether the diagram contains some shapes.
-
#is_modified ⇒ Boolean
(also: #modified?)
Get information about managed diagram’s modification.
-
#is_shape_accepted(type) ⇒ Boolean
(also: #shape_accepted?)
Tells whether the given shape type is accepted by this canvas instance (it means whether this shape can be inserted into it).
-
#is_top_shape_accepted(type) ⇒ Boolean
(also: #top_shape_accepted?)
The function is typically used by the framework for determination whether class type supplied by add_shape or create_shape function can be inserted directly onto shape canvas.
-
#move_shapes_from_negatives ⇒ Object
Move all shapes so none of it will be located in negative position.
-
#move_to_end(shape) ⇒ Object
Move given shape to the end of the shapes list.
-
#remove_shape(shape, refresh = true) ⇒ Object
Remove given shape from the shape canvas.
-
#remove_shapes(selection) ⇒ Object
Remove shapes from the shape canvas.
-
#reparent_shape(shape, parent) ⇒ Wx::SF::Shape
Change shape’s parent (possibly making it unparented i.e. toplevel).
-
#set_modified(state = true) ⇒ Object
(also: #modified=)
Set diagram’s modification flag manually.
-
#set_shape_canvas(canvas) ⇒ Object
(also: #shape_canvas=)
Set the shape canvas.
-
#update_all ⇒ Object
Update all shapes in the diagram manager.
Constructor Details
#initialize ⇒ Diagram
Returns a new instance of Diagram.
22 23 24 25 26 27 28 |
# File 'lib/wx/shapes/diagram.rb', line 22 def initialize @shapes = ShapeList.new @shape_canvas = nil @is_modified = false @accepted_shapes = ::Set.new([ACCEPT_ALL]) @accepted_top_shapes = ::Set.new([ACCEPT_ALL]) end |
Instance Method Details
#accept_shape(type) ⇒ Object
Add given shape type to an acceptance list. The acceptance list contains class names of the shapes which can be inserted into this instance of shapes canvas. Note: Constant value ACCEPT_ALL behaves like any class.
376 377 378 379 |
# File 'lib/wx/shapes/diagram.rb', line 376 def accept_shape(type) ::Kernel.raise ArgumentError, 'Class or ACCEPT_ALL expected' unless type.is_a?(::Class) @accepted_shapes << type end |
#accept_top_shape(type) ⇒ Object
Note: Constant value ACCEPT_ALL behaves like any class.
412 413 414 415 |
# File 'lib/wx/shapes/diagram.rb', line 412 def accept_top_shape(type) ::Kernel.raise ArgumentError, 'Class or ACCEPT_ALL expected' unless type.is_a?(::Class) @accepted_top_shapes << type end |
#add_shape(shape, parent, pos, initialize, save_state = true) ⇒ Wx::SF::ERRCODE
Add an existing shape to the canvas.
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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/wx/shapes/diagram.rb', line 184 def add_shape(shape, parent, pos, initialize, save_state = true) if shape if shape.is_a?(Shape) && is_shape_accepted(shape.class) pos = pos.to_point if @shape_canvas new_pos = @shape_canvas.fit_position_to_grid(@shape_canvas.dp2lp(pos)) shape.set_relative_position(new_pos.to_real) else shape.set_relative_position(pos.to_real) end # add shape if parent shape.set_parent_shape(parent) else if is_top_shape_accepted(shape.class) @shapes << shape shape.set_diagram(self) else return ERRCODE::NOT_ACCEPTED end end # initialize added shape if initialize shape.create_handles shape.set_hover_colour(@shape_canvas.get_hover_colour) if @shape_canvas if has_children(shape) # get shape's children (if exist) lst_children = shape.get_child_shapes(ANY, RECURSIVE) # initialize shape's children lst_children.each do |child| child.create_handles child.update(false) child.set_hover_colour(@shape_canvas.get_hover_colour) if @shape_canvas end end shape.update end # reset scale of assigned shape canvas (if exists and it is necessary...) if @shape_canvas && shape.is_a?(ControlShape) @shape_canvas.set_scale(1.0) end @shape_canvas.save_canvas_state if @shape_canvas && save_state @is_modified = true ERRCODE::OK else ERRCODE::NOT_ACCEPTED end else ERRCODE::INVALID_INPUT end end |
#clear ⇒ Object
Remove all shapes from canvas
321 322 323 324 325 326 327 |
# File 'lib/wx/shapes/diagram.rb', line 321 def clear @shapes.clear if @shape_canvas @shape_canvas.get_multiselection_box.show(false) @shape_canvas.update_virtual_size end end |
#clear_accepted_shapes ⇒ Object
Clear shape object acceptance list
395 396 397 |
# File 'lib/wx/shapes/diagram.rb', line 395 def clear_accepted_shapes @accepted_shapes.clear end |
#clear_accepted_top_shapes ⇒ Object
Clear top shapes acceptance list
431 432 433 |
# File 'lib/wx/shapes/diagram.rb', line 431 def clear_accepted_top_shapes @accepted_top_shapes.clear end |
#contains_shape(shape) ⇒ Boolean Also known as: contains_shape?, contains?
Returns true if the given shape is part of the diagram, false otherwise
314 315 316 |
# File 'lib/wx/shapes/diagram.rb', line 314 def contains_shape(shape) @shapes.include?(shape,true) end |
#create_connection(src_id, trg_id, save_state = true) ⇒ Array(Wx::SF::ERRCODE, Wx::SF::Shape) #create_connection(src_id, trg_id, line_info, save_state = true) ⇒ Array(Wx::SF::ERRCODE, Wx::SF::Shape) #create_connection(src_id, trg_id, line, save_state = true) ⇒ Array(Wx::SF::ERRCODE, Wx::SF::Shape)
Create new direct connection between two shapes.
This function creates new simple connection line (without arrows) between given shapes.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/wx/shapes/diagram.rb', line 83 def create_connection(src, trg, *rest) shape = nil if rest.first.is_a?(LineShape) line = rest.shift save_state = rest.empty? ? true : rest.shift err = add_shape(line, nil, Wx::DEFAULT_POSITION, INITIALIZE, DONT_SAVE_STATE) shape = line if err == ERRCODE::OK else line_type = (rest.empty? || !rest.first.is_a?(::Class)) ? LineShape : rest.shift save_state = rest.empty? ? true : rest.shift err, shape = create_shape(line_type, DONT_SAVE_STATE) end if shape shape.set_src_shape(src) shape.set_trg_shape(trg) if @shape_canvas @shape_canvas.save_canvas_state if save_state shape.refresh end end [err, shape] end |
#create_shape(shape_info, save_state = true) ⇒ Array(Wx::SF::ERRCODE, Wx::SF::Shape) #create_shape(shape_info, pos, save_state = true) ⇒ Array(Wx::SF::ERRCODE, Wx::SF::Shape)
Create new shape and add it to the shape canvas.
117 118 119 120 121 122 123 |
# File 'lib/wx/shapes/diagram.rb', line 117 def create_shape(shape_info, *rest) if shape_info && is_shape_accepted(shape_info) insert_shape(shape_info.new, *rest) else [ERRCODE::NOT_ACCEPTED, nil] end end |
#get_accepted_shapes ⇒ Set<String> Also known as: accepted_shapes
401 402 403 |
# File 'lib/wx/shapes/diagram.rb', line 401 def get_accepted_shapes @accepted_shapes end |
#get_accepted_top_shapes ⇒ Set<String> Also known as: accepted_top_shapes
437 438 439 |
# File 'lib/wx/shapes/diagram.rb', line 437 def get_accepted_top_shapes @accepted_top_shapes end |
#get_all_shapes ⇒ Object
Get all shapes recursively. This is an optimized convenience method which returns all shapes the same as if #get_shapes() was called with default arguments.
476 477 478 |
# File 'lib/wx/shapes/diagram.rb', line 476 def get_all_shapes @shapes.all end |
#get_assigned_connections(parent, shape_info, mode, lines = []) ⇒ Object
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/wx/shapes/diagram.rb', line 449 def get_assigned_connections(parent, shape_info, mode, lines = []) return lines unless parent # lines are all toplevel so we do not have to search recursively... lst_lines = @shapes.select { |shape| shape.is_a?(shape_info) && !shape.stand_alone? } lst_lines.each do |line| case mode when Shape::CONNECTMODE::STARTING lines << line if line.get_src_shape == parent when Shape::CONNECTMODE::ENDING lines << line if line.get_trg_shape == parent when Shape::CONNECTMODE::BOTH lines << line if line.get_src_shape == parent || line.get_trg_shape == parent end end lines end |
#get_neighbours(parent, shape_info, condir, direct = true, neighbours = []) ⇒ Array<Wx::SF::Shape>
otherwise also shapes connected by forked lines will be found (also constants DIRECT and INDIRECT can be used)
591 592 593 594 595 596 597 598 599 600 |
# File 'lib/wx/shapes/diagram.rb', line 591 def get_neighbours(parent, shape_info, condir, direct = true, neighbours = []) if parent parent.get_neighbours(shape_info, condir, direct, neighbours) else @shapes.each do |shape| shape.get_neighbours(shape_info, condir, direct, neighbours) end end neighbours end |
#get_shape_at_position(pos, zorder = 1, mode = SEARCHMODE::BOTH) ⇒ Object
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/wx/shapes/diagram.rb', line 505 def get_shape_at_position(pos, zorder = 1, mode = SEARCHMODE::BOTH) # sort shapes list in the way that the line shapes will be at the top of the list # and all non-line shapes get listed in reversed order as returned from get_shapes (for z order) ins_pos = 0 pos = pos.to_point shapes = get_all_shapes.inject([]) do |list, shape| if shape.is_a?(LineShape) list.prepend(shape) ins_pos += 1 else list.insert(ins_pos, shape) end list end # find the topmost shape according to the given rules counter = 1 shapes.each do |shape| if shape.visible? && shape.active? && shape.contains?(pos) case mode when SEARCHMODE::SELECTED if shape.selected? return shape if counter == zorder counter += 1 end when SEARCHMODE::UNSELECTED unless shape.selected? return shape if counter == zorder counter += 1 end when SEARCHMODE::BOTH return shape if counter == zorder counter += 1 end end end nil end |
#get_shape_canvas ⇒ Wx::SF::ShapeCanvas? Also known as: shape_canvas
Returns the shape canvas.
32 33 34 |
# File 'lib/wx/shapes/diagram.rb', line 32 def get_shape_canvas @shape_canvas end |
#get_shapes(shape_info = Wx::SF::Shape, mode = Shape::SEARCHMODE::BFS, shapes = []) ⇒ Object
486 487 488 489 490 491 492 493 494 495 |
# File 'lib/wx/shapes/diagram.rb', line 486 def get_shapes(shape_info = Wx::SF::Shape, mode = Shape::SEARCHMODE::BFS, shapes = []) @shapes.each do |shape| shapes << shape if shape.is_a?(shape_info) shape.get_children_recursively(shape_info, mode, shapes) if mode == Shape::SEARCHMODE::DFS end if mode == Shape::SEARCHMODE::BFS @shapes.each { |shape| shape.get_children_recursively(shape_info, mode, shapes) } end shapes end |
#get_shapes_at_position(pos, shapes = []) ⇒ Array<Wx::SF::Shape>
Returns shape list.
549 550 551 552 553 554 |
# File 'lib/wx/shapes/diagram.rb', line 549 def get_shapes_at_position(pos, shapes = []) pos = pos.to_point get_all_shapes.each do |shape| shapes << shape if shape.visible? && shape.active? && shape.contains?(pos) end end |
#get_shapes_inside(rct, shapes = []) ⇒ Array<Wx::SF::Shape>
Returns shape list.
560 561 562 563 564 |
# File 'lib/wx/shapes/diagram.rb', line 560 def get_shapes_inside(rct, shapes = []) get_all_shapes.each do |shape| shapes << shape if shape.visible? && shape.active? && shape.intersects?(rct) end end |
#get_top_shapes ⇒ Object
Returns the list of top level shapes
469 470 471 |
# File 'lib/wx/shapes/diagram.rb', line 469 def get_top_shapes @shapes end |
#has_children(parent) ⇒ Boolean Also known as: has_children?
Function finds out whether given shape has some children.
576 577 578 |
# File 'lib/wx/shapes/diagram.rb', line 576 def has_children(parent) parent.has_children? end |
#insert_shape(shape, save_state = true) ⇒ Array(Wx::SF::ERRCODE, Wx::SF::Shape) #insert_shape(shape, pos, save_state = true) ⇒ Array(Wx::SF::ERRCODE, Wx::SF::Shape)
Insert new shape to the shape canvas.
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 |
# File 'lib/wx/shapes/diagram.rb', line 135 def insert_shape(shape, *rest) pos = if rest.first.respond_to?(:to_point) rest.shift.to_point elsif @shape_canvas clt_rect = @shape_canvas.get_client_rect Wx::Point.new((clt_rect.right - clt_rect.left)/2, (clt_rect.bottom - clt_rect.top)/2) else Wx::Point.new end save_state = rest.empty? ? true : rest.shift if shape && is_shape_accepted(shape.class) parent_shape = nil # update given position lpos = @shape_canvas ? @shape_canvas.dp2lp(pos) : pos # line shapes can be assigned to root only parent_shape = get_shape_at_position(lpos) unless shape.is_a?(LineShape) # In case the matching shape does not accept ANY children see if this shape has a # parent that does also match the position and DOES accept children. # This allows dropping shapes onto child shapes inside a (container) shapes like # grids and/or boxes. while parent_shape&.does_not_accept_children? && parent_shape.parent_shape parent_shape = parent_shape.parent_shape parent_shape = nil unless parent_shape.get_bounding_box.contains?(lpos) end # see if the located parent (if any) accepts this particular type of child if parent_shape && parent_shape.is_child_accepted(shape.class) # add shape as child of located parent err = add_shape(shape, parent_shape, pos - parent_shape.get_absolute_position.to_point, INITIALIZE, save_state) else # add shape as new toplevel shape (if acceptable) err = add_shape(shape, nil, pos, INITIALIZE, save_state) end parent_shape.update if parent_shape [err, shape] else [ERRCODE::NOT_ACCEPTED, nil] end end |
#inspect ⇒ Object
673 674 675 |
# File 'lib/wx/shapes/diagram.rb', line 673 public def inspect "#<Wx::SF::Diagram:#{object_id}>" end |
#is_empty ⇒ Object Also known as: empty?
Determines whether the diagram contains some shapes.
568 569 570 |
# File 'lib/wx/shapes/diagram.rb', line 568 def is_empty @shapes.empty? end |
#is_modified ⇒ Boolean Also known as: modified?
Get information about managed diagram’s modification.
The function returns TRUE if the diagram has been modified and its content should be saved. The modification flag is cleared when the content is saved.
49 50 51 |
# File 'lib/wx/shapes/diagram.rb', line 49 def is_modified @is_modified end |
#is_shape_accepted(type) ⇒ Boolean Also known as: shape_accepted?
Tells whether the given shape type is accepted by this canvas instance (it means whether this shape can be inserted into it).
The function is typically used by the framework for determination whether class type supplied by add_shape or create_shape function can be inserted into shape canvas.
388 389 390 |
# File 'lib/wx/shapes/diagram.rb', line 388 def is_shape_accepted(type) @accepted_shapes.include?(type) || @accepted_shapes.include?(ACCEPT_ALL) end |
#is_top_shape_accepted(type) ⇒ Boolean Also known as: top_shape_accepted?
The function is typically used by the framework for determination whether class type supplied by add_shape or create_shape function can be inserted directly onto shape canvas.
424 425 426 |
# File 'lib/wx/shapes/diagram.rb', line 424 def is_top_shape_accepted(type) @accepted_top_shapes.include?(type) || @accepted_top_shapes.include?(ACCEPT_ALL) end |
#move_shapes_from_negatives ⇒ Object
Move all shapes so none of it will be located in negative position
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 |
# File 'lib/wx/shapes/diagram.rb', line 338 def move_shapes_from_negatives min_x = min_y = 0.0 # find the maximal negative position value shapes = get_all_shapes shapes.each_with_index do |shape, ix| shape_pos = shape.get_absolute_position if ix == 0 min_x = shape_pos.x min_y = shape_pos.y else min_x = shape_pos.x if shape_pos.x < min_x min_y = shape_pos.y if shape_pos.y < min_y end end # move all parents shape so they (and their children) will be located in the positive values only if min_x < 0.0 || min_y < 0.0 shapes.each do |shape| unless shape.get_parent_shape shape.move_by(min_x.to_i.abs, 0) if min_x < 0.0 shape.move_by(0, min_y.to_i.abs) if min_y < 0.0 end end end end |
#move_to_end(shape) ⇒ Object
Move given shape to the end of the shapes list
331 332 333 334 335 |
# File 'lib/wx/shapes/diagram.rb', line 331 def move_to_end(shape) if (a_shape = @shapes.delete(shape)) @shapes << a_shape end end |
#remove_shape(shape, refresh = true) ⇒ Object
Remove given shape from the shape canvas.
249 250 251 252 253 254 255 256 257 258 259 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 |
# File 'lib/wx/shapes/diagram.rb', line 249 def remove_shape(shape, refresh = true) return unless shape parent = shape.get_parent_shape # get all shape's children lst_children = shape.get_child_shapes(ANY, RECURSIVE) lst_children << shape # and shape itself # retrieve all assigned lines lst_connections = [] lst_children.each do |child| get_assigned_connections(child, LineShape, Shape::CONNECTMODE::BOTH, lst_connections) end # remove all assigned lines lst_removed_connections = [] lst_connections.each do |line| # one connection may be used by the parent and also by his child unless lst_removed_connections.include?(line) lst_removed_connections << line remove_shape(line,false) end end # remove the shape and it's children from canvas cache and shape index list @shape_canvas.send(:remove_from_temporaries, shape) if @shape_canvas # remove the shape shape.set_parent_shape(nil) # also removes shape from parent if it had a parent shape.set_diagram(nil) @shapes.delete(shape) @is_modified = true parent.update if parent @shape_canvas.refresh(false) if refresh && @shape_canvas end |
#remove_shapes(selection) ⇒ Object
Remove shapes from the shape canvas
291 292 293 |
# File 'lib/wx/shapes/diagram.rb', line 291 def remove_shapes(selection) selection.each { |shape| remove_shape(shape, false) if contains?(shape) } end |
#reparent_shape(shape, parent) ⇒ Wx::SF::Shape
Change shape’s parent (possibly making it unparented i.e. toplevel)
299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/wx/shapes/diagram.rb', line 299 def reparent_shape(shape, parent) prev_parent = shape.get_parent_shape if prev_parent.nil? && parent @shapes.delete(shape) # remove from top level list if the shape will become parented elsif prev_parent && parent.nil? @shapes << shape # add to toplevel shapes if the shape will become unparented shape.set_diagram(self) # make sure the right diagram is set end shape.set_parent_shape(parent) shape end |
#set_modified(state = true) ⇒ Object Also known as: modified=
Set diagram’s modification flag manually.
56 57 58 |
# File 'lib/wx/shapes/diagram.rb', line 56 def set_modified(state = true) @is_modified = state end |
#set_shape_canvas(canvas) ⇒ Object Also known as: shape_canvas=
Set the shape canvas.
39 40 41 |
# File 'lib/wx/shapes/diagram.rb', line 39 def set_shape_canvas(canvas) @shape_canvas = canvas end |
#update_all ⇒ Object
Update all shapes in the diagram manager
367 368 369 |
# File 'lib/wx/shapes/diagram.rb', line 367 def update_all get_all_shapes.each { |shape| shape.update unless shape.has_children? } end |