forked from qt-creator/qt-creator
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:
@@ -8,16 +8,34 @@
|
||||
#include <QEvent>
|
||||
#include <QPainter>
|
||||
|
||||
namespace Utils::Internal {
|
||||
class OverlayWidgetPrivate
|
||||
{
|
||||
public:
|
||||
OverlayWidget::PaintFunction m_paint;
|
||||
OverlayWidget::ResizeFunction m_resize;
|
||||
};
|
||||
} // namespace Utils::Internal
|
||||
|
||||
Utils::OverlayWidget::OverlayWidget(QWidget *parent)
|
||||
: d(new Internal::OverlayWidgetPrivate)
|
||||
{
|
||||
setAttribute(Qt::WA_TransparentForMouseEvents);
|
||||
if (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)
|
||||
{
|
||||
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)
|
||||
@@ -29,9 +47,9 @@ bool Utils::OverlayWidget::eventFilter(QObject *obj, QEvent *ev)
|
||||
|
||||
void Utils::OverlayWidget::paintEvent(QPaintEvent *ev)
|
||||
{
|
||||
if (m_paint) {
|
||||
if (d->m_paint) {
|
||||
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()
|
||||
{
|
||||
QTC_ASSERT(parentWidget(), return );
|
||||
setGeometry(QRect(QPoint(0, 0), parentWidget()->size()));
|
||||
if (d->m_resize)
|
||||
d->m_resize(this, parentWidget()->size());
|
||||
}
|
||||
|
@@ -8,18 +8,26 @@
|
||||
#include <QWidget>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace Utils {
|
||||
|
||||
namespace Internal {
|
||||
class OverlayWidgetPrivate;
|
||||
}
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT OverlayWidget : public QWidget
|
||||
{
|
||||
public:
|
||||
using PaintFunction = std::function<void(QWidget *, QPainter &, QPaintEvent *)>;
|
||||
using ResizeFunction = std::function<void(QWidget *, QSize)>;
|
||||
|
||||
explicit OverlayWidget(QWidget *parent = nullptr);
|
||||
~OverlayWidget();
|
||||
|
||||
void attachToWidget(QWidget *parent);
|
||||
void setPaintFunction(const PaintFunction &paint);
|
||||
void setResizeFunction(const ResizeFunction &resize);
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *obj, QEvent *ev) override;
|
||||
@@ -28,7 +36,7 @@ protected:
|
||||
private:
|
||||
void resizeToParent();
|
||||
|
||||
PaintFunction m_paint;
|
||||
std::unique_ptr<Internal::OverlayWidgetPrivate> d;
|
||||
};
|
||||
|
||||
} // namespace Utils
|
||||
|
@@ -133,13 +133,13 @@ EditorView::EditorView(SplitterOrView *parentSplitterOrView, QWidget *parent)
|
||||
|
||||
auto currentViewOverlay = new OverlayWidget;
|
||||
currentViewOverlay->attachToWidget(this);
|
||||
currentViewOverlay->setPaintFunction([this](QWidget *w, QPainter &p, QPaintEvent *) {
|
||||
const int width = 2;
|
||||
const QPoint margin{0, width};
|
||||
p.setPen({w->palette().color(QPalette::Highlight), width});
|
||||
p.drawLine(
|
||||
m_toolBar->geometry().bottomLeft() + margin,
|
||||
m_toolBar->geometry().bottomRight() + margin);
|
||||
currentViewOverlay->setAttribute(Qt::WA_OpaquePaintEvent);
|
||||
currentViewOverlay->setResizeFunction([this](QWidget *w, const QSize &) {
|
||||
const QRect toolbarRect = m_toolBar->geometry();
|
||||
w->setGeometry(toolbarRect.x(), toolbarRect.bottom() - 1, toolbarRect.width(), 2);
|
||||
});
|
||||
currentViewOverlay->setPaintFunction([](QWidget *w, QPainter &p, QPaintEvent *) {
|
||||
p.fillRect(w->rect(), w->palette().color(QPalette::Highlight));
|
||||
});
|
||||
currentViewOverlay->setVisible(false);
|
||||
const auto updateCurrentViewOverlay = [this, currentViewOverlay] {
|
||||
|
Reference in New Issue
Block a user