SUMO - Simulation of Urban MObility
GNEUndoList.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 // FXUndoList is pretty dandy but some features are missing:
18 // - we cannot find out wether we have currently begun an undo-group and
19 // thus abort() is hard to use.
20 // - onUpd-methods do not disable undo/redo while in an undo-group
21 //
22 // GNEUndoList inherits from FXUndoList and patches some methods. these are
23 // prefixed with p_
24 /****************************************************************************/
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #include <iostream>
40 
41 #include "GNEUndoList.h"
42 #include "GNEChange_Attribute.h"
43 #include "GNEApplicationWindow.h"
44 
45 
46 // ===========================================================================
47 // FOX callback mapping
48 // ===========================================================================
49 FXDEFMAP(GNEUndoList) GNEUndoListMap[] = {
50  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_REVERT, FXUndoList::onCmdRevert),
51  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_UNDO, FXUndoList::onCmdUndo),
52  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_REDO, FXUndoList::onCmdRedo),
53  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_UNDO_ALL, FXUndoList::onCmdUndoAll),
54  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_REDO_ALL, FXUndoList::onCmdRedoAll),
55  //
56  //FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_UNDO_COUNT, FXUndoList::onUpdUndoCount),
57  //FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REDO_COUNT, FXUndoList::onUpdRedoCount),
58  //FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_CLEAR, FXUndoList::onUpdClear),
59  //FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REVERT, FXUndoList::onUpdRevert),
60  FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_UNDO_ALL, GNEUndoList::p_onUpdUndo),
61  FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REDO_ALL, GNEUndoList::p_onUpdRedo),
62  FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_UNDO, GNEUndoList::p_onUpdUndo),
63  FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REDO, GNEUndoList::p_onUpdRedo)
64 };
65 
66 
67 // ===========================================================================
68 // FOX-declarations
69 // ===========================================================================
70 FXIMPLEMENT_ABSTRACT(GNEUndoList, FXUndoList, GNEUndoListMap, ARRAYNUMBER(GNEUndoListMap))
71 
72 
73 // ===========================================================================
74 // member method definitions
75 // ===========================================================================
76 
78  FXUndoList(),
79  myParent(parent) {
80 }
81 
82 
83 void
84 GNEUndoList::p_begin(const std::string& description) {
85  myCommandGroups.push(new CommandGroup(description));
86  begin(myCommandGroups.top());
87 }
88 
89 
90 void
92  myCommandGroups.pop();
93  end();
94 }
95 
96 
97 void
99  p_abort();
100  clear();
101 }
102 
103 
104 void
106  while (hasCommandGroup()) {
107  myCommandGroups.top()->undo();
108  myCommandGroups.pop();
109  abort();
110  }
111 }
112 
113 
114 void
116  if (myCommandGroups.size() > 0) {
117  myCommandGroups.top()->undo();
118  myCommandGroups.pop();
119  abort();
120  }
121 }
122 
123 
124 void
126  //std::cout << undoName().text() << "\n";
127  if (OptionsCont::getOptions().getBool("gui-testing-debug")) {
128  WRITE_WARNING("Keys Ctrl + Z (Undo) pressed");
129  }
130  FXUndoList::undo();
132 }
133 
134 
135 void
137  //std::cout << redoName().text() << "\n";
138  //std::cout << undoName().text() << "\n";
139  if (OptionsCont::getOptions().getBool("gui-testing-debug")) {
140  WRITE_WARNING("Keys Ctrl + Y (Redo) pressed");
141  }
142  FXUndoList::redo();
144 }
145 
146 
147 void
149  if (cmd->trueChange()) {
150  add(cmd, true);
151  } else {
152  delete cmd;
153  }
154 }
155 
156 
157 int
159  if (myCommandGroups.size() > 0) {
160  return myCommandGroups.top()->size();
161  } else {
162  return 0;
163  }
164 }
165 
166 
167 long
168 GNEUndoList::p_onUpdUndo(FXObject* sender, FXSelector, void*) {
169  bool enable = canUndo() && !hasCommandGroup();
170  sender->handle(this, enable ? FXSEL(SEL_COMMAND, FXWindow::ID_ENABLE) : FXSEL(SEL_COMMAND, FXWindow::ID_DISABLE), 0);
171  FXString caption = undoName();
172  if (hasCommandGroup()) {
173  caption = ("Cannot Undo in the middle of " + myCommandGroups.top()->getDescription()).c_str();
174  } else if (!canUndo()) {
175  caption = "Undo";
176  }
177  // only set caption on menu item
178  if (dynamic_cast<FXMenuCommand*>(sender)) {
179  sender->handle(this, FXSEL(SEL_COMMAND, FXMenuCaption::ID_SETSTRINGVALUE), (void*)&caption);
180  }
181  return 1;
182 }
183 
184 
185 long
186 GNEUndoList::p_onUpdRedo(FXObject* sender, FXSelector, void*) {
187  bool enable = canRedo() && !hasCommandGroup();
188  sender->handle(this, enable ? FXSEL(SEL_COMMAND, FXWindow::ID_ENABLE) : FXSEL(SEL_COMMAND, FXWindow::ID_DISABLE), 0);
189  FXString caption = redoName();
190  if (hasCommandGroup()) {
191  caption = ("Cannot Redo in the middle of " + myCommandGroups.top()->getDescription()).c_str();
192  } else if (!canRedo()) {
193  caption = "Redo";
194  }
195  // only set caption on menu item
196  if (dynamic_cast<FXMenuCommand*>(sender)) {
197  sender->handle(this, FXSEL(SEL_COMMAND, FXMenuCaption::ID_SETSTRINGVALUE), (void*)&caption);
198  }
199  return 1;
200 }
201 
202 
203 bool
205  return myCommandGroups.size() != 0;
206 }
207 
208 
209 GNEUndoList::CommandGroup::CommandGroup(std::string description) :
210  myDescription(description) {
211 }
212 
213 
214 const std::string&
216  return myDescription;
217 }
218 
219 
220 FXString
222  return ("Undo " + myDescription).c_str();
223 }
224 
225 
226 FXString
228  return ("Redo " + myDescription).c_str();
229 }
FXDEFMAP(GNEUndoList) GNEUndoListMap[]
The main window of the Netedit.
const std::string myDescription
description of command
Definition: GNEUndoList.h:128
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:84
void redo()
redo the last command group
void updateControls()
update control contents after undo/redo or recompute
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
void undo()
undo the last command group
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
void p_clear()
clears the undo list (implies abort)
Definition: GNEUndoList.cpp:98
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:91
FXString redoName() const
get redo name
std::stack< CommandGroup * > myCommandGroups
Definition: GNEUndoList.h:132
GNEApplicationWindow *const myParent
Definition: GNEUndoList.h:135
int currentCommandGroupSize() const
get size of current CommandGroup
void p_abort()
reverts and discards ALL active command groups
void p_abortLastCommandGroup()
reverts last command group
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:112
const std::string & getDescription()
get description
long p_onUpdUndo(FXObject *, FXSelector, void *)