mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-10 23:46:00 -06:00
#4116 PdmUiListView: Scroll to the first selected item when activated
This solution does not always work, but scrolls to the first selected item in most cases
This commit is contained in:
parent
760fcaef43
commit
24bda0fda2
@ -1,6 +1,7 @@
|
||||
|
||||
#include "ManyGroups.h"
|
||||
#include "cafPdmUiTreeSelectionEditor.h"
|
||||
#include "cafPdmUiListEditor.h"
|
||||
|
||||
CAF_PDM_SOURCE_INIT(ManyGroups, "LargeObject");
|
||||
|
||||
@ -34,6 +35,9 @@ ManyGroups::ManyGroups()
|
||||
m_multiSelectList.v().push_back("First");
|
||||
m_multiSelectList.v().push_back("Second");
|
||||
m_multiSelectList.v().push_back("Third");
|
||||
|
||||
CAF_PDM_InitField(&m_singleStringWithManySelectableItems, "m_singleStringWithManySelectableItems", QString(""), "Text with many items", "", "Text tooltip", "");
|
||||
m_singleStringWithManySelectableItems.uiCapability()->setUiEditorTypeName(caf::PdmUiListEditor::uiEditorTypeName());
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -62,6 +66,18 @@ QList<caf::PdmOptionItemInfo> ManyGroups::calculateValueOptions(const caf::PdmFi
|
||||
{
|
||||
QList<caf::PdmOptionItemInfo> options;
|
||||
|
||||
// Test code used to switch between two lists with different content, but same item count
|
||||
if (fieldNeedingOptions == &m_singleStringWithManySelectableItems)
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
QString text = QString("item %1").arg(i);
|
||||
options.push_back(caf::PdmOptionItemInfo(text, text));
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
// Test code used to switch between two lists with different content, but same item count
|
||||
if (fieldNeedingOptions == &m_multiSelectList)
|
||||
{
|
||||
|
@ -18,6 +18,7 @@ public:
|
||||
caf::PdmProxyValueField<double> m_proxyDoubleField;
|
||||
|
||||
caf::PdmField<std::vector<QString> > m_multiSelectList;
|
||||
caf::PdmField<QString> m_singleStringWithManySelectableItems;
|
||||
|
||||
caf::PdmField<bool> m_toggleField;
|
||||
caf::PdmFieldHandle* objectToggleField() override;
|
||||
|
@ -41,6 +41,7 @@
|
||||
#include "cafPdmUiFieldEditorHandle.h"
|
||||
#include "cafPdmUiFieldEditorHelper.h"
|
||||
#include "cafPdmUiFieldHandle.h"
|
||||
#include "cafPdmUiListEditor.h"
|
||||
#include "cafPdmUiObjectHandle.h"
|
||||
#include "cafPdmUiOrdering.h"
|
||||
#include "cafPdmXmlObjectHandle.h"
|
||||
@ -49,8 +50,9 @@
|
||||
|
||||
#include "QMinimizePanel.h"
|
||||
|
||||
#include <QGridLayout>
|
||||
#include <QCoreApplication>
|
||||
#include <QFrame>
|
||||
#include <QGridLayout>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
@ -468,6 +470,19 @@ 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -34,7 +34,6 @@
|
||||
//
|
||||
//##################################################################################################
|
||||
|
||||
|
||||
#include "cafPdmUiListEditor.h"
|
||||
|
||||
#include "cafPdmUiDefaultObjectEditor.h"
|
||||
@ -138,7 +137,8 @@ CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT(PdmUiListEditor);
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
PdmUiListEditor::PdmUiListEditor() :
|
||||
m_isEditOperationsAvailable(true),
|
||||
m_optionItemCount(0)
|
||||
m_optionItemCount(0),
|
||||
m_isScrollToItemAllowed(true)
|
||||
{
|
||||
}
|
||||
|
||||
@ -149,6 +149,21 @@ PdmUiListEditor::~PdmUiListEditor()
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void PdmUiListEditor::ensureCurrentItemIsVisible()
|
||||
{
|
||||
if (m_isScrollToItemAllowed)
|
||||
{
|
||||
QModelIndex mi = m_listView->currentIndex();
|
||||
if (mi.isValid())
|
||||
{
|
||||
m_listView->scrollTo(mi);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -266,6 +281,8 @@ void PdmUiListEditor::configureAndUpdateUi(const QString& uiConfigName)
|
||||
|
||||
m_listView->selectionModel()->blockSignals(false);
|
||||
}
|
||||
|
||||
//ensureCurrentItemIsVisible();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -306,6 +323,8 @@ void PdmUiListEditor::slotSelectionChanged(const QItemSelection & selected, cons
|
||||
{
|
||||
if (m_optionItemCount == 0) return;
|
||||
|
||||
m_isScrollToItemAllowed = false;
|
||||
|
||||
QVariant fieldValue = uiField()->uiValue();
|
||||
if (fieldValue.type() == QVariant::Int || fieldValue.type() == QVariant::UInt)
|
||||
{
|
||||
@ -359,6 +378,8 @@ void PdmUiListEditor::slotSelectionChanged(const QItemSelection & selected, cons
|
||||
|
||||
this->setValueToField(valuesToSetInField);
|
||||
}
|
||||
|
||||
m_isScrollToItemAllowed = true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -81,6 +81,8 @@ public:
|
||||
PdmUiListEditor();
|
||||
~PdmUiListEditor() override;
|
||||
|
||||
void ensureCurrentItemIsVisible();
|
||||
|
||||
protected:
|
||||
QWidget* createEditorWidget(QWidget * parent) override;
|
||||
QWidget* createLabelWidget(QWidget * parent) override;
|
||||
@ -102,8 +104,9 @@ private:
|
||||
QPointer<QLabel> m_label;
|
||||
QPointer<QStringListModel> m_model;
|
||||
|
||||
bool m_isEditOperationsAvailable;
|
||||
int m_optionItemCount;
|
||||
bool m_isEditOperationsAvailable;
|
||||
int m_optionItemCount;
|
||||
bool m_isScrollToItemAllowed;
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user