NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
historykeeper.h
1 #ifndef HISTORYKEEPER_H
2 #define HISTORYKEEPER_H
3 
4 // Class which handles recording of history of changes of some variable
5 // In order to use it - call "valueChanged(double newValue)" every time your variable changes its value
6 // Call "double rollBack()" to do "Ctrl+z". You will get previous recorded value of variable
7 // Author: Dmytro Pavlichenko <dm.mark999@gmail.com>
8 
9 #include <QObject>
10 #include <QStack>
11 #include <QElapsedTimer>
12 #include <QTimer>
13 #include <QEvent>
14 #include <QKeyEvent>
15 
16 #include <QSlider>
17 #include <QDoubleSpinBox>
18 
19 class HistoryKeeper : public QObject
20 {
21  Q_OBJECT
22 public:
23  HistoryKeeper(unsigned int maxStackSize_, double minElapsedTime_);
24  HistoryKeeper(unsigned int maxStackSize_, double minElapsedTime_, QDoubleSpinBox *spin_);
25  HistoryKeeper(unsigned int maxStackSize_, double minElapsedTime_, QDoubleSpinBox *spin_, QSlider *slider_);
26  ~HistoryKeeper();
27 
28  void setRecorded(bool recorded);
29 
30  void clearHistory();
31  void valueChanged(double newValue);
32  double rollBack(); // Returns previously recorded value. If there is no recorded value - returns -999
33  double rollForward();
34 
35  virtual bool eventFilter(QObject *object, QEvent *event);
36 
37 Q_SIGNALS:
38 
39 private Q_SLOTS:
40  void loop(); // Main loop. Is called every 0.2 seconds
41 
42 private:
43  void init(unsigned int maxStackSize_, double minElapsedTime_);
44 
45  void handleUndo();
46  void handleRedo();
47 
48 private:
49  QTimer *mainLoopTimer; // TODO: reimplement it without this instant loop
50  QElapsedTimer *elapsedTimer; // Counts elapsed time since last edit
51  double minElapsedTime; // Time which must be elapsed before recording new changes
52 
53  QStack<double> stack; // Stack to record history of changes
54  int maxStackSize; // How deep the history will be recorded
55  int currentPosition; // Current position in the stack of changes
56 
57  double currentValue;
58  bool wasRecorded; // True if currentValue was already stored into stack
59 
60  QDoubleSpinBox *spin;
61  QSlider *slider;
62  bool spinOnly;
63 };
64 
65 #endif // HISTORYKEEPER_H