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* combinedWidget() { return m_combinedWidget; }
QWidget* editorWidget() { return m_editorWidget; } QWidget* editorWidget() { return m_editorWidget; }
QWidget* labelWidget() { return m_labelWidget; } QWidget* labelWidget() { return m_labelWidget; }
QMargins labelContentMargins() const;
protected: // Virtual interface to override 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 /// 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 setValueToField(const QVariant& value);
void updateLabelFromField(QLabel* label, const QString& uiConfigName = "") const; void updateLabelFromField(QLabel* label, const QString& uiConfigName = "") const;
virtual QMargins calculateLabelContentMargins() const;
private slots: private slots:
void customMenuRequested(QPoint pos); 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 // Special class used to prevent a combo box to steal focus when scrolling
// the QScrollArea using the mouse wheel // the QScrollArea using the mouse wheel

View File

@ -87,6 +87,7 @@ protected:
virtual QWidget* createEditorWidget(QWidget * parent); virtual QWidget* createEditorWidget(QWidget * parent);
virtual QWidget* createLabelWidget(QWidget * parent); virtual QWidget* createLabelWidget(QWidget * parent);
virtual void configureAndUpdateUi(const QString& uiConfigName); virtual void configureAndUpdateUi(const QString& uiConfigName);
QMargins calculateLabelContentMargins() const override;
protected slots: protected slots:
void slotIndexActivated(int index); 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 // 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. // cause both label and field to appear in the vertical centre of the row.
// That isn't the intention. // That isn't the intention.
int labelHeight = fieldLabelWidget->sizeHint().height(); fieldLabelWidget->setContentsMargins(fieldEditor->labelContentMargins());
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);
} }
fieldEditorWidget->setParent(containerWidgetWithGridLayout); // To make sure this widget has the current group box as parent. 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); 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); 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())); QMargins contentMargins = m_label->contentsMargins();
if (heightDiff > 0)
return m_lineEdit; {
} contentMargins.setTop(contentMargins.top() + heightDiff / 2);
contentMargins.setBottom(contentMargins.bottom() + heightDiff / 2);
//-------------------------------------------------------------------------------------------------- }
/// return contentMargins;
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiLineEditor::createLabelWidget(QWidget * parent)
{
m_label = new QLabel(parent);
return m_label;
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

View File

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

View File

@ -391,6 +391,23 @@ QWidget* PdmUiTreeSelectionEditor::createLabelWidget(QWidget * parent)
return m_label; 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 void configureAndUpdateUi(const QString& uiConfigName);
virtual QWidget* createEditorWidget(QWidget* parent); virtual QWidget* createEditorWidget(QWidget* parent);
virtual QWidget* createLabelWidget(QWidget* parent); virtual QWidget* createLabelWidget(QWidget* parent);
QMargins calculateLabelContentMargins() const override;
private slots: private slots:
void customMenuRequested(const QPoint& pos); void customMenuRequested(const QPoint& pos);