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 "core/display.h"
|
2018-08-28 18:44:11 +00:00
|
|
|
#include "core/qthelper.h"
|
2019-05-31 14:09:14 +00:00
|
|
|
#include "core/trip.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)
|
2019-11-17 17:13:55 +00:00
|
|
|
#include "core/divefilter.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-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
|
|
|
}
|
|
|
|
|
2019-11-17 17:13:55 +00:00
|
|
|
MultiFilterSortModel::MultiFilterSortModel(QObject *parent) : QSortFilterProxyModel(parent)
|
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::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
|
|
|
|
{
|
Dive list: cache shown flag in model (quick-fix for undo-crash)
We have a very fundamental problem with data-duplication in
core and qt-models. In a particular case, this led to an easily
reproducible crash:
1) An undo command moved the last dive of a trip to another.
2) When an undo-command removed the last dive of
a trip to a different trip, the dive was removed from the
trip in the core. Then, the model was updated.
3) That lead at first to a rearrangement of the trips, because
the trip with the added dive is moved before the trip with
the removed dive.
4) In such a case, the filter-model checks the visibility of
the trip.
5) Since the trip with the removed dive has no dives in the core,
visibility was determined as false.
6) From this point on the mappings of the QSortFilterProxyModel
were messed up. Accesses led to crashes. It is unclear
whether this is a Qt bug or only a QOI issue.
As a quick-fix, cache the visibility flag of trips directly
in the Qt-models. Don't set the visibility directly in the
core, but go via the Qt-models. Thus, a more clear layering
is achieved.
In the long run, we can hopefully get rid of the data-duplication
in the models.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-22 18:48:59 +00:00
|
|
|
QAbstractItemModel *m = sourceModel();
|
|
|
|
QModelIndex index0 = m->index(source_row, 0, source_parent);
|
|
|
|
return m->data(index0, DiveTripModelBase::SHOWN_ROLE).value<bool>();
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiFilterSortModel::myInvalidate()
|
|
|
|
{
|
Dive list: cache shown flag in model (quick-fix for undo-crash)
We have a very fundamental problem with data-duplication in
core and qt-models. In a particular case, this led to an easily
reproducible crash:
1) An undo command moved the last dive of a trip to another.
2) When an undo-command removed the last dive of
a trip to a different trip, the dive was removed from the
trip in the core. Then, the model was updated.
3) That lead at first to a rearrangement of the trips, because
the trip with the added dive is moved before the trip with
the removed dive.
4) In such a case, the filter-model checks the visibility of
the trip.
5) Since the trip with the removed dive has no dives in the core,
visibility was determined as false.
6) From this point on the mappings of the QSortFilterProxyModel
were messed up. Accesses led to crashes. It is unclear
whether this is a Qt bug or only a QOI issue.
As a quick-fix, cache the visibility flag of trips directly
in the Qt-models. Don't set the visibility directly in the
core, but go via the Qt-models. Thus, a more clear layering
is achieved.
In the long run, we can hopefully get rid of the data-duplication
in the models.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-22 18:48:59 +00:00
|
|
|
QAbstractItemModel *m = sourceModel();
|
|
|
|
if (!m)
|
|
|
|
return;
|
2014-11-13 18:31:03 +00:00
|
|
|
|
2019-05-06 20:52:52 +00:00
|
|
|
{
|
|
|
|
// This marker prevents the UI from getting notifications on selection changes.
|
|
|
|
// It is active until the end of the scope.
|
|
|
|
// This is actually meant for the undo-commands, so that they can do their work
|
|
|
|
// without having the UI updated.
|
|
|
|
// Here, it is used because invalidating the filter can generate numerous
|
|
|
|
// selection changes, which do full ui reloads. Instead, do that all at once
|
|
|
|
// as a consequence of the filterFinished signal right after the local scope.
|
|
|
|
auto marker = diveListNotifier.enterCommand();
|
|
|
|
|
2019-11-17 17:13:55 +00:00
|
|
|
DiveFilter *filter = DiveFilter::instance();
|
Dive list: cache shown flag in model (quick-fix for undo-crash)
We have a very fundamental problem with data-duplication in
core and qt-models. In a particular case, this led to an easily
reproducible crash:
1) An undo command moved the last dive of a trip to another.
2) When an undo-command removed the last dive of
a trip to a different trip, the dive was removed from the
trip in the core. Then, the model was updated.
3) That lead at first to a rearrangement of the trips, because
the trip with the added dive is moved before the trip with
the removed dive.
4) In such a case, the filter-model checks the visibility of
the trip.
5) Since the trip with the removed dive has no dives in the core,
visibility was determined as false.
6) From this point on the mappings of the QSortFilterProxyModel
were messed up. Accesses led to crashes. It is unclear
whether this is a Qt bug or only a QOI issue.
As a quick-fix, cache the visibility flag of trips directly
in the Qt-models. Don't set the visibility directly in the
core, but go via the Qt-models. Thus, a more clear layering
is achieved.
In the long run, we can hopefully get rid of the data-duplication
in the models.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-22 18:48:59 +00:00
|
|
|
for (int i = 0; i < m->rowCount(QModelIndex()); ++i) {
|
|
|
|
QModelIndex idx = m->index(i, 0, QModelIndex());
|
|
|
|
|
2019-11-11 20:36:51 +00:00
|
|
|
if (m->data(idx, DiveTripModelBase::IS_TRIP_ROLE).toBool()) {
|
Dive list: cache shown flag in model (quick-fix for undo-crash)
We have a very fundamental problem with data-duplication in
core and qt-models. In a particular case, this led to an easily
reproducible crash:
1) An undo command moved the last dive of a trip to another.
2) When an undo-command removed the last dive of
a trip to a different trip, the dive was removed from the
trip in the core. Then, the model was updated.
3) That lead at first to a rearrangement of the trips, because
the trip with the added dive is moved before the trip with
the removed dive.
4) In such a case, the filter-model checks the visibility of
the trip.
5) Since the trip with the removed dive has no dives in the core,
visibility was determined as false.
6) From this point on the mappings of the QSortFilterProxyModel
were messed up. Accesses led to crashes. It is unclear
whether this is a Qt bug or only a QOI issue.
As a quick-fix, cache the visibility flag of trips directly
in the Qt-models. Don't set the visibility directly in the
core, but go via the Qt-models. Thus, a more clear layering
is achieved.
In the long run, we can hopefully get rid of the data-duplication
in the models.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-22 18:48:59 +00:00
|
|
|
// This is a trip -> loop over all dives and see if any is selected
|
|
|
|
|
|
|
|
bool showTrip = false;
|
|
|
|
for (int j = 0; j < m->rowCount(idx); ++j) {
|
|
|
|
QModelIndex idx2 = m->index(j, 0, idx);
|
|
|
|
dive *d = m->data(idx2, DiveTripModelBase::DIVE_ROLE).value<dive *>();
|
|
|
|
if (!d) {
|
|
|
|
qWarning("MultiFilterSortModel::myInvalidate(): subitem not a dive!?");
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-17 17:13:55 +00:00
|
|
|
bool show = filter->showDive(d);
|
2019-11-17 17:32:35 +00:00
|
|
|
if (show)
|
Dive list: cache shown flag in model (quick-fix for undo-crash)
We have a very fundamental problem with data-duplication in
core and qt-models. In a particular case, this led to an easily
reproducible crash:
1) An undo command moved the last dive of a trip to another.
2) When an undo-command removed the last dive of
a trip to a different trip, the dive was removed from the
trip in the core. Then, the model was updated.
3) That lead at first to a rearrangement of the trips, because
the trip with the added dive is moved before the trip with
the removed dive.
4) In such a case, the filter-model checks the visibility of
the trip.
5) Since the trip with the removed dive has no dives in the core,
visibility was determined as false.
6) From this point on the mappings of the QSortFilterProxyModel
were messed up. Accesses led to crashes. It is unclear
whether this is a Qt bug or only a QOI issue.
As a quick-fix, cache the visibility flag of trips directly
in the Qt-models. Don't set the visibility directly in the
core, but go via the Qt-models. Thus, a more clear layering
is achieved.
In the long run, we can hopefully get rid of the data-duplication
in the models.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-22 18:48:59 +00:00
|
|
|
showTrip = true;
|
|
|
|
m->setData(idx2, show, DiveTripModelBase::SHOWN_ROLE);
|
|
|
|
}
|
|
|
|
m->setData(idx, showTrip, DiveTripModelBase::SHOWN_ROLE);
|
|
|
|
} else {
|
|
|
|
dive *d = m->data(idx, DiveTripModelBase::DIVE_ROLE).value<dive *>();
|
2019-11-17 17:13:55 +00:00
|
|
|
bool show = (d != NULL) && filter->showDive(d);
|
Dive list: cache shown flag in model (quick-fix for undo-crash)
We have a very fundamental problem with data-duplication in
core and qt-models. In a particular case, this led to an easily
reproducible crash:
1) An undo command moved the last dive of a trip to another.
2) When an undo-command removed the last dive of
a trip to a different trip, the dive was removed from the
trip in the core. Then, the model was updated.
3) That lead at first to a rearrangement of the trips, because
the trip with the added dive is moved before the trip with
the removed dive.
4) In such a case, the filter-model checks the visibility of
the trip.
5) Since the trip with the removed dive has no dives in the core,
visibility was determined as false.
6) From this point on the mappings of the QSortFilterProxyModel
were messed up. Accesses led to crashes. It is unclear
whether this is a Qt bug or only a QOI issue.
As a quick-fix, cache the visibility flag of trips directly
in the Qt-models. Don't set the visibility directly in the
core, but go via the Qt-models. Thus, a more clear layering
is achieved.
In the long run, we can hopefully get rid of the data-duplication
in the models.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-22 18:48:59 +00:00
|
|
|
m->setData(idx, show, DiveTripModelBase::SHOWN_ROLE);
|
|
|
|
}
|
2019-05-06 20:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
invalidateFilter();
|
|
|
|
|
|
|
|
// Tell the dive trip model to update the displayed-counts
|
|
|
|
DiveTripModelBase::instance()->filterFinished();
|
|
|
|
countsChanged();
|
2018-08-14 22:30:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 16:04:06 +00:00
|
|
|
emit filterFinished();
|
2014-11-13 18:31:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 19:55:05 +00:00
|
|
|
bool MultiFilterSortModel::updateDive(struct dive *d)
|
|
|
|
{
|
2019-11-17 17:13:55 +00:00
|
|
|
bool newStatus = DiveFilter::instance()->showDive(d);
|
2019-11-17 17:32:35 +00:00
|
|
|
return filter_dive(d, newStatus);
|
2019-03-22 19:55:05 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-04-12 13:31:11 +00:00
|
|
|
void MultiFilterSortModel::countsChanged()
|
|
|
|
{
|
|
|
|
updateWindowTitle();
|
2019-01-25 20:30:43 +00:00
|
|
|
}
|