#4924 AppFwk : Add support for editable combo box

This commit is contained in:
Magne Sjaastad 2019-10-24 21:03:17 +02:00
parent 580d826922
commit 32128f0bf3
7 changed files with 168 additions and 24 deletions

View File

@ -20,6 +20,8 @@ void caf::PdmFieldUiCap<FieldType>::setValueFromUiEditor(const QVariant& uiValue
{
QVariant oldUiBasedQVariant = toUiBasedQVariant();
bool setUiValueDirectly = false;
// Check whether we are handling selections of values or actual values
if (m_optionEntryCache.size())
{
@ -77,17 +79,22 @@ void caf::PdmFieldUiCap<FieldType>::setValueFromUiEditor(const QVariant& uiValue
}
else
{
// We are not getting indexes as expected from the UI. For now assert, to catch this condition
// but it should possibly be handled as setting the values explicitly. The code for that is below the assert
CAF_ASSERT(false);
typename FieldType::FieldDataType value;
PdmUiFieldSpecialization<typename FieldType::FieldDataType>::setFromVariant(uiValue, value);
m_field->setValue(value);
m_optionEntryCache.clear();
// We are not getting indexes as usually expected when an option cache is present.
// This situation can occur if a text field is edited by a combobox allowing user defined input
// when a history of recently used strings are stored in a field of string
setUiValueDirectly = true;
}
}
else
{ // Not an option based GUI, the uiValue is a real field value
{
// Not an option based GUI, the uiValue is a real field value
setUiValueDirectly = true;
}
if (setUiValueDirectly)
{
typename FieldType::FieldDataType value;
PdmUiFieldSpecialization<typename FieldType::FieldDataType>::setFromVariant(uiValue, value);
m_field->setValue(value);

View File

@ -59,6 +59,9 @@ set( PROJECT_FILES
CustomObjectEditor.h
MenuItemProducer.cpp
MenuItemProducer.h
TamComboBox.h
TamComboBox.cpp
)

View File

@ -6,6 +6,7 @@
#include "CustomObjectEditor.h"
#include "ManyGroups.h"
#include "MenuItemProducer.h"
#include "TamComboBox.h"
#include "WidgetLayoutTest.h"
#include "cafAppEnum.h"
@ -16,6 +17,7 @@
#include "cafCmdSelectionHelper.h"
#endif
#include "cafCmdFeatureMenuBuilder.h"
#include "cafFilePath.h"
#include "cafPdmDocument.h"
#include "cafPdmObject.h"
@ -37,7 +39,6 @@
#include "cafPdmUiTreeView.h"
#include "cafSelectionManager.h"
#include "cafCmdFeatureMenuBuilder.h"
#include <QAction>
#include <QDockWidget>
#include <QFileDialog>
@ -1166,6 +1167,9 @@ void MainWindow::buildTestModel()
SingleEditorPdmObject* singleEditorObj = new SingleEditorPdmObject;
m_testRoot->objects.push_back(singleEditorObj);
auto tamComboBox = new TamComboBox;
m_testRoot->objects.push_back(tamComboBox);
DemoPdmObject* demoObj2 = new DemoPdmObject;
demoObject->m_textField = "Mitt Demo Obj";

View File

@ -0,0 +1,72 @@
#include "TamComboBox.h"
#include "cafPdmUiComboBoxEditor.h"
CAF_PDM_SOURCE_INIT(TamComboBox, "TamComboBox");
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TamComboBox::TamComboBox()
{
CAF_PDM_InitObject("Cell Filter", "", "", "");
CAF_PDM_InitField(&m_name, "UserDescription", QString("Filter Name"), "Name", "", "", "");
m_name.uiCapability()->setUiEditorTypeName(caf::PdmUiComboBoxEditor::uiEditorTypeName());
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QList<caf::PdmOptionItemInfo> TamComboBox::calculateValueOptions(const caf::PdmFieldHandle* fieldNeedingOptions,
bool* useOptionsOnly)
{
QList<caf::PdmOptionItemInfo> options;
for (const auto& s : m_historyItems)
{
options.push_back(caf::PdmOptionItemInfo(s, s));
}
return options;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void TamComboBox::fieldChangedByUi(const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue)
{
if (changedField == &m_name)
{
QString text = m_name();
if (m_historyItems.indexOf(text) == -1)
{
m_historyItems.push_front(m_name);
while (m_historyItems.size() > 5)
{
m_historyItems.pop_back();
}
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void TamComboBox::defineUiOrdering(QString uiConfigName, caf::PdmUiOrdering& uiOrdering) {}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void TamComboBox::defineEditorAttribute(const caf::PdmFieldHandle* field,
QString uiConfigName,
caf::PdmUiEditorAttribute* attribute)
{
auto attr = dynamic_cast<caf::PdmUiComboBoxEditorAttribute*>(attribute);
if (attr)
{
attr->enableEditableContent = true;
}
}

View File

@ -0,0 +1,39 @@
#pragma once
#include "cafAppEnum.h"
#include "cafPdmField.h"
#include "cafPdmObject.h"
#include <QStringList>
//==================================================================================================
///
///
//==================================================================================================
class TamComboBox : public caf::PdmObject
{
CAF_PDM_HEADER_INIT;
public:
TamComboBox();
virtual QList<caf::PdmOptionItemInfo> calculateValueOptions(const caf::PdmFieldHandle* fieldNeedingOptions,
bool* useOptionsOnly) override;
virtual void
fieldChangedByUi(const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue) override;
private:
caf::PdmField<QString> m_name;
protected:
virtual void defineUiOrdering(QString uiConfigName, caf::PdmUiOrdering& uiOrdering) override;
virtual void defineEditorAttribute(const caf::PdmFieldHandle* field,
QString uiConfigName,
caf::PdmUiEditorAttribute* attribute) override;
private:
QStringList m_historyItems;
};

View File

@ -37,9 +37,9 @@
#include "cafPdmUiComboBoxEditor.h"
#include "cafPdmField.h"
#include "cafPdmObject.h"
#include "cafPdmUiFieldEditorHandle.h"
#include "cafPdmField.h"
#include "cafFactory.h"
#include "cafQShortenedLabel.h"
@ -180,11 +180,10 @@ void PdmUiComboBoxEditor::configureAndUpdateUi(const QString& uiConfigName)
}
// Handle attributes
PdmUiComboBoxEditorAttribute attributes;
caf::PdmUiObjectHandle* uiObject = uiObj(uiField()->fieldHandle()->ownerObject());
if (uiObject)
{
uiObject->editorAttribute(uiField()->fieldHandle(), uiConfigName, &attributes);
uiObject->editorAttribute(uiField()->fieldHandle(), uiConfigName, &m_attributes);
}
if (!m_comboBox.isNull())
@ -211,14 +210,19 @@ void PdmUiComboBoxEditor::configureAndUpdateUi(const QString& uiConfigName)
m_comboBox->setCurrentIndex(0);
}
if (attributes.adjustWidthToContents)
if (m_attributes.adjustWidthToContents)
{
m_comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
}
else if (attributes.minimumContentsLength > 0)
else if (m_attributes.minimumContentsLength > 0)
{
m_comboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
m_comboBox->setMinimumContentsLength(attributes.minimumContentsLength);
m_comboBox->setMinimumContentsLength(m_attributes.minimumContentsLength);
}
if (m_attributes.enableEditableContent)
{
m_comboBox->setEditable(true);
}
m_comboBox->blockSignals(false);
@ -226,7 +230,7 @@ void PdmUiComboBoxEditor::configureAndUpdateUi(const QString& uiConfigName)
if (!m_layout.isNull())
{
if (attributes.showPreviousAndNextButtons)
if (m_attributes.showPreviousAndNextButtons)
{
if (m_previousItemButton.isNull())
{
@ -268,14 +272,14 @@ void PdmUiComboBoxEditor::configureAndUpdateUi(const QString& uiConfigName)
}
// Update button texts
if (!attributes.nextButtonText.isEmpty())
if (!m_attributes.nextButtonText.isEmpty())
{
m_nextItemButton->setToolTip(attributes.nextButtonText);
m_nextItemButton->setToolTip(m_attributes.nextButtonText);
}
if (!attributes.prevButtonText.isEmpty())
if (!m_attributes.prevButtonText.isEmpty())
{
m_previousItemButton->setToolTip(attributes.prevButtonText);
m_previousItemButton->setToolTip(m_attributes.prevButtonText);
}
}
else
@ -397,11 +401,21 @@ QWidget* PdmUiComboBoxEditor::createLabelWidget(QWidget * parent)
//--------------------------------------------------------------------------------------------------
void PdmUiComboBoxEditor::slotIndexActivated(int index)
{
QVariant v;
v = index;
if (m_attributes.enableEditableContent)
{
// Use the text directly, as the selected item text could be entered directly by the user
QVariant uintValue(v.toUInt());
this->setValueToField(uintValue);
auto text = m_comboBox->itemText(index);
this->setValueToField(text);
}
else
{
QVariant v;
v = index;
QVariant uintValue(v.toUInt());
this->setValueToField(uintValue);
}
}
//--------------------------------------------------------------------------------------------------

View File

@ -61,6 +61,7 @@ public:
adjustWidthToContents = false;
showPreviousAndNextButtons = false;
minimumContentsLength = 8;
enableEditableContent = false;
}
public:
@ -71,6 +72,8 @@ public:
QString nextButtonText;
QString prevButtonText;
bool enableEditableContent;
};
@ -106,6 +109,8 @@ private:
QPointer<QToolButton> m_nextItemButton;
QPointer<QHBoxLayout> m_layout;
QPointer<QWidget> m_placeholder;
PdmUiComboBoxEditorAttribute m_attributes;
};