2017-04-27 18:25:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "qt-models/filtermodels.h"
|
|
|
|
#include "qt-models/models.h"
|
|
|
|
#include "core/display.h"
|
2018-08-28 18:44:11 +00:00
|
|
|
#include "core/qthelper.h"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "core/subsurface-string.h"
|
2018-09-06 07:52:02 +00:00
|
|
|
#include "core/subsurface-qt/DiveListNotifier.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "qt-models/divetripmodel.h"
|
2016-07-09 19:45:55 +00:00
|
|
|
|
|
|
|
#if !defined(SUBSURFACE_MOBILE)
|
2016-07-07 19:55:17 +00:00
|
|
|
#include "desktop-widgets/divelistview.h"
|
|
|
|
#include "desktop-widgets/mainwindow.h"
|
2016-07-09 19:45:55 +00:00
|
|
|
#endif
|
2015-09-03 17:20:19 +00:00
|
|
|
|
|
|
|
#include <QDebug>
|
Replace bool * array by std::vector<char> in MultiFilterInterface
This replaces a dynamically allocated array of bool by std::vector<char>.
1) This makes the code shorter and less error prone, because memory
management has not to be done by hand.
2) It fixes a bug in the old code:
memset(checkState, false, list.count()) is wrong, because bool is
not guaranteed to be the same size as char!
Two notes:
1) QMap<>, QVector<>, etc. are used numerous times in the code, so
this doesn't introduce a new C++ concept. Here, the std:: version
is used, because there is no need for reference counting, COW
semantics, etc.
2) std::vector<char> is used instead of std::vector<bool>, because
the latter does a pessimization where a bitfield is used!
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2017-11-25 14:04:38 +00:00
|
|
|
#include <algorithm>
|
2014-11-13 18:31:03 +00:00
|
|
|
|
2018-11-16 12:49:19 +00:00
|
|
|
namespace {
|
2019-01-19 08:59:33 +00:00
|
|
|
// Check if a string-list contains at least one string containing the second argument.
|
|
|
|
// Comparison is non case sensitive and removes white space.
|
|
|
|
bool listContainsSuperstring(const QStringList &list, const QString &s)
|
2018-11-16 12:49:19 +00:00
|
|
|
{
|
2019-01-19 08:59:33 +00:00
|
|
|
return std::any_of(list.begin(), list.end(), [&s](const QString &s2)
|
|
|
|
{ return s2.trimmed().contains(s.trimmed(), Qt::CaseInsensitive); });
|
2018-11-16 12:49:19 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 08:59:33 +00:00
|
|
|
bool hasTags(const QStringList &tags, const struct dive *d)
|
2018-11-16 12:49:19 +00:00
|
|
|
{
|
2019-01-19 08:59:33 +00:00
|
|
|
if (tags.isEmpty())
|
|
|
|
return true;
|
|
|
|
QStringList dive_tags = get_taglist_string(d->tag_list).split(",");
|
|
|
|
|
|
|
|
return std::all_of(tags.begin(), tags.end(), [&dive_tags](const QString &tag)
|
|
|
|
{ return listContainsSuperstring(dive_tags, tag); });
|
2018-11-16 12:49:19 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 08:59:33 +00:00
|
|
|
bool hasPersons(const QStringList &people, const struct dive *d)
|
2018-11-16 12:49:19 +00:00
|
|
|
{
|
2019-01-19 08:59:33 +00:00
|
|
|
if (people.isEmpty())
|
|
|
|
return true;
|
|
|
|
QStringList dive_people = QString(d->buddy).split(",", QString::SkipEmptyParts)
|
|
|
|
+ QString(d->divemaster).split(",", QString::SkipEmptyParts);
|
|
|
|
|
|
|
|
return std::all_of(people.begin(), people.end(), [&dive_people](const QString &person)
|
|
|
|
{ return listContainsSuperstring(dive_people, person); });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasLocations(const QStringList &locations, const struct dive *d)
|
|
|
|
{
|
|
|
|
if (locations.isEmpty())
|
|
|
|
return true;
|
|
|
|
QStringList diveLocations;
|
|
|
|
if (d->divetrip)
|
|
|
|
diveLocations.push_back(QString(d->divetrip->location));
|
|
|
|
|
|
|
|
if (d->dive_site)
|
|
|
|
diveLocations.push_back(QString(d->dive_site->name));
|
|
|
|
|
|
|
|
return std::all_of(locations.begin(), locations.end(), [&diveLocations](const QString &location)
|
|
|
|
{ return listContainsSuperstring(diveLocations, location); });
|
2018-11-16 12:49:19 +00:00
|
|
|
}
|
2018-11-16 14:23:39 +00:00
|
|
|
|
|
|
|
// TODO: Finish this iimplementation.
|
|
|
|
bool hasEquipment(const QStringList& equipment, const struct dive *d)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-16 12:49:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 16:00:02 +00:00
|
|
|
MultiFilterSortModel *MultiFilterSortModel::instance()
|
2018-09-06 07:52:02 +00:00
|
|
|
{
|
2018-10-21 16:00:02 +00:00
|
|
|
static MultiFilterSortModel self;
|
|
|
|
return &self;
|
2017-11-26 21:21:58 +00:00
|
|
|
}
|
|
|
|
|
2017-12-24 16:39:21 +00:00
|
|
|
MultiFilterSortModel::MultiFilterSortModel(QObject *parent) : QSortFilterProxyModel(parent),
|
2015-11-07 21:04:54 +00:00
|
|
|
divesDisplayed(0),
|
2018-12-24 09:14:37 +00:00
|
|
|
curr_dive_site(NULL)
|
2014-11-13 18:31:03 +00:00
|
|
|
{
|
2018-10-29 13:56:48 +00:00
|
|
|
setFilterKeyColumn(-1); // filter all columns
|
|
|
|
setFilterCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
void MultiFilterSortModel::resetModel(DiveTripModelBase::Layout layout)
|
2018-10-29 13:56:48 +00:00
|
|
|
{
|
2018-12-27 09:06:11 +00:00
|
|
|
DiveTripModelBase::resetModel(layout);
|
|
|
|
// DiveTripModelBase::resetModel() generates a new instance.
|
|
|
|
// Thus, the source model must be reset.
|
|
|
|
setSourceModel(DiveTripModelBase::instance());
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 18:16:25 +00:00
|
|
|
bool MultiFilterSortModel::showDive(const struct dive *d) const
|
2014-11-13 18:31:03 +00:00
|
|
|
{
|
2019-01-18 18:14:43 +00:00
|
|
|
// If curr_dive_site is set, we are in a special dive-site editing mode.
|
|
|
|
if (curr_dive_site)
|
|
|
|
return d->dive_site == curr_dive_site;
|
|
|
|
|
2018-11-16 12:49:19 +00:00
|
|
|
if (!filterData.validFilter)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (d->visibility < filterData.minVisibility || d->visibility > filterData.maxVisibility)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (d->rating < filterData.minRating || d->rating > filterData.maxRating)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// TODO: get the preferences for the imperial vs metric data.
|
|
|
|
// ignore the check if it doesn't makes sense.
|
2019-01-18 16:08:48 +00:00
|
|
|
if (d->watertemp.mkelvin &&
|
|
|
|
(d->watertemp.mkelvin < C_to_mkelvin(filterData.minWaterTemp) || d->watertemp.mkelvin > C_to_mkelvin((filterData.maxWaterTemp))))
|
2018-11-16 12:49:19 +00:00
|
|
|
return false;
|
|
|
|
|
2019-01-18 16:08:48 +00:00
|
|
|
if (d->airtemp.mkelvin &&
|
|
|
|
(d->airtemp.mkelvin < C_to_mkelvin(filterData.minAirTemp) || d->airtemp.mkelvin > C_to_mkelvin(filterData.maxAirTemp)))
|
2018-11-16 12:49:19 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (filterData.from.isValid() && d->when < filterData.from.toTime_t())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (filterData.to.isValid() && d->when > filterData.to.toTime_t())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// tags.
|
2019-01-19 08:59:33 +00:00
|
|
|
if (!hasTags(filterData.tags, d))
|
2018-11-16 12:49:19 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// people
|
2019-01-19 08:59:33 +00:00
|
|
|
if (!hasPersons(filterData.people, d))
|
2018-11-16 12:49:19 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Location
|
2019-01-19 08:59:33 +00:00
|
|
|
if (!hasLocations(filterData.location, d))
|
2018-11-16 12:49:19 +00:00
|
|
|
return false;
|
2015-05-26 20:42:45 +00:00
|
|
|
|
2018-11-16 14:23:39 +00:00
|
|
|
if (!hasEquipment(filterData.equipment, d))
|
|
|
|
return false;
|
|
|
|
|
2019-01-01 17:49:56 +00:00
|
|
|
// Planned/Logged
|
|
|
|
if (!filterData.logged && !has_planned(d, true))
|
|
|
|
return false;
|
|
|
|
if (!filterData.planned && !has_planned(d, false))
|
|
|
|
return false;
|
|
|
|
|
2018-08-14 18:16:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
|
|
|
|
{
|
|
|
|
QModelIndex index0 = sourceModel()->index(source_row, 0, source_parent);
|
2018-12-27 09:06:11 +00:00
|
|
|
struct dive *d = sourceModel()->data(index0, DiveTripModelBase::DIVE_ROLE).value<struct dive *>();
|
2018-08-14 18:16:25 +00:00
|
|
|
|
2018-08-14 22:30:49 +00:00
|
|
|
// For dives, simply check the hidden_by_filter flag
|
|
|
|
if (d)
|
|
|
|
return !d->hidden_by_filter;
|
2018-08-14 18:16:25 +00:00
|
|
|
|
2018-08-14 22:30:49 +00:00
|
|
|
// Since this is not a dive, it must be a trip
|
2018-12-27 09:06:11 +00:00
|
|
|
dive_trip *trip = sourceModel()->data(index0, DiveTripModelBase::TRIP_ROLE).value<dive_trip *>();
|
2014-11-16 16:04:06 +00:00
|
|
|
|
2018-08-14 22:30:49 +00:00
|
|
|
if (!trip)
|
|
|
|
return false; // Oops. Neither dive nor trip, something is seriously wrong.
|
2018-08-14 18:16:25 +00:00
|
|
|
|
2018-08-14 22:30:49 +00:00
|
|
|
// Show the trip if any dive is visible
|
2018-11-08 15:58:33 +00:00
|
|
|
for (int i = 0; i < trip->dives.nr; ++i) {
|
|
|
|
if (!trip->dives.dives[i]->hidden_by_filter)
|
2018-08-14 22:30:49 +00:00
|
|
|
return true;
|
2018-08-14 18:16:25 +00:00
|
|
|
}
|
2018-08-14 22:30:49 +00:00
|
|
|
return false;
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 20:03:03 +00:00
|
|
|
void MultiFilterSortModel::filterChanged(const QModelIndex &from, const QModelIndex &to, const QVector<int> &roles)
|
|
|
|
{
|
|
|
|
// Only redo the filter if a checkbox changed. If the count of an entry changed,
|
|
|
|
// we do *not* want to recalculate the filters.
|
|
|
|
if (roles.contains(Qt::CheckStateRole))
|
|
|
|
myInvalidate();
|
|
|
|
}
|
|
|
|
|
2014-11-13 18:31:03 +00:00
|
|
|
void MultiFilterSortModel::myInvalidate()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dive *d;
|
|
|
|
|
2014-11-16 16:04:06 +00:00
|
|
|
divesDisplayed = 0;
|
|
|
|
|
2018-08-14 22:30:49 +00:00
|
|
|
// Apply filter for each dive
|
|
|
|
for_each_dive (i, d) {
|
|
|
|
bool show = showDive(d);
|
|
|
|
filter_dive(d, show);
|
|
|
|
if (show)
|
|
|
|
divesDisplayed++;
|
|
|
|
}
|
|
|
|
|
filter: prevent assert trap on exit filters
This "bug" is found using Qt 5.10 compiled in developer mode. Access
the filers, click a little around here, close the filters. Almost every
time the following assert is triggered:
ASSERT failure in QPersistentModelIndex::~QPersistentModelIndex:
"persistent model indexes corrupted", file itemmodels/qabstractitemmodel.cpp, line 643
This is relatively deep down in Qt, and it is triggered by clearing the
filters. Trying to force a crash when using the same scenario in Qt 5.10
compiled for production (so no active asserts) did not result in a crash.
So, upto this time, it is unclear if the Qt assert points out a real problem,
or it is some false alarm (for whatever reason).
Further investigation shows that the assert can be solved by changing the
invalidate() to an invalidateFilter(). Indeed, the last variant is a little
more lightweigt, and does seem to do the same job from a functional point
of view (in this case).
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
2017-12-27 07:45:36 +00:00
|
|
|
invalidateFilter();
|
2014-11-16 16:04:06 +00:00
|
|
|
|
2018-10-29 13:56:48 +00:00
|
|
|
// Tell the dive trip model to update the displayed-counts
|
2018-12-27 09:06:11 +00:00
|
|
|
DiveTripModelBase::instance()->filterFinished();
|
2014-11-16 16:04:06 +00:00
|
|
|
emit filterFinished();
|
2015-05-26 21:03:37 +00:00
|
|
|
|
2018-10-20 09:56:06 +00:00
|
|
|
#if !defined(SUBSURFACE_MOBILE)
|
|
|
|
if (curr_dive_site)
|
|
|
|
MainWindow::instance()->diveList->expandAll();
|
2016-07-09 19:45:55 +00:00
|
|
|
#endif
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiFilterSortModel::clearFilter()
|
|
|
|
{
|
|
|
|
myInvalidate();
|
|
|
|
}
|
2015-05-26 20:36:06 +00:00
|
|
|
|
2018-10-26 14:57:08 +00:00
|
|
|
void MultiFilterSortModel::startFilterDiveSite(struct dive_site *ds)
|
2015-05-26 20:36:06 +00:00
|
|
|
{
|
2018-10-26 14:57:08 +00:00
|
|
|
curr_dive_site = ds;
|
2015-05-26 20:36:06 +00:00
|
|
|
myInvalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiFilterSortModel::stopFilterDiveSite()
|
|
|
|
{
|
|
|
|
curr_dive_site = NULL;
|
|
|
|
myInvalidate();
|
|
|
|
}
|
2018-10-29 19:17:53 +00:00
|
|
|
|
|
|
|
bool MultiFilterSortModel::lessThan(const QModelIndex &i1, const QModelIndex &i2) const
|
|
|
|
{
|
|
|
|
// Hand sorting down to the source model.
|
2018-12-27 09:06:11 +00:00
|
|
|
return DiveTripModelBase::instance()->lessThan(i1, i2);
|
2018-10-29 19:17:53 +00:00
|
|
|
}
|
2018-12-06 19:07:47 +00:00
|
|
|
|
2018-12-16 19:43:01 +00:00
|
|
|
void MultiFilterSortModel::filterDataChanged(const FilterData &data)
|
2018-12-06 19:07:47 +00:00
|
|
|
{
|
|
|
|
filterData = data;
|
|
|
|
myInvalidate();
|
|
|
|
}
|