Implement a way for editors to fit the label to the field.

This commit is contained in:
Gaute Lindkvist 2018-09-27 14:26:20 +02:00
parent 2e5310bf38
commit 8c5e538f01
9 changed files with 90 additions and 23 deletions

View File

@ -114,6 +114,14 @@ void PdmUiFieldEditorHandle::createWidgets(QWidget * parent)
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QMargins PdmUiFieldEditorHandle::labelContentMargins() const
{
return calculateLabelContentMargins();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -148,6 +156,15 @@ void PdmUiFieldEditorHandle::updateLabelFromField(QLabel* label, const QString&
}
}
//------------------------------------------------------------------------------------------------------------
/// Re-implement this virtual method if a custom PdmUiField is misaligned with its label.
/// See cafPdmUiLineEditor for an example.
//------------------------------------------------------------------------------------------------------------
QMargins PdmUiFieldEditorHandle::calculateLabelContentMargins() const
{
return m_labelWidget->contentsMargins();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -97,6 +97,7 @@ public:
QWidget* combinedWidget() { return m_combinedWidget; }
QWidget* editorWidget() { return m_editorWidget; }
QWidget* labelWidget() { return m_labelWidget; }
QMargins labelContentMargins() const;
protected: // Virtual interface to override
/// Implement one of these, or both editor and label. The widgets will be used in the parent layout according to
@ -109,6 +110,7 @@ protected: // Virtual interface to override
void setValueToField(const QVariant& value);
void updateLabelFromField(QLabel* label, const QString& uiConfigName = "") const;
virtual QMargins calculateLabelContentMargins() const;
private slots:
void customMenuRequested(QPoint pos);

View File

@ -175,6 +175,24 @@ void PdmUiComboBoxEditor::configureAndUpdateUi(const QString& uiConfigName)
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QMargins PdmUiComboBoxEditor::calculateLabelContentMargins() const
{
QSize editorSize = m_comboBox->sizeHint();
QSize labelSize = m_label->sizeHint();
int heightDiff = editorSize.height() - labelSize.height();
QMargins contentMargins = m_label->contentsMargins();
if (heightDiff > 0)
{
contentMargins.setTop(contentMargins.top() + heightDiff / 2);
contentMargins.setBottom(contentMargins.bottom() + heightDiff / 2);
}
return contentMargins;
}
//--------------------------------------------------------------------------------------------------
// Special class used to prevent a combo box to steal focus when scrolling
// the QScrollArea using the mouse wheel

View File

@ -87,6 +87,7 @@ protected:
virtual QWidget* createEditorWidget(QWidget * parent);
virtual QWidget* createLabelWidget(QWidget * parent);
virtual void configureAndUpdateUi(const QString& uiConfigName);
QMargins calculateLabelContentMargins() const override;
protected slots:
void slotIndexActivated(int index);

View File

@ -224,13 +224,7 @@ void caf::PdmUiFormLayoutObjectEditor::recursivelyConfigureAndUpdateUiOrderingIn
// Shift label a bit to make it appear centered on the field. Using Qt::AlignVCenter will
// cause both label and field to appear in the vertical centre of the row.
// That isn't the intention.
int labelHeight = fieldLabelWidget->sizeHint().height();
int fieldHeight = fieldEditorWidget->sizeHint().height();
int heightDiff = std::max(0, fieldHeight - labelHeight);
QMargins contentMargins = fieldLabelWidget->contentsMargins();
contentMargins.setTop(heightDiff / 2);
contentMargins.setBottom(heightDiff / 2);
fieldLabelWidget->setContentsMargins(contentMargins);
fieldLabelWidget->setContentsMargins(fieldEditor->labelContentMargins());
}
fieldEditorWidget->setParent(containerWidgetWithGridLayout); // To make sure this widget has the current group box as parent.
parentLayout->addWidget(fieldEditorWidget, currentRowIndex, currentColumn + leftLabelColumnSpan, 1, fieldColumnSpan, Qt::AlignTop);

View File

@ -151,6 +151,26 @@ namespace caf
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT(PdmUiLineEditor);
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiLineEditor::createEditorWidget(QWidget * parent)
{
m_lineEdit = new PdmUiLineEdit(parent);
connect(m_lineEdit, SIGNAL(editingFinished()), this, SLOT(slotEditingFinished()));
return m_lineEdit;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiLineEditor::createLabelWidget(QWidget * parent)
{
m_label = new QLabel(parent);
return m_label;
}
//--------------------------------------------------------------------------------------------------
///
@ -253,26 +273,22 @@ void PdmUiLineEditor::configureAndUpdateUi(const QString& uiConfigName)
}
}
//--------------------------------------------------------------------------------------------------
///
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiLineEditor::createEditorWidget(QWidget * parent)
QMargins PdmUiLineEditor::calculateLabelContentMargins() const
{
m_lineEdit = new PdmUiLineEdit(parent);
QSize editorSize = m_lineEdit->sizeHint();
QSize labelSize = m_label->sizeHint();
int heightDiff = editorSize.height() - labelSize.height();
connect(m_lineEdit, SIGNAL(editingFinished()), this, SLOT(slotEditingFinished()));
return m_lineEdit;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiLineEditor::createLabelWidget(QWidget * parent)
{
m_label = new QLabel(parent);
return m_label;
QMargins contentMargins = m_label->contentsMargins();
if (heightDiff > 0)
{
contentMargins.setTop(contentMargins.top() + heightDiff / 2);
contentMargins.setBottom(contentMargins.bottom() + heightDiff / 2);
}
return contentMargins;
}
//--------------------------------------------------------------------------------------------------

View File

@ -127,6 +127,7 @@ protected:
virtual QWidget* createEditorWidget(QWidget * parent);
virtual QWidget* createLabelWidget(QWidget * parent);
virtual void configureAndUpdateUi(const QString& uiConfigName);
QMargins calculateLabelContentMargins() const override;
protected slots:
void slotEditingFinished();

View File

@ -391,6 +391,23 @@ QWidget* PdmUiTreeSelectionEditor::createLabelWidget(QWidget * parent)
return m_label;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QMargins PdmUiTreeSelectionEditor::calculateLabelContentMargins() const
{
QSize editorSize = m_textFilterLineEdit->sizeHint();
QSize labelSize = m_label->sizeHint();
int heightDiff = editorSize.height() - labelSize.height();
QMargins contentMargins = m_label->contentsMargins();
if (heightDiff > 0)
{
contentMargins.setTop(contentMargins.top() + heightDiff / 2);
contentMargins.setBottom(contentMargins.bottom() + heightDiff / 2);
}
return contentMargins;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -98,6 +98,7 @@ protected:
virtual void configureAndUpdateUi(const QString& uiConfigName);
virtual QWidget* createEditorWidget(QWidget* parent);
virtual QWidget* createLabelWidget(QWidget* parent);
QMargins calculateLabelContentMargins() const override;
private slots:
void customMenuRequested(const QPoint& pos);