summaryrefslogtreecommitdiffstats
path: root/qt-models/filterpresetmodel.cpp
blob: 3853b4c34c4c4fdecf46589292d1d9b062ecd7b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// SPDX-License-Identifier: GPL-2.0
#include "filterpresetmodel.h"
#include "core/filterconstraint.h"
#include "core/filterpreset.h"
#include "core/qthelper.h"
#include "core/subsurface-qt/divelistnotifier.h"

FilterPresetModel::FilterPresetModel()
{
	setHeaderDataStrings(QStringList{ "", tr("Name") });
	connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &FilterPresetModel::reset);
	connect(&diveListNotifier, &DiveListNotifier::filterPresetAdded, this, &FilterPresetModel::filterPresetAdded);
	connect(&diveListNotifier, &DiveListNotifier::filterPresetRemoved, this, &FilterPresetModel::filterPresetRemoved);
	connect(&diveListNotifier, &DiveListNotifier::filterPresetChanged, this, &FilterPresetModel::filterPresetChanged);
}

FilterPresetModel::~FilterPresetModel()
{
}

FilterPresetModel *FilterPresetModel::instance()
{
	static FilterPresetModel self;
	return &self;
}

QVariant FilterPresetModel::data(const QModelIndex &index, int role) const
{
	if (!index.isValid() || index.row() >= filter_presets_count())
		return QVariant();

	switch (role) {
	case Qt::DisplayRole:
		if (index.column() == NAME)
			return filter_preset_name_qstring(index.row());
		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
{
	return filter_presets_count();
}

void FilterPresetModel::reset()
{
	beginResetModel();
	endResetModel();
}

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();
}