KWidgetsAddons

kadjustingscrollarea.cpp
1// SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
2// SPDX-FileCopyrightText: 2022 g10 Code GmbH
3// SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
4// SPDX-FileContributor: Carl Schwan <carl.schwan@gnupg.com>
5//
6// SPDX-License-Identifier: LGPL-2.1-or-later
7
8#include "kadjustingscrollarea.h"
9
10#include <QApplication>
11#include <QResizeEvent>
12#include <QScreen>
13#include <QScrollBar>
14#include <QVBoxLayout>
15
17 : QScrollArea{parent}
18{
20
21 connect(qApp, &QApplication::focusChanged, this, [this](QWidget *old, QWidget *now) {
22 Q_UNUSED(old);
24 });
25
27}
28
29KAdjustingScrollArea::~KAdjustingScrollArea()
30{
32 if (auto w = widget()) {
33 w->removeEventFilter(this);
34 }
35}
36
38{
39 const int fw = frameWidth();
40 QSize sz{2 * fw, 2 * fw};
41 sz += {widget()->minimumSizeHint().width(), 0};
43 sz.setWidth(sz.width() + verticalScrollBar()->sizeHint().width());
44 }
46 sz.setHeight(sz.height() + horizontalScrollBar()->sizeHint().height());
47 }
49}
50
52{
53 const int fw = frameWidth();
54 QSize sz{2 * fw, 2 * fw};
55 sz += viewportSizeHint();
57 sz.setWidth(sz.width() + verticalScrollBar()->sizeHint().width());
58 }
60 sz.setHeight(sz.height() + horizontalScrollBar()->sizeHint().height());
61 }
63 return sz;
64}
65
66static void adjustSizeOfWindowBy(const QSize &extent, QWidget *window)
67{
68 if (window) {
69 const auto currentSize = window->size();
70 // we limit the automatic size adjustment to 2/3 of the screen's size
71 const auto maxWindowSize = window->screen()->geometry().size() * 2 / 3;
72 const auto newWindowSize = currentSize.expandedTo((currentSize + extent).boundedTo(maxWindowSize));
73 if (newWindowSize != currentSize) {
74 window->resize(newWindowSize);
75 }
76 }
77}
78
79bool KAdjustingScrollArea::eventFilter(QObject *obj, QEvent *ev)
80{
81 if (ev->type() == QEvent::ChildAdded && viewport() == obj) {
82 auto childAddedEvent = static_cast<QChildEvent *>(ev);
83 auto widget = childAddedEvent->child();
84 if (widget->objectName().isEmpty()) {
85 widget->setObjectName(QLatin1StringView("scrollarea_widget"));
86 }
88 }
89 if (ev->type() == QEvent::ChildRemoved && viewport() == obj) {
90 auto childRemovedEvent = static_cast<QChildEvent *>(ev);
91 auto widget = childRemovedEvent->child();
93 }
94 if (ev->type() == QEvent::Resize && obj == widget() && sizeAdjustPolicy() == AdjustToContents) {
95 const auto *const event = static_cast<QResizeEvent *>(ev);
96 if (event->size().height() > event->oldSize().height()) {
97 const auto currentViewportHeight = viewport()->height();
98 const auto wantedViewportHeight = event->size().height();
99 const auto wantedAdditionalHeight = wantedViewportHeight - currentViewportHeight;
100 if (wantedAdditionalHeight > 0) {
101 adjustSizeOfWindowBy(QSize{0, wantedAdditionalHeight}, window());
102 }
103 }
104 }
105 return QScrollArea::eventFilter(obj, ev);
106}
107
108bool KAdjustingScrollArea::event(QEvent *event)
109{
110 return QScrollArea::event(event);
111}
112
113#include "moc_kadjustingscrollarea.cpp"
KAdjustingScrollArea(QWidget *parent=nullptr)
Creates a scroll area with a QWidget with QVBoxLayout that is flagged as resizable.
QSize sizeHint() const override
Reimplemented to remove the caching of the size/size hint of the widget and to add the horizontal siz...
QSize minimumSizeHint() const override
Reimplemented to add the minimum size hint of the widget.
QScrollBar * horizontalScrollBar() const const
QScrollBar * verticalScrollBar() const const
QWidget * viewport() const const
void focusChanged(QWidget *old, QWidget *now)
Type type() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void installEventFilter(QObject *filterObj)
void removeEventFilter(QObject *obj)
void setObjectName(QAnyStringView name)
void ensureWidgetVisible(QWidget *childWidget, int xmargin, int ymargin)
virtual bool event(QEvent *e) override
virtual bool eventFilter(QObject *o, QEvent *e) override
virtual QSize sizeHint() const const override
virtual QSize viewportSizeHint() const const override
QWidget * widget() const const
void setWidgetResizable(bool resizable)
QSize expandedTo(const QSize &otherSize) const const
ScrollBarAlwaysOff
QWidget * window() const const
void resize(const QSize &newSize)
QScreen * screen() const const
virtual QSize size() const const override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:51 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.