~ubuntu-branches/ubuntu/wily/grass/wily

« back to all changes in this revision

Viewing changes to gui/wxpython/vdigit/undo.cpp

Tags: 7.0.0~rc1+ds1-1~exp1
* New upstream release candidate.
* Repack upstream tarball, remove precompiled Python objects.
* Add upstream metadata.
* Update gbp.conf and Vcs-Git URL to use the experimental branch.
* Update watch file for GRASS 7.0.
* Drop build dependencies for Tcl/Tk, add build dependencies:
  python-numpy, libnetcdf-dev, netcdf-bin, libblas-dev, liblapack-dev
* Update Vcs-Browser URL to use cgit instead of gitweb.
* Update paths to use grass70.
* Add configure options: --with-netcdf, --with-blas, --with-lapack,
  remove --with-tcltk-includes.
* Update patches for GRASS 7.
* Update copyright file, changes:
  - Update copyright years
  - Group files by license
  - Remove unused license sections
* Add patches for various typos.
* Fix desktop file with patch instead of d/rules.
* Use minimal dh rules.
* Bump Standards-Version to 3.9.6, no changes.
* Use dpkg-maintscript-helper to replace directories with symlinks.
  (closes: #776349)
* Update my email to use @debian.org address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
   \file vdigit/undo.cpp
3
 
 
4
 
   \brief wxvdigit - Undo/Redo functionality
5
 
 
6
 
   This program is free software under the GNU General Public
7
 
   License (>=v2). Read the file COPYING that comes with GRASS
8
 
   for details.
9
 
 
10
 
   (C) 2008-2010 by Martin Landa, and the GRASS development team
11
 
 
12
 
   \author Martin Landa <landa.martin gmail.com>
13
 
*/
14
 
 
15
 
#include "driver.h"
16
 
#include "digit.h"
17
 
 
18
 
/**
19
 
   \brief Undo/Redo changes in geometry
20
 
 
21
 
   level=0 to revert all changes
22
 
 
23
 
   \param level level for undo/redo
24
 
 
25
 
   \return id of current chanset
26
 
   \return -2 on error
27
 
*/
28
 
int Digit::Undo(int level)
29
 
{
30
 
    int changesetLast;
31
 
 
32
 
    changesetLast = (int) changesets.size() - 1;
33
 
 
34
 
    if (changesetLast < 0)
35
 
        return changesetLast;
36
 
 
37
 
    if (changesetCurrent == -2) { /* value uninitialized */
38
 
        changesetCurrent = changesetLast;
39
 
    }
40
 
 
41
 
    if (level > 0 && changesetCurrent < 0) {
42
 
        changesetCurrent = 0;
43
 
    }
44
 
 
45
 
    if (level == 0) {
46
 
        /* 0 -> undo all */
47
 
        level = -(changesetLast + 1);
48
 
    }
49
 
 
50
 
    G_debug(2, "Digit.Undo(): changeset_last=%d, changeset_current=%d, level=%d",
51
 
            changesetLast, changesetCurrent, level);
52
 
    
53
 
    if (level < 0) { /* undo */
54
 
        if (changesetCurrent + level < -1)
55
 
            return changesetCurrent;
56
 
        for (int changeset = changesetCurrent; changeset > changesetCurrent + level; --changeset) {
57
 
            ApplyChangeset(changeset, true);
58
 
        }
59
 
    }
60
 
    else if (level > 0) { /* redo */
61
 
        if (changesetCurrent + level > (int) changesets.size())
62
 
            return changesetCurrent;
63
 
        for (int changeset = changesetCurrent; changeset < changesetCurrent + level; ++changeset) {
64
 
            ApplyChangeset(changeset, false);
65
 
        }
66
 
    }
67
 
 
68
 
    changesetCurrent += level;
69
 
 
70
 
    G_debug(2, "Digit.Undo(): changeset_current=%d, changeset_last=%d, changeset_end=%d",
71
 
            changesetCurrent, changesetLast, changesetEnd);
72
 
    
73
 
    if (changesetCurrent == changesetEnd) {
74
 
        changesetEnd = changesetLast;
75
 
        return -1;
76
 
    }
77
 
    
78
 
    return changesetCurrent;
79
 
}
80
 
 
81
 
/**
82
 
   \brief Apply changeset (undo/redo changeset)
83
 
 
84
 
   \param changeset changeset id
85
 
   \param undo if true -> undo otherwise redo
86
 
 
87
 
   \return 1 changeset applied
88
 
   \return 0 changeset not applied
89
 
   \return -1 on error
90
 
*/
91
 
int Digit::ApplyChangeset(int changeset, bool undo)
92
 
93
 
    int ret, line, type;
94
 
 
95
 
    if (changeset < 0 || changeset > (int) changesets.size())
96
 
        return -1;
97
 
 
98
 
    if (changesetEnd < 0)
99
 
        changesetEnd = changeset;
100
 
    
101
 
    ret = 0;
102
 
    std::vector<action_meta> action = changesets[changeset];
103
 
    for (std::vector<action_meta>::const_reverse_iterator i = action.rbegin(), e = action.rend();
104
 
         i != e; ++i) {
105
 
        type = (*i).type;
106
 
        line = (*i).line;
107
 
        
108
 
        if ((undo && type == ADD) ||
109
 
            (!undo && type == DEL)) {
110
 
            if (Vect_line_alive(display->mapInfo, line)) {
111
 
                G_debug(3, "Digit.ApplyChangeset(): changeset=%d, action=add, line=%d -> deleted",
112
 
                        changeset, line);
113
 
                Vect_delete_line(display->mapInfo, line);
114
 
                if (!ret)
115
 
                    ret = 1;
116
 
            }
117
 
            else {
118
 
                G_debug(3, "Digit.ApplyChangeset(): changeset=%d, action=add, line=%d dead",
119
 
                        changeset, (*i).line);
120
 
            }
121
 
        }
122
 
        else { /* DELETE */
123
 
            long offset = (*i).offset;
124
 
            if (!Vect_line_alive(display->mapInfo, line)) {
125
 
                G_debug(3, "Digit.ApplyChangeset(): changeset=%d, action=delete, line=%d -> added",
126
 
                        changeset, line);
127
 
                if (Vect_restore_line(display->mapInfo, line, offset) < 0)
128
 
                    return -1;
129
 
                if (!ret)
130
 
                    ret = 1;
131
 
            }
132
 
            else {
133
 
                G_debug(3, "Digit.ApplyChangeset(): changeset=%d, action=delete, line=%d alive",
134
 
                        changeset, line);
135
 
            }
136
 
        }
137
 
    }
138
 
    
139
 
    return ret;
140
 
}
141
 
 
142
 
/**
143
 
   \brief Add action to changeset
144
 
 
145
 
   \param type action type (ADD, DEL)
146
 
 
147
 
   \return 0 on success
148
 
   \return -1 on error
149
 
*/
150
 
int Digit::AddActionToChangeset(int changeset, Digit::action_type type, int line)
151
 
{
152
 
    long offset;
153
 
 
154
 
    if (!display->mapInfo) {
155
 
        display->DisplayMsg();
156
 
        return -1;
157
 
    }
158
 
 
159
 
    if (!Vect_line_alive(display->mapInfo, line)) {
160
 
        // display->DeadLineMsg(line);
161
 
        return -1;
162
 
    }
163
 
 
164
 
    offset = Vect_get_line_offset(display->mapInfo, line);
165
 
 
166
 
    action_meta data = { type, line, offset };
167
 
    if (changesets.find(changeset) == changesets.end()) {
168
 
        changesets[changeset] = std::vector<action_meta>();
169
 
        changesetCurrent = changeset;
170
 
    }
171
 
    changesets[changeset].push_back(data);
172
 
    G_debug (3, "Digit.AddActionToChangeset(): changeset=%d, type=%d, line=%d, offset=%ld",
173
 
             changeset, type, line, offset);
174
 
 
175
 
    return 0;
176
 
}
177
 
 
178
 
/**
179
 
   \brief Free changeset structures
180
 
 
181
 
   \param changeset changeset id
182
 
*/
183
 
void Digit::FreeChangeset(int changeset)
184
 
{
185
 
    if (changesets.find(changeset) == changesets.end())
186
 
        return;
187
 
 
188
 
    std::vector<action_meta> action = changesets[changeset];
189
 
    for (std::vector<action_meta>::iterator i = action.begin(), e = action.end();
190
 
         i != e; ++i) {
191
 
        ;
192
 
    }
193
 
 
194
 
    return;
195
 
}
196
 
 
197
 
/**
198
 
   \brief Remove action from changeset
199
 
 
200
 
   \param changeset changeset id
201
 
   \param type action type (ADD, DEL)
202
 
   \param line line id
203
 
 
204
 
   \return number of actions in changeset
205
 
   \return -1 on error
206
 
*/
207
 
int Digit::RemoveActionFromChangeset(int changeset, Digit::action_type type, int line)
208
 
{
209
 
    if (changesets.find(changeset) == changesets.end())
210
 
        return -1;
211
 
 
212
 
    std::vector<action_meta>& action = changesets[changeset];
213
 
    for (std::vector<action_meta>::iterator i = action.begin(); i != action.end(); ++i) {
214
 
        if ((*i).type == type && (*i).line == line) {
215
 
            G_debug (3, "Digit.RemoveActionFromChangeset(): changeset=%d, type=%d, line=%d",
216
 
                     changeset, type, line);
217
 
            action.erase(i--);
218
 
        }
219
 
    }
220
 
 
221
 
    return action.size();
222
 
}
223
 
 
224
 
/**
225
 
   \brief Get undo level (number of active changesets)
226
 
 
227
 
   \return number
228
 
*/
229
 
int Digit::GetUndoLevel()
230
 
{
231
 
    return changesetCurrent;
232
 
}