2020-05-27 06:17:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "filterpresetmodel.h"
|
2024-06-08 16:54:23 +00:00
|
|
|
#include "core/divelog.h"
|
2020-05-27 06:17:06 +00:00
|
|
|
#include "core/filterconstraint.h"
|
|
|
|
#include "core/filterpreset.h"
|
2024-06-08 16:54:23 +00:00
|
|
|
#include "core/filterpresettable.h"
|
2020-05-27 06:17:06 +00:00
|
|
|
#include "core/qthelper.h"
|
|
|
|
#include "core/subsurface-qt/divelistnotifier.h"
|
|
|
|
|
|
|
|
FilterPresetModel::FilterPresetModel()
|
|
|
|
{
|
|
|
|
setHeaderDataStrings(QStringList{ "", tr("Name") });
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &FilterPresetModel::reset);
|
2020-05-27 22:19:22 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::filterPresetAdded, this, &FilterPresetModel::filterPresetAdded);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::filterPresetRemoved, this, &FilterPresetModel::filterPresetRemoved);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::filterPresetChanged, this, &FilterPresetModel::filterPresetChanged);
|
2020-05-27 06:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FilterPresetModel::~FilterPresetModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FilterPresetModel *FilterPresetModel::instance()
|
|
|
|
{
|
|
|
|
static FilterPresetModel self;
|
|
|
|
return &self;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FilterPresetModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2024-06-08 16:54:23 +00:00
|
|
|
if (index.row() < 0 || static_cast<size_t>(index.row()) >= divelog.filter_presets.size())
|
2020-05-27 06:17:06 +00:00
|
|
|
return QVariant();
|
|
|
|
|
2024-06-08 16:54:23 +00:00
|
|
|
const auto &filter_preset = divelog.filter_presets[index.row()];
|
2020-05-27 06:17:06 +00:00
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
if (index.column() == NAME)
|
2024-06-08 16:54:23 +00:00
|
|
|
return QString::fromStdString(filter_preset.name);
|
2020-05-27 06:17:06 +00:00
|
|
|
break;
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
if (index.column() == REMOVE)
|
|
|
|
return trashIcon();
|
|
|
|
break;
|
|
|
|
case Qt::SizeHintRole:
|
|
|
|
if (index.column() == REMOVE)
|
|
|
|
return trashIcon().size();
|
|
|
|
break;
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
if (index.column() == REMOVE)
|
|
|
|
return tr("Clicking here will remove this filter set.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
int FilterPresetModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2024-06-08 16:54:23 +00:00
|
|
|
return static_cast<int>(divelog.filter_presets.size());
|
2020-05-27 06:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FilterPresetModel::reset()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
|
|
|
}
|
2020-05-27 22:19:22 +00:00
|
|
|
|
|
|
|
void FilterPresetModel::filterPresetAdded(int index)
|
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(), index, index);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterPresetModel::filterPresetChanged(int i)
|
|
|
|
{
|
|
|
|
QModelIndex idx = index(i, 0);
|
|
|
|
dataChanged(idx, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterPresetModel::filterPresetRemoved(int index)
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), index, index);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|