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-05-11 15:25:41 +00:00
|
|
|
#include "core/subsurface-string.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
|
|
|
|
2017-12-24 16:39:21 +00:00
|
|
|
#define CREATE_INSTANCE_METHOD(CLASS) \
|
|
|
|
CLASS *CLASS::instance() \
|
|
|
|
{ \
|
|
|
|
static CLASS *self = new CLASS(); \
|
|
|
|
return self; \
|
|
|
|
}
|
2014-11-13 18:31:03 +00:00
|
|
|
|
2017-12-23 14:25:54 +00:00
|
|
|
CREATE_INSTANCE_METHOD(TagFilterModel)
|
|
|
|
CREATE_INSTANCE_METHOD(BuddyFilterModel)
|
|
|
|
CREATE_INSTANCE_METHOD(LocationFilterModel)
|
|
|
|
CREATE_INSTANCE_METHOD(SuitsFilterModel)
|
2015-05-17 17:39:07 +00:00
|
|
|
CREATE_INSTANCE_METHOD(MultiFilterSortModel)
|
2014-11-13 18:48:13 +00:00
|
|
|
|
2017-12-24 16:39:21 +00:00
|
|
|
FilterModelBase::FilterModelBase(QObject *parent) : QStringListModel(parent),
|
2017-12-24 13:35:59 +00:00
|
|
|
anyChecked(false),
|
|
|
|
negate(false)
|
2017-11-25 15:15:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-27 09:49:16 +00:00
|
|
|
// Update the stringList and the items array.
|
2017-11-25 15:15:57 +00:00
|
|
|
// The last item is supposed to be the "Show Empty Tags" entry.
|
|
|
|
void FilterModelBase::updateList(const QStringList &newList)
|
|
|
|
{
|
2018-08-27 09:49:16 +00:00
|
|
|
// Keep copy of the old items array to reimport the checked state later.
|
|
|
|
// Note that by using std::move(), this is an essentially free operation:
|
|
|
|
// The data is moved from the old array to the new one and the old array
|
|
|
|
// is reset to zero size.
|
|
|
|
std::vector<Item> oldItems = std::move(items);
|
|
|
|
|
|
|
|
// Resize the cleared array to the new size. This leaves the checked
|
|
|
|
// flag in an undefined state (since we didn't define a default constructor).
|
|
|
|
items.resize(newList.count());
|
|
|
|
|
|
|
|
// First, reset all checked states to false
|
2017-11-25 15:15:57 +00:00
|
|
|
anyChecked = false;
|
2018-08-27 09:49:16 +00:00
|
|
|
for (Item &item: items)
|
|
|
|
item.checked = false;
|
2017-11-25 15:15:57 +00:00
|
|
|
|
2018-08-27 09:49:16 +00:00
|
|
|
// Then, restore the checked state. Ignore the last item, since
|
|
|
|
// this is the "Show Empty Tags" entry.
|
|
|
|
for (int i = 0; i < (int)oldItems.size() - 1; i++) {
|
|
|
|
if (oldItems[i].checked) {
|
2017-11-25 15:15:57 +00:00
|
|
|
int ind = newList.indexOf(stringList()[i]);
|
|
|
|
if (ind >= 0 && ind < newList.count() - 1) {
|
2018-08-27 09:49:16 +00:00
|
|
|
items[ind].checked = true;
|
2017-11-25 15:15:57 +00:00
|
|
|
anyChecked = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 09:49:16 +00:00
|
|
|
// Reset the state of the "Show Empty Tags" entry. But be careful:
|
|
|
|
// on program startup, the old list is empty.
|
|
|
|
if (!oldItems.empty() && !items.empty() && oldItems.back().checked) {
|
|
|
|
items.back().checked = true;
|
2017-11-25 15:15:57 +00:00
|
|
|
anyChecked = true;
|
|
|
|
}
|
2018-08-27 10:53:46 +00:00
|
|
|
|
|
|
|
// Finally, calculate and cache the counts. Ignore the last item, since
|
|
|
|
// this is the "Show Empty Tags" entry.
|
|
|
|
for (int i = 0; i < (int)newList.size() - 1; i++)
|
|
|
|
items[i].count = countDives(qPrintable(newList[i]));
|
|
|
|
|
|
|
|
// Calculate count of "Empty Tags".
|
|
|
|
if (!items.empty())
|
|
|
|
items.back().count = countDives("");
|
|
|
|
|
2017-11-25 15:15:57 +00:00
|
|
|
setStringList(newList);
|
|
|
|
}
|
|
|
|
|
2017-12-23 14:25:54 +00:00
|
|
|
Qt::ItemFlags FilterModelBase::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
return QStringListModel::flags(index) | Qt::ItemIsUserCheckable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FilterModelBase::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
if (role == Qt::CheckStateRole) {
|
2018-08-27 09:49:16 +00:00
|
|
|
items[index.row()].checked = value.toBool();
|
2017-12-23 14:25:54 +00:00
|
|
|
anyChecked = false;
|
2018-08-27 09:49:16 +00:00
|
|
|
for (const Item &item: items) {
|
|
|
|
if (item.checked) {
|
2017-12-23 14:25:54 +00:00
|
|
|
anyChecked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dataChanged(index, index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FilterModelBase::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (role == Qt::CheckStateRole) {
|
2018-08-27 09:49:16 +00:00
|
|
|
return items[index.row()].checked ? Qt::Checked : Qt::Unchecked;
|
2017-12-23 14:25:54 +00:00
|
|
|
} else if (role == Qt::DisplayRole) {
|
2018-08-27 10:53:46 +00:00
|
|
|
int row = index.row();
|
|
|
|
return QStringLiteral("%1 (%2)").arg(stringList()[row], QString::number(items[row].count));
|
2017-12-23 14:25:54 +00:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterModelBase::clearFilter()
|
|
|
|
{
|
2018-08-27 09:49:16 +00:00
|
|
|
for (Item &item: items)
|
|
|
|
item.checked = false;
|
2017-12-23 14:25:54 +00:00
|
|
|
anyChecked = false;
|
2017-12-24 16:39:21 +00:00
|
|
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, 0));
|
2017-12-23 14:25:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-23 14:49:21 +00:00
|
|
|
void FilterModelBase::selectAll()
|
|
|
|
{
|
2018-08-27 09:49:16 +00:00
|
|
|
for (Item &item: items)
|
|
|
|
item.checked = true;
|
2017-12-23 14:49:21 +00:00
|
|
|
anyChecked = true;
|
2017-12-24 16:39:21 +00:00
|
|
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, 0));
|
2017-12-23 14:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FilterModelBase::invertSelection()
|
|
|
|
{
|
2018-08-28 05:57:59 +00:00
|
|
|
for (Item &item: items)
|
2018-08-27 09:49:16 +00:00
|
|
|
item.checked = !item.checked;
|
|
|
|
anyChecked = std::any_of(items.begin(), items.end(), [](Item &item) { return !!item.checked; });
|
2017-12-24 16:39:21 +00:00
|
|
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, 0));
|
2017-12-23 14:49:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-24 13:35:59 +00:00
|
|
|
void FilterModelBase::setNegate(bool negateParam)
|
|
|
|
{
|
|
|
|
negate = negateParam;
|
|
|
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, 0));
|
|
|
|
}
|
|
|
|
|
2017-11-25 15:15:57 +00:00
|
|
|
SuitsFilterModel::SuitsFilterModel(QObject *parent) : FilterModelBase(parent)
|
2014-11-13 19:07:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-23 14:25:54 +00:00
|
|
|
int SuitsFilterModel::countDives(const char *s) const
|
|
|
|
{
|
|
|
|
return count_dives_with_suit(s);
|
|
|
|
}
|
|
|
|
|
2018-08-14 18:09:30 +00:00
|
|
|
bool SuitsFilterModel::doFilter(const dive *d) const
|
2014-11-13 19:07:05 +00:00
|
|
|
{
|
2017-12-24 13:35:59 +00:00
|
|
|
// rowCount() == 0 should never happen, because we have the "no suits" row
|
|
|
|
// let's handle it gracefully anyway.
|
|
|
|
if (!anyChecked || rowCount() == 0)
|
2014-11-13 19:12:46 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Checked means 'Show', Unchecked means 'Hide'.
|
|
|
|
QString suit(d->suit);
|
|
|
|
// only show empty suit dives if the user checked that.
|
2017-12-24 13:35:59 +00:00
|
|
|
if (suit.isEmpty())
|
2018-08-27 09:49:16 +00:00
|
|
|
return items[rowCount() - 1].checked != negate;
|
2014-11-13 19:12:46 +00:00
|
|
|
|
|
|
|
// there is a suit selected
|
|
|
|
QStringList suitList = stringList();
|
2017-11-25 17:26:00 +00:00
|
|
|
// Ignore last item, since this is the "Show Empty Tags" entry
|
|
|
|
for (int i = 0; i < rowCount() - 1; i++) {
|
2018-08-27 09:49:16 +00:00
|
|
|
if (items[i].checked && suit == suitList[i])
|
2017-12-24 13:35:59 +00:00
|
|
|
return !negate;
|
2014-11-13 19:12:46 +00:00
|
|
|
}
|
2017-12-24 13:35:59 +00:00
|
|
|
return negate;
|
2014-11-13 19:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SuitsFilterModel::repopulate()
|
|
|
|
{
|
2014-11-13 19:09:54 +00:00
|
|
|
QStringList list;
|
|
|
|
struct dive *dive;
|
|
|
|
int i = 0;
|
|
|
|
for_each_dive (i, dive) {
|
|
|
|
QString suit(dive->suit);
|
|
|
|
if (!suit.isEmpty() && !list.contains(suit)) {
|
|
|
|
list.append(suit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qSort(list);
|
|
|
|
list << tr("No suit set");
|
2017-11-25 15:15:57 +00:00
|
|
|
updateList(list);
|
2014-11-13 19:07:05 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 15:15:57 +00:00
|
|
|
TagFilterModel::TagFilterModel(QObject *parent) : FilterModelBase(parent)
|
2014-11-13 18:31:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-23 14:25:54 +00:00
|
|
|
int TagFilterModel::countDives(const char *s) const
|
|
|
|
{
|
|
|
|
return count_dives_with_tag(s);
|
|
|
|
}
|
|
|
|
|
2014-11-13 18:31:03 +00:00
|
|
|
void TagFilterModel::repopulate()
|
|
|
|
{
|
|
|
|
if (g_tag_list == NULL)
|
|
|
|
return;
|
|
|
|
QStringList list;
|
2014-12-10 03:55:31 +00:00
|
|
|
struct tag_entry *current_tag_entry = g_tag_list;
|
2014-11-13 18:31:03 +00:00
|
|
|
while (current_tag_entry != NULL) {
|
2014-11-17 21:15:35 +00:00
|
|
|
if (count_dives_with_tag(current_tag_entry->tag->name) > 0)
|
|
|
|
list.append(QString(current_tag_entry->tag->name));
|
2014-11-13 18:31:03 +00:00
|
|
|
current_tag_entry = current_tag_entry->next;
|
|
|
|
}
|
|
|
|
qSort(list);
|
2014-11-25 15:47:24 +00:00
|
|
|
list << tr("Empty tags");
|
2017-11-25 15:15:57 +00:00
|
|
|
updateList(list);
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 18:09:30 +00:00
|
|
|
bool TagFilterModel::doFilter(const dive *d) const
|
2014-11-13 18:31:03 +00:00
|
|
|
{
|
|
|
|
// If there's nothing checked, this should show everything
|
2017-12-24 13:35:59 +00:00
|
|
|
// rowCount() == 0 should never happen, because we have the "no tags" row
|
|
|
|
// let's handle it gracefully anyway.
|
|
|
|
if (!anyChecked || rowCount() == 0)
|
2014-11-13 18:31:03 +00:00
|
|
|
return true;
|
2017-12-24 13:35:59 +00:00
|
|
|
|
2014-11-13 18:31:03 +00:00
|
|
|
// Checked means 'Show', Unchecked means 'Hide'.
|
|
|
|
struct tag_entry *head = d->tag_list;
|
|
|
|
|
2017-12-24 13:35:59 +00:00
|
|
|
if (!head) // last tag means "Show empty tags";
|
2018-08-27 09:49:16 +00:00
|
|
|
return items[rowCount() - 1].checked != negate;
|
2014-11-13 18:31:03 +00:00
|
|
|
|
|
|
|
// have at least one tag.
|
|
|
|
QStringList tagList = stringList();
|
|
|
|
if (!tagList.isEmpty()) {
|
|
|
|
tagList.removeLast(); // remove the "Show Empty Tags";
|
|
|
|
while (head) {
|
|
|
|
QString tagName(head->tag->name);
|
|
|
|
int index = tagList.indexOf(tagName);
|
2018-08-27 09:49:16 +00:00
|
|
|
if (index >= 0 && items[index].checked)
|
2017-12-24 13:35:59 +00:00
|
|
|
return !negate;
|
2014-11-13 18:31:03 +00:00
|
|
|
head = head->next;
|
|
|
|
}
|
|
|
|
}
|
2017-12-24 13:35:59 +00:00
|
|
|
return negate;
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 15:15:57 +00:00
|
|
|
BuddyFilterModel::BuddyFilterModel(QObject *parent) : FilterModelBase(parent)
|
2014-11-13 18:31:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-23 14:25:54 +00:00
|
|
|
int BuddyFilterModel::countDives(const char *s) const
|
|
|
|
{
|
|
|
|
return count_dives_with_person(s);
|
|
|
|
}
|
|
|
|
|
2018-08-14 18:09:30 +00:00
|
|
|
bool BuddyFilterModel::doFilter(const dive *d) const
|
2014-11-13 18:31:03 +00:00
|
|
|
{
|
|
|
|
// If there's nothing checked, this should show everything
|
2017-12-24 13:35:59 +00:00
|
|
|
// rowCount() == 0 should never happen, because we have the "no tags" row
|
|
|
|
// let's handle it gracefully anyway.
|
|
|
|
if (!anyChecked || rowCount() == 0)
|
2014-11-13 18:31:03 +00:00
|
|
|
return true;
|
2017-12-24 13:35:59 +00:00
|
|
|
|
2014-11-13 18:31:03 +00:00
|
|
|
// Checked means 'Show', Unchecked means 'Hide'.
|
2017-12-21 19:11:32 +00:00
|
|
|
QString persons = QString(d->buddy) + "," + QString(d->divemaster);
|
|
|
|
QStringList personsList = persons.split(',', QString::SkipEmptyParts);
|
2018-08-28 05:57:59 +00:00
|
|
|
for (QString &s: personsList)
|
2017-12-21 19:11:32 +00:00
|
|
|
s = s.trimmed();
|
2014-11-13 18:31:03 +00:00
|
|
|
// only show empty buddie dives if the user checked that.
|
2017-12-24 13:35:59 +00:00
|
|
|
if (personsList.isEmpty())
|
2018-08-27 09:49:16 +00:00
|
|
|
return items[rowCount() - 1].checked != negate;
|
2014-11-13 18:31:03 +00:00
|
|
|
|
|
|
|
// have at least one buddy
|
|
|
|
QStringList buddyList = stringList();
|
2017-11-25 17:26:00 +00:00
|
|
|
// Ignore last item, since this is the "Show Empty Tags" entry
|
|
|
|
for (int i = 0; i < rowCount() - 1; i++) {
|
2018-08-27 09:49:16 +00:00
|
|
|
if (items[i].checked && personsList.contains(buddyList[i], Qt::CaseInsensitive))
|
2017-12-24 13:35:59 +00:00
|
|
|
return !negate;
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
2017-12-24 13:35:59 +00:00
|
|
|
return negate;
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BuddyFilterModel::repopulate()
|
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
struct dive *dive;
|
|
|
|
int i = 0;
|
|
|
|
for_each_dive (i, dive) {
|
|
|
|
QString persons = QString(dive->buddy) + "," + QString(dive->divemaster);
|
2014-11-13 20:51:23 +00:00
|
|
|
Q_FOREACH (const QString &person, persons.split(',', QString::SkipEmptyParts)) {
|
2014-11-13 18:31:03 +00:00
|
|
|
// Remove any leading spaces
|
|
|
|
if (!list.contains(person.trimmed())) {
|
|
|
|
list.append(person.trimmed());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qSort(list);
|
|
|
|
list << tr("No buddies");
|
2017-11-25 15:15:57 +00:00
|
|
|
updateList(list);
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 15:15:57 +00:00
|
|
|
LocationFilterModel::LocationFilterModel(QObject *parent) : FilterModelBase(parent)
|
2014-11-13 18:31:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-23 14:25:54 +00:00
|
|
|
int LocationFilterModel::countDives(const char *s) const
|
|
|
|
{
|
|
|
|
return count_dives_with_location(s);
|
|
|
|
}
|
|
|
|
|
2018-08-14 18:09:30 +00:00
|
|
|
bool LocationFilterModel::doFilter(const dive *d) const
|
2014-11-13 18:31:03 +00:00
|
|
|
{
|
2017-12-24 13:35:59 +00:00
|
|
|
// rowCount() == 0 should never happen, because we have the "no location" row
|
|
|
|
// let's handle it gracefully anyway.
|
|
|
|
if (!anyChecked || rowCount() == 0)
|
2014-11-13 18:31:03 +00:00
|
|
|
return true;
|
2017-12-24 13:35:59 +00:00
|
|
|
|
2014-11-13 18:31:03 +00:00
|
|
|
// Checked means 'Show', Unchecked means 'Hide'.
|
2015-02-13 06:20:35 +00:00
|
|
|
QString location(get_dive_location(d));
|
2014-11-13 18:31:03 +00:00
|
|
|
// only show empty location dives if the user checked that.
|
2017-12-24 13:35:59 +00:00
|
|
|
if (location.isEmpty())
|
2018-08-27 09:49:16 +00:00
|
|
|
return items[rowCount() - 1].checked != negate;
|
2014-11-13 18:31:03 +00:00
|
|
|
|
2017-11-25 11:38:25 +00:00
|
|
|
// There is a location selected
|
2014-11-13 18:31:03 +00:00
|
|
|
QStringList locationList = stringList();
|
2017-11-25 11:38:25 +00:00
|
|
|
// Ignore last item, since this is the "Show Empty Tags" entry
|
|
|
|
for (int i = 0; i < rowCount() - 1; i++) {
|
2018-08-27 09:49:16 +00:00
|
|
|
if (items[i].checked && location == locationList[i])
|
2017-12-24 13:35:59 +00:00
|
|
|
return !negate;
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
2017-12-24 13:35:59 +00:00
|
|
|
return negate;
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationFilterModel::repopulate()
|
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
struct dive *dive;
|
|
|
|
int i = 0;
|
|
|
|
for_each_dive (i, dive) {
|
2015-02-13 06:20:35 +00:00
|
|
|
QString location(get_dive_location(dive));
|
2014-11-13 18:31:03 +00:00
|
|
|
if (!location.isEmpty() && !list.contains(location)) {
|
|
|
|
list.append(location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qSort(list);
|
|
|
|
list << tr("No location set");
|
2017-11-25 15:15:57 +00:00
|
|
|
updateList(list);
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 09:42:22 +00:00
|
|
|
void LocationFilterModel::changeName(const QString &oldName, const QString &newName)
|
|
|
|
{
|
|
|
|
if (oldName.isEmpty() || newName.isEmpty() || oldName == newName)
|
|
|
|
return;
|
|
|
|
QStringList list = stringList();
|
|
|
|
int oldIndex = list.indexOf(oldName);
|
|
|
|
if (oldIndex < 0)
|
|
|
|
return;
|
|
|
|
int newIndex = list.indexOf(newName);
|
|
|
|
list[oldIndex] = newName;
|
|
|
|
|
|
|
|
// If there was already an entry with the new name, we are merging entries.
|
|
|
|
// Thus, if the old entry was selected, also select the new entry.
|
2018-08-27 09:49:16 +00:00
|
|
|
if (newIndex >= 0 && items[oldIndex].checked)
|
|
|
|
items[newIndex].checked = true;
|
2017-12-27 08:02:52 +00:00
|
|
|
setStringList(list);
|
2017-11-26 09:42:22 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 21:21:58 +00:00
|
|
|
void LocationFilterModel::addName(const QString &newName)
|
|
|
|
{
|
|
|
|
// If any item is checked and a new location is added, add the name
|
|
|
|
// of the new location in front of the list and mark it as checked.
|
|
|
|
// Thus, on subsequent repopulation of the list, the new entry will
|
|
|
|
// be registered as already checked.
|
|
|
|
QStringList list = stringList();
|
|
|
|
if (!anyChecked || newName.isEmpty() || list.indexOf(newName) >= 0)
|
|
|
|
return;
|
|
|
|
list.prepend(newName);
|
2018-08-27 09:49:16 +00:00
|
|
|
items.insert(items.begin(), { true });
|
2017-12-27 08:02:52 +00:00
|
|
|
setStringList(list);
|
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),
|
2016-04-05 05:02:03 +00:00
|
|
|
curr_dive_site(NULL)
|
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
|
|
|
{
|
2015-05-26 20:42:45 +00:00
|
|
|
if (curr_dive_site) {
|
2018-08-14 18:16:25 +00:00
|
|
|
dive_site *ds = get_dive_site_by_uuid(d->dive_site_uuid);
|
2015-08-21 00:29:24 +00:00
|
|
|
if (!ds)
|
|
|
|
return false;
|
2018-02-17 20:21:16 +00:00
|
|
|
return same_string(ds->name, curr_dive_site->name) || ds->uuid == curr_dive_site->uuid;
|
2015-05-26 20:42:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 22:30:49 +00:00
|
|
|
if (models.isEmpty())
|
2015-05-26 20:42:45 +00:00
|
|
|
return true;
|
|
|
|
|
2018-08-14 18:16:25 +00:00
|
|
|
for (const FilterModelBase *model: models) {
|
|
|
|
if (!model->doFilter(d))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
|
|
|
|
{
|
|
|
|
QModelIndex index0 = sourceModel()->index(source_row, 0, source_parent);
|
|
|
|
QVariant diveVariant = sourceModel()->data(index0, DiveTripModel::DIVE_ROLE);
|
|
|
|
struct dive *d = (struct dive *)diveVariant.value<void *>();
|
|
|
|
|
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
|
|
|
|
QVariant tripVariant = sourceModel()->data(index0, DiveTripModel::TRIP_ROLE);
|
|
|
|
dive_trip *trip = (dive_trip *)tripVariant.value<void *>();
|
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
|
|
|
|
for (d = trip->dives; d; d = d->next) {
|
|
|
|
if (!d->hidden_by_filter)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void MultiFilterSortModel::myInvalidate()
|
|
|
|
{
|
2016-07-09 19:45:55 +00:00
|
|
|
#if !defined(SUBSURFACE_MOBILE)
|
2014-11-13 18:31:03 +00:00
|
|
|
int i;
|
|
|
|
struct dive *d;
|
2016-07-07 19:55:17 +00:00
|
|
|
DiveListView *dlv = MainWindow::instance()->dive_list();
|
2014-11-13 18:31:03 +00:00
|
|
|
|
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
|
|
|
|
2014-11-13 18:31:03 +00:00
|
|
|
// first make sure the trips are no longer shown as selected
|
|
|
|
// (but without updating the selection state of the dives... this just cleans
|
|
|
|
// up an oddity in the filter handling)
|
|
|
|
// TODO: This should go internally to DiveList, to be triggered after a filter is due.
|
|
|
|
dlv->clearTripSelection();
|
|
|
|
|
|
|
|
// if we have no more selected dives, clean up the display - this later triggers us
|
|
|
|
// to pick one of the dives that are shown in the list as selected dive which is the
|
|
|
|
// natural behavior
|
|
|
|
if (amount_selected == 0) {
|
|
|
|
MainWindow::instance()->cleanUpEmpty();
|
|
|
|
} else {
|
|
|
|
// otherwise find the dives that should still be selected (the filter above unselected any
|
|
|
|
// dive that's no longer visible) and select them again
|
2014-11-13 20:51:23 +00:00
|
|
|
QList<int> curSelectedDives;
|
2014-11-13 18:31:03 +00:00
|
|
|
for_each_dive (i, d) {
|
2014-11-13 20:51:23 +00:00
|
|
|
if (d->selected)
|
2014-11-13 18:31:03 +00:00
|
|
|
curSelectedDives.append(get_divenr(d));
|
|
|
|
}
|
|
|
|
dlv->selectDives(curSelectedDives);
|
|
|
|
}
|
2014-11-16 16:04:06 +00:00
|
|
|
|
|
|
|
emit filterFinished();
|
2015-05-26 21:03:37 +00:00
|
|
|
|
|
|
|
if (curr_dive_site) {
|
|
|
|
dlv->expandAll();
|
|
|
|
}
|
2016-07-09 19:45:55 +00:00
|
|
|
#endif
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-23 13:55:59 +00:00
|
|
|
void MultiFilterSortModel::addFilterModel(FilterModelBase *model)
|
2014-11-13 18:31:03 +00:00
|
|
|
{
|
|
|
|
models.append(model);
|
2017-12-27 21:27:13 +00:00
|
|
|
connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(myInvalidate()));
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-23 13:55:59 +00:00
|
|
|
void MultiFilterSortModel::removeFilterModel(FilterModelBase *model)
|
2014-11-13 18:31:03 +00:00
|
|
|
{
|
|
|
|
models.removeAll(model);
|
2017-12-27 21:27:13 +00:00
|
|
|
disconnect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(myInvalidate()));
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiFilterSortModel::clearFilter()
|
|
|
|
{
|
2017-12-23 13:55:59 +00:00
|
|
|
Q_FOREACH (FilterModelBase *iface, models) {
|
2014-11-13 18:31:03 +00:00
|
|
|
iface->clearFilter();
|
|
|
|
}
|
|
|
|
myInvalidate();
|
|
|
|
}
|
2015-05-26 20:36:06 +00:00
|
|
|
|
2015-05-26 20:42:45 +00:00
|
|
|
void MultiFilterSortModel::startFilterDiveSite(uint32_t uuid)
|
2015-05-26 20:36:06 +00:00
|
|
|
{
|
|
|
|
curr_dive_site = get_dive_site_by_uuid(uuid);
|
|
|
|
myInvalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiFilterSortModel::stopFilterDiveSite()
|
|
|
|
{
|
|
|
|
curr_dive_site = NULL;
|
|
|
|
myInvalidate();
|
|
|
|
}
|