From 0a323274d48bc4233d71d94fe072ee77fb31f9d8 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Wed, 16 Aug 2023 12:36:41 +0200 Subject: [PATCH] Editor: transform the line column label into button Having a button instead of a labal makes it more obvious that there is an action triggered when clicking the label. Also fix the flickering introduced by f0bb7e8bfa9f9880a09872160c7765be1e44a594 by saving the maximum width of the sizeHint. Since the width can get big if we have a selection we do not save the size in that case to avoid blocking the width when the selection is removed again. Change-Id: If8210ecbabb8c6449c4f88652fbed99f8526057d Reviewed-by: Eike Ziller --- src/plugins/texteditor/texteditor.cpp | 29 +++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index ce6421d80c9..21aff756935 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -150,16 +150,16 @@ using ListTransformationMethod = void(QStringList &); static constexpr char dropProperty[] = "dropProp"; -class LineColumnLabel : public FixedSizeClickLabel +class LineColumnButton : public QToolButton { Q_OBJECT public: - LineColumnLabel(TextEditorWidget *parent) - : FixedSizeClickLabel(parent) + LineColumnButton(TextEditorWidget *parent) + : QToolButton(parent) , m_editor(parent) { - connect(m_editor, &QPlainTextEdit::cursorPositionChanged, this, &LineColumnLabel::update); - connect(this, &FixedSizeClickLabel::clicked, ActionManager::instance(), [this] { + connect(m_editor, &QPlainTextEdit::cursorPositionChanged, this, &LineColumnButton::update); + connect(this, &QToolButton::pressed, ActionManager::instance(), [this] { emit m_editor->activateEditor(EditorManager::IgnoreNavigationHistory); QMetaObject::invokeMethod(ActionManager::instance(), [] { if (Command *cmd = ActionManager::command(Core::Constants::GOTO)) { @@ -198,7 +198,7 @@ private: bool event(QEvent *event) override { if (event->type() != QEvent::ToolTip) - return FixedSizeClickLabel::event(event); + return QToolButton::event(event); QString tooltipText = "\n"; @@ -243,6 +243,19 @@ private: return true; } + QSize sizeHint() const override + { + const QSize size = QToolButton::sizeHint(); + auto wider = [](const QSize &left, const QSize &right) { + return left.width() < right.width(); + }; + if (m_editor->multiTextCursor().hasSelection()) + return std::max(m_maxSize, size, wider); // do not save the size if we have a selection + m_maxSize = std::max(m_maxSize, size, wider); + return m_maxSize; + } + + mutable QSize m_maxSize; TextEditorWidget *m_editor; }; @@ -714,7 +727,7 @@ public: QWidget *m_stretchWidget = nullptr; QAction *m_stretchAction = nullptr; QAction *m_toolbarOutlineAction = nullptr; - LineColumnLabel *m_cursorPositionLabel = nullptr; + LineColumnButton *m_cursorPositionLabel = nullptr; FixedSizeClickLabel *m_fileEncodingLabel = nullptr; QAction *m_fileEncodingLabelAction = nullptr; BaseTextFind *m_find = nullptr; @@ -1023,8 +1036,8 @@ TextEditorWidgetPrivate::TextEditorWidgetPrivate(TextEditorWidget *parent) m_stretchAction = m_toolBar->addWidget(m_stretchWidget); m_toolBarWidget->layout()->addWidget(m_toolBar); - m_cursorPositionLabel = new LineColumnLabel(q); const int spacing = q->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing) / 2; + m_cursorPositionLabel = new LineColumnButton(q); m_cursorPositionLabel->setContentsMargins(spacing, 0, spacing, 0); m_toolBarWidget->layout()->addWidget(m_cursorPositionLabel);