2015-04-22 19:30:09 +02:00
|
|
|
#ifndef QTGUI_STACKSUPPORT_H
|
|
|
|
#define QTGUI_STACKSUPPORT_H
|
|
|
|
|
2015-09-06 20:33:09 +02:00
|
|
|
#include "./undocommands.h"
|
2015-04-22 19:30:09 +02:00
|
|
|
|
|
|
|
#include <QUndoStack>
|
|
|
|
|
2018-06-10 22:51:43 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2015-04-22 19:30:09 +02:00
|
|
|
namespace QtGui {
|
|
|
|
|
|
|
|
class StackAbsorper;
|
|
|
|
|
2017-05-01 03:26:04 +02:00
|
|
|
class StackSupport {
|
2015-04-22 19:30:09 +02:00
|
|
|
friend class StackAbsorper;
|
2017-05-01 03:26:04 +02:00
|
|
|
|
2015-04-22 19:30:09 +02:00
|
|
|
public:
|
|
|
|
StackSupport(QUndoStack *undoStack = nullptr);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
QUndoStack *undoStack();
|
2018-06-10 22:51:43 +02:00
|
|
|
bool push(std::unique_ptr<CustomUndoCommand> command);
|
2015-04-22 19:30:09 +02:00
|
|
|
void clearUndoStack();
|
|
|
|
|
|
|
|
private:
|
|
|
|
QUndoStack *m_undoStack;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns the undo stack for the current instance.
|
|
|
|
*/
|
|
|
|
inline QUndoStack *StackSupport::undoStack()
|
|
|
|
{
|
|
|
|
return m_undoStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Pushes the specified custom undo \a command to the undo stack and returns whether the redo action was successful.
|
|
|
|
*/
|
2018-06-10 22:51:43 +02:00
|
|
|
inline bool StackSupport::push(std::unique_ptr<CustomUndoCommand> command)
|
2015-04-22 19:30:09 +02:00
|
|
|
{
|
2018-03-14 00:15:12 +01:00
|
|
|
if (!m_undoStack) {
|
|
|
|
return false;
|
2015-04-22 19:30:09 +02:00
|
|
|
}
|
2018-03-14 00:15:12 +01:00
|
|
|
if (command->isNoop()) {
|
|
|
|
return true; // doing nothing can never fail
|
|
|
|
}
|
2018-06-19 13:32:21 +02:00
|
|
|
auto *const rawCommand(command.release());
|
|
|
|
m_undoStack->push(rawCommand);
|
|
|
|
return rawCommand->redoResult();
|
2015-04-22 19:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Clears the undo stack.
|
|
|
|
*/
|
|
|
|
inline void StackSupport::clearUndoStack()
|
|
|
|
{
|
2017-05-01 03:26:04 +02:00
|
|
|
if (m_undoStack) {
|
2015-04-22 19:30:09 +02:00
|
|
|
m_undoStack->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief The StackAbsorper class is used by the CustomUndoCommand class to prevent infinite recursion when pushing
|
|
|
|
* a new command to the stack.
|
|
|
|
*/
|
2017-05-01 03:26:04 +02:00
|
|
|
class StackAbsorper {
|
2015-04-22 19:30:09 +02:00
|
|
|
public:
|
|
|
|
StackAbsorper(StackSupport *supported);
|
|
|
|
~StackAbsorper();
|
|
|
|
QUndoStack *stack();
|
2017-05-01 03:26:04 +02:00
|
|
|
|
2015-04-22 19:30:09 +02:00
|
|
|
private:
|
|
|
|
StackSupport *m_supported;
|
|
|
|
QUndoStack *m_stack;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Detaches the undo stack from the specified stack support temporary.
|
|
|
|
*/
|
2017-05-01 03:26:04 +02:00
|
|
|
inline StackAbsorper::StackAbsorper(StackSupport *supported)
|
|
|
|
: m_supported(supported)
|
|
|
|
, m_stack(supported->m_undoStack)
|
2015-04-22 19:30:09 +02:00
|
|
|
{
|
|
|
|
m_supported->m_undoStack = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Restores the undo stack of the stack support.
|
|
|
|
*/
|
|
|
|
inline StackAbsorper::~StackAbsorper()
|
|
|
|
{
|
|
|
|
m_supported->m_undoStack = m_stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns the stack for the current instance.
|
|
|
|
*/
|
|
|
|
inline QUndoStack *StackAbsorper::stack()
|
|
|
|
{
|
|
|
|
return m_stack;
|
|
|
|
}
|
2017-09-29 17:17:12 +02:00
|
|
|
} // namespace QtGui
|
2015-04-22 19:30:09 +02:00
|
|
|
|
|
|
|
#endif // QTGUI_STACKSUPPORT_H
|