From a7aef3f4e746e0a02c75b33dd57b46929ff4187a Mon Sep 17 00:00:00 2001 From: Gaute Lindkvist Date: Mon, 25 Nov 2019 08:11:40 +0100 Subject: [PATCH] Make the combo box drop down menus fit the text content even if the widget itself doesn't. --- .../cafPdmUiComboBoxEditor.cpp | 19 +++++++++++++++++++ .../cafUserInterface/cafPdmUiComboBoxEditor.h | 11 ++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp index d615538800..6fdd736e8c 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp @@ -230,6 +230,25 @@ void PdmUiComboBoxEditor::configureAndUpdateUi(const QString& uiConfigName) { m_comboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength); m_comboBox->setMinimumContentsLength(m_attributes.minimumContentsLength); + // Make sure the popup adjusts to the content even if the widget itself doesn't + QFont font = m_comboBox->view()->font(); + + int maxTextWidth = 0; + bool labelsElided = false; + for (const PdmOptionItemInfo& option : options) + { + QString label = option.optionUiText(); + if (label.size() > m_attributes.maximumMenuContentsLength) + { + label.resize(m_attributes.maximumMenuContentsLength); + labelsElided = true; + } + maxTextWidth = std::max(maxTextWidth, QFontMetrics(font).boundingRect(label).width()); + } + + int marginWidth = m_comboBox->view()->contentsMargins().left() + m_comboBox->view()->contentsMargins().right(); + m_comboBox->view()->setMinimumWidth(maxTextWidth + marginWidth); + m_comboBox->view()->setTextElideMode(labelsElided ? Qt::ElideMiddle : Qt::ElideNone); } if (m_attributes.enableEditableContent) diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.h b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.h index abca518183..36848f5ea1 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.h +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.h @@ -61,17 +61,18 @@ public: adjustWidthToContents = false; showPreviousAndNextButtons = false; minimumContentsLength = 8; + maximumMenuContentsLength = 40; enableEditableContent = false; minimumWidth = -1; iconSize = QSize(14, 14); } public: - bool adjustWidthToContents; - bool showPreviousAndNextButtons; - int minimumContentsLength; // The length of string to adjust to if adjustWidthToContents = false. - // Set to <= 0 to ignore and use AdjustToContentsOnFirstShow instead - + bool adjustWidthToContents; + bool showPreviousAndNextButtons; + int minimumContentsLength; // The length of string to adjust to if adjustWidthToContents = false. + // Set to <= 0 to ignore and use AdjustToContentsOnFirstShow instead + int maximumMenuContentsLength; bool enableEditableContent; int minimumWidth; QString placeholderText;