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), DivesiteImportDialog::DivesiteImportDialog(dive_site_table imported, QString source, QWidget *parent) : QDialog(parent),
importedSites(std::move(imported)), importedSites(std::move(imported)),
importedSource(std::move(source)), 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 *close = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_W), this);
QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q), 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())); connect(quit, SIGNAL(activated()), parent, SLOT(close()));
ui.ok->setEnabled(true); ui.ok->setEnabled(true);
divesiteImportedModel->repopulate(&importedSites);
} }
DivesiteImportDialog::~DivesiteImportDialog() DivesiteImportDialog::~DivesiteImportDialog()

View file

@ -1,13 +1,17 @@
#include "divesiteimportmodel.h" #include "divesiteimportmodel.h"
#include "core/divelog.h" #include "core/divelog.h"
#include "core/qthelper.h" #include "core/qthelper.h"
#include "core/range.h"
#include "core/taxonomy.h" #include "core/taxonomy.h"
DivesiteImportedModel::DivesiteImportedModel(QObject *o) : QAbstractTableModel(o), DivesiteImportedModel::DivesiteImportedModel(dive_site_table &table, QObject *o) : QAbstractTableModel(o),
firstIndex(0), firstIndex(0),
lastIndex(-1), 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 int DivesiteImportedModel::columnCount(const QModelIndex &) const
@ -17,7 +21,7 @@ int DivesiteImportedModel::columnCount(const QModelIndex &) const
int DivesiteImportedModel::rowCount(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 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()) if (!index.isValid())
return QVariant(); return QVariant();
if (index.row() + firstIndex > lastIndex) if (index.row() < 0 || index.row() >= (int)importedSitesTable.size())
return QVariant(); return QVariant();
if (index.row() < 0 || index.row() >= (int)importedSitesTable->size()) struct dive_site *ds = importedSitesTable[index.row()].get();
return QVariant();
struct dive_site *ds = (*importedSitesTable)[index.row()].get();
// widgets access the model via index.column() // widgets access the model via index.column()
// Not supporting QML access via roles // Not supporting QML access via roles
@ -95,25 +97,27 @@ QVariant DivesiteImportedModel::data(const QModelIndex &index, int role) const
void DivesiteImportedModel::changeSelected(QModelIndex clickedIndex) void DivesiteImportedModel::changeSelected(QModelIndex clickedIndex)
{ {
checkStates[clickedIndex.row()] = !checkStates[clickedIndex.row()]; 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() void DivesiteImportedModel::selectAll()
{ {
std::fill(checkStates.begin(), checkStates.end(), true); 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) void DivesiteImportedModel::selectRow(int row)
{ {
checkStates[row] = !checkStates[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() void DivesiteImportedModel::selectNone()
{ {
std::fill(checkStates.begin(), checkStates.end(), false); 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 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);
return QAbstractTableModel::flags(index) | Qt::ItemIsUserCheckable; 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: public:
enum columnNames { NAME, LOCATION, COUNTRY, NEAREST, DISTANCE, SELECTED }; enum columnNames { NAME, LOCATION, COUNTRY, NEAREST, DISTANCE, SELECTED };
DivesiteImportedModel(QObject *parent = 0); DivesiteImportedModel(dive_site_table &, QObject *parent = 0);
int columnCount(const QModelIndex& index = QModelIndex()) const; int columnCount(const QModelIndex& index = QModelIndex()) const override;
int rowCount(const QModelIndex& index = QModelIndex()) const; int rowCount(const QModelIndex& index = QModelIndex()) const override;
QVariant data(const QModelIndex& index, int role) const; QVariant data(const QModelIndex& index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
Qt::ItemFlags flags(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const override;
void repopulate(dive_site_table *sites);
public public
slots: slots:
void changeSelected(QModelIndex clickedIndex); void changeSelected(QModelIndex clickedIndex);
@ -29,7 +28,7 @@ private:
int firstIndex; int firstIndex;
int lastIndex; int lastIndex;
std::vector<char> checkStates; // char instead of bool to avoid silly pessimization of std::vector. std::vector<char> checkStates; // char instead of bool to avoid silly pessimization of std::vector.
dive_site_table *importedSitesTable; dive_site_table &importedSitesTable;
}; };
#endif #endif