filter: add filter preset undo commands

Add undo commands to add / edit / delete filter presets.
These are styled after the other undo commands: On changes,
the UI is informed by DiveListNotifier signals. Editing is
a simple std::swap of values.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-05-27 09:31:26 +02:00 committed by Dirk Hohndel
parent 536fc05dd6
commit 2f5223035a
7 changed files with 184 additions and 0 deletions

View file

@ -16,6 +16,8 @@ set(SUBSURFACE_GENERIC_COMMANDS_SRCS
command_edit_trip.h
command_event.cpp
command_event.h
command_filter.cpp
command_filter.h
command_pictures.cpp
command_pictures.h
)

View file

@ -6,6 +6,7 @@
#include "command_edit.h"
#include "command_edit_trip.h"
#include "command_event.h"
#include "command_filter.h"
#include "command_pictures.h"
namespace Command {
@ -377,4 +378,19 @@ void addPictures(const std::vector<PictureListForAddition> &pictures)
execute(new AddPictures(pictures));
}
void createFilterPreset(const QString &name, const FilterData &data)
{
execute(new CreateFilterPreset(name, data));
}
void removeFilterPreset(int index)
{
execute(new RemoveFilterPreset(index));
}
void editFilterPreset(int index, const FilterData &data)
{
execute(new EditFilterPreset(index, data));
}
} // namespace Command

View file

@ -9,6 +9,7 @@
#include <vector>
struct DiveAndLocation;
struct FilterData;
// We put everything in a namespace, so that we can shorten names without polluting the global namespace
namespace Command {
@ -133,6 +134,12 @@ void setPictureOffset(dive *d, const QString &filename, offset_t offset);
void removePictures(const std::vector<PictureListForDeletion> &pictures);
void addPictures(const std::vector<PictureListForAddition> &pictures);
// 8) Filter commands
void createFilterPreset(const QString &name, const FilterData &data);
void removeFilterPreset(int index);
void editFilterPreset(int index, const FilterData &data);
} // namespace Command
#endif // COMMAND_H

View file

@ -0,0 +1,91 @@
// SPDX-License-Identifier: GPL-2.0
#include "command_filter.h"
#include "core/filterpreset.h"
#include "core/subsurface-qt/divelistnotifier.h"
namespace Command {
static int createFilterPreset(const QString &name, const FilterData &data)
{
int index = filter_preset_add(name, data);
emit diveListNotifier.filterPresetAdded(index);
return index;
}
static std::pair<QString, FilterData> removeFilterPreset(int index)
{
QString oldName = filter_preset_name_qstring(index);
FilterData oldData = filter_preset_get(index);
filter_preset_delete(index);
emit diveListNotifier.filterPresetRemoved(index);
return { oldName, oldData };
}
CreateFilterPreset::CreateFilterPreset(const QString &nameIn, const FilterData &dataIn) :
name(nameIn), data(dataIn), index(0)
{
setText(Command::Base::tr("Create filter preset %1").arg(nameIn));
}
bool CreateFilterPreset::workToBeDone()
{
return true;
}
void CreateFilterPreset::redo()
{
index = createFilterPreset(name, data);
}
void CreateFilterPreset::undo()
{
// with std::tie() we can conveniently assign tuples
std::tie(name, data) = removeFilterPreset(index);
}
RemoveFilterPreset::RemoveFilterPreset(int indexIn) : index(indexIn)
{
setText(Command::Base::tr("Delete filter preset %1").arg(filter_preset_name_qstring(index)));
}
bool RemoveFilterPreset::workToBeDone()
{
return true;
}
void RemoveFilterPreset::redo()
{
// with std::tie() we can conveniently assign tuples
std::tie(name, data) = removeFilterPreset(index);
}
void RemoveFilterPreset::undo()
{
index = createFilterPreset(name, data);
}
EditFilterPreset::EditFilterPreset(int indexIn, const FilterData &dataIn) :
index(indexIn), data(dataIn)
{
setText(Command::Base::tr("Edit filter preset %1").arg(filter_preset_name_qstring(index)));
}
bool EditFilterPreset::workToBeDone()
{
return true;
}
void EditFilterPreset::redo()
{
FilterData oldData = filter_preset_get(index);
filter_preset_set(index, data);
data = std::move(oldData);
}
void EditFilterPreset::undo()
{
redo(); // undo() and redo() do the same thing
}
} // namespace Command

61
commands/command_filter.h Normal file
View file

@ -0,0 +1,61 @@
// SPDX-License-Identifier: GPL-2.0
// Note: this header file is used by the undo-machinery and should not be included elsewhere.
#ifndef COMMAND_FILTER_H
#define COMMAND_FILTER_H
#include "command_base.h"
#include "core/divefilter.h"
struct FilterData;
// We put everything in a namespace, so that we can shorten names without polluting the global namespace
namespace Command {
class CreateFilterPreset final : public Base {
public:
CreateFilterPreset(const QString &name, const FilterData &data);
private:
// for redo
QString name;
FilterData data;
// for undo
int index;
void undo() override;
void redo() override;
bool workToBeDone() override;
};
class RemoveFilterPreset final : public Base {
public:
RemoveFilterPreset(int index);
private:
// for undo
QString name;
FilterData data;
// for redo
int index;
void undo() override;
void redo() override;
bool workToBeDone() override;
};
class EditFilterPreset final : public Base {
public:
EditFilterPreset(int index, const FilterData &data);
private:
// for redo and undo
int index;
FilterData data;
void undo() override;
void redo() override;
bool workToBeDone() override;
};
} // namespace Command
#endif

View file

@ -125,6 +125,11 @@ signals:
void picturesRemoved(dive *d, QVector<QString> filenames);
void picturesAdded(dive *d, QVector<PictureObj> pics);
// Filter related signals
void filterPresetAdded(int index);
void filterPresetRemoved(int index);
void filterPresetChanged(int index);
// This signal is emited every time a command is executed.
// This is used to hide an old multi-dives-edited warning message.
// This is necessary, so that the user can't click on the "undo" button and undo

View file

@ -21,6 +21,7 @@ SOURCES += ../../subsurface-mobile-main.cpp \
../../commands/command_divesite.cpp \
../../commands/command_edit.cpp \
../../commands/command_edit_trip.cpp \
../../commands/command_filter.cpp \
../../commands/command_pictures.cpp \
../../core/cloudstorage.cpp \
../../core/configuredivecomputerthreads.cpp \
@ -192,6 +193,7 @@ HEADERS += \
../../commands/command_divesite.h \
../../commands/command_edit.h \
../../commands/command_edit_trip.h \
../../commands/command_filter.h \
../../commands/command_pictures.h \
../../core/libdivecomputer.h \
../../core/cloudstorage.h \