为 38 个公共头文件补齐/规范化 Doxygen 注释: - 新增 @file/@ingroup 文件头与模块分组标注 - 函数/方法统一 @brief/@param/@return/@pre/@see 中文注释 - 枚举值与结构体字段补齐 ///< 行尾注释 - 修正 tolerance.h 模块归属(foundation → brep) - 修复 large_assembly.h 注释错位,补 fastener_library.h 四函数注释
173 lines
5.1 KiB
C++
173 lines
5.1 KiB
C++
#pragma once
|
||
/**
|
||
* @file undo_stack.h
|
||
* @brief 撤销/重做栈 (E4d) — vde::core::UndoStack
|
||
*
|
||
* VDE-583 §4 撤销/重做基础设施。在现有 vde::core::Command 基类
|
||
* (transaction.h)之上提供标准命令栈:
|
||
* - push:执行命令并入栈(可与栈顶合并),自动截断重做分支
|
||
* - undo / redo:撤销/重做,支持 undo_text / redo_text
|
||
* - 干净状态:set_clean / is_clean(用于"未保存修改"提示)
|
||
* - 宏:begin_macro / end_macro 将一组命令折叠为一个撤销单元
|
||
* - 深度限制:set_max_undo_levels(0 = 无限)
|
||
*
|
||
* @code{.cpp}
|
||
* UndoStack stack;
|
||
* stack.push(make_command([]{ x = 5; }, []{ x = 0; }, "set x=5"));
|
||
* stack.undo(); // x 恢复
|
||
* stack.redo(); // x 再次 = 5
|
||
* @endcode
|
||
*
|
||
* @ingroup core
|
||
*/
|
||
|
||
#include "vde/core/transaction.h"
|
||
#include <memory>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
namespace vde::core {
|
||
|
||
/**
|
||
* @brief 撤销/重做栈(VDE-583 §4)
|
||
*
|
||
* 语义对齐 Qt QUndoStack:push 立即执行并压栈,undo/redo 在
|
||
* 栈上移动当前索引;push 新命令时清空重做分支。
|
||
*/
|
||
class UndoStack {
|
||
public:
|
||
/**
|
||
* @brief 设置最大撤销深度
|
||
*
|
||
* 0 表示无限。超出限制时从最旧命令开始裁剪。
|
||
* @param max 最大撤销深度(0 = 无限)
|
||
*/
|
||
void set_max_undo_levels(int max);
|
||
|
||
/**
|
||
* @brief 获取当前最大撤销深度
|
||
* @return 最大撤销深度(0 = 无限)
|
||
*/
|
||
[[nodiscard]] int max_undo_levels() const { return max_levels_; }
|
||
|
||
/**
|
||
* @brief 执行命令并压入撤销栈
|
||
*
|
||
* 若栈顶命令 merge() 返回 true,则本命令被栈顶吸收(不新增条目)。
|
||
* 宏录制期间仅记录(不压栈),由 end_macro 折叠为一个撤销单元。
|
||
* @param cmd 要执行的命令(可能被移动)
|
||
* @return 命令非空时成功
|
||
*/
|
||
bool push(std::unique_ptr<Command> cmd);
|
||
|
||
/**
|
||
* @brief 撤销最近一步
|
||
* @return 撤销成功返回 true;无可撤销步骤时返回 false
|
||
*/
|
||
bool undo();
|
||
|
||
/**
|
||
* @brief 重做最近撤销的一步
|
||
* @return 重做成功返回 true;无可重做步骤时返回 false
|
||
*/
|
||
bool redo();
|
||
|
||
/**
|
||
* @brief 判断是否可撤销
|
||
* @return 存在可撤销命令时返回 true
|
||
*/
|
||
[[nodiscard]] bool can_undo() const { return current_ >= 0; }
|
||
|
||
/**
|
||
* @brief 判断是否可重做
|
||
* @return 存在可重做命令时返回 true
|
||
*/
|
||
[[nodiscard]] bool can_redo() const {
|
||
return current_ + 1 < static_cast<int>(stack_.size());
|
||
}
|
||
|
||
/**
|
||
* @brief 获取下一步撤销命令的描述
|
||
* @return 描述文本;无可撤销命令时返回空串
|
||
*/
|
||
[[nodiscard]] std::string undo_text() const;
|
||
|
||
/**
|
||
* @brief 获取下一步重做命令的描述
|
||
* @return 描述文本;无可重做命令时返回空串
|
||
*/
|
||
[[nodiscard]] std::string redo_text() const;
|
||
|
||
/**
|
||
* @brief 清空全部历史(含未结束的宏)
|
||
*/
|
||
void clear();
|
||
|
||
/**
|
||
* @brief 将当前位置标记为"干净"(保存点)
|
||
*
|
||
* 用于"未保存修改"提示:undo/redo 离开该位置后 is_clean() 返回 false。
|
||
*/
|
||
void set_clean();
|
||
|
||
/**
|
||
* @brief 判断当前位置是否为干净标记(无未保存修改)
|
||
* @return 当前位置等于干净标记时返回 true
|
||
*/
|
||
[[nodiscard]] bool is_clean() const {
|
||
return has_clean_ && clean_index_ == current_;
|
||
}
|
||
|
||
/**
|
||
* @brief 开始宏录制
|
||
*
|
||
* 此后的 push 命令被记录而不入栈,end_macro 时折叠为一个撤销单元。
|
||
* @param desc 宏的撤销/重做描述
|
||
*/
|
||
void begin_macro(const std::string& desc);
|
||
|
||
/**
|
||
* @brief 结束宏录制并压入一个 MacroCommand
|
||
*
|
||
* 宏命令本身不再执行(其子命令已在录制期间执行)。
|
||
*/
|
||
void end_macro();
|
||
|
||
/**
|
||
* @brief 判断当前是否处于宏录制中
|
||
* @return 宏录制进行中返回 true
|
||
*/
|
||
[[nodiscard]] bool is_macro_active() const { return macro_active_; }
|
||
|
||
/**
|
||
* @brief 获取栈上命令总数
|
||
* @return 命令条目数量
|
||
*/
|
||
[[nodiscard]] size_t count() const { return stack_.size(); }
|
||
|
||
/**
|
||
* @brief 获取当前索引
|
||
* @return 当前索引(-1 = 初始空状态;0 = 已应用第 1 条)
|
||
*/
|
||
[[nodiscard]] int index() const { return current_; }
|
||
|
||
private:
|
||
/// 内部压栈(already_executed 用于宏:子命令已在录制时执行)
|
||
void push_impl(std::unique_ptr<Command> cmd, bool already_executed);
|
||
|
||
/// 按 max_levels_ 裁剪最旧命令
|
||
void trim();
|
||
|
||
std::vector<std::unique_ptr<Command>> stack_;
|
||
int current_ = -1; ///< 已应用的最新命令下标(-1 = 初始空状态)
|
||
int clean_index_ = -1; ///< 干净标记对应下标(-1 = 初始空状态)
|
||
bool has_clean_ = true; ///< 干净标记是否仍有效(被裁剪后失效)
|
||
int max_levels_ = 0; ///< 0 = 无限
|
||
|
||
bool macro_active_ = false;
|
||
std::string macro_desc_;
|
||
std::vector<std::unique_ptr<Command>> macro_cmds_;
|
||
};
|
||
|
||
} // namespace vde::core
|