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