2018-07-23 21:41:23 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "command_base.h"
|
2019-03-30 17:39:27 +00:00
|
|
|
#include "core/qthelper.h" // for updateWindowTitle()
|
2020-02-03 18:33:06 +00:00
|
|
|
#include "core/subsurface-qt/divelistnotifier.h"
|
2020-03-05 17:00:00 +00:00
|
|
|
#include <QVector>
|
2018-07-23 21:41:23 +00:00
|
|
|
|
|
|
|
namespace Command {
|
|
|
|
|
|
|
|
static QUndoStack undoStack;
|
|
|
|
|
2020-03-05 16:57:02 +00:00
|
|
|
// forward declaration
|
|
|
|
QString changesMade();
|
|
|
|
|
2018-07-23 21:41:23 +00:00
|
|
|
// General commands
|
2019-03-30 17:39:27 +00:00
|
|
|
void init()
|
|
|
|
{
|
|
|
|
QObject::connect(&undoStack, &QUndoStack::cleanChanged, &updateWindowTitle);
|
2020-03-05 16:57:02 +00:00
|
|
|
changesCallback = &changesMade;
|
2019-03-30 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 21:41:23 +00:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
undoStack.clear();
|
|
|
|
}
|
|
|
|
|
2019-03-30 17:39:27 +00:00
|
|
|
void setClean()
|
|
|
|
{
|
|
|
|
undoStack.setClean();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isClean()
|
|
|
|
{
|
|
|
|
return undoStack.isClean();
|
|
|
|
}
|
|
|
|
|
2020-01-11 23:22:50 +00:00
|
|
|
// this can be used to get access to the signals emitted by the QUndoStack
|
|
|
|
QUndoStack *getUndoStack()
|
|
|
|
{
|
|
|
|
return &undoStack;
|
|
|
|
}
|
|
|
|
|
2018-07-23 21:41:23 +00:00
|
|
|
QAction *undoAction(QObject *parent)
|
|
|
|
{
|
|
|
|
return undoStack.createUndoAction(parent, QCoreApplication::translate("Command", "&Undo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QAction *redoAction(QObject *parent)
|
|
|
|
{
|
|
|
|
return undoStack.createRedoAction(parent, QCoreApplication::translate("Command", "&Redo"));
|
|
|
|
}
|
|
|
|
|
2020-03-05 17:00:00 +00:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-05 16:56:17 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2019-05-24 19:17:22 +00:00
|
|
|
bool execute(Base *cmd)
|
2018-07-23 21:41:23 +00:00
|
|
|
{
|
2019-05-24 19:17:22 +00:00
|
|
|
if (cmd->workToBeDone()) {
|
2018-07-23 21:41:23 +00:00
|
|
|
undoStack.push(cmd);
|
2019-05-24 19:38:56 +00:00
|
|
|
emit diveListNotifier.commandExecuted();
|
2019-05-24 19:17:22 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2018-07-23 21:41:23 +00:00
|
|
|
delete cmd;
|
2019-05-24 19:17:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-23 21:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Command
|
|
|
|
|
|
|
|
|