From cab0147093b829d971ca0a1ce92e9d881440311f Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 27 Apr 2019 16:53:13 +0200 Subject: [PATCH] Cleanup: implement proper Qt-model semantics in WeightInfoModel - Use a beginResetModel()/endResetModel() pair instead of distinct addRows / removeRows pairs. - Reuse the update function in the constructor(). - Let "rows" be the number of rows, not the number of rows minus one. - Remove updateInfo() function as it does the same as update(). Signed-off-by: Berthold Stoeger --- desktop-widgets/mainwindow.cpp | 2 +- qt-models/weightsysteminfomodel.cpp | 44 +++++------------------------ qt-models/weightsysteminfomodel.h | 1 - 3 files changed, 8 insertions(+), 39 deletions(-) diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index 0760e747d..9ff6808c5 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -425,7 +425,7 @@ void MainWindow::refreshDisplay(bool doRecreateDiveList) setApplicationState("Default"); diveList->setEnabled(true); diveList->setFocus(); - WSInfoModel::instance()->updateInfo(); + WSInfoModel::instance()->update(); ui.actionAutoGroup->setChecked(autogroup); } diff --git a/qt-models/weightsysteminfomodel.cpp b/qt-models/weightsysteminfomodel.cpp index 58cc0ba47..21d6f538f 100644 --- a/qt-models/weightsysteminfomodel.cpp +++ b/qt-models/weightsysteminfomodel.cpp @@ -68,50 +68,20 @@ QVariant WSInfoModel::data(const QModelIndex &index, int role) const int WSInfoModel::rowCount(const QModelIndex&) const { - return rows + 1; + return rows; } -WSInfoModel::WSInfoModel() : rows(-1) +WSInfoModel::WSInfoModel() { setHeaderDataStrings(QStringList() << tr("Description") << tr("kg")); - struct ws_info_t *info; - for (info = ws_info; info->name && info < ws_info + MAX_WS_INFO; info++, rows++) - ; - - if (rows > -1) { - beginInsertRows(QModelIndex(), 0, rows); - endInsertRows(); - } -} - -void WSInfoModel::updateInfo() -{ - struct ws_info_t *info; - beginRemoveRows(QModelIndex(), 0, this->rows); - endRemoveRows(); - rows = -1; - for (info = ws_info; info->name && info < ws_info + MAX_WS_INFO; info++, rows++) - ; - - if (rows > -1) { - beginInsertRows(QModelIndex(), 0, rows); - endInsertRows(); - } + update(); } void WSInfoModel::update() { - if (rows > -1) { - beginRemoveRows(QModelIndex(), 0, rows); - endRemoveRows(); - rows = -1; - } - struct ws_info_t *info; - for (info = ws_info; info->name && info < ws_info + MAX_WS_INFO; info++, rows++) + beginResetModel(); + rows = 0; + for (struct ws_info_t *info = ws_info; info->name && info < ws_info + MAX_WS_INFO; info++, rows++) ; - - if (rows > -1) { - beginInsertRows(QModelIndex(), 0, rows); - endInsertRows(); - } + endResetModel(); } diff --git a/qt-models/weightsysteminfomodel.h b/qt-models/weightsysteminfomodel.h index 6aeb59262..6e572f337 100644 --- a/qt-models/weightsysteminfomodel.h +++ b/qt-models/weightsysteminfomodel.h @@ -22,7 +22,6 @@ public: bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; void clear(); void update(); - void updateInfo(); private: int rows;