Skip to content

Commit 9d2c0d5

Browse files
committed
tiny prototype of how undo stack could work
1 parent 261cfab commit 9d2c0d5

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/CLGui/CLParameter.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import CLProject as clp
22
from qtpy.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout, QLabel, QLineEdit, QComboBox, QDoubleSpinBox, QAbstractSpinBox, QPushButton, QFileDialog, QCheckBox
33
import numpy as np
4+
from CLGui import undo_stack
45

56
# collection of combination classes for displaying and entering configuration parameters
67
# typically a label on the left, text box or similar element in the middle, and sometimes a unit label or dropdown on the right
@@ -95,9 +96,11 @@ def valueChanged(new_val): # can also catch textChanged. textChanged and valueCh
9596
new_val = int(round(new_val))
9697
#self.spin_box.setValue(self.value) # fires an extra callback, call self.set_value() instead
9798
self.set_value(min(max(new_val, self.min), self.max)) # update if rounded or changed to min/max
98-
if not self.update_callback is None:
99+
if self.update_callback is not None:
99100
self.update_callback(self.value)
101+
undo_stack.push(self.undo_redo, self.last_value)
100102
self.last_value = self.value
103+
101104
self.spin_box.valueChanged.connect(valueChanged)
102105

103106
# Only add a unit label if the unit is specified
@@ -148,6 +151,12 @@ def set_numtype(self, new_type):
148151
self.spin_box.setDecimals(0)
149152
# default step value should still be 1
150153

154+
def undo_redo(self, undo_redo, value):
155+
self.set_value(value)
156+
self.last_value = value
157+
if self.update_callback is not None:
158+
self.update_callback(self.value)
159+
151160

152161
class CLParamDropdown(QWidget):
153162
def __init__(self, label_text, item_list, unit=None, editable=False):

src/CLGui/MainWindow.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import CLProject as clp
22
from qtpy.QtWidgets import QMainWindow, QTabWidget, QTabBar, QGridLayout, QWidget, QApplication, QFileDialog, QErrorMessage, QMessageBox, QDialog, QDialogButtonBox, QVBoxLayout, QProxyStyle, QStyle, QLabel, QSizePolicy
33
from qtpy.QtGui import QAction, QIcon, QPalette, QKeySequence, QPixmap
4-
from CLGui import ChirpTab, CLParamDropdown, CLParameter, QHSeparator
4+
from CLGui import ChirpTab, CLParamDropdown, CLParameter, QHSeparator, undo_stack
55
from CLMeasurements import init_measurements, is_valid_measurement_name
66
from CLAnalysis import generate_stimulus
77
from pathlib import Path
@@ -161,6 +161,15 @@ def filterSelected(filter_string):
161161
file_menu.addAction(quit_action)
162162
quit_action.triggered.connect(self.close)
163163

164+
165+
edit_menu = menubar.addMenu(' &Edit ')
166+
edit_menu.setStyle(MenuProxyStyle(edit_menu.style()))
167+
168+
undo = QAction('Undo', self, shortcut=QKeySequence('Ctrl+Z'))
169+
edit_menu.addAction(undo)
170+
undo.triggered.connect(undo_stack.undo)
171+
#todo: initialize disabled, add handle for undo stack to enable/disable undo/redo
172+
164173

165174
measurement_menu = menubar.addMenu(' &Measurement')
166175
file_menu.setStyle(MenuProxyStyle(file_menu.style()))

src/CLGui/Undo.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
class UndoStack():
4+
def __init__(self):
5+
self.history = []
6+
self.index = None
7+
8+
def push(self, callback, value):
9+
self.history.append([callback, value])
10+
if self.index is None:
11+
self.index = 0
12+
else:
13+
self.index += 1
14+
15+
def undo(self):
16+
self.history[self.index][0]('undo', self.history[self.index][1])
17+
self.index -= 1
18+
19+
20+
undo_stack = UndoStack()

src/CLGui/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def __init__(self, parent=None):
3232

3333

3434

35+
from CLGui.Undo import undo_stack
3536
from CLGui.CLTab import CLTab
3637
from CLGui.CLParameter import CLParameter, CLParamNum, CLParamDropdown, CLParamFile, FreqPointsParams
3738
from CLGui.QCollapsible.QCollapsible import QCollapsible

0 commit comments

Comments
 (0)