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-2017 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 
44 // ===========================================================================
45 // FOX callback mapping
46 // ===========================================================================
47 FXDEFMAP(GNEUndoList) GNEUndoListMap[] = {
48  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_REVERT, FXUndoList::onCmdRevert),
49  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_UNDO, FXUndoList::onCmdUndo),
50  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_REDO, FXUndoList::onCmdRedo),
51  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_UNDO_ALL, FXUndoList::onCmdUndoAll),
52  //FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_REDO_ALL, FXUndoList::onCmdRedoAll),
53  //
54  //FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_UNDO_COUNT, FXUndoList::onUpdUndoCount),
55  //FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REDO_COUNT, FXUndoList::onUpdRedoCount),
56  //FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_CLEAR, FXUndoList::onUpdClear),
57  //FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REVERT, FXUndoList::onUpdRevert),
58  FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_UNDO_ALL, GNEUndoList::p_onUpdUndo),
59  FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REDO_ALL, GNEUndoList::p_onUpdRedo),
60  FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_UNDO, GNEUndoList::p_onUpdUndo),
61  FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REDO, GNEUndoList::p_onUpdRedo)
62 };
63 
64 
65 // ===========================================================================
66 // FOX-declarations
67 // ===========================================================================
68 FXIMPLEMENT_ABSTRACT(GNEUndoList, FXUndoList, GNEUndoListMap, ARRAYNUMBER(GNEUndoListMap))
69 
70 
71 // ===========================================================================
72 // member method definitions
73 // ===========================================================================
74 
76  FXUndoList(),
77  myParent(parent) {
78 }
79 
80 
81 void
82 GNEUndoList::p_begin(const std::string& description) {
83  myCommandGroups.push(new CommandGroup(description));
84  begin(myCommandGroups.top());
85 }
86 
87 
88 void
90  myCommandGroups.pop();
91  end();
92 }
93 
94 
95 void
97  p_abort();
98  clear();
99 }
100 
101 
102 void
104  while (hasCommandGroup()) {
105  myCommandGroups.top()->undo();
106  myCommandGroups.pop();
107  abort();
108  }
109 }
110 
111 
112 void
114  //std::cout << undoName().text() << "\n";
115  FXUndoList::undo();
117 }
118 
119 
120 void
122  //std::cout << redoName().text() << "\n";
123  FXUndoList::redo();
125 }
126 
127 
128 void
130  if (cmd->trueChange()) {
131  add(cmd, true);
132  } else {
133  delete cmd;
134  }
135 }
136 
137 long
138 GNEUndoList::p_onUpdUndo(FXObject* sender, FXSelector, void*) {
139  bool enable = canUndo() && !hasCommandGroup();
140  sender->handle(this, enable ? FXSEL(SEL_COMMAND, FXWindow::ID_ENABLE) : FXSEL(SEL_COMMAND, FXWindow::ID_DISABLE), 0);
141  FXString caption = undoName();
142  if (hasCommandGroup()) {
143  caption = ("Cannot Undo in the middle of " + myCommandGroups.top()->getDescription()).c_str();
144  } else if (!canUndo()) {
145  caption = "Undo";
146  }
147  // only set caption on menu item
148  if (dynamic_cast<FXMenuCommand*>(sender)) {
149  sender->handle(this, FXSEL(SEL_COMMAND, FXMenuCaption::ID_SETSTRINGVALUE), (void*)&caption);
150  }
151  return 1;
152 }
153 
154 
155 long
156 GNEUndoList::p_onUpdRedo(FXObject* sender, FXSelector, void*) {
157  bool enable = canRedo() && !hasCommandGroup();
158  sender->handle(this, enable ? FXSEL(SEL_COMMAND, FXWindow::ID_ENABLE) : FXSEL(SEL_COMMAND, FXWindow::ID_DISABLE), 0);
159  FXString caption = redoName();
160  if (hasCommandGroup()) {
161  caption = ("Cannot Redo in the middle of " + myCommandGroups.top()->getDescription()).c_str();
162  } else if (!canRedo()) {
163  caption = "Redo";
164  }
165  // only set caption on menu item
166  if (dynamic_cast<FXMenuCommand*>(sender)) {
167  sender->handle(this, FXSEL(SEL_COMMAND, FXMenuCaption::ID_SETSTRINGVALUE), (void*)&caption);
168  }
169  return 1;
170 }
171 
172 
173 bool
175  return myCommandGroups.size() != 0;
176 }
177 
178 
179 GNEUndoList::CommandGroup::CommandGroup(std::string description) :
180  myDescription(description) {
181 }
182 
183 
184 const std::string&
186  return myDescription;
187 }
188 
189 
190 FXString
192  return ("Undo " + myDescription).c_str();
193 }
194 
195 
196 FXString
198  return ("Redo " + myDescription).c_str();
199 }
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:82
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)
Definition: GNEUndoList.cpp:96
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:89
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 *)