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

@ -13,7 +13,7 @@
DivesiteImportDialog::DivesiteImportDialog(dive_site_table imported, QString source, QWidget *parent) : QDialog(parent),
importedSites(std::move(imported)),
importedSource(std::move(source)),
divesiteImportedModel(std::make_unique<DivesiteImportedModel>())
divesiteImportedModel(std::make_unique<DivesiteImportedModel>(importedSites))
{
QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_W), this);
QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q), this);
@ -40,8 +40,6 @@ DivesiteImportDialog::DivesiteImportDialog(dive_site_table imported, QString sou
connect(quit, SIGNAL(activated()), parent, SLOT(close()));
ui.ok->setEnabled(true);
divesiteImportedModel->repopulate(&importedSites);
}
DivesiteImportDialog::~DivesiteImportDialog()

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();
}

View file

@ -11,13 +11,12 @@ class DivesiteImportedModel : public QAbstractTableModel
public:
enum columnNames { NAME, LOCATION, COUNTRY, NEAREST, DISTANCE, SELECTED };
DivesiteImportedModel(QObject *parent = 0);
int columnCount(const QModelIndex& index = QModelIndex()) const;
int rowCount(const QModelIndex& index = QModelIndex()) const;
QVariant data(const QModelIndex& index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
void repopulate(dive_site_table *sites);
DivesiteImportedModel(dive_site_table &, QObject *parent = 0);
int columnCount(const QModelIndex& index = QModelIndex()) const override;
int rowCount(const QModelIndex& index = QModelIndex()) const override;
QVariant data(const QModelIndex& index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
public
slots:
void changeSelected(QModelIndex clickedIndex);
@ -29,7 +28,7 @@ private:
int firstIndex;
int lastIndex;
std::vector<char> checkStates; // char instead of bool to avoid silly pessimization of std::vector.
dive_site_table *importedSitesTable;
dive_site_table &importedSitesTable;
};
#endif