refactor(undo): de-Qt oakundo and wrap it in a pure C ABI

- strip QAbstractItemModel/QAction/signals-slots from UndoStack (UI
  concerns belong to the app layer), index_changed becomes a
  std::function callback
- decouple UndoCommand from Project via an optional modified-flag
  callback pair
- add pure C ABI in include/undo + src/undo/c_api (oakundo_ prefix,
  vtable-based command wrapper, OAKUNDO_E_* error codes)
- fix three engine-side defects: jump(0) infinite loop,
  MultiUndoCommand child leak, push_pre_executed not undoable
- add gtest suites (22 cases) and a standalone build driver
This commit is contained in:
2026-08-05 18:02:24 +08:00
parent bad52feda3
commit c50017127b
21 changed files with 2307 additions and 2 deletions
+4
View File
@@ -0,0 +1,4 @@
target_sources(oakundo PRIVATE
undocommand.cpp
undostack.cpp
)
+41
View File
@@ -0,0 +1,41 @@
/***
Oak Video Editor - Non-Linear Video Editor
Copyright (C) 2026 Oak Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef OAK_UNDO_COMMANDHANDLE_H
#define OAK_UNDO_COMMANDHANDLE_H
#include "undo/undocommand.h"
#include "../src/undocommand.h"
/**
* @brief Internal layout of the OakUndoCommand handle, shared between the
* c_api translation units.
*
* `owned` is true for handles created by oakundo_command_init() /
* oakundo_command_init_multi() and false for borrowed wrappers handed out
* by oakundo_command_multi_child().
*/
struct OakUndoCommand {
olive::UndoCommand *command;
bool owned;
};
#endif // OAK_UNDO_COMMANDHANDLE_H
+217
View File
@@ -0,0 +1,217 @@
/***
Oak Video Editor - Non-Linear Video Editor
Copyright (C) 2026 Oak Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "undo/undocommand.h"
#include <new>
#include "commandhandle.h"
namespace
{
/**
* @brief olive::UndoCommand subclass forwarding to a C callback table.
*/
class CallbackUndoCommand : public olive::UndoCommand {
public:
CallbackUndoCommand(const OakUndoCommandVtable &vtable, void *userdata)
: vtable_(vtable), userdata_(userdata)
{
}
virtual ~CallbackUndoCommand() override
{
if (vtable_.free_fn) {
vtable_.free_fn(userdata_);
}
}
protected:
virtual void redo() override
{
if (vtable_.redo) {
vtable_.redo(userdata_);
}
}
virtual void undo() override
{
if (vtable_.undo) {
vtable_.undo(userdata_);
}
}
private:
OakUndoCommandVtable vtable_;
void *userdata_;
};
}
OakUndoCommand *oakundo_command_init(const OakUndoCommandVtable *vtable,
void *userdata)
{
if (!vtable) {
return NULL;
}
try {
OakUndoCommand *handle = new (std::nothrow) OakUndoCommand();
if (!handle) {
return NULL;
}
handle->command = new CallbackUndoCommand(*vtable, userdata);
handle->owned = true;
return handle;
} catch (...) {
return NULL;
}
}
OakUndoCommand *oakundo_command_init_multi(void)
{
try {
OakUndoCommand *handle = new (std::nothrow) OakUndoCommand();
if (!handle) {
return NULL;
}
handle->command = new olive::MultiUndoCommand();
handle->owned = true;
return handle;
} catch (...) {
return NULL;
}
}
int oakundo_command_multi_add_child(OakUndoCommand *multi,
OakUndoCommand *child)
{
if (!multi || !multi->command || !child || !child->command) {
return OAKUNDO_E_INVALID;
}
olive::MultiUndoCommand *mcu =
dynamic_cast<olive::MultiUndoCommand *>(multi->command);
if (!mcu) {
return OAKUNDO_E_INVALID;
}
try {
mcu->add_child(child->command);
// Ownership of the underlying command moved to the multi command;
// consume the wrapper.
delete child;
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_command_multi_child_count(OakUndoCommand *multi, int *out_count)
{
if (!multi || !multi->command || !out_count) {
return OAKUNDO_E_INVALID;
}
olive::MultiUndoCommand *mcu =
dynamic_cast<olive::MultiUndoCommand *>(multi->command);
if (!mcu) {
return OAKUNDO_E_INVALID;
}
try {
*out_count = mcu->child_count();
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_command_multi_child(OakUndoCommand *multi, int index,
OakUndoCommand **out_child)
{
if (!multi || !multi->command || !out_child) {
return OAKUNDO_E_INVALID;
}
olive::MultiUndoCommand *mcu =
dynamic_cast<olive::MultiUndoCommand *>(multi->command);
if (!mcu) {
return OAKUNDO_E_INVALID;
}
try {
if (index < 0 || index >= mcu->child_count()) {
return OAKUNDO_E_NOT_FOUND;
}
OakUndoCommand *handle = new (std::nothrow) OakUndoCommand();
if (!handle) {
return OAKUNDO_E_NOMEM;
}
handle->command = mcu->child(index);
handle->owned = false;
*out_child = handle;
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_command_redo_now(OakUndoCommand *command)
{
if (!command || !command->command) {
return OAKUNDO_E_INVALID;
}
try {
command->command->redo_now();
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_command_undo_now(OakUndoCommand *command)
{
if (!command || !command->command) {
return OAKUNDO_E_INVALID;
}
try {
command->command->undo_now();
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
void oakundo_command_free(OakUndoCommand *command)
{
if (!command) {
return;
}
if (command->owned) {
delete command->command;
}
delete command;
}
+242
View File
@@ -0,0 +1,242 @@
/***
Oak Video Editor - Non-Linear Video Editor
Copyright (C) 2026 Oak Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "undo/undostack.h"
#include <cstring>
#include <new>
#include "../src/undostack.h"
#include "commandhandle.h"
struct OakUndoStack {
olive::UndoStack impl;
};
OakUndoStack *oakundo_undostack_init(void)
{
try {
return new (std::nothrow) OakUndoStack();
} catch (...) {
return NULL;
}
}
void oakundo_undostack_free(OakUndoStack *stack)
{
delete stack;
}
int oakundo_undostack_push(OakUndoStack *stack, OakUndoCommand *command,
const char *name)
{
if (!stack || !command || !command->command) {
return OAKUNDO_E_INVALID;
}
try {
olive::UndoCommand *impl = command->command;
stack->impl.push(impl, name ? name : "");
// Ownership of the underlying command moved to the stack (or was
// deleted as an empty multi command); consume the wrapper.
delete command;
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_undostack_push_pre_executed(OakUndoStack *stack,
OakUndoCommand *command,
const char *name)
{
if (!stack || !command || !command->command) {
return OAKUNDO_E_INVALID;
}
try {
olive::UndoCommand *impl = command->command;
stack->impl.push_pre_executed(impl, name ? name : "");
delete command;
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_undostack_undo(OakUndoStack *stack)
{
if (!stack) {
return OAKUNDO_E_INVALID;
}
try {
stack->impl.undo();
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_undostack_redo(OakUndoStack *stack)
{
if (!stack) {
return OAKUNDO_E_INVALID;
}
try {
stack->impl.redo();
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_undostack_jump(OakUndoStack *stack, int64_t index)
{
if (!stack) {
return OAKUNDO_E_INVALID;
}
try {
stack->impl.jump(index < 0 ? 0 : static_cast<size_t>(index));
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_undostack_clear(OakUndoStack *stack)
{
if (!stack) {
return OAKUNDO_E_INVALID;
}
try {
stack->impl.clear();
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_undostack_can_undo(OakUndoStack *stack, int *out_value)
{
if (!stack || !out_value) {
return OAKUNDO_E_INVALID;
}
try {
*out_value = stack->impl.can_undo() ? 1 : 0;
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_undostack_can_redo(OakUndoStack *stack, int *out_value)
{
if (!stack || !out_value) {
return OAKUNDO_E_INVALID;
}
try {
*out_value = stack->impl.can_redo() ? 1 : 0;
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_undostack_count(OakUndoStack *stack, int64_t *out_count)
{
if (!stack || !out_count) {
return OAKUNDO_E_INVALID;
}
try {
*out_count = stack->impl.command_count();
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_undostack_index(OakUndoStack *stack, int64_t *out_index)
{
if (!stack || !out_index) {
return OAKUNDO_E_INVALID;
}
try {
*out_index = stack->impl.done_count();
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_undostack_command_text(OakUndoStack *stack, int64_t row,
char *buf, int buf_size)
{
if (!stack) {
return OAKUNDO_E_INVALID;
}
try {
if (row < 0 || row >= stack->impl.command_count()) {
return OAKUNDO_E_NOT_FOUND;
}
std::string name = stack->impl.command_name(int(row));
int required = static_cast<int>(name.size()) + 1;
if (buf && buf_size > 0) {
size_t copy_len = name.size();
if (copy_len > static_cast<size_t>(buf_size) - 1) {
copy_len = static_cast<size_t>(buf_size) - 1;
}
memcpy(buf, name.data(), copy_len);
buf[copy_len] = '\0';
}
return required;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}
int oakundo_undostack_command_is_done(OakUndoStack *stack, int64_t row,
int *out_value)
{
if (!stack || !out_value) {
return OAKUNDO_E_INVALID;
}
try {
if (row < 0 || row >= stack->impl.command_count()) {
return OAKUNDO_E_NOT_FOUND;
}
*out_value = stack->impl.command_is_done(int(row)) ? 1 : 0;
return OAKUNDO_OK;
} catch (...) {
return OAKUNDO_E_FAILED;
}
}