Editors: Improve the highlight of the current view

Make the overlay widget as small as possible, paint it opaque, and paint
only on the editor tool bar.

Change-Id: If48f8f7c4dd221cb605548449f5bbb1a30c25a76
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Eike Ziller
2024-05-15 14:45:17 +02:00
parent 355b8e4d1a
commit 7996c5afac
3 changed files with 39 additions and 12 deletions

View File

@@ -8,16 +8,34 @@
#include <QEvent> #include <QEvent>
#include <QPainter> #include <QPainter>
namespace Utils::Internal {
class OverlayWidgetPrivate
{
public:
OverlayWidget::PaintFunction m_paint;
OverlayWidget::ResizeFunction m_resize;
};
} // namespace Utils::Internal
Utils::OverlayWidget::OverlayWidget(QWidget *parent) Utils::OverlayWidget::OverlayWidget(QWidget *parent)
: d(new Internal::OverlayWidgetPrivate)
{ {
setAttribute(Qt::WA_TransparentForMouseEvents); setAttribute(Qt::WA_TransparentForMouseEvents);
if (parent) if (parent)
attachToWidget(parent); attachToWidget(parent);
d->m_resize = [](QWidget *w, const QSize &size) { w->setGeometry(QRect(QPoint(0, 0), size)); };
} }
Utils::OverlayWidget::~OverlayWidget() = default;
void Utils::OverlayWidget::setPaintFunction(const Utils::OverlayWidget::PaintFunction &paint) void Utils::OverlayWidget::setPaintFunction(const Utils::OverlayWidget::PaintFunction &paint)
{ {
m_paint = paint; d->m_paint = paint;
}
void Utils::OverlayWidget::setResizeFunction(const ResizeFunction &resize)
{
d->m_resize = resize;
} }
bool Utils::OverlayWidget::eventFilter(QObject *obj, QEvent *ev) bool Utils::OverlayWidget::eventFilter(QObject *obj, QEvent *ev)
@@ -29,9 +47,9 @@ bool Utils::OverlayWidget::eventFilter(QObject *obj, QEvent *ev)
void Utils::OverlayWidget::paintEvent(QPaintEvent *ev) void Utils::OverlayWidget::paintEvent(QPaintEvent *ev)
{ {
if (m_paint) { if (d->m_paint) {
QPainter p(this); QPainter p(this);
m_paint(this, p, ev); d->m_paint(this, p, ev);
} }
} }
@@ -50,5 +68,6 @@ void Utils::OverlayWidget::attachToWidget(QWidget *parent)
void Utils::OverlayWidget::resizeToParent() void Utils::OverlayWidget::resizeToParent()
{ {
QTC_ASSERT(parentWidget(), return ); QTC_ASSERT(parentWidget(), return );
setGeometry(QRect(QPoint(0, 0), parentWidget()->size())); if (d->m_resize)
d->m_resize(this, parentWidget()->size());
} }

View File

@@ -8,18 +8,26 @@
#include <QWidget> #include <QWidget>
#include <functional> #include <functional>
#include <memory>
namespace Utils { namespace Utils {
namespace Internal {
class OverlayWidgetPrivate;
}
class QTCREATOR_UTILS_EXPORT OverlayWidget : public QWidget class QTCREATOR_UTILS_EXPORT OverlayWidget : public QWidget
{ {
public: public:
using PaintFunction = std::function<void(QWidget *, QPainter &, QPaintEvent *)>; using PaintFunction = std::function<void(QWidget *, QPainter &, QPaintEvent *)>;
using ResizeFunction = std::function<void(QWidget *, QSize)>;
explicit OverlayWidget(QWidget *parent = nullptr); explicit OverlayWidget(QWidget *parent = nullptr);
~OverlayWidget();
void attachToWidget(QWidget *parent); void attachToWidget(QWidget *parent);
void setPaintFunction(const PaintFunction &paint); void setPaintFunction(const PaintFunction &paint);
void setResizeFunction(const ResizeFunction &resize);
protected: protected:
bool eventFilter(QObject *obj, QEvent *ev) override; bool eventFilter(QObject *obj, QEvent *ev) override;
@@ -28,7 +36,7 @@ protected:
private: private:
void resizeToParent(); void resizeToParent();
PaintFunction m_paint; std::unique_ptr<Internal::OverlayWidgetPrivate> d;
}; };
} // namespace Utils } // namespace Utils

View File

@@ -133,13 +133,13 @@ EditorView::EditorView(SplitterOrView *parentSplitterOrView, QWidget *parent)
auto currentViewOverlay = new OverlayWidget; auto currentViewOverlay = new OverlayWidget;
currentViewOverlay->attachToWidget(this); currentViewOverlay->attachToWidget(this);
currentViewOverlay->setPaintFunction([this](QWidget *w, QPainter &p, QPaintEvent *) { currentViewOverlay->setAttribute(Qt::WA_OpaquePaintEvent);
const int width = 2; currentViewOverlay->setResizeFunction([this](QWidget *w, const QSize &) {
const QPoint margin{0, width}; const QRect toolbarRect = m_toolBar->geometry();
p.setPen({w->palette().color(QPalette::Highlight), width}); w->setGeometry(toolbarRect.x(), toolbarRect.bottom() - 1, toolbarRect.width(), 2);
p.drawLine( });
m_toolBar->geometry().bottomLeft() + margin, currentViewOverlay->setPaintFunction([](QWidget *w, QPainter &p, QPaintEvent *) {
m_toolBar->geometry().bottomRight() + margin); p.fillRect(w->rect(), w->palette().color(QPalette::Highlight));
}); });
currentViewOverlay->setVisible(false); currentViewOverlay->setVisible(false);
const auto updateCurrentViewOverlay = [this, currentViewOverlay] { const auto updateCurrentViewOverlay = [this, currentViewOverlay] {