import: initialize DiveSiteImportModel in constructor

The old code would construct and then initialize the object
in a separate function, which added lots of complication.

Just initialize the thing in the constructor, store a
reference, not a pointer to the table. And do a few other
code cleanups. The result is distinctly more pleasing.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-05-12 22:09:18 +02:00 committed by bstoeger
parent 512eada468
commit 858a0aecba
3 changed files with 23 additions and 35 deletions

View file

@ -1,13 +1,17 @@
#include "divesiteimportmodel.h"
#include "core/divelog.h"
#include "core/qthelper.h"
#include "core/range.h"
#include "core/taxonomy.h"
DivesiteImportedModel::DivesiteImportedModel(QObject *o) : QAbstractTableModel(o),
DivesiteImportedModel::DivesiteImportedModel(dive_site_table &table, QObject *o) : QAbstractTableModel(o),
firstIndex(0),
lastIndex(-1),
importedSitesTable(nullptr)
importedSitesTable(table)
{
checkStates.resize(importedSitesTable.size());
for (const auto &[row, item]: enumerated_range(importedSitesTable))
checkStates[row] = !divelog.sites->get_by_gps(&item->location);
}
int DivesiteImportedModel::columnCount(const QModelIndex &) const
@ -17,7 +21,7 @@ int DivesiteImportedModel::columnCount(const QModelIndex &) const
int DivesiteImportedModel::rowCount(const QModelIndex &) const
{
return lastIndex - firstIndex + 1;
return static_cast<int>(importedSitesTable.size());
}
QVariant DivesiteImportedModel::headerData(int section, Qt::Orientation orientation, int role) const
@ -47,12 +51,10 @@ QVariant DivesiteImportedModel::data(const QModelIndex &index, int role) const
if (!index.isValid())
return QVariant();
if (index.row() + firstIndex > lastIndex)
if (index.row() < 0 || index.row() >= (int)importedSitesTable.size())
return QVariant();
if (index.row() < 0 || index.row() >= (int)importedSitesTable->size())
return QVariant();
struct dive_site *ds = (*importedSitesTable)[index.row()].get();
struct dive_site *ds = importedSitesTable[index.row()].get();
// widgets access the model via index.column()
// Not supporting QML access via roles
@ -95,25 +97,27 @@ QVariant DivesiteImportedModel::data(const QModelIndex &index, int role) const
void DivesiteImportedModel::changeSelected(QModelIndex clickedIndex)
{
checkStates[clickedIndex.row()] = !checkStates[clickedIndex.row()];
dataChanged(index(clickedIndex.row(), 0), index(clickedIndex.row(), 0), QVector<int>() << Qt::CheckStateRole << SELECTED);
dataChanged(index(clickedIndex.row(), 0), index(clickedIndex.row(), 0), QVector<int> { Qt::CheckStateRole, SELECTED });
}
void DivesiteImportedModel::selectAll()
{
std::fill(checkStates.begin(), checkStates.end(), true);
dataChanged(index(0, 0), index(lastIndex - firstIndex, 0), QVector<int>() << Qt::CheckStateRole << SELECTED);
// Qt is mad: for empty lists, last index would be -1, but that makes it crash.
dataChanged(index(0, 0), index(rowCount() - 1, 0), QVector<int> { Qt::CheckStateRole, SELECTED });
}
void DivesiteImportedModel::selectRow(int row)
{
checkStates[row] = !checkStates[row];
dataChanged(index(row, 0), index(row, 0), QVector<int>() << Qt::CheckStateRole << SELECTED);
dataChanged(index(row, 0), index(row, 0), QVector<int> { Qt::CheckStateRole, SELECTED });
}
void DivesiteImportedModel::selectNone()
{
std::fill(checkStates.begin(), checkStates.end(), false);
dataChanged(index(0, 0), index(lastIndex - firstIndex,0 ), QVector<int>() << Qt::CheckStateRole << SELECTED);
// Qt is mad: for empty lists, last index would be -1, but that makes it crash.
dataChanged(index(0, 0), index(rowCount() - 1, 0), QVector<int> { Qt::CheckStateRole, SELECTED });
}
Qt::ItemFlags DivesiteImportedModel::flags(const QModelIndex &index) const
@ -122,16 +126,3 @@ Qt::ItemFlags DivesiteImportedModel::flags(const QModelIndex &index) const
return QAbstractTableModel::flags(index);
return QAbstractTableModel::flags(index) | Qt::ItemIsUserCheckable;
}
void DivesiteImportedModel::repopulate(dive_site_table *sites)
{
beginResetModel();
importedSitesTable = sites;
firstIndex = 0;
lastIndex = (int)importedSitesTable->size() - 1; // Qt: the "last index" is negative for empty lists. Insane.
checkStates.resize(importedSitesTable->size());
for (size_t row = 0; row < importedSitesTable->size(); row++)
checkStates[row] = !divelog.sites->get_by_gps(&(*importedSitesTable)[row]->location);
endResetModel();
}