core: make the QUndoStack a "global object"

Thus, it will be freed before application exit.

Freeing it later led to crashes.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-03-13 18:41:45 +01:00 committed by Dirk Hohndel
parent 8577b00cb7
commit 16aa761e86

View file

@ -1,13 +1,14 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include "command_base.h" #include "command_base.h"
#include "core/globals.h"
#include "core/qthelper.h" // for updateWindowTitle() #include "core/qthelper.h" // for updateWindowTitle()
#include "core/subsurface-qt/divelistnotifier.h" #include "core/subsurface-qt/divelistnotifier.h"
#include <QVector> #include <QVector>
namespace Command { namespace Command {
static QUndoStack undoStack; static QUndoStack *undoStack;
// forward declaration // forward declaration
QString changesMade(); QString changesMade();
@ -15,39 +16,40 @@ QString changesMade();
// General commands // General commands
void init() void init()
{ {
QObject::connect(&undoStack, &QUndoStack::cleanChanged, &updateWindowTitle); undoStack = make_global<QUndoStack>();
QObject::connect(undoStack, &QUndoStack::cleanChanged, &updateWindowTitle);
changesCallback = &changesMade; changesCallback = &changesMade;
} }
void clear() void clear()
{ {
undoStack.clear(); undoStack->clear();
} }
void setClean() void setClean()
{ {
undoStack.setClean(); undoStack->setClean();
} }
bool isClean() bool isClean()
{ {
return undoStack.isClean(); return undoStack->isClean();
} }
// this can be used to get access to the signals emitted by the QUndoStack // this can be used to get access to the signals emitted by the QUndoStack
QUndoStack *getUndoStack() QUndoStack *getUndoStack()
{ {
return &undoStack; return undoStack;
} }
QAction *undoAction(QObject *parent) QAction *undoAction(QObject *parent)
{ {
return undoStack.createUndoAction(parent, QCoreApplication::translate("Command", "&Undo")); return undoStack->createUndoAction(parent, QCoreApplication::translate("Command", "&Undo"));
} }
QAction *redoAction(QObject *parent) QAction *redoAction(QObject *parent)
{ {
return undoStack.createRedoAction(parent, QCoreApplication::translate("Command", "&Redo")); return undoStack->createRedoAction(parent, QCoreApplication::translate("Command", "&Redo"));
} }
QString diveNumberOrDate(struct dive *d) QString diveNumberOrDate(struct dive *d)
@ -85,8 +87,8 @@ QString getListOfDives(QVector<struct dive *> dives)
QString changesMade() QString changesMade()
{ {
QString changeTexts; QString changeTexts;
for (int i = 0; i < undoStack.index(); i++) for (int i = 0; i < undoStack->index(); i++)
changeTexts += undoStack.text(i) + "\n"; changeTexts += undoStack->text(i) + "\n";
return changeTexts; return changeTexts;
} }
@ -95,7 +97,7 @@ bool execute(Base *cmd)
{ {
if (cmd->workToBeDone()) { if (cmd->workToBeDone()) {
executingCommand = true; executingCommand = true;
undoStack.push(cmd); undoStack->push(cmd);
executingCommand = false; executingCommand = false;
emit diveListNotifier.commandExecuted(); emit diveListNotifier.commandExecuted();
return true; return true;