mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-17 22:16:16 +00:00
Selection: move translation of indexes to filter model
The DiveListView caught signals from the DiveTripModel with the corresponding indexes. However, the DiveListView is actually connected to the MultiFilterSortModel and thus has to translate the indexes. Instead, catch the signals in the MultiFilterSortModel, transform them and resend. Let the DiveListView get its signal from the MultiFilterSortModel. Yes, this makes things less efficient because there is an extra signal. On the upside, the makes data-flow much more logical. Selection will have to be fixed anyway. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
a431840075
commit
e46b1e88d9
3 changed files with 44 additions and 32 deletions
|
@ -37,7 +37,10 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
|
||||||
setItemDelegate(new DiveListDelegate(this));
|
setItemDelegate(new DiveListDelegate(this));
|
||||||
setUniformRowHeights(true);
|
setUniformRowHeights(true);
|
||||||
setItemDelegateForColumn(DiveTripModelBase::RATING, new StarWidgetsDelegate(this));
|
setItemDelegateForColumn(DiveTripModelBase::RATING, new StarWidgetsDelegate(this));
|
||||||
setModel(MultiFilterSortModel::instance());
|
MultiFilterSortModel *m = MultiFilterSortModel::instance();
|
||||||
|
setModel(m);
|
||||||
|
connect(m, &MultiFilterSortModel::selectionChanged, this, &DiveListView::diveSelectionChanged);
|
||||||
|
connect(m, &MultiFilterSortModel::currentDiveChanged, this, &DiveListView::currentDiveChanged);
|
||||||
|
|
||||||
setSortingEnabled(true);
|
setSortingEnabled(true);
|
||||||
setContextMenuPolicy(Qt::DefaultContextMenu);
|
setContextMenuPolicy(Qt::DefaultContextMenu);
|
||||||
|
@ -83,14 +86,8 @@ DiveListView::~DiveListView()
|
||||||
|
|
||||||
void DiveListView::resetModel()
|
void DiveListView::resetModel()
|
||||||
{
|
{
|
||||||
MultiFilterSortModel::instance()->resetModel(currentLayout);
|
MultiFilterSortModel *m = MultiFilterSortModel::instance();
|
||||||
// If the model was reset, we have to reconnect the signals and tell
|
m->resetModel(currentLayout);
|
||||||
// the filter model to update its source model.
|
|
||||||
DiveTripModelBase *m = DiveTripModelBase::instance();
|
|
||||||
connect(m, &DiveTripModelBase::selectionChanged, this, &DiveListView::diveSelectionChanged);
|
|
||||||
connect(m, &DiveTripModelBase::currentDiveChanged, this, &DiveListView::currentDiveChanged);
|
|
||||||
// Get the initial selection
|
|
||||||
m->initSelection();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiveListView::calculateInitialColumnWidth(int col)
|
void DiveListView::calculateInitialColumnWidth(int col)
|
||||||
|
@ -201,25 +198,14 @@ void DiveListView::reset()
|
||||||
void DiveListView::diveSelectionChanged(const QVector<QModelIndex> &indexes)
|
void DiveListView::diveSelectionChanged(const QVector<QModelIndex> &indexes)
|
||||||
{
|
{
|
||||||
clearSelection();
|
clearSelection();
|
||||||
MultiFilterSortModel *m = MultiFilterSortModel::instance();
|
|
||||||
QItemSelectionModel *s = selectionModel();
|
QItemSelectionModel *s = selectionModel();
|
||||||
for (const QModelIndex &index: indexes) {
|
for (const QModelIndex &index: indexes) {
|
||||||
// We have to transform the indices into local indices, since
|
s->select(index, QItemSelectionModel::Rows | QItemSelectionModel::Select);
|
||||||
// there might be sorting or filtering in effect.
|
|
||||||
QModelIndex localIndex = m->mapFromSource(index);
|
|
||||||
|
|
||||||
// It might be possible that the item is not shown (filter is
|
|
||||||
// in effect). Then we get an invalid index and should ignore
|
|
||||||
// this selection.
|
|
||||||
if (!localIndex.isValid())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
s->select(localIndex, QItemSelectionModel::Rows | QItemSelectionModel::Select);
|
|
||||||
|
|
||||||
// If an item of a not-yet expanded trip is selected, expand the trip.
|
// If an item of a not-yet expanded trip is selected, expand the trip.
|
||||||
if (localIndex.parent().isValid() && !isExpanded(localIndex.parent())) {
|
if (index.parent().isValid() && !isExpanded(index.parent())) {
|
||||||
setAnimated(false);
|
setAnimated(false);
|
||||||
expand(localIndex.parent());
|
expand(index.parent());
|
||||||
setAnimated(true);
|
setAnimated(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -229,17 +215,12 @@ void DiveListView::diveSelectionChanged(const QVector<QModelIndex> &indexes)
|
||||||
|
|
||||||
void DiveListView::currentDiveChanged(QModelIndex index)
|
void DiveListView::currentDiveChanged(QModelIndex index)
|
||||||
{
|
{
|
||||||
// Transform the index into a local index, since
|
// Set the currently activated row.
|
||||||
// there might be sorting or filtering in effect.
|
|
||||||
MultiFilterSortModel *m = MultiFilterSortModel::instance();
|
|
||||||
QModelIndex localIndex = m->mapFromSource(index);
|
|
||||||
|
|
||||||
// Then, set the currently activated row.
|
|
||||||
// Note, we have to use the QItemSelectionModel::Current mode to avoid
|
// Note, we have to use the QItemSelectionModel::Current mode to avoid
|
||||||
// changing our selection (in contrast to Qt's documentation, which
|
// changing our selection (in contrast to Qt's documentation, which
|
||||||
// instructs to use QItemSelectionModel::NoUpdate, which results in
|
// instructs to use QItemSelectionModel::NoUpdate, which results in
|
||||||
// funny side-effects).
|
// funny side-effects).
|
||||||
selectionModel()->setCurrentIndex(localIndex, QItemSelectionModel::Current);
|
selectionModel()->setCurrentIndex(index, QItemSelectionModel::Current);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If rows are added, check which of these rows is a trip and expand the first column
|
// If rows are added, check which of these rows is a trip and expand the first column
|
||||||
|
|
|
@ -24,8 +24,33 @@ void MultiFilterSortModel::resetModel(DiveTripModelBase::Layout layout)
|
||||||
{
|
{
|
||||||
DiveTripModelBase::resetModel(layout);
|
DiveTripModelBase::resetModel(layout);
|
||||||
// DiveTripModelBase::resetModel() generates a new instance.
|
// DiveTripModelBase::resetModel() generates a new instance.
|
||||||
// Thus, the source model must be reset.
|
// Thus, the source model must be reset and the connections must be reset.
|
||||||
setSourceModel(DiveTripModelBase::instance());
|
DiveTripModelBase *m = DiveTripModelBase::instance();
|
||||||
|
setSourceModel(m);
|
||||||
|
connect(m, &DiveTripModelBase::selectionChanged, this, &MultiFilterSortModel::selectionChangedSlot);
|
||||||
|
connect(m, &DiveTripModelBase::currentDiveChanged, this, &MultiFilterSortModel::currentDiveChangedSlot);
|
||||||
|
m->initSelection();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Translate selection into local indexes and re-emit signal
|
||||||
|
void MultiFilterSortModel::selectionChangedSlot(const QVector<QModelIndex> &indexes)
|
||||||
|
{
|
||||||
|
QVector<QModelIndex> indexesLocal;
|
||||||
|
indexesLocal.reserve(indexes.size());
|
||||||
|
for (const QModelIndex &index: indexes) {
|
||||||
|
QModelIndex local = mapFromSource(index);
|
||||||
|
if (local.isValid())
|
||||||
|
indexesLocal.push_back(local);
|
||||||
|
}
|
||||||
|
emit selectionChanged(indexesLocal);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Translate current dive into local indexes and re-emit signal
|
||||||
|
void MultiFilterSortModel::currentDiveChangedSlot(QModelIndex index)
|
||||||
|
{
|
||||||
|
QModelIndex local = mapFromSource(index);
|
||||||
|
if (local.isValid())
|
||||||
|
emit currentDiveChanged(mapFromSource(index));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
|
bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
|
||||||
|
|
|
@ -14,6 +14,12 @@ public:
|
||||||
bool lessThan(const QModelIndex &, const QModelIndex &) const override;
|
bool lessThan(const QModelIndex &, const QModelIndex &) const override;
|
||||||
|
|
||||||
void resetModel(DiveTripModelBase::Layout layout);
|
void resetModel(DiveTripModelBase::Layout layout);
|
||||||
|
signals:
|
||||||
|
void selectionChanged(const QVector<QModelIndex> &indexes);
|
||||||
|
void currentDiveChanged(QModelIndex index);
|
||||||
|
private slots:
|
||||||
|
void selectionChangedSlot(const QVector<QModelIndex> &indexes);
|
||||||
|
void currentDiveChangedSlot(QModelIndex index);
|
||||||
private:
|
private:
|
||||||
MultiFilterSortModel(QObject *parent = 0);
|
MultiFilterSortModel(QObject *parent = 0);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue