1/*
2* Copyright (c) 2018 (https://github.com/phase1geo/Minder)
3*
4* This program is free software; you can redistribute it and/or
5* modify it under the terms of the GNU General Public
6* License as published by the Free Software Foundation; either
7* version 2 of the License, or (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12* General Public License for more details.
13*
14* You should have received a copy of the GNU General Public
15* License along with this program; if not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17* Boston, MA 02110-1301 USA
18*
19* Authored by: Trevor Williams <phase1geo@gmail.com>
20*/
21
22using Gtk;
23
24public class UndoNodesCut : UndoItem {
25
26  private class NodeInfo {
27    public Node  node;
28    public Node? parent;
29    public int   index;
30    public NodeInfo( Node n ) {
31      node   = n;
32      parent = n.parent;
33      index  = n.index();
34    }
35  }
36
37  Array<NodeInfo>        _nodes;
38  Array<Connection>      _conns;
39  Array<UndoNodeGroups?> _groups;
40
41  /* Default constructor */
42  public UndoNodesCut( Array<Node> nodes, Array<Connection> conns, Array<UndoNodeGroups?> groups ) {
43    base( _( "cut nodes" ) );
44    _nodes = new Array<NodeInfo>();
45    for( int i=0; i<nodes.length; i++ ) {
46      _nodes.append_val( new NodeInfo( nodes.index( i ) ) );
47    }
48    _conns  = conns;
49    _groups = groups;
50  }
51
52  /* Undoes a node deletion */
53  public override void undo( DrawArea da ) {
54    var clipboard = Clipboard.get_default( da.get_display() );
55    clipboard.clear();
56    da.get_selections().clear();
57    for( int i=0; i<_nodes.length; i++ ) {
58      var ni = _nodes.index( i );
59      ni.node.attach_only( ni.parent, ni.index );
60      da.get_selections().add_node( ni.node );
61    }
62    for( int i=0; i<_conns.length; i++ ) {
63      da.get_connections().add_connection( _conns.index( i ) );
64    }
65    da.groups.apply_undos( _groups );
66    da.queue_draw();
67    da.auto_save();
68  }
69
70  /* Redoes a node deletion */
71  public override void redo( DrawArea da ) {
72    MinderClipboard.copy_nodes( da );
73    da.get_selections().clear();
74    for( int i=0; i<_nodes.length; i++ ) {
75      UndoNodeGroups? tmp_group = null;
76      _nodes.index( i ).node.delete_only();
77      da.groups.remove_node( _nodes.index( i ).node, ref tmp_group );
78    }
79    for( int i=0; i<_conns.length; i++ ) {
80      da.get_connections().remove_connection( _conns.index( i ), false );
81    }
82    da.queue_draw();
83    da.auto_save();
84  }
85
86}
87