#4116 PdmUiListView: Use timer and singleShot to trigger scroll to selected item

This commit is contained in:
Magne Sjaastad 2019-02-22 10:03:24 +01:00
parent 26f4577803
commit 5a45d37ee1
6 changed files with 50 additions and 19 deletions

View File

@ -68,6 +68,21 @@ caf::PdmUiFormLayoutObjectEditor::~PdmUiFormLayoutObjectEditor()
CAF_ASSERT(m_fieldViews.size() == 0);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void caf::PdmUiFormLayoutObjectEditor::scrollToSelectedItemsInFieldEditors() const
{
for (auto fieldView : m_fieldViews)
{
auto listEditor = dynamic_cast<PdmUiListEditor*>(fieldView.second);
if (listEditor)
{
listEditor->scrollToSelectedItem();
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -476,19 +491,6 @@ void caf::PdmUiFormLayoutObjectEditor::configureAndUpdateUi(const QString& uiCon
{
uiObject->onEditorWidgetsCreated();
}
// Process events to make sure the layout has completed before scrolling the list editor to current item
// If this step is omitted, the scrollTo method ends up at arbitrary positions
//qApp->processEvents();
for (it = m_fieldViews.begin(); it != m_fieldViews.end(); ++it)
{
auto myObj = dynamic_cast<PdmUiListEditor*>(it->second);
if (myObj)
{
myObj->ensureCurrentItemIsVisible();
}
}
}
//--------------------------------------------------------------------------------------------------

View File

@ -66,6 +66,8 @@ public:
PdmUiFormLayoutObjectEditor();
~PdmUiFormLayoutObjectEditor() override;
void scrollToSelectedItemsInFieldEditors() const;
protected:
/// When overriding this function, use findOrCreateGroupBox() or findOrCreateFieldEditor() for detailed control
/// Use recursivelyConfigureAndUpdateUiItemsInGridLayoutColumn() for automatic layout of group and field widgets

View File

@ -152,7 +152,7 @@ PdmUiListEditor::~PdmUiListEditor()
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiListEditor::ensureCurrentItemIsVisible()
void PdmUiListEditor::scrollToSelectedItem() const
{
if (m_isScrollToItemAllowed)
{

View File

@ -81,7 +81,7 @@ public:
PdmUiListEditor();
~PdmUiListEditor() override;
void ensureCurrentItemIsVisible();
void scrollToSelectedItem() const;
protected:
QWidget* createEditorWidget(QWidget * parent) override;

View File

@ -44,6 +44,7 @@
#include <QHBoxLayout>
#include <QScrollArea>
#include <QScrollBar>
#include "QTimer"
@ -99,6 +100,9 @@ PdmUiPropertyView::PdmUiPropertyView(QWidget* parent, Qt::WindowFlags f)
dummy->addWidget(scrollArea);
m_currentObjectView = nullptr;
m_scrollToSelectedItemTimer = new QTimer(this);
connect(m_scrollToSelectedItemTimer, SIGNAL(timeout()), this, SLOT(slotScrollToSelectedItemsInFieldEditors()));
}
//--------------------------------------------------------------------------------------------------
@ -168,8 +172,7 @@ void PdmUiPropertyView::showProperties( PdmObjectHandle* object)
if (!m_currentObjectView)
{
PdmUiDefaultObjectEditor* defaultEditor = new PdmUiDefaultObjectEditor();
m_currentObjectView = defaultEditor;
m_currentObjectView = new PdmUiDefaultObjectEditor();
}
// Create widget to handle this
@ -185,6 +188,26 @@ void PdmUiPropertyView::showProperties( PdmObjectHandle* object)
m_currentObjectView->setPdmObject(object);
m_currentObjectView->updateUi(m_uiConfigName);
if (object)
{
if (!m_scrollToSelectedItemTimer->isActive())
{
m_scrollToSelectedItemTimer->setSingleShot(true);
m_scrollToSelectedItemTimer->start(100);
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiPropertyView::slotScrollToSelectedItemsInFieldEditors()
{
if (m_currentObjectView)
{
m_currentObjectView->scrollToSelectedItemsInFieldEditors();
}
}
//--------------------------------------------------------------------------------------------------

View File

@ -42,6 +42,7 @@
#include <QPointer>
class QVBoxLayout;
class QTimer;
#include <QScrollArea>
@ -62,7 +63,7 @@ namespace caf
{
class PdmObjectHandle;
class PdmUiWidgetObjectEditorHandle;
class PdmUiDefaultObjectEditor;
//==================================================================================================
///
@ -82,13 +83,16 @@ public:
public slots:
void showProperties(caf::PdmObjectHandle* object); // Signal/Slot system needs caf:: prefix in some cases
void slotScrollToSelectedItemsInFieldEditors();
private:
PdmUiWidgetObjectEditorHandle* m_currentObjectView;
PdmUiDefaultObjectEditor* m_currentObjectView;
QString m_uiConfigName;
QPointer<QVBoxLayout> m_placeHolderLayout;
QPointer<QWidget> m_placeholder;
QTimer* m_scrollToSelectedItemTimer;
};