mirror of
https://github.com/OPM/ResInsight.git
synced 2025-01-08 23:23:01 -06:00
Implement a way for editors to fit the label to the field.
This commit is contained in:
parent
2e5310bf38
commit
8c5e538f01
@ -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();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user