refs #768, allow reselection of values when a view is sorted

* reselect callbacks
* remark: Only working in some cases as sorting is part of the model, while selection is part of the view (and sorting can take place without the view knowing the model is sorted)
* allow to sort by property index
* renamed to m_sortColumn
This commit is contained in:
Klaus Basan
2016-10-17 02:49:54 +02:00
parent 93f0e6582b
commit 630fecf8e8
8 changed files with 145 additions and 36 deletions

View File

@@ -39,6 +39,6 @@ namespace BlackGui
//! Constructor
explicit CAircraftIcaoCodeView(QWidget *parent = nullptr);
};
}
}
} // ns
} // ns
#endif // guard

View File

@@ -97,11 +97,6 @@ namespace BlackGui
CViewBaseNonTemplate::CViewBaseNonTemplate(QWidget *parent) :
QTableView(parent)
{
// this->viewport()->setAttribute(Qt::WA_Hover, true);
// this->viewport()->setMouseTracking(true);
// this->setStyle(new CViewBaseProxyStyle(this, this->style()));
// this->setItemDelegate(new CViewBaseItemDelegate(this));
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &QWidget::customContextMenuRequested, this, &CViewBaseNonTemplate::ps_customMenuRequested);
connect(this, &QTableView::clicked, this, &CViewBaseNonTemplate::ps_clicked);
@@ -126,7 +121,7 @@ namespace BlackGui
bool CViewBaseNonTemplate::setParentDockWidgetInfoArea(CDockWidgetInfoArea *parentDockableWidget)
{
bool c = CEnableForDockWidgetInfoArea::setParentDockWidgetInfoArea(parentDockableWidget);
const bool c = CEnableForDockWidgetInfoArea::setParentDockWidgetInfoArea(parentDockableWidget);
return c;
}
@@ -142,7 +137,7 @@ namespace BlackGui
{
disconnect(this->m_filterWidget);
this->menuRemoveItems(MenuFilter);
if (m_filterWidget->parent() == this) { m_filterWidget->deleteLater(); }
if (this->m_filterWidget->parent() == this) { m_filterWidget->deleteLater(); }
m_filterWidget = nullptr;
}
@@ -379,8 +374,8 @@ namespace BlackGui
int CViewBaseNonTemplate::getHorizontalHeaderFontHeight() const
{
QFontMetrics m(this->getHorizontalHeaderFont());
int h = m.height();
const QFontMetrics m(this->getHorizontalHeaderFont());
const int h = m.height();
return h;
}
@@ -412,7 +407,7 @@ namespace BlackGui
void CViewBaseNonTemplate::init()
{
int fh = qRound(1.5 * this->getHorizontalHeaderFontHeight());
const int fh = qRound(1.5 * this->getHorizontalHeaderFontHeight());
this->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); // faster mode
this->horizontalHeader()->setStretchLastSection(true);
this->verticalHeader()->setDefaultSectionSize(fh); // for height
@@ -738,7 +733,10 @@ namespace BlackGui
this->fullResizeToContents();
}
}
const ContainerType selected(this->selectedObjects());
const int c = this->m_model->update(container, sort);
this->reselect(selected);
// resize after real update according to mode
if (presizeThresholdReached)
@@ -792,11 +790,11 @@ namespace BlackGui
if (container.size() > ASyncRowsCountThreshold && sort)
{
// larger container with sorting
updateContainerAsync(container, sort, resize);
this->updateContainerAsync(container, sort, resize);
}
else
{
updateContainer(container, sort, resize);
this->updateContainer(container, sort, resize);
}
}
@@ -866,6 +864,22 @@ namespace BlackGui
return c;
}
template <class ModelClass, class ContainerType, class ObjectType>
ObjectType CViewBase<ModelClass, ContainerType, ObjectType>::firstSelectedOrDefaultObject() const
{
if (this->hasSelection())
{
return this->selectedObjects().front();
}
if (this->rowCount() < 2)
{
return this->containerOrFilteredContainer().frontOrDefault();
}
// too many, not selected
return ObjectType();
}
template <class ModelClass, class ContainerType, class ObjectType>
int CViewBase<ModelClass, ContainerType, ObjectType>::updateSelected(const CPropertyIndexVariantMap &vm)
{
@@ -1045,6 +1059,30 @@ namespace BlackGui
this->m_model->setSorting(propertyIndex, order);
}
template <class ModelClass, class ContainerType, class ObjectType>
void CViewBase<ModelClass, ContainerType, ObjectType>::sortByPropertyIndex(const CPropertyIndex &propertyIndex, Qt::SortOrder order, bool reselect)
{
if (!reselect)
{
this->m_model->sortByPropertyIndex(propertyIndex, order);
}
else
{
// hack: we reselect the already selected objects
// as sorting takes place (sync/async) in the model, and the model does not know about the selection
// we do this deferred as the model sort can be asynchronously
const ContainerType selected(this->selectedObjects());
this->m_model->sortByPropertyIndex(propertyIndex, order);
if (!selected.isEmpty())
{
QTimer::singleShot(2000, [ = ]()
{
this->reselect(selected);
});
}
}
}
template <class ModelClass, class ContainerType, class ObjectType>
QJsonObject CViewBase<ModelClass, ContainerType, ObjectType>::toJson() const
{
@@ -1137,6 +1175,8 @@ namespace BlackGui
Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed");
c = connect(this->m_model, &ModelClass::changed, this, &CViewBase::onModelChanged);
Q_ASSERT_X(c, Q_FUNC_INFO, "Connect failed");
Q_UNUSED(c);
}
@@ -1199,6 +1239,12 @@ namespace BlackGui
this->m_dropIndicator = indicator;
}
template <class ModelClass, class ContainerType, class ObjectType>
void CViewBase<ModelClass, ContainerType, ObjectType>::reselect(const ContainerType &selectedObjects)
{
Q_UNUSED(selectedObjects);
}
template <class ModelClass, class ContainerType, class ObjectType>
CStatusMessage CViewBase<ModelClass, ContainerType, ObjectType>::modifyLoadedJsonData(ContainerType &data) const
{
@@ -1277,6 +1323,13 @@ namespace BlackGui
}
}
template <class ModelClass, class ContainerType, class ObjectType>
void CViewBase<ModelClass, ContainerType, ObjectType>::ps_selectedObjectsLoopback(const CVariant &selectedObjects)
{
const ContainerType selectedObjs = selectedObjects.value<ContainerType>();
this->reselect(selectedObjs);
}
template <class ModelClass, class ContainerType, class ObjectType>
bool CViewBase<ModelClass, ContainerType, ObjectType>::ps_filterDialogFinished(int status)
{

View File

@@ -50,7 +50,6 @@ class QShowEvent;
class QWidget;
namespace BlackMisc { class CWorker; }
namespace BlackGui
{
class CDockWidgetInfoArea;
@@ -147,6 +146,9 @@ namespace BlackGui
//! \copydoc BlackGui::Models::CListModelBaseNonTemplate::setSorting
virtual void setSorting(const BlackMisc::CPropertyIndex &propertyIndex, Qt::SortOrder order = Qt::AscendingOrder) = 0;
//! Sort by index
virtual void sortByPropertyIndex(const BlackMisc::CPropertyIndex &propertyIndex, Qt::SortOrder order = Qt::AscendingOrder, bool reselect = false) = 0;
//! Allow to drag and/or drop value objects
virtual void allowDragDrop(bool allowDrag, bool allowDrop) = 0;
@@ -400,6 +402,9 @@ namespace BlackGui
//! Helper method with template free signature serving as callback from threaded worker
int ps_updateContainer(const BlackMisc::CVariant &variant, bool sort, bool resize);
//! Helper method with template free signature to allow reselection of objects
virtual void ps_selectedObjectsLoopback(const BlackMisc::CVariant &selectedObjects) = 0;
//! Display the filter dialog
void ps_displayFilterDialog();
@@ -516,6 +521,9 @@ namespace BlackGui
//! Selected objects
ContainerType selectedObjects() const;
//! First selected, the only one, or default
ObjectType firstSelectedOrDefaultObject() const;
//! Update selected objects
int updateSelected(const BlackMisc::CVariant &variant, const BlackMisc::CPropertyIndex &index);
@@ -564,6 +572,7 @@ namespace BlackGui
virtual bool isDropAllowed() const override;
virtual bool acceptDrop(const QMimeData *mimeData) const override;
virtual void setSorting(const BlackMisc::CPropertyIndex &propertyIndex, Qt::SortOrder order = Qt::AscendingOrder) override;
virtual void sortByPropertyIndex(const BlackMisc::CPropertyIndex &propertyIndex, Qt::SortOrder order = Qt::AscendingOrder, bool reselect = false) override;
//! @}
//! Column count
@@ -618,6 +627,10 @@ namespace BlackGui
virtual void drawDropIndicator(bool indicator) override;
//! @}
//! Reselect given objects
//! \remark override this function to select models again
virtual void reselect(const ContainerType &selectedObjects);
//! Modify JSON data loaded in BlackGui::Views::CViewBaseNonTemplate::ps_loadJson
virtual BlackMisc::CStatusMessage modifyLoadedJsonData(ContainerType &data) const;
@@ -640,6 +653,7 @@ namespace BlackGui
virtual void ps_rowSelected(const QModelIndex &index) override;
virtual BlackMisc::CStatusMessage ps_loadJson() override;
virtual BlackMisc::CStatusMessage ps_saveJson() const override;
virtual void ps_selectedObjectsLoopback(const BlackMisc::CVariant &selectedObjects) override;
//! @}
};
} // namespace

View File

@@ -47,7 +47,14 @@ namespace BlackGui
}
template <class ModelClass, class ContainerType, class ObjectType, class KeyType>
void CViewWithDbObjects<ModelClass, ContainerType, ObjectType, KeyType>::selectDbKeys(const QList<KeyType> &keys)
void CViewWithDbObjects<ModelClass, ContainerType, ObjectType, KeyType>::selectDbKey(const KeyType &key)
{
const QSet<KeyType> set({key});
this->selectDbKeys(set);
}
template <class ModelClass, class ContainerType, class ObjectType, class KeyType>
void CViewWithDbObjects<ModelClass, ContainerType, ObjectType, KeyType>::selectDbKeys(const QSet<KeyType> &keys)
{
if (keys.isEmpty()) { return; }
this->clearSelection();
@@ -64,7 +71,15 @@ namespace BlackGui
}
template <class ModelClass, class ContainerType, class ObjectType, class KeyType>
int CViewWithDbObjects<ModelClass, ContainerType, ObjectType, KeyType>::removeDbKeys(const QList<KeyType> &keys)
QSet<KeyType> CViewWithDbObjects<ModelClass, ContainerType, ObjectType, KeyType>::selectedDbKeys() const
{
if (!this->hasSelection()) { return QSet<KeyType>(); }
const ContainerType selected(this->selectedObjects());
return selected.toDbKeySet();
}
template <class ModelClass, class ContainerType, class ObjectType, class KeyType>
int CViewWithDbObjects<ModelClass, ContainerType, ObjectType, KeyType>::removeDbKeys(const QSet<KeyType> &keys)
{
if (keys.isEmpty()) { return 0; }
if (this->isEmpty()) { return 0; }
@@ -148,6 +163,15 @@ namespace BlackGui
CViewWithDbObjects<ModelClass, ContainerType, ObjectType, KeyType>::customMenu(menuActions);
}
template <class ModelClass, class ContainerType, class ObjectType, class KeyType>
void COrderableViewWithDbObjects<ModelClass, ContainerType, ObjectType, KeyType>::reselect(const ContainerType &selectedObjects)
{
if (!selectedObjects.isEmpty())
{
this->selectDbKeys(selectedObjects.toDbKeySet());
}
}
template <class ModelClass, class ContainerType, class ObjectType, class KeyType>
void COrderableViewWithDbObjects<ModelClass, ContainerType, ObjectType, KeyType>::moveSelectedItems(int order)
{

View File

@@ -27,7 +27,7 @@
#include "blackmisc/simulation/distributor.h"
#include "blackmisc/simulation/distributorlist.h"
#include <QList>
#include <QSet>
#include <QObject>
#include <QString>
#include <QtGlobal>
@@ -40,7 +40,6 @@ class QWidget;
namespace BlackGui
{
namespace Menus { class CMenuActions; }
namespace Views
{
//! Base class for views with DB objects
@@ -54,11 +53,17 @@ namespace BlackGui
//! Get oldets object
ObjectType oldestObject() const;
//! Select given DB key
void selectDbKey(const KeyType &key);
//! Select given DB keys
void selectDbKeys(const QList<KeyType> &keys);
void selectDbKeys(const QSet<KeyType> &keys);
//! Get selected DB keys
QSet<KeyType> selectedDbKeys() const;
//! Remove keys
int removeDbKeys(const QList<KeyType> &keys);
int removeDbKeys(const QSet<KeyType> &keys);
//! Update or insert data (based on DB key)
int replaceOrAddObjectsByKey(const ContainerType &container);
@@ -82,6 +87,9 @@ namespace BlackGui
//! \copydoc BlackGui::Views::CViewBaseNonTemplate::customMenu
virtual void customMenu(BlackGui::Menus::CMenuActions &menuActions) override;
//! Reselect by DB keys
virtual void reselect(const ContainerType &selectedObjects) override;
//! Move selected items
void moveSelectedItems(int order);