Fixed selection of view rows when changing to filtered data

* return values for select functions
* select function must be "public override"
* reselect filtered data as well
This commit is contained in:
Klaus Basan
2019-03-23 19:56:08 +01:00
committed by Mat Sutcliffe
parent 88e82404e2
commit 30b65d6c36
7 changed files with 54 additions and 27 deletions

View File

@@ -176,9 +176,10 @@ namespace BlackGui
this->beginResetModel(); this->beginResetModel();
m_container = performSort ? sortedContainer : container; m_container = performSort ? sortedContainer : container;
this->updateFilteredContainer(); this->updateFilteredContainer(); // use sorted container for filtered if applicable
this->endResetModel(); this->endResetModel();
// reselect if implemented in specialized view
if (!selection.isEmpty()) if (!selection.isEmpty())
{ {
m_selectionModel->selectObjects(selection); m_selectionModel->selectObjects(selection);
@@ -265,6 +266,12 @@ namespace BlackGui
template <typename T, bool UseCompare> template <typename T, bool UseCompare>
void CListModelBase<T, UseCompare>::takeFilterOwnership(std::unique_ptr<IModelFilter<ContainerType> > &filter) void CListModelBase<T, UseCompare>::takeFilterOwnership(std::unique_ptr<IModelFilter<ContainerType> > &filter)
{ {
ContainerType selection;
if (m_selectionModel)
{
selection = m_selectionModel->selectedObjects();
}
if (!filter) if (!filter)
{ {
this->removeFilter(); // clear filter this->removeFilter(); // clear filter
@@ -283,6 +290,12 @@ namespace BlackGui
// invalid filter, so clear filter // invalid filter, so clear filter
this->removeFilter(); this->removeFilter();
} }
// reselect if implemented in specialized views
if (!selection.isEmpty())
{
m_selectionModel->selectObjects(selection);
}
} }
template <typename T, bool UseCompare> template <typename T, bool UseCompare>

View File

@@ -138,8 +138,8 @@ namespace BlackGui
int removeIf(K0 k0, V0 v0, KeysValues... keysValues) int removeIf(K0 k0, V0 v0, KeysValues... keysValues)
{ {
int c = m_container.removeIf(BlackMisc::Predicates::MemberEqual(k0, v0, keysValues...)); int c = m_container.removeIf(BlackMisc::Predicates::MemberEqual(k0, v0, keysValues...));
if (c > 0) { this->emitModelDataChanged();}
this->updateFilteredContainer(); this->updateFilteredContainer();
if (c > 0) { this->emitModelDataChanged();}
return c; return c;
} }

View File

@@ -583,7 +583,7 @@ namespace BlackGui
} }
m_model = model; m_model = model;
m_model->setSelectionModel(this); m_model->setSelectionModel(this); // set myself as selection model
bool c = connect(m_model, &ModelClass::modelDataChanged, this, &CViewBase::modelDataChanged); bool c = connect(m_model, &ModelClass::modelDataChanged, this, &CViewBase::modelDataChanged);
Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed"); Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed");
c = connect(m_model, &ModelClass::modelDataChangedDigest, this, &CViewBase::modelDataChangedDigest); c = connect(m_model, &ModelClass::modelDataChangedDigest, this, &CViewBase::modelDataChangedDigest);

View File

@@ -232,7 +232,7 @@ namespace BlackGui
virtual QModelIndexList unselectedRows() const; virtual QModelIndexList unselectedRows() const;
//! Select given rows //! Select given rows
void selectRows(const QSet<int> &rows); int selectRows(const QSet<int> &rows);
//! Number of selected rows //! Number of selected rows
int selectedRowCount() const; int selectedRowCount() const;
@@ -725,7 +725,7 @@ namespace BlackGui
virtual void sortByPropertyIndex(const BlackMisc::CPropertyIndex &propertyIndex, Qt::SortOrder order = Qt::AscendingOrder) override; virtual void sortByPropertyIndex(const BlackMisc::CPropertyIndex &propertyIndex, Qt::SortOrder order = Qt::AscendingOrder) override;
virtual void setNoSorting() override { m_model->setNoSorting(); } virtual void setNoSorting() override { m_model->setNoSorting(); }
virtual BlackMisc::CPropertyIndex getSortProperty() const override { return m_model->getSortProperty(); } virtual BlackMisc::CPropertyIndex getSortProperty() const override { return m_model->getSortProperty(); }
virtual int getSortColumn() const override { return m_model->getSortColumn(); } virtual int getSortColumn() const override { return m_model->getSortColumn(); }
virtual bool hasValidSortColumn() const override { return m_model->hasValidSortColumn(); } virtual bool hasValidSortColumn() const override { return m_model->hasValidSortColumn(); }
virtual Qt::SortOrder getSortOrder() const override { return m_model->getSortOrder(); } virtual Qt::SortOrder getSortOrder() const override { return m_model->getSortOrder(); }
//! @} //! @}
@@ -780,7 +780,7 @@ namespace BlackGui
//! @{ //! @{
virtual bool reachedResizeThreshold(int containrerSize = -1) const override; virtual bool reachedResizeThreshold(int containrerSize = -1) const override;
virtual void performModeBasedResizeToContent() override; virtual void performModeBasedResizeToContent() override;
virtual int performUpdateContainer(const BlackMisc::CVariant &variant, bool sort, bool resize) override; virtual int performUpdateContainer(const BlackMisc::CVariant &variant, bool sort, bool resize) override;
virtual void updateSortIndicator() override; virtual void updateSortIndicator() override;
virtual void mouseOverCallback(const QModelIndex &index, bool mouseOver) override; virtual void mouseOverCallback(const QModelIndex &index, bool mouseOver) override;
virtual void drawDropIndicator(bool indicator) override; virtual void drawDropIndicator(bool indicator) override;

View File

@@ -144,10 +144,10 @@ namespace BlackGui
this->setFilterWidgetImpl(filterWidget); this->setFilterWidgetImpl(filterWidget);
if (filterWidget) if (filterWidget)
{ {
bool s = connect(filterWidget, &CFilterWidget::changeFilter, this, &CViewBaseNonTemplate::filterWidgetChangedFilter); bool s = connect(filterWidget, &CFilterWidget::changeFilter, this, &CViewBaseNonTemplate::filterWidgetChangedFilter, Qt::QueuedConnection);
Q_ASSERT_X(s, Q_FUNC_INFO, "filter connect"); Q_ASSERT_X(s, Q_FUNC_INFO, "filter connect changeFilter");
s = connect(this, &CViewBaseNonTemplate::modelDataChanged, filterWidget, &CFilterWidget::onRowCountChanged); s = connect(this, &CViewBaseNonTemplate::modelDataChanged, filterWidget, &CFilterWidget::onRowCountChanged, Qt::QueuedConnection);
Q_ASSERT_X(s, Q_FUNC_INFO, "filter connect"); Q_ASSERT_X(s, Q_FUNC_INFO, "filter connect modelDataChanged");
Q_UNUSED(s); Q_UNUSED(s);
} }
} }
@@ -490,11 +490,11 @@ namespace BlackGui
return unselected; return unselected;
} }
void CViewBaseNonTemplate::selectRows(const QSet<int> &rows) int CViewBaseNonTemplate::selectRows(const QSet<int> &rows)
{ {
if (!this->selectionModel()) { return; } if (!this->selectionModel()) { return 0; }
// multiple times faster than multiple than this->selectRow() // multiple times faster than multiple this->selectRow()
this->clearSelection(); this->clearSelection();
QItemSelection selectedItems; QItemSelection selectedItems;
const int columns = this->model()->columnCount() - 1; const int columns = this->model()->columnCount() - 1;
@@ -503,6 +503,7 @@ namespace BlackGui
selectedItems.select(this->model()->index(r, 0), this->model()->index(r, columns)); selectedItems.select(this->model()->index(r, 0), this->model()->index(r, columns));
} }
this->selectionModel()->select(selectedItems, QItemSelectionModel::Select); this->selectionModel()->select(selectedItems, QItemSelectionModel::Select);
return selectedItems.size();
} }
int CViewBaseNonTemplate::selectedRowCount() const int CViewBaseNonTemplate::selectedRowCount() const

View File

@@ -60,16 +60,16 @@ namespace BlackGui
} }
template <class T> template <class T>
void CViewWithDbObjects<T>::selectDbKey(const KeyType &key) bool CViewWithDbObjects<T>::selectDbKey(const KeyType &key)
{ {
const QSet<KeyType> set({key}); const QSet<KeyType> set({key});
this->selectDbKeys(set); return this->selectDbKeys(set) > 0;
} }
template <class T> template <class T>
void CViewWithDbObjects<T>::selectDbKeys(const QSet<KeyType> &keys) int CViewWithDbObjects<T>::selectDbKeys(const QSet<KeyType> &keys)
{ {
if (keys.isEmpty()) { return; } if (keys.isEmpty()) { return 0; }
this->clearSelection(); this->clearSelection();
int r = -1; int r = -1;
QSet<int> rows; QSet<int> rows;
@@ -82,7 +82,7 @@ namespace BlackGui
rows.insert(r); rows.insert(r);
} }
} }
this->selectRows(rows); return this->selectRows(rows);
} }
template <class T> template <class T>
@@ -119,9 +119,17 @@ namespace BlackGui
return c; return c;
} }
template<class T>
void CViewWithDbObjects<T>::selectObjects(const ContainerType &selectedObjects)
{
if (selectedObjects.isEmpty()) { return; }
this->selectDbKeys(selectedObjects.toDbKeySet());
}
template <class T> template <class T>
void CViewWithDbObjects<T>::customMenu(Menus::CMenuActions &menuActions) void CViewWithDbObjects<T>::customMenu(Menus::CMenuActions &menuActions)
{ {
// extensions would go here
CViewBase<ModelClass>::customMenu(menuActions); CViewBase<ModelClass>::customMenu(menuActions);
} }
@@ -181,10 +189,8 @@ namespace BlackGui
template <class T> template <class T>
void COrderableViewWithDbObjects<T>::selectObjects(const ContainerType &selectedObjects) void COrderableViewWithDbObjects<T>::selectObjects(const ContainerType &selectedObjects)
{ {
if (!selectedObjects.isEmpty()) if (selectedObjects.isEmpty()) { return; }
{ this->selectDbKeys(selectedObjects.toDbKeySet());
this->selectDbKeys(selectedObjects.toDbKeySet());
}
} }
template <class T> template <class T>
@@ -232,6 +238,7 @@ namespace BlackGui
// see here for the reason of thess forward instantiations // see here for the reason of thess forward instantiations
// https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl // https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl
template class CViewWithDbObjects<BlackGui::Models::CAircraftIcaoCodeListModel>; template class CViewWithDbObjects<BlackGui::Models::CAircraftIcaoCodeListModel>;
template class CViewWithDbObjects<BlackGui::Models::CAircraftCategoryListModel>; template class CViewWithDbObjects<BlackGui::Models::CAircraftCategoryListModel>;
template class CViewWithDbObjects<BlackGui::Models::CAirlineIcaoCodeListModel>; template class CViewWithDbObjects<BlackGui::Models::CAirlineIcaoCodeListModel>;

View File

@@ -52,10 +52,10 @@ namespace BlackGui
ObjectType oldestObject() const; ObjectType oldestObject() const;
//! Select given DB key //! Select given DB key
void selectDbKey(const KeyType &key); bool selectDbKey(const KeyType &key);
//! Select given DB keys //! Select given DB keys
void selectDbKeys(const QSet<KeyType> &keys); int selectDbKeys(const QSet<KeyType> &keys);
//! Get selected DB keys //! Get selected DB keys
QSet<KeyType> selectedDbKeys() const; QSet<KeyType> selectedDbKeys() const;
@@ -66,6 +66,9 @@ namespace BlackGui
//! Update or insert data (based on DB key) //! Update or insert data (based on DB key)
int replaceOrAddObjectsByKey(const ContainerType &container); int replaceOrAddObjectsByKey(const ContainerType &container);
//! Select by DB keys
virtual void selectObjects(const ContainerType &selectedObjects) override;
protected: protected:
//! Constructor //! Constructor
explicit CViewWithDbObjects(QWidget *parent = nullptr); explicit CViewWithDbObjects(QWidget *parent = nullptr);
@@ -91,6 +94,9 @@ namespace BlackGui
//! Model DB key type //! Model DB key type
using KeyType = typename T::KeyType; using KeyType = typename T::KeyType;
//! Select by DB keys
virtual void selectObjects(const ContainerType &selectedObjects) override;
protected: protected:
//! Constructor //! Constructor
explicit COrderableViewWithDbObjects(QWidget *parent = nullptr); explicit COrderableViewWithDbObjects(QWidget *parent = nullptr);
@@ -98,9 +104,6 @@ namespace BlackGui
//! \copydoc BlackGui::Views::CViewBaseNonTemplate::customMenu //! \copydoc BlackGui::Views::CViewBaseNonTemplate::customMenu
virtual void customMenu(BlackGui::Menus::CMenuActions &menuActions) override; virtual void customMenu(BlackGui::Menus::CMenuActions &menuActions) override;
//! Reselect by DB keys
virtual void selectObjects(const ContainerType &selectedObjects) override;
//! Move selected items //! Move selected items
void moveSelectedItems(int order); void moveSelectedItems(int order);
@@ -124,4 +127,7 @@ namespace BlackGui
}; };
} // namespace } // namespace
} // namespace } // namespace
//! \endcond
#endif // guard #endif // guard