From acd2cc9f792ec29c0f0d6ebe49e14663eccf55d9 Mon Sep 17 00:00:00 2001 From: Vincent <46542431+VincidaB@users.noreply.github.com> Date: Mon, 9 Dec 2024 10:38:54 -0600 Subject: [PATCH] Gui: allow LineEdit to grow with text - fixes #17747 (#18099) --- src/Gui/Tree.cpp | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Gui/Tree.cpp b/src/Gui/Tree.cpp index 877d82ffdc..2558afa57c 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -540,6 +540,32 @@ void TreeWidgetItemDelegate::initStyleOption(QStyleOptionViewItem *option, } } +class DynamicQLineEdit : public ExpLineEdit +{ +public: + DynamicQLineEdit(QWidget *parent = nullptr) : ExpLineEdit(parent) {} + + QSize sizeHint() const override + { + QSize size = QLineEdit::sizeHint(); + QFontMetrics fm(font()); + int availableWidth = parentWidget()->width() - geometry().x(); // Calculate available width + int margin = 2 * (style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1) + + 2 * style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing) + + TreeParams::getItemBackgroundPadding(); + size.setWidth(std::min(fm.horizontalAdvance(text()) + margin , availableWidth)); + return size; + } + + // resize on key presses + void keyPressEvent(QKeyEvent *event) override + { + ExpLineEdit::keyPressEvent(event); + setMinimumWidth(sizeHint().width()); + } + +}; + QWidget* TreeWidgetItemDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const { @@ -555,15 +581,15 @@ QWidget* TreeWidgetItemDelegate::createEditor( App::GetApplication().setActiveTransaction(str.str().c_str()); FC_LOG("create editor transaction " << App::GetApplication().getActiveTransaction()); - QLineEdit *editor; + DynamicQLineEdit *editor; if(TreeParams::getLabelExpression()) { - ExpLineEdit *le = new ExpLineEdit(parent); + DynamicQLineEdit *le = new DynamicQLineEdit(parent); le->setAutoApply(true); le->setFrame(false); le->bind(App::ObjectIdentifier(prop)); editor = le; } else { - editor = new QLineEdit(parent); + editor = new DynamicQLineEdit(parent); } editor->setReadOnly(prop.isReadOnly()); return editor;