Skip to content

Commit

Permalink
Implement ability to set undo/redo steps
Browse files Browse the repository at this point in the history
And set default to 100 steps.
  • Loading branch information
MrStevns committed Apr 24, 2024
1 parent 55f2001 commit 5da7bf6
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 19 deletions.
59 changes: 51 additions & 8 deletions app/src/generalpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ GeneralPage::GeneralPage() : ui(new Ui::GeneralPage)
ui->backgroundButtons->setId(ui->dotsBackgroundButton, 4);
ui->backgroundButtons->setId(ui->weaveBackgroundButton, 5);

ui->undoRedoGroupApplyButton->setDisabled(true);
ui->undoRedoGroupCancelButton->setDisabled(true);

auto buttonClicked = static_cast<void (QButtonGroup::*)(QAbstractButton*)>(&QButtonGroup::buttonClicked);
auto curIndexChanged = static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged);
auto spinValueChanged = static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged);
Expand All @@ -115,6 +118,9 @@ GeneralPage::GeneralPage() : ui(new Ui::GeneralPage)
connect(ui->framePoolSizeSpin, spinValueChanged, this, &GeneralPage::frameCacheNumberChanged);
connect(ui->invertScrollDirectionBox, &QCheckBox::stateChanged, this, &GeneralPage::invertScrollDirectionBoxStateChanged);
connect(ui->newUndoRedoCheckBox, &QCheckBox::stateChanged, this, &GeneralPage::newUndoRedoCheckBoxStateChanged);
connect(ui->undoStepsBox, spinValueChanged, this, &GeneralPage::undoRedoMaxStepsChanged);
connect(ui->undoRedoGroupApplyButton, &QPushButton::clicked, this, &GeneralPage::undoRedoApplyButtonPressed);
connect(ui->undoRedoGroupCancelButton, &QPushButton::clicked, this, &GeneralPage::undoRedoCancelButtonPressed);
}

GeneralPage::~GeneralPage()
Expand Down Expand Up @@ -177,6 +183,9 @@ void GeneralPage::updateValues()
QSignalBlocker bNewUndoRedoCheckBox(ui->newUndoRedoCheckBox);
ui->newUndoRedoCheckBox->setChecked(mManager->isOn(SETTING::NEW_UNDO_REDO_SYSTEM_ON));

QSignalBlocker bUndoRedoLimitSpinBox(ui->undoStepsBox);
ui->undoStepsBox->setValue(mManager->getInt(SETTING::UNDO_REDO_MAX_STEPS));

int buttonIdx = 1;
if (bgName == "checkerboard") buttonIdx = 1;
else if (bgName == "white") buttonIdx = 2;
Expand All @@ -188,7 +197,6 @@ void GeneralPage::updateValues()
ui->backgroundButtons->button(buttonIdx)->setChecked(true);

ui->invertScrollDirectionBox->setChecked(mManager->isOn(SETTING::INVERT_SCROLL_ZOOM_DIRECTION));
mInitialNewUndoSystemStateEnabled = mManager->isOn(SETTING::NEW_UNDO_REDO_SYSTEM_ON);
}

void GeneralPage::languageChanged(int i)
Expand Down Expand Up @@ -308,7 +316,33 @@ void GeneralPage::invertScrollDirectionBoxStateChanged(int b)

void GeneralPage::newUndoRedoCheckBoxStateChanged(bool b)
{
if (b) {
ui->undoRedoGroupCancelButton->setDisabled(b == mManager->isOn(SETTING::NEW_UNDO_REDO_SYSTEM_ON));
ui->undoRedoGroupApplyButton->setDisabled(b == mManager->isOn(SETTING::NEW_UNDO_REDO_SYSTEM_ON));
}

void GeneralPage::undoRedoMaxStepsChanged(int value)
{
ui->undoRedoGroupCancelButton->setDisabled(value == mManager->getInt(SETTING::UNDO_REDO_MAX_STEPS));
ui->undoRedoGroupApplyButton->setDisabled(value == mManager->getInt(SETTING::UNDO_REDO_MAX_STEPS));
}

void GeneralPage::undoRedoApplyButtonPressed()
{
if (ui->undoStepsBox->value() != mManager->getInt(SETTING::UNDO_REDO_MAX_STEPS)) {
QMessageBox messageBox(this);
messageBox.setIcon(QMessageBox::Warning);
messageBox.setText(tr("Resets your current undo history"));
messageBox.setInformativeText(tr("Changing the max undo/redo step value, resets your current undo/redo history, are you sure you want to proceed?"));
messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);

if (messageBox.exec() == QMessageBox::Yes) {
mManager->set(SETTING::UNDO_REDO_MAX_STEPS, ui->undoStepsBox->value());
} else {
ui->undoStepsBox->setValue(mManager->getInt(SETTING::UNDO_REDO_MAX_STEPS));
}
}

if (ui->newUndoRedoCheckBox->isChecked()) {
QMessageBox messageBox(this);
messageBox.setIcon(QMessageBox::Warning);
messageBox.setText(tr("Experimental feature!"));
Expand All @@ -322,12 +356,21 @@ void GeneralPage::newUndoRedoCheckBoxStateChanged(bool b)
mManager->set(SETTING::NEW_UNDO_REDO_SYSTEM_ON, false);
}
} else {
if (mInitialNewUndoSystemStateEnabled) {
QMessageBox messageBox(this);
messageBox.setIcon(QMessageBox::Information);
messageBox.setText(tr("This will first take effect after a restart."));
messageBox.exec();
}
QMessageBox messageBox(this);
messageBox.setIcon(QMessageBox::Information);
messageBox.setText(tr("The undo/redo system will be changed on the next launch of the application"));
messageBox.exec();
mManager->set(SETTING::NEW_UNDO_REDO_SYSTEM_ON, false);
}

ui->undoRedoGroupCancelButton->setDisabled(true);
ui->undoRedoGroupApplyButton->setDisabled(true);
}

void GeneralPage::undoRedoCancelButtonPressed()
{
ui->undoStepsBox->setValue(mManager->getInt(SETTING::UNDO_REDO_MAX_STEPS));
ui->newUndoRedoCheckBox->setCheckState(mManager->isOn(SETTING::NEW_UNDO_REDO_SYSTEM_ON) ? Qt::Checked : Qt::Unchecked);
ui->undoRedoGroupCancelButton->setDisabled(true);
ui->undoRedoGroupApplyButton->setDisabled(true);
}
6 changes: 4 additions & 2 deletions app/src/generalpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ private slots:
void frameCacheNumberChanged(int value);
void invertScrollDirectionBoxStateChanged(int b);
void newUndoRedoCheckBoxStateChanged(bool b);
void undoRedoMaxStepsChanged(int value);

void undoRedoApplyButtonPressed();
void undoRedoCancelButtonPressed();

private:

void updateSafeHelperTextEnabledState();

Ui::GeneralPage* ui = nullptr;
PreferenceManager* mManager = nullptr;

bool mInitialNewUndoSystemStateEnabled = false;
};

#endif // GENERALPAGE_H
68 changes: 67 additions & 1 deletion app/ui/generalpage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,79 @@
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Undo/Redo</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="newUndoRedoCheckBox">
<property name="text">
<string>Enable New Undo/Redo System (Experimental)</string>
<string>Enable New System (Experimental)</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="topMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="undoStepsLabel">
<property name="toolTip">
<string>How many steps you're allowed to undo/redo</string>
</property>
<property name="text">
<string>Max undo steps</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="undoStepsBox">
<property name="maximumSize">
<size>
<width>80</width>
<height>16777215</height>
</size>
</property>
<property name="minimum">
<number>20</number>
</property>
<property name="maximum">
<number>250</number>
</property>
<property name="value">
<number>100</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="topMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="undoRedoGroupApplyButton">
<property name="text">
<string>Apply</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="undoRedoGroupCancelButton">
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
Expand Down
1 change: 1 addition & 0 deletions core_lib/src/interface/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ void Editor::makeConnections()
{
connect(mPreferenceManager, &PreferenceManager::optionChanged, this, &Editor::settingUpdated);
connect(mUndoRedoManager, &UndoRedoManager::didUpdateUndoStack, this, &Editor::updateAutoSaveCounter);
connect(mPreferenceManager, &PreferenceManager::optionChanged, mUndoRedoManager, &UndoRedoManager::onSettingChanged);

// XXX: This is a hack to prevent crashes until #864 is done (see #1412)
connect(mLayerManager, &LayerManager::layerDeleted, mUndoRedoManager, &UndoRedoManager::sanitizeLegacyBackupElementsAfterLayerDeletion);
Expand Down
4 changes: 4 additions & 0 deletions core_lib/src/managers/preferencemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ void PreferenceManager::loadPrefs()
set(SETTING::LAYOUT_LOCK, settings.value(SETTING_LAYOUT_LOCK, false).toBool());
set(SETTING::FRAME_POOL_SIZE, settings.value(SETTING_FRAME_POOL_SIZE, 1024).toInt());
set(SETTING::NEW_UNDO_REDO_SYSTEM_ON, settings.value(SETTING_NEW_UNDO_REDO_ON, false).toBool());
set(SETTING::UNDO_REDO_MAX_STEPS, settings.value(SETTING_UNDO_REDO_MAX_STEPS, 100).toInt());

set(SETTING::FPS, settings.value(SETTING_FPS, 12).toInt());
set(SETTING::FIELD_W, settings.value(SETTING_FIELD_W, 800).toInt());
Expand Down Expand Up @@ -312,6 +313,9 @@ void PreferenceManager::set(SETTING option, int value)
case SETTING::FRAME_POOL_SIZE:
settings.setValue(SETTING_FRAME_POOL_SIZE, value);
break;
case SETTING::UNDO_REDO_MAX_STEPS:
settings.setValue(SETTING_UNDO_REDO_MAX_STEPS, value);
break;
case SETTING::DRAW_ON_EMPTY_FRAME_ACTION:
settings.setValue( SETTING_DRAW_ON_EMPTY_FRAME_ACTION, value);
break;
Expand Down
18 changes: 12 additions & 6 deletions core_lib/src/managers/undoredomanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,22 @@ bool UndoRedoManager::init()
mUndoStack = new QUndoStack(this);
qDebug() << "UndoRedoManager: init";

mUndoStack->setUndoLimit(editor()->preference()->getInt(SETTING::UNDO_REDO_MAX_STEPS));
mNewBackupSystemEnabled = editor()->preference()->isOn(SETTING::NEW_UNDO_REDO_SYSTEM_ON);

return true;
}

void UndoRedoManager::onSettingChanged(SETTING setting)
{
if (setting == SETTING::UNDO_REDO_MAX_STEPS) {
// The stack needs to be cleared in order to change the undo redo limit
clearStack();
qDebug() << "updated undo stack limit";
mUndoStack->setUndoLimit(editor()->preference()->getInt(SETTING::UNDO_REDO_MAX_STEPS));
}
}

Status UndoRedoManager::load(Object* /*o*/)
{
clearStack();
Expand Down Expand Up @@ -135,11 +146,6 @@ void UndoRedoManager::add(UndoRedoType undoRedoType)
}
}

const UndoRedoCommand* UndoRedoManager::latestBackupElement() const
{
return static_cast<const UndoRedoCommand*>(mUndoStack->command(mUndoStack->index() - 1));
}

bool UndoRedoManager::hasUnsavedChanges() const
{
if (mNewBackupSystemEnabled) {
Expand Down Expand Up @@ -405,7 +411,7 @@ bool UndoRedoManager::legacyBackup(int backupLayer, int backupFrame, const QStri
{
delete mLegacyBackupList.takeLast();
}
while (mLegacyBackupList.size() > 19) // we authorize only 20 levels of cancellation
while (mLegacyBackupList.size() > editor()->preference()->getInt(SETTING::UNDO_REDO_MAX_STEPS))
{
delete mLegacyBackupList.takeFirst();
mLegacyBackupIndex--;
Expand Down
6 changes: 4 additions & 2 deletions core_lib/src/managers/undoredomanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ GNU General Public License for more details.
#include "layer.h"
#include "keyframe.h"

#include "preferencesdef.h"

#include <QRectF>

class QAction;
Expand Down Expand Up @@ -82,8 +84,6 @@ class UndoRedoManager : public BaseManager
void updateUndoAction(QAction* undoAction);
void updateRedoAction(QAction* redoAction);

const UndoRedoCommand* latestBackupElement() const;

void clearStack();

// Legacy System
Expand All @@ -105,6 +105,8 @@ class UndoRedoManager : public BaseManager

void rememberLastModifiedFrame(int layerNumber, int frameNumber);

void onSettingChanged(SETTING setting);

signals:
void didUpdateUndoStack();

Expand Down
1 change: 1 addition & 0 deletions core_lib/src/util/pencildef.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ const static int MaxFramesBound = 9999;
#define SETTING_SOUND_SCRUB_ACTIVE "SoundScrubActive"
#define SETTING_SOUND_SCRUB_MSEC "SoundScrubMsec"
#define SETTING_NEW_UNDO_REDO_ON "NewUndoRedoOn"
#define SETTING_UNDO_REDO_MAX_STEPS "UndoRedoMaxSteps"

#define SETTING_BUCKET_TOLERANCE "Tolerance"
#define SETTING_BUCKET_TOLERANCE_ON "BucketToleranceEnabled"
Expand Down
1 change: 1 addition & 0 deletions core_lib/src/util/preferencesdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ enum class SETTING
LAYOUT_LOCK,
DRAW_ON_EMPTY_FRAME_ACTION,
FRAME_POOL_SIZE,
UNDO_REDO_MAX_STEPS,
ROTATION_INCREMENT,
SHOW_SELECTION_INFO,
ASK_FOR_PRESET,
Expand Down

0 comments on commit 5da7bf6

Please sign in to comment.