Cleanup: remove DiveListView::fixMessyQtModelBehaviour()

The function DiveListView::fixMessyQtModelBehaviour() was used to
expand the first columns of dive-trips in the dive-list view.
This function was called everytime that the dive-list was modified.
It is kind of ludicrous that external callers would have to
tell the DiveListView, when it has to update its column headers.

Instead, place this functionality in the overriden reset() and
rowsInserted() functions, as these are the only ways that
rows can be added. Change the DiveTripModel to use the proper
beginResetModel()/endResetModel() pair instead of the previous
full deletion and full repopulation using the beginRemoveRows()/
endRemoveRows() and beginInsertRows()/endInsertRows().

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-07-29 17:40:53 +02:00 committed by Dirk Hohndel
parent 5e1e6110ce
commit 4bdd811f06
5 changed files with 28 additions and 23 deletions

View file

@ -39,7 +39,6 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
DiveTripModel *tripModel = new DiveTripModel(this);
model->setSourceModel(tripModel);
setModel(model);
connect(model, SIGNAL(layoutChanged()), this, SLOT(fixMessyQtModelBehaviour()));
setSortingEnabled(false);
setContextMenuPolicy(Qt::DefaultContextMenu);
@ -160,12 +159,34 @@ void DiveListView::restoreExpandedRows()
setExpanded(model()->index(i, 0), true);
setAnimated(true);
}
void DiveListView::fixMessyQtModelBehaviour()
// If the model is reset, check which items are trip-items and expand the first column
void DiveListView::reset()
{
// First, let the QTreeView do its thing.
QTreeView::reset();
QAbstractItemModel *m = model();
for (int i = 0; i < model()->rowCount(); i++)
for (int i = 0; i < m->rowCount(); ++i) {
if (m->rowCount(m->index(i, 0)) != 0)
setFirstColumnSpanned(i, QModelIndex(), true);
}
}
// If rows are added, check which of these rows is a trip and expand the first column
void DiveListView::rowsInserted(const QModelIndex &parent, int start, int end)
{
// First, let the QTreeView do its thing.
QTreeView::rowsInserted(parent, start, end);
// Now check for each inserted row whether this is a trip and expand the first column
if (parent.isValid()) // Trips don't have a parent
return;
QAbstractItemModel *m = model();
for (int i = start; i <= end; ++i) {
if (m->rowCount(m->index(i, 0)) != 0)
setFirstColumnSpanned(i, QModelIndex(), true);
}
}
// this only remembers dives that were selected, not trips
@ -435,9 +456,6 @@ void DiveListView::reload(DiveTripModel::Layout layout, bool forceSort)
setAnimated(true);
}
}
if (currentLayout == DiveTripModel::TREE) {
fixMessyQtModelBehaviour();
}
}
void DiveListView::reloadHeaderActions()
@ -627,7 +645,6 @@ void DiveListView::merge_trip(const QModelIndex &a, int offset)
combine_trips(trip_a, trip_b);
rememberSelection();
reload(currentLayout, false);
fixMessyQtModelBehaviour();
restoreSelection();
mark_divelist_changed(true);
//TODO: emit a signal to signalize that the divelist changed?
@ -658,7 +675,6 @@ void DiveListView::removeFromTrip()
rememberSelection();
reload(currentLayout, false);
fixMessyQtModelBehaviour();
restoreSelection();
mark_divelist_changed(true);
}
@ -679,7 +695,6 @@ void DiveListView::newTripAbove()
}
trip->expanded = 1;
reload(currentLayout, false);
fixMessyQtModelBehaviour();
mark_divelist_changed(true);
restoreSelection();
}
@ -730,7 +745,6 @@ void DiveListView::addToTrip(int delta)
reload(currentLayout, false);
restoreSelection();
fixMessyQtModelBehaviour();
}
void DiveListView::markDiveInvalid()
@ -756,7 +770,6 @@ void DiveListView::markDiveInvalid()
// select top dive that isn't marked invalid
rememberSelection();
}
fixMessyQtModelBehaviour();
}
void DiveListView::deleteDive()
@ -787,7 +800,6 @@ void DiveListView::deleteDive()
selectDive(lastDiveNr);
rememberSelection();
}
fixMessyQtModelBehaviour();
}
void DiveListView::contextMenuEvent(QContextMenuEvent *event)

View file

@ -45,7 +45,8 @@ slots:
void removeFromTrip();
void deleteDive();
void markDiveInvalid();
void fixMessyQtModelBehaviour();
void rowsInserted(const QModelIndex &parent, int start, int end) override;
void reset() override;
void mergeTripAbove();
void mergeTripBelow();
void newTripAbove();

View file

@ -174,7 +174,6 @@ void RenumberDialog::buttonClicked(QAbstractButton *button)
UndoRenumberDives *undoCommand = new UndoRenumberDives(renumberedDives);
MainWindow::instance()->undoStack->push(undoCommand);
MainWindow::instance()->dive_list()->fixMessyQtModelBehaviour();
mark_divelist_changed(true);
MainWindow::instance()->dive_list()->restoreSelection();
}

View file

@ -584,10 +584,7 @@ void DiveTripModel::setupModelData()
{
int i = dive_table.nr;
if (rowCount()) {
beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
endRemoveRows();
}
beginResetModel();
if (autogroup)
autogroup_dives();
@ -621,10 +618,7 @@ void DiveTripModel::setupModelData()
tripItem->children.push_back(diveItem);
}
if (rowCount()) {
beginInsertRows(QModelIndex(), 0, rowCount() - 1);
endInsertRows();
}
endResetModel();
}
DiveTripModel::Layout DiveTripModel::layout() const

View file

@ -430,7 +430,6 @@ void MultiFilterSortModel::myInvalidate()
divesDisplayed = 0;
invalidateFilter();
MainWindow::instance()->dive_list()->fixMessyQtModelBehaviour();
// first make sure the trips are no longer shown as selected
// (but without updating the selection state of the dives... this just cleans