Class: Wx::SF::CanvasHistory

Inherits:
Object
  • Object
show all
Defined in:
lib/wx/shapes/canvas_history.rb

Defined Under Namespace

Modules: DEFAULT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCanvasHistory

Constructor.



13
14
15
16
17
18
# File 'lib/wx/shapes/canvas_history.rb', line 13

def initialize
  @canvas_states = []
  @current_state = nil
  @current_state_index = nil
  @max_states = DEFAULT::MAX_CANVAS_STATES
end

Instance Attribute Details

#current_stateObject (readonly)

Returns the value of attribute current_state.



21
22
23
# File 'lib/wx/shapes/canvas_history.rb', line 21

def current_state
  @current_state
end

#max_statesObject

Returns the value of attribute max_states.



20
21
22
# File 'lib/wx/shapes/canvas_history.rb', line 20

def max_states
  @max_states
end

Instance Method Details

#at_latest?Boolean

Returns true if the current state is the newest state saved.

Returns:

  • (Boolean)


31
32
33
# File 'lib/wx/shapes/canvas_history.rb', line 31

def at_latest?
  @current_state && @current_state_index == (@canvas_states.size-1)
end

#can_redoObject

The function gives information whether the ‘redo’ operation is available (exists any stored canvas state newer than the current one.

Returns:

  • true if the ‘redo’ operation can be performed, otherwise false



98
99
100
# File 'lib/wx/shapes/canvas_history.rb', line 98

def can_redo
  @current_state && @current_state_index < (@canvas_states.size - 1)
end

#can_undoBoolean

The function gives information whether the ‘Undo’ operation is available (exists any stored canvas state older than the current one.

Returns:

  • (Boolean)

    true if the ‘undo’ operation can be performed, otherwise false



91
92
93
# File 'lib/wx/shapes/canvas_history.rb', line 91

def can_undo
  @current_state && @current_state_index>0
end

#clearObject

Clear all canvas history.



82
83
84
85
86
# File 'lib/wx/shapes/canvas_history.rb', line 82

def clear
  @canvas_states.clear
  @current_state = nil
  @current_state_index = nil
end

#clear_current_stateObject



74
75
76
77
78
79
# File 'lib/wx/shapes/canvas_history.rb', line 74

def clear_current_state
  restore_older_state
  if @current_state
    @canvas_states.slice!(@current_state_index+1, @canvas_states.size)
  end
end

#empty?Boolean

Returns true if no state has been saved yet.

Returns:

  • (Boolean)


25
26
27
# File 'lib/wx/shapes/canvas_history.rb', line 25

def empty?
  @current_state.nil?
end

#restore_newer_stateString

Perform the ‘Redo’ operation.

Returns:

  • (String)

    state to redo



66
67
68
69
70
71
72
# File 'lib/wx/shapes/canvas_history.rb', line 66

def restore_newer_state
  return nil unless @current_state && @current_state_index<(@canvas_states.size-1)

  # move to next canvas state and restore
  @current_state_index += 1
  @current_state = @canvas_states[@current_state_index]
end

#restore_older_stateString

Perform the ‘Undo’ operation.

Returns:

  • (String)

    state to undo



56
57
58
59
60
61
62
# File 'lib/wx/shapes/canvas_history.rb', line 56

def restore_older_state
  return nil unless @current_state && @current_state_index>0

  # move to previous canvas state and restore
  @current_state_index -= 1
  @current_state = @canvas_states[@current_state_index]
end

#save_canvas_state(state) ⇒ Object

Save current canvas state.

Parameters:

  • state (String)

    serialized diagram state



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wx/shapes/canvas_history.rb', line 37

def save_canvas_state(state)
  # delete all states newer than the current state
  if @current_state
    @canvas_states.slice!(@current_state_index+1, @canvas_states.size)
  end

  # append new canvas state
  @current_state_index = @canvas_states.size
  @canvas_states << (@current_state = state)

  # check the history bounds
  if @canvas_states.size > @max_states
    @canvas_states.shift
    @current_state_index -= 1
  end
end