~cern-kicad/kicad/kicad-pns-tom

« back to all changes in this revision

Viewing changes to pagelayout_editor/pl_editor_undo_redo.cpp

  • Committer: Maciej Suminski
  • Date: 2013-08-02 13:57:24 UTC
  • mfrom: (4024.1.238 kicad)
  • mto: This revision was merged to the branch mainline in revision 4221.
  • Revision ID: maciej.suminski@cern.ch-20130802135724-gix6orezshkukodv
Upstream merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file pl_editor_undo_redo.cpp
 
3
 *  @brief page layout editor: undo and redo functions
 
4
 */
 
5
 
 
6
/*
 
7
 * This program source code file is part of KiCad, a free EDA CAD application.
 
8
 *
 
9
 * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
 
10
 * Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.txt for contributors.
 
11
 *
 
12
 * This program is free software; you can redistribute it and/or
 
13
 * modify it under the terms of the GNU General Public License
 
14
 * as published by the Free Software Foundation; either version 2
 
15
 * of the License, or (at your option) any later version.
 
16
 *
 
17
 * This program is distributed in the hope that it will be useful,
 
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 * GNU General Public License for more details.
 
21
 *
 
22
 * You should have received a copy of the GNU General Public License
 
23
 * along with this program; if not, you may find one here:
 
24
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 
25
 * or you may search the http://www.gnu.org website for the version 2 license,
 
26
 * or you may write to the Free Software Foundation, Inc.,
 
27
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
28
 */
 
29
 
 
30
#include <fctsys.h>
 
31
#include <class_drawpanel.h>
 
32
#include <macros.h>
 
33
#include <worksheet_shape_builder.h>
 
34
 
 
35
#include <pl_editor_frame.h>
 
36
 
 
37
/* Note: the Undo/redo commands use a "brute" method:
 
38
 * the full page layout is converted to a S expression, and saved as string.
 
39
 * When a previous version is needed, the old string is parsed,
 
40
 * and the description replaces the current desc, just like reading a new file
 
41
 *
 
42
 * This is not optimal from the memory point of view, but:
 
43
 * - the descriptions are never very long (max few thousand of bytes)
 
44
 * - this is very easy to code
 
45
 */
 
46
 
 
47
// A helper class used in undo/redo commad:
 
48
class PL_ITEM_LAYOUT: public EDA_ITEM
 
49
{
 
50
public:
 
51
    wxString m_Layout;
 
52
 
 
53
public:
 
54
    PL_ITEM_LAYOUT() : EDA_ITEM( TYPE_PL_EDITOR_LAYOUT ) {}
 
55
 
 
56
    // Required to keep compiler happy on debug builds.
 
57
#if defined(DEBUG)
 
58
    virtual void Show( int nestLevel, std::ostream& os ) const {}
 
59
#endif
 
60
};
 
61
 
 
62
void PL_EDITOR_FRAME::SaveCopyInUndoList()
 
63
{
 
64
    PL_ITEM_LAYOUT* copyItem = new PL_ITEM_LAYOUT;
 
65
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
 
66
    pglayout.SaveInString( copyItem->m_Layout );
 
67
 
 
68
    PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
 
69
    ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );
 
70
    lastcmd->PushItem(wrapper);
 
71
    GetScreen()->PushCommandToUndoList( lastcmd );
 
72
 
 
73
    // Clear redo list, because after new save there is no redo to do.
 
74
    GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
 
75
}
 
76
 
 
77
 
 
78
/* Redo the last edition:
 
79
 * - Place the current edited layout in undo list
 
80
 * - Get previous version of the current edited layput
 
81
 */
 
82
void PL_EDITOR_FRAME::GetLayoutFromRedoList( wxCommandEvent& event )
 
83
{
 
84
    if ( GetScreen()->GetRedoCommandCount() <= 0 )
 
85
        return;
 
86
 
 
87
    PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
 
88
    PL_ITEM_LAYOUT* copyItem = new PL_ITEM_LAYOUT;
 
89
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
 
90
    pglayout.SaveInString( copyItem->m_Layout );
 
91
 
 
92
    ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );
 
93
 
 
94
    lastcmd->PushItem( wrapper );
 
95
    GetScreen()->PushCommandToUndoList( lastcmd );
 
96
 
 
97
    lastcmd = GetScreen()->PopCommandFromRedoList();
 
98
 
 
99
    wrapper = lastcmd->PopItem();
 
100
    copyItem = (PL_ITEM_LAYOUT*)wrapper.GetItem();
 
101
    pglayout.SetPageLayout( TO_UTF8(copyItem->m_Layout) );
 
102
    delete copyItem;
 
103
 
 
104
    OnModify();
 
105
    RebuildDesignTree();
 
106
    m_canvas->Refresh();
 
107
}
 
108
 
 
109
 
 
110
/* Undo the last edition:
 
111
 * - Place the current layout in Redo list
 
112
 * - Get previous version of the current edited layout
 
113
 */
 
114
void PL_EDITOR_FRAME::GetLayoutFromUndoList( wxCommandEvent& event )
 
115
{
 
116
    if ( GetScreen()->GetUndoCommandCount() <= 0 )
 
117
        return;
 
118
 
 
119
    PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
 
120
    PL_ITEM_LAYOUT* copyItem = new PL_ITEM_LAYOUT;
 
121
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
 
122
    pglayout.SaveInString( copyItem->m_Layout );
 
123
 
 
124
    ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );
 
125
    lastcmd->PushItem( wrapper );
 
126
    GetScreen()->PushCommandToRedoList( lastcmd );
 
127
 
 
128
    lastcmd = GetScreen()->PopCommandFromUndoList();
 
129
 
 
130
    wrapper = lastcmd->PopItem();
 
131
    copyItem = (PL_ITEM_LAYOUT*)wrapper.GetItem();
 
132
    pglayout.SetPageLayout( TO_UTF8(copyItem->m_Layout) );
 
133
    delete copyItem;
 
134
 
 
135
    OnModify();
 
136
    RebuildDesignTree();
 
137
    m_canvas->Refresh();
 
138
}
 
139
 
 
140
/* Remove the last command in Undo List.
 
141
 * Used to clean the uUndo stack after a cancel command
 
142
 */
 
143
void PL_EDITOR_FRAME::RemoveLastCommandInUndoList()
 
144
{
 
145
    if ( GetScreen()->GetUndoCommandCount() <= 0 )
 
146
        return;
 
147
 
 
148
    PICKED_ITEMS_LIST* lastcmd = GetScreen()->PopCommandFromUndoList();
 
149
 
 
150
    ITEM_PICKER wrapper = lastcmd->PopItem();
 
151
    PL_ITEM_LAYOUT* copyItem = (PL_ITEM_LAYOUT*)wrapper.GetItem();
 
152
    delete copyItem;
 
153
}