~kicad-gost-committers/kicad/kicad

« back to all changes in this revision

Viewing changes to pagelayout_editor/pl_editor_undo_redo.cpp

  • Committer: Alexander Lunev
  • Date: 2013-07-26 19:08:10 UTC
  • mfrom: (4090.1.164 kicad)
  • Revision ID: al.lunev@yahoo.com-20130726190810-l70601hqg4v2swxg
merge with revision 4254 of lp:kicad branch

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
 
 
57
void PL_EDITOR_FRAME::SaveCopyInUndoList()
 
58
{
 
59
    PL_ITEM_LAYOUT* copyItem = new PL_ITEM_LAYOUT;
 
60
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
 
61
    pglayout.SaveInString( copyItem->m_Layout );
 
62
 
 
63
    PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
 
64
    ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );
 
65
    lastcmd->PushItem(wrapper);
 
66
    GetScreen()->PushCommandToUndoList( lastcmd );
 
67
 
 
68
    // Clear redo list, because after new save there is no redo to do.
 
69
    GetScreen()->ClearUndoORRedoList( GetScreen()->m_RedoList );
 
70
}
 
71
 
 
72
 
 
73
/* Redo the last edition:
 
74
 * - Place the current edited layout in undo list
 
75
 * - Get previous version of the current edited layput
 
76
 */
 
77
void PL_EDITOR_FRAME::GetLayoutFromRedoList( wxCommandEvent& event )
 
78
{
 
79
    if ( GetScreen()->GetRedoCommandCount() <= 0 )
 
80
        return;
 
81
 
 
82
    PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
 
83
    PL_ITEM_LAYOUT* copyItem = new PL_ITEM_LAYOUT;
 
84
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
 
85
    pglayout.SaveInString( copyItem->m_Layout );
 
86
 
 
87
    ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );
 
88
 
 
89
    lastcmd->PushItem( wrapper );
 
90
    GetScreen()->PushCommandToUndoList( lastcmd );
 
91
 
 
92
    lastcmd = GetScreen()->PopCommandFromRedoList();
 
93
 
 
94
    wrapper = lastcmd->PopItem();
 
95
    copyItem = (PL_ITEM_LAYOUT*)wrapper.GetItem();
 
96
    pglayout.SetPageLayout( TO_UTF8(copyItem->m_Layout) );
 
97
    delete copyItem;
 
98
 
 
99
    OnModify();
 
100
    RebuildDesignTree();
 
101
    m_canvas->Refresh();
 
102
}
 
103
 
 
104
 
 
105
/* Undo the last edition:
 
106
 * - Place the current layout in Redo list
 
107
 * - Get previous version of the current edited layout
 
108
 */
 
109
void PL_EDITOR_FRAME::GetLayoutFromUndoList( wxCommandEvent& event )
 
110
{
 
111
    if ( GetScreen()->GetUndoCommandCount() <= 0 )
 
112
        return;
 
113
 
 
114
    PICKED_ITEMS_LIST* lastcmd = new PICKED_ITEMS_LIST();
 
115
    PL_ITEM_LAYOUT* copyItem = new PL_ITEM_LAYOUT;
 
116
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
 
117
    pglayout.SaveInString( copyItem->m_Layout );
 
118
 
 
119
    ITEM_PICKER wrapper( copyItem, UR_LIBEDIT );
 
120
    lastcmd->PushItem( wrapper );
 
121
    GetScreen()->PushCommandToRedoList( lastcmd );
 
122
 
 
123
    lastcmd = GetScreen()->PopCommandFromUndoList();
 
124
 
 
125
    wrapper = lastcmd->PopItem();
 
126
    copyItem = (PL_ITEM_LAYOUT*)wrapper.GetItem();
 
127
    pglayout.SetPageLayout( TO_UTF8(copyItem->m_Layout) );
 
128
    delete copyItem;
 
129
 
 
130
    OnModify();
 
131
    RebuildDesignTree();
 
132
    m_canvas->Refresh();
 
133
}
 
134
 
 
135
/* Remove the last command in Undo List.
 
136
 * Used to clean the uUndo stack after a cancel command
 
137
 */
 
138
void PL_EDITOR_FRAME::RemoveLastCommandInUndoList()
 
139
{
 
140
    if ( GetScreen()->GetUndoCommandCount() <= 0 )
 
141
        return;
 
142
 
 
143
    PICKED_ITEMS_LIST* lastcmd = GetScreen()->PopCommandFromUndoList();
 
144
 
 
145
    ITEM_PICKER wrapper = lastcmd->PopItem();
 
146
    PL_ITEM_LAYOUT* copyItem = (PL_ITEM_LAYOUT*)wrapper.GetItem();
 
147
    delete copyItem;
 
148
}