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/divelistmodel.h"
|
2018-06-03 20:15:19 +00:00
|
|
|
#include "core/qthelper.h"
|
2018-10-20 01:17:39 +00:00
|
|
|
#include "core/settings/qPrefGeneral.h"
|
2018-10-22 13:00:53 +00:00
|
|
|
#include <QDateTime>
|
2015-06-09 19:20:44 +00:00
|
|
|
|
2016-01-29 02:23:14 +00:00
|
|
|
DiveListSortModel::DiveListSortModel(QObject *parent) : QSortFilterProxyModel(parent)
|
|
|
|
{
|
2018-10-22 13:00:53 +00:00
|
|
|
updateFilterState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListSortModel::updateFilterState()
|
|
|
|
{
|
|
|
|
if (filterString.isEmpty()) {
|
|
|
|
filteredRows.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// store this in local variables to avoid having to call these methods over and over
|
|
|
|
bool includeNotes = qPrefGeneral::filterFullTextNotes();
|
|
|
|
Qt::CaseSensitivity cs = qPrefGeneral::filterCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
|
2016-01-29 02:23:14 +00:00
|
|
|
|
2018-10-22 13:00:53 +00:00
|
|
|
// get the underlying model and re-calculate the filter value for each dive
|
|
|
|
DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel());
|
|
|
|
filteredRows.clear();
|
|
|
|
filteredRows.resize(mySourceModel->rowCount());
|
|
|
|
for (int i = 0; i < mySourceModel->rowCount(); i++) {
|
|
|
|
QString fullText = includeNotes? mySourceModel->at(i)->fullText() : mySourceModel->at(i)->fullTextNoNotes();
|
|
|
|
filteredRows.at(i) = fullText.contains(filterString, cs);
|
|
|
|
}
|
2018-10-24 20:21:38 +00:00
|
|
|
updateDivesShownInTrips();
|
2016-01-29 02:23:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 13:00:53 +00:00
|
|
|
void DiveListSortModel::setSourceModel(QAbstractItemModel *sourceModel)
|
|
|
|
{
|
|
|
|
QSortFilterProxyModel::setSourceModel(sourceModel);
|
|
|
|
}
|
2018-02-04 15:46:03 +00:00
|
|
|
void DiveListSortModel::setFilter(QString f)
|
|
|
|
{
|
2018-10-22 13:00:53 +00:00
|
|
|
filterString = f;
|
|
|
|
updateFilterState();
|
|
|
|
invalidateFilter();
|
2018-10-24 20:21:38 +00:00
|
|
|
updateDivesShownInTrips();
|
2018-02-04 15:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListSortModel::resetFilter()
|
|
|
|
{
|
2018-10-22 13:00:53 +00:00
|
|
|
filterString = "";
|
|
|
|
filteredRows.clear();
|
|
|
|
invalidateFilter();
|
2018-10-24 20:21:38 +00:00
|
|
|
updateDivesShownInTrips();
|
2018-10-22 13:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// filtering is way too slow on mobile. Maybe we should roll our own?
|
|
|
|
bool DiveListSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(source_parent)
|
|
|
|
|
|
|
|
return filteredRows.size() > source_row ? filteredRows[source_row] : true;
|
2018-02-04 15:46:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 10:28:53 +00:00
|
|
|
int DiveListSortModel::shown()
|
|
|
|
{
|
|
|
|
return rowCount();
|
|
|
|
}
|
|
|
|
|
2016-01-29 02:23:14 +00:00
|
|
|
int DiveListSortModel::getDiveId(int idx)
|
|
|
|
{
|
|
|
|
DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel());
|
|
|
|
return mySourceModel->getDiveId(mapToSource(index(idx,0)).row());
|
|
|
|
}
|
|
|
|
|
|
|
|
int DiveListSortModel::getIdxForId(int id)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < rowCount(); i++) {
|
|
|
|
QVariant v = data(index(i, 0), DiveListModel::DiveRole);
|
|
|
|
DiveObjectHelper *d = v.value<DiveObjectHelper *>();
|
|
|
|
if (d->id() == id)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-28 18:48:30 +00:00
|
|
|
void DiveListSortModel::clear()
|
|
|
|
{
|
|
|
|
DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel());
|
|
|
|
mySourceModel->clear();
|
2018-10-22 13:00:53 +00:00
|
|
|
filteredRows.clear();
|
2017-05-28 18:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListSortModel::addAllDives()
|
|
|
|
{
|
|
|
|
DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel());
|
|
|
|
mySourceModel->addAllDives();
|
2018-10-22 13:00:53 +00:00
|
|
|
updateFilterState();
|
2017-05-28 18:48:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 20:21:38 +00:00
|
|
|
void DiveListSortModel::updateDivesShownInTrips()
|
|
|
|
{
|
|
|
|
// if filtering is active, reset all the counts to zero, otherwise set them to the full count
|
|
|
|
struct dive_trip *dt = dive_trip_list;
|
|
|
|
int rc = rowCount();
|
|
|
|
while (dt) {
|
2018-11-08 15:58:33 +00:00
|
|
|
dt->showndives = rc ? 0 : dt->dives.nr;
|
2018-10-24 20:21:38 +00:00
|
|
|
dt = dt->next;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < rowCount(); i++) {
|
|
|
|
QVariant v = data(index(i, 0), DiveListModel::DiveRole);
|
|
|
|
DiveObjectHelper *d = v.value<DiveObjectHelper *>();
|
|
|
|
dt = d->getDive()->divetrip;
|
|
|
|
if (dt)
|
|
|
|
dt->showndives++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:20:44 +00:00
|
|
|
DiveListModel *DiveListModel::m_instance = NULL;
|
|
|
|
|
|
|
|
DiveListModel::DiveListModel(QObject *parent) : QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
m_instance = this;
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:17:37 +00:00
|
|
|
void DiveListModel::addDive(QList<dive *>listOfDives)
|
2015-06-09 19:20:44 +00:00
|
|
|
{
|
2016-04-06 04:17:37 +00:00
|
|
|
if (listOfDives.isEmpty())
|
|
|
|
return;
|
|
|
|
beginInsertRows(QModelIndex(), rowCount(), rowCount() + listOfDives.count() - 1);
|
|
|
|
foreach (dive *d, listOfDives) {
|
|
|
|
m_dives.append(new DiveObjectHelper(d));
|
|
|
|
}
|
2015-06-09 19:20:44 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:17:37 +00:00
|
|
|
void DiveListModel::addAllDives()
|
|
|
|
{
|
|
|
|
QList<dive *>listOfDives;
|
|
|
|
int i;
|
|
|
|
struct dive *d;
|
|
|
|
for_each_dive (i, d)
|
|
|
|
listOfDives.append(d);
|
|
|
|
addDive(listOfDives);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-27 19:27:41 +00:00
|
|
|
void DiveListModel::insertDive(int i, DiveObjectHelper *newDive)
|
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(), i, i);
|
|
|
|
m_dives.insert(i, newDive);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListModel::removeDive(int i)
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), i, i);
|
2018-01-07 09:53:13 +00:00
|
|
|
delete m_dives.at(i);
|
2016-01-27 19:27:41 +00:00
|
|
|
m_dives.removeAt(i);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2016-01-29 14:25:13 +00:00
|
|
|
void DiveListModel::removeDiveById(int id)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < rowCount(); i++) {
|
|
|
|
if (m_dives.at(i)->id() == id) {
|
|
|
|
removeDive(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 19:50:04 +00:00
|
|
|
void DiveListModel::updateDive(int i, dive *d)
|
|
|
|
{
|
|
|
|
DiveObjectHelper *newDive = new DiveObjectHelper(d);
|
2018-06-25 08:39:56 +00:00
|
|
|
// we need to make sure that QML knows that this dive has changed -
|
|
|
|
// the only reliable way I've found is to remove and re-insert it
|
|
|
|
removeDive(i);
|
|
|
|
insertDive(i, newDive);
|
2015-12-08 06:23:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 18:09:46 +00:00
|
|
|
void DiveListModel::clear()
|
|
|
|
{
|
2015-12-03 20:22:18 +00:00
|
|
|
if (m_dives.count()) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_dives.count() - 1);
|
2016-01-07 18:01:24 +00:00
|
|
|
qDeleteAll(m_dives);
|
2015-12-03 20:22:18 +00:00
|
|
|
m_dives.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
2015-11-30 18:09:46 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 15:08:25 +00:00
|
|
|
void DiveListModel::resetInternalData()
|
|
|
|
{
|
|
|
|
// this is a hack. There is a long standing issue, that seems related to a
|
|
|
|
// sync problem between QML engine and underlying model data. It causes delete
|
|
|
|
// from divelist (on mobile) to crash. But not always. This function is part of
|
|
|
|
// an attempt to fix this. See commit.
|
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:20:44 +00:00
|
|
|
int DiveListModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return m_dives.count();
|
|
|
|
}
|
|
|
|
|
2016-01-29 02:23:14 +00:00
|
|
|
int DiveListModel::getDiveId(int idx) const
|
|
|
|
{
|
|
|
|
if (idx < 0 || idx >= m_dives.count())
|
|
|
|
return -1;
|
|
|
|
return m_dives[idx]->id();
|
|
|
|
}
|
|
|
|
|
2016-03-02 12:41:36 +00:00
|
|
|
int DiveListModel::getDiveIdx(int id) const
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < m_dives.count(); i++) {
|
|
|
|
if (m_dives.at(i)->id() == id)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:20:44 +00:00
|
|
|
QVariant DiveListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if(index.row() < 0 || index.row() > m_dives.count())
|
|
|
|
return QVariant();
|
|
|
|
|
2016-01-07 18:01:24 +00:00
|
|
|
DiveObjectHelper *curr_dive = m_dives[index.row()];
|
|
|
|
switch(role) {
|
|
|
|
case DiveRole: return QVariant::fromValue<QObject*>(curr_dive);
|
|
|
|
case DiveDateRole: return (qlonglong)curr_dive->timestamp();
|
2018-10-16 11:56:41 +00:00
|
|
|
case FullTextRole: return curr_dive->fullText();
|
2018-10-20 01:16:50 +00:00
|
|
|
case FullTextNoNotesRole: return curr_dive->fullTextNoNotes();
|
2016-01-07 18:01:24 +00:00
|
|
|
}
|
2015-06-09 19:20:44 +00:00
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> DiveListModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles;
|
2016-01-07 18:01:24 +00:00
|
|
|
roles[DiveRole] = "dive";
|
|
|
|
roles[DiveDateRole] = "date";
|
2018-10-16 11:56:41 +00:00
|
|
|
roles[FullTextRole] = "fulltext";
|
2018-10-20 01:16:50 +00:00
|
|
|
roles[FullTextNoNotesRole] = "fulltextnonotes";
|
2015-06-09 19:20:44 +00:00
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
2015-12-27 05:34:45 +00:00
|
|
|
// create a new dive. set the current time and add it to the end of the dive list
|
2015-12-27 05:37:18 +00:00
|
|
|
QString DiveListModel::startAddDive()
|
2015-08-12 12:01:39 +00:00
|
|
|
{
|
2015-08-16 08:57:18 +00:00
|
|
|
struct dive *d;
|
|
|
|
d = alloc_dive();
|
2015-12-27 05:34:45 +00:00
|
|
|
d->when = QDateTime::currentMSecsSinceEpoch() / 1000L + gettimezoneoffset();
|
2016-01-29 19:53:22 +00:00
|
|
|
|
|
|
|
// find the highest dive nr we have and pick the next one
|
|
|
|
struct dive *pd;
|
|
|
|
int i, nr = 0;
|
|
|
|
for_each_dive(i, pd) {
|
|
|
|
if (pd->number > nr)
|
|
|
|
nr = pd->number;
|
|
|
|
}
|
|
|
|
nr++;
|
2015-12-27 05:34:45 +00:00
|
|
|
d->number = nr;
|
2016-01-01 08:32:30 +00:00
|
|
|
d->dc.model = strdup("manually added dive");
|
2015-12-27 05:34:45 +00:00
|
|
|
add_single_dive(-1, d);
|
2016-03-02 17:02:00 +00:00
|
|
|
insertDive(get_idx_by_uniq_id(d->id), new DiveObjectHelper(d));
|
2015-12-27 05:37:18 +00:00
|
|
|
return QString::number(d->id);
|
2015-08-12 12:01:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 19:20:44 +00:00
|
|
|
DiveListModel *DiveListModel::instance()
|
|
|
|
{
|
|
|
|
return m_instance;
|
|
|
|
}
|
2016-01-07 18:01:24 +00:00
|
|
|
|
2016-01-29 14:25:13 +00:00
|
|
|
DiveObjectHelper* DiveListModel::at(int i)
|
|
|
|
{
|
2016-01-07 18:01:24 +00:00
|
|
|
return m_dives.at(i);
|
2016-01-27 19:27:41 +00:00
|
|
|
}
|