mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Mobile: Generate DiveObjectHelpers on the fly
Instead of keeping track of a list of DiveObjectHelpers, generate them on-the-fly in DiveListModel. Thus, there is less danger of model and core getting out of sync. On the flip-side, now the DiveListModel and the DiveListSortModel might get out of sync. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
be763452ad
commit
f8c5c8bedf
4 changed files with 31 additions and 32 deletions
|
@ -67,6 +67,16 @@ DiveObjectHelper::DiveObjectHelper(struct dive *d) :
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DiveObjectHelper::operator bool() const
|
||||||
|
{
|
||||||
|
return !!m_dive;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DiveObjectHelper::operator!() const
|
||||||
|
{
|
||||||
|
return !m_dive;
|
||||||
|
}
|
||||||
|
|
||||||
int DiveObjectHelper::number() const
|
int DiveObjectHelper::number() const
|
||||||
{
|
{
|
||||||
return m_dive->number;
|
return m_dive->number;
|
||||||
|
|
|
@ -50,6 +50,8 @@ class DiveObjectHelper {
|
||||||
public:
|
public:
|
||||||
DiveObjectHelper(); // This is only to be used by Qt's metatype system!
|
DiveObjectHelper(); // This is only to be used by Qt's metatype system!
|
||||||
DiveObjectHelper(struct dive *dive);
|
DiveObjectHelper(struct dive *dive);
|
||||||
|
operator bool() const; // Returns false if this is an invalid default-generated object
|
||||||
|
bool operator!() const; // Returns true if this is an invalid default-generated object
|
||||||
int number() const;
|
int number() const;
|
||||||
int id() const;
|
int id() const;
|
||||||
struct dive *getDive() const;
|
struct dive *getDive() const;
|
||||||
|
|
|
@ -51,8 +51,8 @@ void DiveListSortModel::resetFilter()
|
||||||
bool DiveListSortModel::filterAcceptsRow(int source_row, const QModelIndex &) const
|
bool DiveListSortModel::filterAcceptsRow(int source_row, const QModelIndex &) const
|
||||||
{
|
{
|
||||||
DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel());
|
DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel());
|
||||||
DiveObjectHelper *d = mySourceModel->at(source_row);
|
DiveObjectHelper d = mySourceModel->at(source_row);
|
||||||
return d && !d->getDive()->hidden_by_filter;
|
return d && !d.getDive()->hidden_by_filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DiveListSortModel::shown()
|
int DiveListSortModel::shown()
|
||||||
|
@ -148,9 +148,6 @@ void DiveListModel::addDive(const QList<dive *> &listOfDives)
|
||||||
if (listOfDives.isEmpty())
|
if (listOfDives.isEmpty())
|
||||||
return;
|
return;
|
||||||
beginInsertRows(QModelIndex(), rowCount(), rowCount() + listOfDives.count() - 1);
|
beginInsertRows(QModelIndex(), rowCount(), rowCount() + listOfDives.count() - 1);
|
||||||
for (dive *d: listOfDives) {
|
|
||||||
m_dives.append(new DiveObjectHelper(d));
|
|
||||||
}
|
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,25 +162,22 @@ void DiveListModel::addAllDives()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiveListModel::insertDive(int i, DiveObjectHelper *newDive)
|
void DiveListModel::insertDive(int i, DiveObjectHelper *)
|
||||||
{
|
{
|
||||||
beginInsertRows(QModelIndex(), i, i);
|
beginInsertRows(QModelIndex(), i, i);
|
||||||
m_dives.insert(i, newDive);
|
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiveListModel::removeDive(int i)
|
void DiveListModel::removeDive(int i)
|
||||||
{
|
{
|
||||||
beginRemoveRows(QModelIndex(), i, i);
|
beginRemoveRows(QModelIndex(), i, i);
|
||||||
delete m_dives.at(i);
|
|
||||||
m_dives.removeAt(i);
|
|
||||||
endRemoveRows();
|
endRemoveRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiveListModel::removeDiveById(int id)
|
void DiveListModel::removeDiveById(int id)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < rowCount(); i++) {
|
for (int i = 0; i < dive_table.nr; i++) {
|
||||||
if (m_dives.at(i)->id() == id) {
|
if (dive_table.dives[i]->id == id) {
|
||||||
removeDive(i);
|
removeDive(i);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -192,21 +186,16 @@ void DiveListModel::removeDiveById(int id)
|
||||||
|
|
||||||
void DiveListModel::updateDive(int i, dive *d)
|
void DiveListModel::updateDive(int i, dive *d)
|
||||||
{
|
{
|
||||||
DiveObjectHelper *newDive = new DiveObjectHelper(d);
|
|
||||||
// we need to make sure that QML knows that this dive has changed -
|
// 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
|
// the only reliable way I've found is to remove and re-insert it
|
||||||
removeDive(i);
|
removeDive(i);
|
||||||
insertDive(i, newDive);
|
insertDive(i, nullptr); // TODO: DiveObjectHelper not needed anymore - remove second argument
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiveListModel::clear()
|
void DiveListModel::clear()
|
||||||
{
|
{
|
||||||
if (m_dives.count()) {
|
beginResetModel();
|
||||||
beginRemoveRows(QModelIndex(), 0, m_dives.count() - 1);
|
endResetModel();
|
||||||
qDeleteAll(m_dives);
|
|
||||||
m_dives.clear();
|
|
||||||
endRemoveRows();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiveListModel::resetInternalData()
|
void DiveListModel::resetInternalData()
|
||||||
|
@ -221,25 +210,20 @@ void DiveListModel::resetInternalData()
|
||||||
|
|
||||||
int DiveListModel::rowCount(const QModelIndex &) const
|
int DiveListModel::rowCount(const QModelIndex &) const
|
||||||
{
|
{
|
||||||
return m_dives.count();
|
return dive_table.nr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DiveListModel::getDiveIdx(int id) const
|
int DiveListModel::getDiveIdx(int id) const
|
||||||
{
|
{
|
||||||
int i;
|
return get_idx_by_uniq_id(id);
|
||||||
for (i = 0; i < m_dives.count(); i++) {
|
|
||||||
if (m_dives.at(i)->id() == id)
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant DiveListModel::data(const QModelIndex &index, int role) const
|
QVariant DiveListModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
if(index.row() < 0 || index.row() >= m_dives.count())
|
if(index.row() < 0 || index.row() >= dive_table.nr)
|
||||||
return QVariant();
|
return QVariant();
|
||||||
|
|
||||||
DiveObjectHelper &curr_dive = *m_dives[index.row()];
|
DiveObjectHelper curr_dive(dive_table.dives[index.row()]);
|
||||||
const dive *d = curr_dive.getDive();
|
const dive *d = curr_dive.getDive();
|
||||||
if (!d)
|
if (!d)
|
||||||
return QVariant();
|
return QVariant();
|
||||||
|
@ -305,7 +289,11 @@ DiveListModel *DiveListModel::instance()
|
||||||
return m_instance;
|
return m_instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiveObjectHelper *DiveListModel::at(int i)
|
DiveObjectHelper DiveListModel::at(int i)
|
||||||
{
|
{
|
||||||
return m_dives.at(i);
|
if (i < 0 || i >= dive_table.nr) {
|
||||||
|
qWarning("DiveListModel::at(): accessing invalid dive with id %d", i);
|
||||||
|
return DiveObjectHelper(); // Returns an invalid DiveObjectHelper that will crash on access.
|
||||||
|
}
|
||||||
|
return DiveObjectHelper(dive_table.dives[i]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,9 +62,8 @@ public:
|
||||||
QHash<int, QByteArray> roleNames() const;
|
QHash<int, QByteArray> roleNames() const;
|
||||||
QString startAddDive();
|
QString startAddDive();
|
||||||
void resetInternalData();
|
void resetInternalData();
|
||||||
Q_INVOKABLE DiveObjectHelper* at(int i);
|
Q_INVOKABLE DiveObjectHelper at(int i);
|
||||||
private:
|
private:
|
||||||
QList<DiveObjectHelper*> m_dives;
|
|
||||||
static DiveListModel *m_instance;
|
static DiveListModel *m_instance;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue