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  FXUndoList::undo();
120 }
121 
122 
123 void
125  FXUndoList::redo();
127 }
128 
129 
130 void
132  if (cmd->trueChange()) {
133  add(cmd, true);
134  } else {
135  delete cmd;
136  }
137 }
138 
139 long
140 GNEUndoList::p_onUpdUndo(FXObject* sender, FXSelector, void*) {
141  bool enable = canUndo() && !hasCommandGroup();
142  sender->handle(this, enable ? FXSEL(SEL_COMMAND, FXWindow::ID_ENABLE) : FXSEL(SEL_COMMAND, FXWindow::ID_DISABLE), 0);
143  FXString caption = undoName();
144  if (hasCommandGroup()) {
145  caption = ("Cannot Undo in the middle of " + myCommandGroups.top()->getDescription()).c_str();
146  } else if (!canUndo()) {
147  caption = "Undo";
148  }
149  // only set caption on menu item
150  if (dynamic_cast<FXMenuCommand*>(sender)) {
151  sender->handle(this, FXSEL(SEL_COMMAND, FXMenuCaption::ID_SETSTRINGVALUE), (void*)&caption);
152  }
153  return 1;
154 }
155 
156 
157 long
158 GNEUndoList::p_onUpdRedo(FXObject* sender, FXSelector, void*) {
159  bool enable = canRedo() && !hasCommandGroup();
160  sender->handle(this, enable ? FXSEL(SEL_COMMAND, FXWindow::ID_ENABLE) : FXSEL(SEL_COMMAND, FXWindow::ID_DISABLE), 0);
161  FXString caption = redoName();
162  if (hasCommandGroup()) {
163  caption = ("Cannot Redo in the middle of " + myCommandGroups.top()->getDescription()).c_str();
164  } else if (!canRedo()) {
165  caption = "Redo";
166  }
167  // only set caption on menu item
168  if (dynamic_cast<FXMenuCommand*>(sender)) {
169  sender->handle(this, FXSEL(SEL_COMMAND, FXMenuCaption::ID_SETSTRINGVALUE), (void*)&caption);
170  }
171  return 1;
172 }
173 
174 
175 bool
177  return myCommandGroups.size() != 0;
178 }
179 
180 
181 GNEUndoList::CommandGroup::CommandGroup(std::string description) :
182  myDescription(description) {
183 }
184 
185 
186 const std::string&
188  return myDescription;
189 }
190 
191 
192 FXString
194  return ("Undo " + myDescription).c_str();
195 }
196 
197 
198 FXString
200  return ("Redo " + myDescription).c_str();
201 }
FXDEFMAP(GNEUndoList) GNEUndoListMap[]
The main window of the Netedit.
FXString undoName() const
get undo Name
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
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
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
bool hasCommandGroup() const
Check if undoList has command group.
CommandGroup(std::string description)
Constructor.
FXString redoName() const
get redo name
class CommandGroup
Definition: GNEUndoList.h:107
const std::string & getDescription()
get description
long p_onUpdUndo(FXObject *, FXSelector, void *)