diff --git a/desktop-widgets/divesiteimportdialog.cpp b/desktop-widgets/divesiteimportdialog.cpp index 237f3d5ea..63163279f 100644 --- a/desktop-widgets/divesiteimportdialog.cpp +++ b/desktop-widgets/divesiteimportdialog.cpp @@ -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(std::make_unique(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() diff --git a/qt-models/divesiteimportmodel.cpp b/qt-models/divesiteimportmodel.cpp index 731f6bf47..7408ebcdc 100644 --- a/qt-models/divesiteimportmodel.cpp +++ b/qt-models/divesiteimportmodel.cpp @@ -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(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() << Qt::CheckStateRole << SELECTED); + dataChanged(index(clickedIndex.row(), 0), index(clickedIndex.row(), 0), QVector { Qt::CheckStateRole, SELECTED }); } void DivesiteImportedModel::selectAll() { std::fill(checkStates.begin(), checkStates.end(), true); - dataChanged(index(0, 0), index(lastIndex - firstIndex, 0), QVector() << 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 { Qt::CheckStateRole, SELECTED }); } void DivesiteImportedModel::selectRow(int row) { checkStates[row] = !checkStates[row]; - dataChanged(index(row, 0), index(row, 0), QVector() << Qt::CheckStateRole << SELECTED); + dataChanged(index(row, 0), index(row, 0), QVector { Qt::CheckStateRole, SELECTED }); } void DivesiteImportedModel::selectNone() { std::fill(checkStates.begin(), checkStates.end(), false); - dataChanged(index(0, 0), index(lastIndex - firstIndex,0 ), QVector() << 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 { 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(); -} diff --git a/qt-models/divesiteimportmodel.h b/qt-models/divesiteimportmodel.h index 8b1b13b20..5da80223f 100644 --- a/qt-models/divesiteimportmodel.h +++ b/qt-models/divesiteimportmodel.h @@ -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 checkStates; // char instead of bool to avoid silly pessimization of std::vector. - dive_site_table *importedSitesTable; + dive_site_table &importedSitesTable; }; #endif