mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
mobile/divelist: derive MobileListModel from a base model
In analogy to the DiveTripModel split, derive MobileListModel from a base model that exports the roles, etc. This will allow us to create a second model, which nevertheless possesses the same roles and all that without too much code duplication. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
7e1ac2167b
commit
71b378aa82
2 changed files with 62 additions and 40 deletions
|
@ -2,23 +2,11 @@
|
||||||
#include "mobilelistmodel.h"
|
#include "mobilelistmodel.h"
|
||||||
#include "core/divelist.h" // for shown_dives
|
#include "core/divelist.h" // for shown_dives
|
||||||
|
|
||||||
MobileListModel::MobileListModel(DiveTripModelBase *sourceIn) :
|
MobileListModelBase::MobileListModelBase(DiveTripModelBase *sourceIn) : source(sourceIn)
|
||||||
source(sourceIn),
|
|
||||||
expandedRow(-1)
|
|
||||||
{
|
{
|
||||||
connect(source, &DiveTripModelBase::modelAboutToBeReset, this, &MobileListModel::beginResetModel);
|
|
||||||
connect(source, &DiveTripModelBase::modelReset, this, &MobileListModel::endResetModel);
|
|
||||||
connect(source, &DiveTripModelBase::rowsAboutToBeRemoved, this, &MobileListModel::prepareRemove);
|
|
||||||
connect(source, &DiveTripModelBase::rowsRemoved, this, &MobileListModel::doneRemove);
|
|
||||||
connect(source, &DiveTripModelBase::rowsAboutToBeInserted, this, &MobileListModel::prepareInsert);
|
|
||||||
connect(source, &DiveTripModelBase::rowsInserted, this, &MobileListModel::doneInsert);
|
|
||||||
connect(source, &DiveTripModelBase::rowsAboutToBeMoved, this, &MobileListModel::prepareMove);
|
|
||||||
connect(source, &DiveTripModelBase::rowsMoved, this, &MobileListModel::doneMove);
|
|
||||||
connect(source, &DiveTripModelBase::dataChanged, this, &MobileListModel::changed);
|
|
||||||
connect(&diveListNotifier, &DiveListNotifier::numShownChanged, this, &MobileListModel::shownChanged);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QHash<int, QByteArray> MobileListModel::roleNames() const
|
QHash<int, QByteArray> MobileListModelBase::roleNames() const
|
||||||
{
|
{
|
||||||
QHash<int, QByteArray> roles;
|
QHash<int, QByteArray> roles;
|
||||||
roles[DiveTripModelBase::IS_TRIP_ROLE] = "isTrip";
|
roles[DiveTripModelBase::IS_TRIP_ROLE] = "isTrip";
|
||||||
|
@ -66,6 +54,40 @@ int MobileListModel::shown() const
|
||||||
return shown_dives;
|
return shown_dives;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int MobileListModelBase::columnCount(const QModelIndex &parent) const
|
||||||
|
{
|
||||||
|
return source->columnCount(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
QModelIndex MobileListModelBase::index(int row, int column, const QModelIndex &parent) const
|
||||||
|
{
|
||||||
|
if (!hasIndex(row, column, parent))
|
||||||
|
return QModelIndex();
|
||||||
|
|
||||||
|
return createIndex(row, column);
|
||||||
|
}
|
||||||
|
|
||||||
|
QModelIndex MobileListModelBase::parent(const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
// These are flat models - there is no parent
|
||||||
|
return QModelIndex();
|
||||||
|
}
|
||||||
|
|
||||||
|
MobileListModel::MobileListModel(DiveTripModelBase *source) : MobileListModelBase(source),
|
||||||
|
expandedRow(-1)
|
||||||
|
{
|
||||||
|
connect(source, &DiveTripModelBase::modelAboutToBeReset, this, &MobileListModel::beginResetModel);
|
||||||
|
connect(source, &DiveTripModelBase::modelReset, this, &MobileListModel::endResetModel);
|
||||||
|
connect(source, &DiveTripModelBase::rowsAboutToBeRemoved, this, &MobileListModel::prepareRemove);
|
||||||
|
connect(source, &DiveTripModelBase::rowsRemoved, this, &MobileListModel::doneRemove);
|
||||||
|
connect(source, &DiveTripModelBase::rowsAboutToBeInserted, this, &MobileListModel::prepareInsert);
|
||||||
|
connect(source, &DiveTripModelBase::rowsInserted, this, &MobileListModel::doneInsert);
|
||||||
|
connect(source, &DiveTripModelBase::rowsAboutToBeMoved, this, &MobileListModel::prepareMove);
|
||||||
|
connect(source, &DiveTripModelBase::rowsMoved, this, &MobileListModel::doneMove);
|
||||||
|
connect(source, &DiveTripModelBase::dataChanged, this, &MobileListModel::changed);
|
||||||
|
connect(&diveListNotifier, &DiveListNotifier::numShownChanged, this, &MobileListModel::shownChanged);
|
||||||
|
}
|
||||||
|
|
||||||
// We want to show the newest dives first. Therefore, we have to invert
|
// We want to show the newest dives first. Therefore, we have to invert
|
||||||
// the indexes with respect to the source model. To avoid mental gymnastics
|
// the indexes with respect to the source model. To avoid mental gymnastics
|
||||||
// in the rest of the code, we do this right before sending to the
|
// in the rest of the code, we do this right before sending to the
|
||||||
|
@ -201,20 +223,6 @@ QModelIndex MobileListModel::mapToSource(const QModelIndex &idx) const
|
||||||
return sourceIndex(row - expandedRow - 1, col, expandedRow);
|
return sourceIndex(row - expandedRow - 1, col, expandedRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
QModelIndex MobileListModel::index(int row, int column, const QModelIndex &parent) const
|
|
||||||
{
|
|
||||||
if (!hasIndex(row, column, parent))
|
|
||||||
return QModelIndex();
|
|
||||||
|
|
||||||
return createIndex(row, column);
|
|
||||||
}
|
|
||||||
|
|
||||||
QModelIndex MobileListModel::parent(const QModelIndex &index) const
|
|
||||||
{
|
|
||||||
// This is a flat model - there is no parent
|
|
||||||
return QModelIndex();
|
|
||||||
}
|
|
||||||
|
|
||||||
int MobileListModel::rowCount(const QModelIndex &parent) const
|
int MobileListModel::rowCount(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
if (parent.isValid())
|
if (parent.isValid())
|
||||||
|
@ -222,11 +230,6 @@ int MobileListModel::rowCount(const QModelIndex &parent) const
|
||||||
return source->rowCount() + numSubItems();
|
return source->rowCount() + numSubItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
int MobileListModel::columnCount(const QModelIndex &parent) const
|
|
||||||
{
|
|
||||||
return source->columnCount(parent);
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariant MobileListModel::data(const QModelIndex &index, int role) const
|
QVariant MobileListModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
if (role == IsTopLevelRole)
|
if (role == IsTopLevelRole)
|
||||||
|
@ -270,7 +273,6 @@ void MobileListModel::prepareRemove(const QModelIndex &parent, int first, int la
|
||||||
beginRemoveRows(QModelIndex(), range.first, range.last);
|
beginRemoveRows(QModelIndex(), range.first, range.last);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MobileListModel::updateRowAfterRemove(const IndexRange &range, int &row)
|
void MobileListModel::updateRowAfterRemove(const IndexRange &range, int &row)
|
||||||
{
|
{
|
||||||
if (row < 0)
|
if (row < 0)
|
||||||
|
@ -534,6 +536,7 @@ MobileModels *MobileModels::instance()
|
||||||
MobileModels::MobileModels() :
|
MobileModels::MobileModels() :
|
||||||
lm(&source)
|
lm(&source)
|
||||||
{
|
{
|
||||||
|
reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
MobileListModel *MobileModels::listModel()
|
MobileListModel *MobileModels::listModel()
|
||||||
|
|
|
@ -1,10 +1,21 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
|
// This header files declares two linear models used by the mobile UI.
|
||||||
|
//
|
||||||
|
// MobileListModel presents a list of trips and optionally the dives of
|
||||||
|
// one expanded trip. It is used for quick navigation through trips.
|
||||||
|
//
|
||||||
|
// MobileDiveListModel gives a linearized view of all dives, sorted by
|
||||||
|
// trip. Even if there is temporal overlap of trips, all dives of
|
||||||
|
// a trip are listed in a contiguous block. This model is used for
|
||||||
|
// swiping through dives.
|
||||||
#ifndef MOBILELISTMODEL_H
|
#ifndef MOBILELISTMODEL_H
|
||||||
#define MOBILELISTMODEL_H
|
#define MOBILELISTMODEL_H
|
||||||
|
|
||||||
#include "divetripmodel.h"
|
#include "divetripmodel.h"
|
||||||
|
|
||||||
class MobileListModel : public QAbstractItemModel {
|
// This is the base class of the mobile-list model. All it does
|
||||||
|
// is exporting the various dive fields as roles.
|
||||||
|
class MobileListModelBase : public QAbstractItemModel {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
enum Roles {
|
enum Roles {
|
||||||
|
@ -44,6 +55,19 @@ public:
|
||||||
FirstGasRole,
|
FirstGasRole,
|
||||||
SelectedRole,
|
SelectedRole,
|
||||||
};
|
};
|
||||||
|
QHash<int, QByteArray> roleNames() const override;
|
||||||
|
protected:
|
||||||
|
DiveTripModelBase *source;
|
||||||
|
MobileListModelBase(DiveTripModelBase *source);
|
||||||
|
private:
|
||||||
|
int columnCount(const QModelIndex &parent) const override;
|
||||||
|
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
|
||||||
|
QModelIndex parent(const QModelIndex &index) const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
class MobileListModel : public MobileListModelBase {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
MobileListModel(DiveTripModelBase *source);
|
MobileListModel(DiveTripModelBase *source);
|
||||||
void expand(int row);
|
void expand(int row);
|
||||||
void unexpand();
|
void unexpand();
|
||||||
|
@ -71,14 +95,9 @@ private:
|
||||||
static void updateRowAfterRemove(const IndexRange &range, int &row);
|
static void updateRowAfterRemove(const IndexRange &range, int &row);
|
||||||
static void updateRowAfterMove(const IndexRange &range, const IndexRange &dest, int &row);
|
static void updateRowAfterMove(const IndexRange &range, const IndexRange &dest, int &row);
|
||||||
QVariant data(const QModelIndex &index, int role) const override;
|
QVariant data(const QModelIndex &index, int role) const override;
|
||||||
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
|
|
||||||
QModelIndex parent(const QModelIndex &index) const override;
|
|
||||||
int rowCount(const QModelIndex &parent) const override;
|
int rowCount(const QModelIndex &parent) const override;
|
||||||
int columnCount(const QModelIndex &parent) const override;
|
|
||||||
QHash<int, QByteArray> roleNames() const override;
|
|
||||||
int shown() const;
|
int shown() const;
|
||||||
|
|
||||||
DiveTripModelBase *source;
|
|
||||||
int expandedRow;
|
int expandedRow;
|
||||||
private slots:
|
private slots:
|
||||||
void prepareRemove(const QModelIndex &parent, int first, int last);
|
void prepareRemove(const QModelIndex &parent, int first, int last);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue