소스 검색

UI: Add disable push/pop to undo/redo stack

This allows disabling and re-enabling in certain situations where these
actions doing so might end up being used in recursion.
jp9000 4 년 전
부모
커밋
1afe092c6e
2개의 변경된 파일39개의 추가작업 그리고 6개의 파일을 삭제
  1. 32 6
      UI/undo-stack-obs.cpp
  2. 7 0
      UI/undo-stack-obs.hpp

+ 32 - 6
UI/undo-stack-obs.cpp

@@ -64,7 +64,7 @@ void undo_stack::add_action(const QString &name, undo_redo_cb undo,
 
 void undo_stack::undo()
 {
-	if (undo_items.size() == 0 || !enabled)
+	if (undo_items.size() == 0 || !is_enabled())
 		return;
 
 	last_is_repeatable = false;
@@ -88,7 +88,7 @@ void undo_stack::undo()
 
 void undo_stack::redo()
 {
-	if (redo_items.size() == 0 || !enabled)
+	if (redo_items.size() == 0 || !is_enabled())
 		return;
 
 	last_is_repeatable = false;
@@ -110,9 +110,8 @@ void undo_stack::redo()
 	}
 }
 
-void undo_stack::enable()
+void undo_stack::enable_internal()
 {
-	enabled = true;
 	last_is_repeatable = false;
 
 	ui->actionMainUndo->setDisabled(false);
@@ -120,15 +119,42 @@ void undo_stack::enable()
 		ui->actionMainRedo->setDisabled(false);
 }
 
-void undo_stack::disable()
+void undo_stack::disable_internal()
 {
-	enabled = false;
 	last_is_repeatable = false;
 
 	ui->actionMainUndo->setDisabled(true);
 	ui->actionMainRedo->setDisabled(true);
 }
 
+void undo_stack::enable()
+{
+	enabled = true;
+	if (is_enabled())
+		enable_internal();
+}
+
+void undo_stack::disable()
+{
+	if (is_enabled())
+		disable_internal();
+	enabled = false;
+}
+
+void undo_stack::push_disabled()
+{
+	if (is_enabled())
+		disable_internal();
+	disable_refs++;
+}
+
+void undo_stack::pop_disabled()
+{
+	disable_refs--;
+	if (is_enabled())
+		enable_internal();
+}
+
 void undo_stack::clear_redo()
 {
 	redo_items.clear();

+ 7 - 0
UI/undo-stack-obs.hpp

@@ -29,11 +29,16 @@ class undo_stack : public QObject {
 	ui_ptr ui;
 	std::deque<undo_redo_t> undo_items;
 	std::deque<undo_redo_t> redo_items;
+	int disable_refs = 0;
 	bool enabled = true;
 	bool last_is_repeatable = false;
 
 	QTimer repeat_reset_timer;
 
+	inline bool is_enabled() const { return !disable_refs && enabled; }
+
+	void enable_internal();
+	void disable_internal();
 	void clear_redo();
 
 private slots:
@@ -44,6 +49,8 @@ public:
 
 	void enable();
 	void disable();
+	void push_disabled();
+	void pop_disabled();
 
 	void clear();
 	void add_action(const QString &name, undo_redo_cb undo,