aboutsummaryrefslogtreecommitdiffstats
path: root/qt-models/filterconstraintmodel.cpp
blob: 8ccc23d81495562391fceb7a23709a6743ca8c4e (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// SPDX-License-Identifier: GPL-2.0
#include "filterconstraintmodel.h"
#include "core/qthelper.h"	// for timestamp conversions
#include <QTime>

FilterConstraintModel::~FilterConstraintModel()
{
}

// QTime <-> seconds in integer conversion functions
static QTime secondsToTime(int seconds)
{
	return QTime::fromMSecsSinceStartOfDay(seconds * 1000);
}

static int timeToSeconds(const QTime &t)
{
	return t.msecsSinceStartOfDay() / 1000;
}

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

	const filter_constraint &constraint = constraints[index.row()];

	switch (role) {
	case TYPE_ROLE:
		return constraint.type;
	case IS_STAR_WIDGET_ROLE:
		return filter_constraint_is_star(constraint.type);
	case HAS_DATE_WIDGET_ROLE:
		return filter_constraint_has_date_widget(constraint.type);
	case HAS_TIME_WIDGET_ROLE:
		return filter_constraint_has_time_widget(constraint.type);
	case NUM_DECIMALS_ROLE:
		return filter_constraint_num_decimals(constraint.type);
	case NEGATE_COMBO_ROLE:
		return filter_constraint_negate_list_translated();
	case STRING_MODE_COMBO_ROLE:
		return filter_constraint_has_string_mode(constraint.type) ?
			filter_constraint_string_mode_list_translated() : QStringList();
	case RANGE_MODE_COMBO_ROLE:
		return filter_constraint_has_range_mode(constraint.type) ?
			filter_constraint_range_mode_list_translated() : QStringList();
	case MULTIPLE_CHOICE_LIST_ROLE:
		return filter_contraint_multiple_choice_translated(constraint.type);
	case STRING_MODE_ROLE:
		return static_cast<int>(constraint.string_mode);
	case RANGE_MODE_ROLE:
		return static_cast<int>(constraint.range_mode);
	case TYPE_DISPLAY_ROLE:
		return filter_constraint_type_to_string_translated(constraint.type);
	case NEGATE_DISPLAY_ROLE:
		return filter_constraint_negate_to_string_translated(constraint.negate);
	case STRING_MODE_DISPLAY_ROLE:
		return filter_constraint_string_mode_to_string_translated(constraint.string_mode);
	case RANGE_MODE_DISPLAY_ROLE:
		return filter_constraint_range_mode_to_string_translated(constraint.range_mode);
	case TYPE_INDEX_ROLE:
		return filter_constraint_type_to_index(constraint.type);
	case NEGATE_INDEX_ROLE:
		return static_cast<int>(constraint.negate);
	case STRING_MODE_INDEX_ROLE:
		return filter_constraint_string_mode_to_index(constraint.string_mode);
	case RANGE_MODE_INDEX_ROLE:
		return filter_constraint_range_mode_to_index(constraint.range_mode);
	case UNIT_ROLE:
		return filter_constraint_get_unit(constraint.type);
	case STRING_ROLE:
		return filter_constraint_get_string(constraint);
	case INTEGER_FROM_ROLE:
		return filter_constraint_get_integer_from(constraint);
	case INTEGER_TO_ROLE:
		return filter_constraint_get_integer_to(constraint);
	case FLOAT_FROM_ROLE:
		return filter_constraint_get_float_from(constraint); // Converts from integers to metric or imperial units
	case FLOAT_TO_ROLE:
		return filter_constraint_get_float_to(constraint); // Converts from integers to metric or imperial units
	case TIMESTAMP_FROM_ROLE:
		return timestampToDateTime(filter_constraint_get_timestamp_from(constraint));
	case TIMESTAMP_TO_ROLE:
		return timestampToDateTime(filter_constraint_get_timestamp_to(constraint));
	case TIME_FROM_ROLE:
		return secondsToTime(filter_constraint_get_integer_from(constraint));
	case TIME_TO_ROLE:
		return secondsToTime(filter_constraint_get_integer_from(constraint));
	case MULTIPLE_CHOICE_ROLE:
		return (qulonglong)filter_constraint_get_multiple_choice(constraint);
	}
	return QVariant();
}

bool FilterConstraintModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
	if (!index.isValid() || index.row() > (int)constraints.size())
		return false;
	filter_constraint &constraint = constraints[index.row()];

	switch (role) {
	case NEGATE_INDEX_ROLE:
		constraint.negate = value.value<bool>();
		break;
	case STRING_ROLE:
		filter_constraint_set_stringlist(constraint, value.value<QString>());
		break;
	case STRING_MODE_INDEX_ROLE:
		constraint.string_mode = filter_constraint_string_mode_from_index(value.value<int>());
		break;
	case RANGE_MODE_INDEX_ROLE:
		constraint.range_mode = filter_constraint_range_mode_from_index(value.value<int>());
		break;
	case INTEGER_FROM_ROLE:
		filter_constraint_set_integer_from(constraint, value.value<int>());
		break;
	case INTEGER_TO_ROLE:
		filter_constraint_set_integer_to(constraint, value.value<int>());
		break;
	case FLOAT_FROM_ROLE:
		filter_constraint_set_float_from(constraint, value.value<double>());
		break;
	case FLOAT_TO_ROLE:
		filter_constraint_set_float_to(constraint, value.value<double>());
		break;
	case TIMESTAMP_FROM_ROLE:
		filter_constraint_set_timestamp_from(constraint, dateTimeToTimestamp(value.value<QDateTime>()));
		break;
	case TIMESTAMP_TO_ROLE:
		filter_constraint_set_timestamp_to(constraint, dateTimeToTimestamp(value.value<QDateTime>()));
		break;
	case TIME_FROM_ROLE:
		filter_constraint_set_integer_from(constraint, timeToSeconds(value.value<QTime>()));
		break;
	case TIME_TO_ROLE:
		filter_constraint_set_integer_to(constraint, timeToSeconds(value.value<QTime>()));
		break;
	case MULTIPLE_CHOICE_ROLE:
		filter_constraint_set_multiple_choice(constraint, value.value<uint64_t>());
		break;
	default:
		return false;
	}
	emit dataChanged(index, index, QVector<int> { role });
	return true;
}

int FilterConstraintModel::rowCount(const QModelIndex&) const
{
	return constraints.size();
}

void FilterConstraintModel::reload(const std::vector<filter_constraint> &constraintsIn)
{
	beginResetModel();
	constraints = constraintsIn;
	endResetModel();
}

std::vector<filter_constraint> FilterConstraintModel::getConstraints() const
{
	std::vector<filter_constraint> res;
	res.reserve(constraints.size());
	for (const filter_constraint &c: constraints) {
		if (filter_constraint_is_valid(&c))
			res.push_back(c);
	}
	return res;
}

void FilterConstraintModel::addConstraint(filter_constraint_type type)
{
	int count = (int)constraints.size();
	beginInsertRows(QModelIndex(), count, count);
	constraints.emplace_back(type);
	endInsertRows();
}

void FilterConstraintModel::deleteConstraint(int row)
{
	if (row < 0 || row >= (int)constraints.size())
		return;
	beginRemoveRows(QModelIndex(), row, row);
	constraints.erase(constraints.begin() + row);
	endRemoveRows();
}