change: change code style to Linux style except indent.

This commit is contained in:
Mike Solar
2025-08-03 03:09:40 +08:00
parent 65ab76edc8
commit 74f73ab3be
789 changed files with 77113 additions and 68888 deletions
+32 -31
View File
@@ -22,68 +22,69 @@
#include "core.h"
namespace olive {
namespace olive
{
void MultiUndoCommand::redo()
{
for (auto it=children_.cbegin(); it!=children_.cend(); it++) {
(*it)->redo_and_set_modified();
}
for (auto it = children_.cbegin(); it != children_.cend(); it++) {
(*it)->redo_and_set_modified();
}
}
void MultiUndoCommand::undo()
{
for (auto it=children_.crbegin(); it!=children_.crend(); it++) {
(*it)->undo_and_set_modified();
}
for (auto it = children_.crbegin(); it != children_.crend(); it++) {
(*it)->undo_and_set_modified();
}
}
UndoCommand::UndoCommand()
{
prepared_ = false;
done_ = false;
prepared_ = false;
done_ = false;
}
void UndoCommand::redo_and_set_modified()
{
project_ = GetRelevantProject();
project_ = GetRelevantProject();
redo_now();
redo_now();
if (project_) {
modified_ = project_->is_modified();
project_->set_modified(true);
}
if (project_) {
modified_ = project_->is_modified();
project_->set_modified(true);
}
}
void UndoCommand::undo_and_set_modified()
{
undo_now();
undo_now();
if (project_) {
project_->set_modified(modified_);
}
if (project_) {
project_->set_modified(modified_);
}
}
void UndoCommand::redo_now()
{
if (!done_) {
if (!prepared_) {
prepare();
prepared_ = true;
}
if (!done_) {
if (!prepared_) {
prepare();
prepared_ = true;
}
redo();
done_ = true;
}
redo();
done_ = true;
}
}
void UndoCommand::undo_now()
{
if (done_) {
undo();
done_ = false;
}
if (done_) {
undo();
done_ = false;
}
}
}
+51 -44
View File
@@ -27,78 +27,85 @@
#include "common/define.h"
namespace olive {
namespace olive
{
class Project;
class UndoCommand
{
class UndoCommand {
public:
UndoCommand();
UndoCommand();
virtual ~UndoCommand(){}
virtual ~UndoCommand()
{
}
DISABLE_COPY_MOVE(UndoCommand)
DISABLE_COPY_MOVE(UndoCommand)
bool has_prepared() const {return prepared_;}
void set_prepared(bool e) {prepared_ = true;}
bool has_prepared() const
{
return prepared_;
}
void set_prepared(bool e)
{
prepared_ = true;
}
void redo_now();
void undo_now();
void redo_now();
void undo_now();
void redo_and_set_modified();
void undo_and_set_modified();
void redo_and_set_modified();
void undo_and_set_modified();
virtual Project* GetRelevantProject() const = 0;
virtual Project *GetRelevantProject() const = 0;
protected:
virtual void prepare(){}
virtual void redo() = 0;
virtual void undo() = 0;
virtual void prepare()
{
}
virtual void redo() = 0;
virtual void undo() = 0;
private:
bool modified_;
bool modified_;
Project* project_;
Project *project_;
bool prepared_;
bool done_;
bool prepared_;
bool done_;
};
class MultiUndoCommand : public UndoCommand
{
class MultiUndoCommand : public UndoCommand {
public:
MultiUndoCommand() = default;
MultiUndoCommand() = default;
virtual Project* GetRelevantProject() const override
{
return nullptr;
}
virtual Project *GetRelevantProject() const override
{
return nullptr;
}
void add_child(UndoCommand* command)
{
children_.push_back(command);
}
void add_child(UndoCommand *command)
{
children_.push_back(command);
}
int child_count() const
{
return children_.size();
}
int child_count() const
{
return children_.size();
}
UndoCommand* child(int i) const
{
return children_[i];
}
UndoCommand *child(int i) const
{
return children_[i];
}
protected:
virtual void redo() override;
virtual void undo() override;
virtual void redo() override;
virtual void undo() override;
private:
std::vector<UndoCommand*> children_;
std::vector<UndoCommand *> children_;
};
}
+153 -136
View File
@@ -22,232 +22,249 @@
#include <QCoreApplication>
namespace olive {
namespace olive
{
const int UndoStack::kMaxUndoCommands = 200;
class EmptyCommand : public UndoCommand
{
class EmptyCommand : public UndoCommand {
public:
EmptyCommand(){}
EmptyCommand()
{
}
virtual Project* GetRelevantProject() const override {return nullptr;}
virtual Project *GetRelevantProject() const override
{
return nullptr;
}
protected:
virtual void redo() override {}
virtual void undo() override {}
virtual void redo() override
{
}
virtual void undo() override
{
}
};
UndoStack::UndoStack()
{
undo_action_ = new QAction();
connect(undo_action_, &QAction::triggered, this, &UndoStack::undo);
undo_action_ = new QAction();
connect(undo_action_, &QAction::triggered, this, &UndoStack::undo);
redo_action_ = new QAction();
connect(redo_action_, &QAction::triggered, this, &UndoStack::redo);
redo_action_ = new QAction();
connect(redo_action_, &QAction::triggered, this, &UndoStack::redo);
clear();
UpdateActions();
clear();
UpdateActions();
}
UndoStack::~UndoStack()
{
clear();
clear();
delete undo_action_;
delete redo_action_;
delete undo_action_;
delete redo_action_;
}
void UndoStack::push(UndoCommand *command, const QString &name)
{
MultiUndoCommand *mcu = dynamic_cast<MultiUndoCommand*>(command);
if (mcu && mcu->child_count() == 0) {
delete command;
return;
}
MultiUndoCommand *mcu = dynamic_cast<MultiUndoCommand *>(command);
if (mcu && mcu->child_count() == 0) {
delete command;
return;
}
// Clear any redoable commands
this->beginRemoveRows(QModelIndex(), commands_.size(), commands_.size() + undone_commands_.size());
if (CanRedo()) {
for (auto it=undone_commands_.cbegin(); it!=undone_commands_.cend(); it++) {
delete (*it).command;
}
undone_commands_.clear();
}
this->endRemoveRows();
// Clear any redoable commands
this->beginRemoveRows(QModelIndex(), commands_.size(),
commands_.size() + undone_commands_.size());
if (CanRedo()) {
for (auto it = undone_commands_.cbegin(); it != undone_commands_.cend();
it++) {
delete (*it).command;
}
undone_commands_.clear();
}
this->endRemoveRows();
// Do command and push
this->beginInsertRows(QModelIndex(), commands_.size(), commands_.size());
command->redo_and_set_modified();
commands_.push_back({command, name});
this->endInsertRows();
// Do command and push
this->beginInsertRows(QModelIndex(), commands_.size(), commands_.size());
command->redo_and_set_modified();
commands_.push_back({ command, name });
this->endInsertRows();
// Delete oldest
if (commands_.size() > kMaxUndoCommands) {
this->beginRemoveRows(QModelIndex(), 0, 0);
delete commands_.front().command;
commands_.pop_front();
this->endRemoveRows();
}
// Delete oldest
if (commands_.size() > kMaxUndoCommands) {
this->beginRemoveRows(QModelIndex(), 0, 0);
delete commands_.front().command;
commands_.pop_front();
this->endRemoveRows();
}
UpdateActions();
UpdateActions();
}
void UndoStack::jump(size_t index)
{
while (commands_.size() > index) {
undo();
}
while (commands_.size() < index) {
redo();
}
while (commands_.size() > index) {
undo();
}
while (commands_.size() < index) {
redo();
}
}
void UndoStack::undo()
{
if (CanUndo()) {
// Undo most recently done command
commands_.back().command->undo_and_set_modified();
if (CanUndo()) {
// Undo most recently done command
commands_.back().command->undo_and_set_modified();
// Place at the front of the "undone commands" list
undone_commands_.push_front(commands_.back());
// Place at the front of the "undone commands" list
undone_commands_.push_front(commands_.back());
// Remove undone command from the commands list
commands_.pop_back();
// Remove undone command from the commands list
commands_.pop_back();
// Update actions
UpdateActions();
}
// Update actions
UpdateActions();
}
}
void UndoStack::redo()
{
if (CanRedo()) {
// Redo most recently undone command
undone_commands_.front().command->redo_and_set_modified();
if (CanRedo()) {
// Redo most recently undone command
undone_commands_.front().command->redo_and_set_modified();
// Place at the back of the done commands list
commands_.push_back(undone_commands_.front());
// Place at the back of the done commands list
commands_.push_back(undone_commands_.front());
// Remove done command from undone list
undone_commands_.pop_front();
// Remove done command from undone list
undone_commands_.pop_front();
// Update actions
UpdateActions();
}
// Update actions
UpdateActions();
}
}
void UndoStack::clear()
{
this->beginResetModel();
this->beginResetModel();
for (auto it=commands_.cbegin(); it!=commands_.cend(); it++) {
delete (*it).command;
}
commands_.clear();
for (auto it=undone_commands_.cbegin(); it!=undone_commands_.cend(); it++) {
delete (*it).command;
}
undone_commands_.clear();
for (auto it = commands_.cbegin(); it != commands_.cend(); it++) {
delete (*it).command;
}
commands_.clear();
for (auto it = undone_commands_.cbegin(); it != undone_commands_.cend();
it++) {
delete (*it).command;
}
undone_commands_.clear();
this->endResetModel();
this->endResetModel();
push(new EmptyCommand(), tr("New/Open Project"));
push(new EmptyCommand(), tr("New/Open Project"));
}
bool UndoStack::CanUndo() const
{
return !commands_.empty() && !dynamic_cast<EmptyCommand*>(commands_.back().command);
return !commands_.empty() &&
!dynamic_cast<EmptyCommand *>(commands_.back().command);
}
void UndoStack::UpdateActions()
{
undo_action_->setEnabled(CanUndo());
redo_action_->setEnabled(CanRedo());
undo_action_->setEnabled(CanUndo());
redo_action_->setEnabled(CanRedo());
undo_action_->setText(QCoreApplication::translate("UndoStack", "Undo %1").arg(CanUndo() ? commands_.back().name : QString()));
redo_action_->setText(QCoreApplication::translate("UndoStack", "Redo %1").arg(CanRedo() ? undone_commands_.front().name : QString()));
undo_action_->setText(
QCoreApplication::translate("UndoStack", "Undo %1")
.arg(CanUndo() ? commands_.back().name : QString()));
redo_action_->setText(
QCoreApplication::translate("UndoStack", "Redo %1")
.arg(CanRedo() ? undone_commands_.front().name : QString()));
emit indexChanged(commands_.size());
emit indexChanged(commands_.size());
}
int UndoStack::columnCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return 2;
if (parent.isValid()) {
return 0;
}
return 2;
}
QVariant UndoStack::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole) {
switch (index.column()) {
case 0:
return index.row() + 1;
case 1:
{
std::list<CommandEntry>::const_iterator it;
size_t real_index = index.row();
if (real_index < commands_.size()) {
it = commands_.cbegin();
} else {
real_index -= commands_.size();
it = undone_commands_.cbegin();
}
for (size_t i = 0; i < real_index; i++) {
it++;
}
const QString &name = (*it).name;
return (name.isEmpty()) ? tr("Command") : name;
}
}
} else if (role == Qt::ForegroundRole) {
if (size_t(index.row()) >= commands_.size()) {
return QVariant(QColor(Qt::gray));
}
}
if (role == Qt::DisplayRole) {
switch (index.column()) {
case 0:
return index.row() + 1;
case 1: {
std::list<CommandEntry>::const_iterator it;
size_t real_index = index.row();
if (real_index < commands_.size()) {
it = commands_.cbegin();
} else {
real_index -= commands_.size();
it = undone_commands_.cbegin();
}
for (size_t i = 0; i < real_index; i++) {
it++;
}
const QString &name = (*it).name;
return (name.isEmpty()) ? tr("Command") : name;
}
}
} else if (role == Qt::ForegroundRole) {
if (size_t(index.row()) >= commands_.size()) {
return QVariant(QColor(Qt::gray));
}
}
return QVariant();
return QVariant();
}
QModelIndex UndoStack::index(int row, int column, const QModelIndex &parent) const
QModelIndex UndoStack::index(int row, int column,
const QModelIndex &parent) const
{
return createIndex(row, column, nullptr);
return createIndex(row, column, nullptr);
}
QModelIndex UndoStack::parent(const QModelIndex &index) const
{
return QModelIndex();
return QModelIndex();
}
int UndoStack::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
if (parent.isValid()) {
return 0;
}
return commands_.size() + undone_commands_.size();
return commands_.size() + undone_commands_.size();
}
QVariant UndoStack::headerData(int section, Qt::Orientation orientation, int role) const
QVariant UndoStack::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (role == Qt::DisplayRole) {
switch (section) {
case 0:
return QStringLiteral("Number");
case 1:
return QStringLiteral("Action");
}
}
if (role == Qt::DisplayRole) {
switch (section) {
case 0:
return QStringLiteral("Number");
case 1:
return QStringLiteral("Action");
}
}
return QVariant();
return QVariant();
}
bool UndoStack::hasChildren(const QModelIndex &parent) const
{
return !parent.isValid();
return !parent.isValid();
}
}
+49 -44
View File
@@ -27,74 +27,79 @@
#include "common/define.h"
#include "undo/undocommand.h"
namespace olive {
class UndoStack : public QAbstractItemModel
namespace olive
{
Q_OBJECT
class UndoStack : public QAbstractItemModel {
Q_OBJECT
public:
UndoStack();
UndoStack();
virtual ~UndoStack() override;
virtual ~UndoStack() override;
void push(UndoCommand* command, const QString &name);
void push(UndoCommand *command, const QString &name);
void jump(size_t index);
void jump(size_t index);
void clear();
void clear();
bool CanUndo() const;
bool CanUndo() const;
bool CanRedo() const
{
return !undone_commands_.empty();
}
bool CanRedo() const
{
return !undone_commands_.empty();
}
void UpdateActions();
void UpdateActions();
QAction* GetUndoAction()
{
return undo_action_;
}
QAction *GetUndoAction()
{
return undo_action_;
}
QAction* GetRedoAction()
{
return redo_action_;
}
QAction *GetRedoAction()
{
return redo_action_;
}
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
virtual QModelIndex parent(const QModelIndex &index) const override;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
virtual int
columnCount(const QModelIndex &parent = QModelIndex()) const override;
virtual QVariant data(const QModelIndex &index,
int role = Qt::DisplayRole) const override;
virtual QModelIndex
index(int row, int column,
const QModelIndex &parent = QModelIndex()) const override;
virtual QModelIndex parent(const QModelIndex &index) const override;
virtual int
rowCount(const QModelIndex &parent = QModelIndex()) const override;
virtual QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const override;
virtual bool
hasChildren(const QModelIndex &parent = QModelIndex()) const override;
signals:
void indexChanged(int i);
void indexChanged(int i);
public slots:
void undo();
void undo();
void redo();
void redo();
private:
static const int kMaxUndoCommands;
static const int kMaxUndoCommands;
struct CommandEntry
{
UndoCommand *command;
QString name;
};
struct CommandEntry {
UndoCommand *command;
QString name;
};
std::list<CommandEntry> commands_;
std::list<CommandEntry> commands_;
std::list<CommandEntry> undone_commands_;
std::list<CommandEntry> undone_commands_;
QAction* undo_action_;
QAction* redo_action_;
QAction *undo_action_;
QAction *redo_action_;
};
}