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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-04-27 16:53:13 +02:00 committed by Dirk Hohndel
parent 36d8dcc3bf
commit cab0147093
3 changed files with 8 additions and 39 deletions

View file

@ -425,7 +425,7 @@ void MainWindow::refreshDisplay(bool doRecreateDiveList)
setApplicationState("Default"); setApplicationState("Default");
diveList->setEnabled(true); diveList->setEnabled(true);
diveList->setFocus(); diveList->setFocus();
WSInfoModel::instance()->updateInfo(); WSInfoModel::instance()->update();
ui.actionAutoGroup->setChecked(autogroup); ui.actionAutoGroup->setChecked(autogroup);
} }

View file

@ -68,50 +68,20 @@ QVariant WSInfoModel::data(const QModelIndex &index, int role) const
int WSInfoModel::rowCount(const QModelIndex&) const int WSInfoModel::rowCount(const QModelIndex&) const
{ {
return rows + 1; return rows;
} }
WSInfoModel::WSInfoModel() : rows(-1) WSInfoModel::WSInfoModel()
{ {
setHeaderDataStrings(QStringList() << tr("Description") << tr("kg")); setHeaderDataStrings(QStringList() << tr("Description") << tr("kg"));
struct ws_info_t *info; update();
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();
}
} }
void WSInfoModel::update() void WSInfoModel::update()
{ {
if (rows > -1) { beginResetModel();
beginRemoveRows(QModelIndex(), 0, rows); rows = 0;
endRemoveRows(); for (struct ws_info_t *info = ws_info; info->name && info < ws_info + MAX_WS_INFO; info++, rows++)
rows = -1;
}
struct ws_info_t *info;
for (info = ws_info; info->name && info < ws_info + MAX_WS_INFO; info++, rows++)
; ;
endResetModel();
if (rows > -1) {
beginInsertRows(QModelIndex(), 0, rows);
endInsertRows();
}
} }

View file

@ -22,7 +22,6 @@ public:
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
void clear(); void clear();
void update(); void update();
void updateInfo();
private: private:
int rows; int rows;