2020-05-27 07:31:26 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "command_filter.h"
|
2024-06-08 16:54:23 +00:00
|
|
|
#include "core/divelog.h"
|
2020-05-27 07:31:26 +00:00
|
|
|
#include "core/filterpreset.h"
|
2024-06-08 16:54:23 +00:00
|
|
|
#include "core/filterpresettable.h"
|
2020-05-27 07:31:26 +00:00
|
|
|
#include "core/subsurface-qt/divelistnotifier.h"
|
|
|
|
|
|
|
|
namespace Command {
|
|
|
|
|
2024-02-29 12:39:17 +00:00
|
|
|
static int createFilterPreset(const std::string &name, const FilterData &data)
|
2020-05-27 07:31:26 +00:00
|
|
|
{
|
2024-06-08 16:54:23 +00:00
|
|
|
int index = divelog.filter_presets.add(name, data);
|
2020-05-27 07:31:26 +00:00
|
|
|
emit diveListNotifier.filterPresetAdded(index);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2024-02-29 12:39:17 +00:00
|
|
|
static std::pair<std::string, FilterData> removeFilterPreset(int index)
|
2020-05-27 07:31:26 +00:00
|
|
|
{
|
2024-06-08 16:54:23 +00:00
|
|
|
const filter_preset &preset = divelog.filter_presets[index];
|
|
|
|
std::string oldName = preset.name;
|
|
|
|
FilterData oldData = preset.data;
|
|
|
|
divelog.filter_presets.remove(index);
|
2020-05-27 07:31:26 +00:00
|
|
|
emit diveListNotifier.filterPresetRemoved(index);
|
|
|
|
return { oldName, oldData };
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateFilterPreset::CreateFilterPreset(const QString &nameIn, const FilterData &dataIn) :
|
2024-02-29 12:39:17 +00:00
|
|
|
name(nameIn.toStdString()), data(dataIn), index(0)
|
2020-05-27 07:31:26 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2024-06-08 16:54:23 +00:00
|
|
|
const std::string &name = divelog.filter_presets[index].name;
|
|
|
|
setText(Command::Base::tr("Delete filter preset %1").arg(QString::fromStdString(name)));
|
2020-05-27 07:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2024-06-08 16:54:23 +00:00
|
|
|
const std::string &name = divelog.filter_presets[index].name;
|
|
|
|
setText(Command::Base::tr("Edit filter preset %1").arg(QString::fromStdString(name)));
|
2020-05-27 07:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EditFilterPreset::workToBeDone()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditFilterPreset::redo()
|
|
|
|
{
|
2024-06-08 16:54:23 +00:00
|
|
|
filter_preset &preset = divelog.filter_presets[index];
|
|
|
|
std::swap(data, preset.data);
|
2020-05-27 07:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditFilterPreset::undo()
|
|
|
|
{
|
|
|
|
redo(); // undo() and redo() do the same thing
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Command
|