SUMO - Simulation of Urban MObility
GNEUndoList.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // FXUndoList is pretty dandy but some features are missing:
8 // - we cannot find out wether we have currently begun an undo-group and
9 // thus abort() is hard to use.
10 // - onUpd-methods do not disable undo/redo while in an undo-group
11 //
12 // GNEUndoList inherits from FXUndoList and patches some methods. these are
13 // prefixed with p_
14 /****************************************************************************/
15 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
16 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
17 /****************************************************************************/
18 //
19 // This file is part of SUMO.
20 // SUMO is free software: you can redistribute it and/or modify
21 // it under the terms of the GNU General Public License as published by
22 // the Free Software Foundation, either version 3 of the License, or
23 // (at your option) any later version.
24 //
25 /****************************************************************************/
26 
27 
28 // ===========================================================================
29 // included modules
30 // ===========================================================================
31 #ifdef _MSC_VER
32 #include <windows_config.h>
33 #else
34 #include <config.h>
35 #endif
36 
37 #include <iostream>
39 #include "GNEUndoList.h"
40 #include "GNEChange_Attribute.h"
41 #include "GNEApplicationWindow.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif
46 
47 
48 // ===========================================================================
49 // FOX callback mapping
50 // ===========================================================================
51 FXDEFMAP(GNEUndoList) GNEUndoListMap[] = {
52  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_REVERT, FXUndoList::onCmdRevert),
53  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_UNDO, FXUndoList::onCmdUndo),
54  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_REDO, FXUndoList::onCmdRedo),
55  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_UNDO_ALL, FXUndoList::onCmdUndoAll),
56  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_REDO_ALL, FXUndoList::onCmdRedoAll),
57  //
58  //FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_UNDO_COUNT, FXUndoList::onUpdUndoCount),
59  //FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REDO_COUNT, FXUndoList::onUpdRedoCount),
60  //FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_CLEAR, FXUndoList::onUpdClear),
61  //FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REVERT, FXUndoList::onUpdRevert),
62  FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_UNDO_ALL, GNEUndoList::p_onUpdUndo),
63  FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REDO_ALL, GNEUndoList::p_onUpdRedo),
64  FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_UNDO, GNEUndoList::p_onUpdUndo),
65  FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REDO, GNEUndoList::p_onUpdRedo)
66 };
67 
68 
69 // ===========================================================================
70 // FOX-declarations
71 // ===========================================================================
72 FXIMPLEMENT_ABSTRACT(GNEUndoList, FXUndoList, GNEUndoListMap, ARRAYNUMBER(GNEUndoListMap))
73 
74 
75 // ===========================================================================
76 // member method definitions
77 // ===========================================================================
78 
80  FXUndoList(),
81  myParent(parent) {
82 }
83 
84 
85 void
86 GNEUndoList::p_begin(const std::string& description) {
87  myCommandGroups.push(new CommandGroup(description));
88  begin(myCommandGroups.top());
89 }
90 
91 
92 void
94  myCommandGroups.pop();
95  end();
96 }
97 
98 
99 void
101  p_abort();
102  clear();
103 }
104 
105 
106 void
108  while (hasCommandGroup()) {
109  myCommandGroups.top()->undo();
110  myCommandGroups.pop();
111  abort();
112  }
113 }
114 
115 
116 void
118  //std::cout << undoName().text() << "\n";
119  FXUndoList::undo();
121 }
122 
123 
124 void
126  //std::cout << redoName().text() << "\n";
127  FXUndoList::redo();
129 }
130 
131 
132 void
134  if (cmd->trueChange()) {
135  add(cmd, true);
136  } else {
137  delete cmd;
138  }
139 }
140 
141 long
142 GNEUndoList::p_onUpdUndo(FXObject* sender, FXSelector, void*) {
143  bool enable = canUndo() && !hasCommandGroup();
144  sender->handle(this, enable ? FXSEL(SEL_COMMAND, FXWindow::ID_ENABLE) : FXSEL(SEL_COMMAND, FXWindow::ID_DISABLE), 0);
145  FXString caption = undoName();
146  if (hasCommandGroup()) {
147  caption = ("Cannot Undo in the middle of " + myCommandGroups.top()->getDescription()).c_str();
148  } else if (!canUndo()) {
149  caption = "Undo";
150  }
151  // only set caption on menu item
152  if (dynamic_cast<FXMenuCommand*>(sender)) {
153  sender->handle(this, FXSEL(SEL_COMMAND, FXMenuCaption::ID_SETSTRINGVALUE), (void*)&caption);
154  }
155  return 1;
156 }
157 
158 
159 long
160 GNEUndoList::p_onUpdRedo(FXObject* sender, FXSelector, void*) {
161  bool enable = canRedo() && !hasCommandGroup();
162  sender->handle(this, enable ? FXSEL(SEL_COMMAND, FXWindow::ID_ENABLE) : FXSEL(SEL_COMMAND, FXWindow::ID_DISABLE), 0);
163  FXString caption = redoName();
164  if (hasCommandGroup()) {
165  caption = ("Cannot Redo in the middle of " + myCommandGroups.top()->getDescription()).c_str();
166  } else if (!canRedo()) {
167  caption = "Redo";
168  }
169  // only set caption on menu item
170  if (dynamic_cast<FXMenuCommand*>(sender)) {
171  sender->handle(this, FXSEL(SEL_COMMAND, FXMenuCaption::ID_SETSTRINGVALUE), (void*)&caption);
172  }
173  return 1;
174 }
175 
176 
177 bool
179  return myCommandGroups.size() != 0;
180 }
181 
182 
183 GNEUndoList::CommandGroup::CommandGroup(std::string description) :
184  myDescription(description) {
185 }
186 
187 
188 const std::string&
190  return myDescription;
191 }
192 
193 
194 FXString
196  return ("Undo " + myDescription).c_str();
197 }
198 
199 
200 FXString
202  return ("Redo " + myDescription).c_str();
203 }
FXDEFMAP(GNEUndoList) GNEUndoListMap[]
The main window of the Netedit.
const std::string myDescription
description of command
Definition: GNEUndoList.h:123
void p_begin(const std::string &description)
Begin undo command sub-group. This begins a new group of commands that are treated as a single comman...
Definition: GNEUndoList.cpp:86
void redo()
redo the last command group
void updateControls()
update control contents after undo/redo or recompute
void undo()
undo the last command group
void p_clear()
clears the undo list (implies abort)
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
the function-object for an editing operation (abstract base)
bool trueChange()
wether original and new value differ
bool hasCommandGroup() const
Check if undoList has command group.
void p_end()
End undo command sub-group. If the sub-group is still empty, it will be deleted; otherwise, the sub-group will be added as a new command into parent group. A matching begin() must have been called previously.
Definition: GNEUndoList.cpp:93
FXString redoName() const
get redo name
std::stack< CommandGroup * > myCommandGroups
Definition: GNEUndoList.h:127
GNEApplicationWindow *const myParent
Definition: GNEUndoList.h:130
void p_abort()
reverts and discards ALL active command groups
long p_onUpdRedo(FXObject *, FXSelector, void *)
event after Redo
CommandGroup(std::string description)
Constructor.
FXString undoName() const
get undo Name
class CommandGroup
Definition: GNEUndoList.h:107
const std::string & getDescription()
get description
long p_onUpdUndo(FXObject *, FXSelector, void *)