Parent Directory | Revision Log
Fiox in undo disable
1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. |
3 | * |
4 | * This file is part of OSM2Go. |
5 | * |
6 | * OSM2Go is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * OSM2Go is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with OSM2Go. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include "appdata.h" |
21 | |
22 | /* return plain text of type */ |
23 | char *undo_type_string(type_t type) { |
24 | const struct { undo_type_t type; char *name; } types[] = { |
25 | { UNDO_DELETE, "DELETE" }, |
26 | { UNDO_CREATE, "CREATE" }, |
27 | { UNDO_MODIFY, "MODIFY" }, |
28 | { 0, NULL } |
29 | }; |
30 | |
31 | int i; |
32 | for(i=0;types[i].name;i++) |
33 | if(type == types[i].type) |
34 | return types[i].name; |
35 | |
36 | return NULL; |
37 | } |
38 | |
39 | static void undo_object_free(undo_object_t *obj) { |
40 | char *msg = osm_object_string(obj->type, obj->data.ptr); |
41 | printf(" object %s\n", msg); |
42 | g_free(msg); |
43 | |
44 | if(obj->data.ptr) { |
45 | switch(obj->type) { |
46 | case NODE: |
47 | osm_node_free(NULL, obj->data.node); |
48 | break; |
49 | |
50 | case WAY: |
51 | osm_way_free(obj->data.way); |
52 | break; |
53 | |
54 | default: |
55 | printf("ERROR: unsupported object %s\n", |
56 | osm_type_string(obj->type)); |
57 | g_assert(0); |
58 | break; |
59 | } |
60 | } |
61 | |
62 | g_free(obj); |
63 | } |
64 | |
65 | static void undo_op_free(undo_op_t *op) { |
66 | printf(" op: %s\n", undo_type_string(op->type)); |
67 | if(op->object) undo_object_free(op->object); |
68 | g_free(op); |
69 | } |
70 | |
71 | static void undo_state_free(undo_state_t *state) { |
72 | printf(" state: %s\n", undo_type_string(state->type)); |
73 | |
74 | undo_op_t *op = state->op; |
75 | while(op) { |
76 | undo_op_t *next = op->next; |
77 | undo_op_free(op); |
78 | op = next; |
79 | } |
80 | |
81 | g_free(state); |
82 | } |
83 | |
84 | /* free all undo states, thus forgetting the entire history */ |
85 | /* called at program exit or e.g. at project change */ |
86 | void undo_free(undo_state_t *state) { |
87 | printf("Freeing all UNDO states:\n"); |
88 | |
89 | while(state) { |
90 | undo_state_t *next = state->next; |
91 | undo_state_free(state); |
92 | state = next; |
93 | } |
94 | } |
95 | |
96 | /* append a new state to the chain of undo states */ |
97 | static undo_state_t *undo_append_state(appdata_t *appdata) { |
98 | undo_state_t *new_state = NULL; |
99 | |
100 | /* create new undo state at end of undo chain */ |
101 | int undo_chain_length = 0; |
102 | undo_state_t **undo_stateP = &appdata->undo.state; |
103 | while(*undo_stateP) { |
104 | undo_chain_length++; |
105 | undo_stateP = &(*undo_stateP)->next; |
106 | } |
107 | |
108 | /* append new entry to chain */ |
109 | new_state = *undo_stateP = g_new0(undo_state_t, 1); |
110 | |
111 | /* delete first entry if the chain is too long */ |
112 | if(undo_chain_length >= UNDO_QUEUE_LEN) { |
113 | undo_state_t *second = appdata->undo.state->next; |
114 | undo_state_free(appdata->undo.state); |
115 | appdata->undo.state = second; |
116 | } |
117 | |
118 | printf("UNDO: current chain length = %d\n", undo_chain_length); |
119 | |
120 | return new_state; |
121 | } |
122 | |
123 | /* create a local copy of the entire object */ |
124 | static undo_object_t *undo_object_copy(type_t type, void *object) { |
125 | switch(type) { |
126 | case NODE: { |
127 | undo_object_t *ob = g_new0(undo_object_t, 1); |
128 | ob->type = type; |
129 | |
130 | /* fields ignored in this copy operation: */ |
131 | /* ways, icon_buf, map_item_chain, next */ |
132 | |
133 | node_t *node = (node_t*)object; |
134 | ob->data.node = g_new0(node_t, 1); |
135 | /* copy all important parts, omitting icon pointers etc. */ |
136 | ob->data.node->id = node->id; |
137 | ob->data.node->lpos = node->lpos; |
138 | ob->data.node->pos = node->pos; |
139 | /* user is a pointer, but since the users list */ |
140 | /* is never touched it's ok */ |
141 | ob->data.node->user = node->user; |
142 | ob->data.node->visible = node->visible; |
143 | ob->data.node->time = node->time; |
144 | ob->data.node->tag = osm_tags_copy(node->tag, FALSE); |
145 | ob->data.node->flags = node->flags; |
146 | ob->data.node->zoom_max = node->zoom_max; |
147 | |
148 | return ob; |
149 | } break; |
150 | |
151 | case WAY: { |
152 | undo_object_t *ob = g_new0(undo_object_t, 1); |
153 | ob->type = type; |
154 | |
155 | /* fields ignored in this copy operation: */ |
156 | |
157 | way_t *way = (way_t*)object; |
158 | ob->data.way = g_new0(way_t, 1); |
159 | /* copy all important parts */ |
160 | ob->data.way->id = way->id; |
161 | /* user is a pointer, but since the users list */ |
162 | /* is never touched it's ok */ |
163 | ob->data.way->user = way->user; |
164 | ob->data.way->visible = way->visible; |
165 | ob->data.way->time = way->time; |
166 | ob->data.way->tag = osm_tags_copy(way->tag, FALSE); |
167 | ob->data.way->flags = way->flags; |
168 | |
169 | return ob; |
170 | } break; |
171 | |
172 | default: |
173 | printf("UNDO WARNING: ignoring unsupported object %s\n", |
174 | osm_type_string(type)); |
175 | break; |
176 | } |
177 | |
178 | return NULL; |
179 | } |
180 | |
181 | void undo_remember_delete(appdata_t *appdata, type_t type, |
182 | void *object) { |
183 | |
184 | /* don't do anything if undo isn't enabled */ |
185 | if(!appdata->menu_item_osm_undo) |
186 | return; |
187 | |
188 | printf("UNDO: remembering delete operation for %s\n", |
189 | osm_type_string(type)); |
190 | |
191 | /* create a new undo state */ |
192 | undo_state_t *state = undo_append_state(appdata); |
193 | state->type = UNDO_DELETE; |
194 | |
195 | /* a simple stand-alone node deletion is just a single */ |
196 | /* operation on the database/map so only one undo_op is saved */ |
197 | undo_op_t *op = state->op = g_new0(undo_op_t, 1); |
198 | op->type = UNDO_DELETE; |
199 | op->object = undo_object_copy(type, object); |
200 | } |
201 | |
202 | /* undo the deletion of an object */ |
203 | static void undo_operation_object_delete(appdata_t *appdata, |
204 | undo_object_t *obj) { |
205 | |
206 | char *msg = osm_object_string(obj->type, obj->data.ptr); |
207 | printf("UNDO deletion of object %s\n", msg); |
208 | g_free(msg); |
209 | |
210 | switch(obj->type) { |
211 | case NODE: { |
212 | node_t *node = obj->data.node; |
213 | |
214 | /* there must be an "deleted" entry which needs to be */ |
215 | /* removed */ |
216 | node_t *orig = osm_get_node_by_id(appdata->osm, node->id); |
217 | g_assert(orig); |
218 | g_assert(orig->flags & OSM_FLAG_DELETED); |
219 | way_chain_t *wchain = |
220 | osm_node_delete(appdata->osm, &appdata->icon, orig, TRUE, TRUE); |
221 | g_assert(!wchain); |
222 | |
223 | /* then restore old node */ |
224 | osm_node_dump(node); |
225 | osm_node_restore(appdata->osm, node); |
226 | josm_elemstyles_colorize_node(appdata->map->style, node); |
227 | map_node_draw(appdata->map, node); |
228 | obj->data.ptr = NULL; |
229 | } break; |
230 | |
231 | default: |
232 | printf("Unsupported object type\n"); |
233 | break; |
234 | } |
235 | } |
236 | |
237 | /* undo a single operation */ |
238 | static void undo_operation(appdata_t *appdata, undo_op_t *op) { |
239 | printf("UNDO operation: %s\n", undo_type_string(op->type)); |
240 | |
241 | switch(op->type) { |
242 | case UNDO_DELETE: |
243 | undo_operation_object_delete(appdata, op->object); |
244 | break; |
245 | |
246 | default: |
247 | printf("unsupported UNDO operation\n"); |
248 | g_assert(0); |
249 | break; |
250 | } |
251 | } |
252 | |
253 | /* undo the last undo_state */ |
254 | void undo(appdata_t *appdata) { |
255 | undo_state_t *state = appdata->undo.state; |
256 | printf("user selected undo\n"); |
257 | |
258 | /* search last (newest) entry */ |
259 | while(state && state->next) state = state->next; |
260 | |
261 | if(!state) { |
262 | banner_show_info(appdata, _("No further undo data")); |
263 | return; |
264 | } |
265 | |
266 | |
267 | printf("UNDO state: %s\n", undo_type_string(state->type)); |
268 | |
269 | /* since the operations list was built by prepending new */ |
270 | /* entries, just going through the list will run the operations */ |
271 | /* in reverse order. That's exactly what we want! */ |
272 | |
273 | undo_op_t *op = state->op; |
274 | while(op) { |
275 | undo_operation(appdata, op); |
276 | op = op->next; |
277 | } |
278 | |
279 | /* remove this entry from chain */ |
280 | undo_state_t **stateP = &appdata->undo.state; |
281 | while(*stateP && (*stateP)->next) stateP = &(*stateP)->next; |
282 | |
283 | undo_state_free(*stateP); |
284 | *stateP = NULL; |
285 | } |