mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
If we clear out our dive data, we also need to clear out the undo stack because it otherwise will refer to dives that no longer exist. The cleanest way to do that would be to do it in the same function used to clear the dive data. Which causes us to call C++ code from C code and really is a bit of a mess with a circular dependency between our libraries. But this does seem better than relying on people to remember to call into a second function after clearing the data. Suggested-by: Michael Andreen <michael@andreen.dev> Suggested-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
124 lines
2.6 KiB
C++
124 lines
2.6 KiB
C++
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "command_base.h"
|
|
#include "core/globals.h"
|
|
#include "core/qthelper.h" // for updateWindowTitle()
|
|
#include "core/subsurface-qt/divelistnotifier.h"
|
|
#include <QVector>
|
|
|
|
namespace Command {
|
|
|
|
static QUndoStack *undoStack;
|
|
|
|
// forward declaration
|
|
QString changesMade();
|
|
|
|
// General commands
|
|
void init()
|
|
{
|
|
undoStack = make_global<QUndoStack>();
|
|
QObject::connect(undoStack, &QUndoStack::cleanChanged, &updateWindowTitle);
|
|
changesCallback = &changesMade;
|
|
}
|
|
|
|
void clear()
|
|
{
|
|
// this can get called from C code even if the Command code was never initialized
|
|
// (really only in the case of tests like TestParse)
|
|
if (undoStack)
|
|
undoStack->clear();
|
|
}
|
|
|
|
void setClean()
|
|
{
|
|
undoStack->setClean();
|
|
}
|
|
|
|
bool isClean()
|
|
{
|
|
return undoStack->isClean();
|
|
}
|
|
|
|
// this can be used to get access to the signals emitted by the QUndoStack
|
|
QUndoStack *getUndoStack()
|
|
{
|
|
return undoStack;
|
|
}
|
|
|
|
QAction *undoAction(QObject *parent)
|
|
{
|
|
return undoStack->createUndoAction(parent, QCoreApplication::translate("Command", "&Undo"));
|
|
}
|
|
|
|
QAction *redoAction(QObject *parent)
|
|
{
|
|
return undoStack->createRedoAction(parent, QCoreApplication::translate("Command", "&Redo"));
|
|
}
|
|
|
|
QString diveNumberOrDate(struct dive *d)
|
|
{
|
|
if (d->number != 0)
|
|
return QStringLiteral("#%1").arg(d->number);
|
|
else
|
|
return QStringLiteral("@%1").arg(get_short_dive_date_string(d->when));
|
|
}
|
|
|
|
QString getListOfDives(const std::vector<struct dive*> &dives)
|
|
{
|
|
QString listOfDives;
|
|
if ((int)dives.size() == dive_table.nr)
|
|
return Base::tr("all dives");
|
|
int i = 0;
|
|
for (dive *d: dives) {
|
|
// we show a maximum of five dive numbers, or 4 plus ellipsis
|
|
if (++i == 4 && dives.size() >= 5)
|
|
return listOfDives + "...";
|
|
listOfDives += diveNumberOrDate(d) + ", ";
|
|
}
|
|
if (!listOfDives.isEmpty())
|
|
listOfDives.truncate(listOfDives.length() - 2);
|
|
return listOfDives;
|
|
}
|
|
|
|
QString getListOfDives(QVector<struct dive *> dives)
|
|
{
|
|
return getListOfDives(std::vector<struct dive *>(dives.begin(), dives.end()));
|
|
}
|
|
|
|
// return a string that can be used for the commit message and should list the changes that
|
|
// were made to the dive list
|
|
QString changesMade()
|
|
{
|
|
QString changeTexts;
|
|
for (int i = 0; i < undoStack->index(); i++)
|
|
changeTexts += undoStack->text(i) + "\n";
|
|
return changeTexts;
|
|
}
|
|
|
|
static bool executingCommand = false;
|
|
bool execute(Base *cmd)
|
|
{
|
|
if (cmd->workToBeDone()) {
|
|
executingCommand = true;
|
|
undoStack->push(cmd);
|
|
executingCommand = false;
|
|
emit diveListNotifier.commandExecuted();
|
|
return true;
|
|
} else {
|
|
delete cmd;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool placingCommand()
|
|
{
|
|
return executingCommand;
|
|
}
|
|
|
|
} // namespace Command
|
|
|
|
extern "C" {
|
|
void command_clear_from_C() {
|
|
Command::clear();
|
|
}
|
|
}
|