summaryrefslogtreecommitdiff
path: root/pagelayout_editor/pl_editor_undo_redo.cpp
blob: d1711e7c5c95dd089c47d90596aab0e9c4de8347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
/**
 * @file pl_editor_undo_redo.cpp
 * @brief page layout editor: undo and redo functions
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <fctsys.h>
#include <class_drawpanel.h>
#include <macros.h>
#include <worksheet_shape_builder.h>

#include <pl_editor_frame.h>

/* Note: the Undo/redo commands use a "brute" method:
 * the full page layout is converted to a S expression, and saved as string.
 * When a previous version is needed, the old string is parsed,
 * and the description replaces the current desc, just like reading a new file
 *
 * This is not optimal from the memory point of view, but:
 * - the descriptions are never very long (max few thousand of bytes)
 * - this is very easy to code
 */

// A helper class used in undo/redo commad:
class PL_ITEM_LAYOUT: public EDA_ITEM
{
public:
    wxString m_Layout;

public:
    PL_ITEM_LAYOUT() : EDA_ITEM( TYPE_PL_EDITOR_LAYOUT ) {}

    // Required to keep compiler happy on debug builds.
#if defined(DEBUG)
    virtual void Show( int nestLevel, std::ostream& os ) const {}
#endif

    /** Get class name
     * @return  string "PL_ITEM_LAYOUT"
     */
    virtual wxString GetClass() const
    {
        return wxT( "PL_ITEM_LAYOUT" );
    }
};

void PL_EDITOR_FRAME::SaveCopyInUndoList()
{
    PL_ITEM_LAYOUT* copyItem = new PL_ITEM_LAYOUT;
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
    pglayout.SaveInString( copyItem->m_Layout );

    PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
    ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );
    lastcmd->PushItem(wrapper);
    GetScreen()->PushCommandToUndoList( lastcmd );

    // Clear redo list, because after new save there is no redo to do.
    GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
}


/* Redo the last edition:
 * - Place the current edited layout in undo list
 * - Get previous version of the current edited layput
 */
void PL_EDITOR_FRAME::GetLayoutFromRedoList( wxCommandEvent& event )
{
    if ( GetScreen()->GetRedoCommandCount() <= 0 )
        return;

    PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
    PL_ITEM_LAYOUT* copyItem = new PL_ITEM_LAYOUT;
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
    pglayout.SaveInString( copyItem->m_Layout );

    ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );

    lastcmd->PushItem( wrapper );
    GetScreen()->PushCommandToUndoList( lastcmd );

    lastcmd = GetScreen()->PopCommandFromRedoList();

    wrapper = lastcmd->PopItem();
    copyItem = (PL_ITEM_LAYOUT*)wrapper.GetItem();
    pglayout.SetPageLayout( TO_UTF8(copyItem->m_Layout) );
    delete copyItem;

    OnModify();
    RebuildDesignTree();
    m_canvas->Refresh();
}


/* Undo the last edition:
 * - Place the current layout in Redo list
 * - Get previous version of the current edited layout
 */
void PL_EDITOR_FRAME::GetLayoutFromUndoList( wxCommandEvent& event )
{
    if ( GetScreen()->GetUndoCommandCount() <= 0 )
        return;

    PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
    PL_ITEM_LAYOUT* copyItem = new PL_ITEM_LAYOUT;
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
    pglayout.SaveInString( copyItem->m_Layout );

    ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );
    lastcmd->PushItem( wrapper );
    GetScreen()->PushCommandToRedoList( lastcmd );

    lastcmd = GetScreen()->PopCommandFromUndoList();

    wrapper = lastcmd->PopItem();
    copyItem = (PL_ITEM_LAYOUT*)wrapper.GetItem();
    pglayout.SetPageLayout( TO_UTF8(copyItem->m_Layout) );
    delete copyItem;

    OnModify();
    RebuildDesignTree();
    m_canvas->Refresh();
}

/* Remove the last command in Undo List.
 * Used to clean the uUndo stack after a cancel command
 */
void PL_EDITOR_FRAME::RemoveLastCommandInUndoList()
{
    if ( GetScreen()->GetUndoCommandCount() <= 0 )
        return;

    PICKED_ITEMS_LIST* lastcmd = GetScreen()->PopCommandFromUndoList();

    ITEM_PICKER wrapper = lastcmd->PopItem();
    PL_ITEM_LAYOUT* copyItem = (PL_ITEM_LAYOUT*)wrapper.GetItem();
    delete copyItem;
}