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//
5// SPDX-License-Identifier: LGPL-2.1-or-later
6
7#ifndef KADJUSTINGSCROLLAREA_H
8#define KADJUSTINGSCROLLAREA_H
9
10#include <kwidgetsaddons_export.h>
11
12#include <QScrollArea>
13
14/*!
15 * \class KAdjustingScrollArea
16 * \inmodule KWidgetsAddons
17 *
18 * \brief Special scroll area widget which adjust its size to avoid scroll bars
19 * as much as possible
20 *
21 * This widget improves a few aspects of QScrollArea in particular, for
22 * vertically scrollable widgets and dialogs.
23 *
24 * If sizeAdjustPolicy is set to QAbstractScrollArea::AdjustToContents,
25 * then the scroll area will (try to) adjust its size to the widget to avoid
26 * scroll bars as much as possible.
27 *
28 * In particular, this widget will automatically increase the height of its
29 * window when the content of the scroll area grows in height (e.g. because
30 * more widgets are added dynamically). The automatic resizing stops at 2/3
31 * of the screen's height.
32 *
33 * Instead of setting the scrollarea's content via QScrollArea::setWidget,
34 * add the child widgets to the QVBoxLayout of the scrollarea's widget.
35 *
36 * \code
37 * auto scrollArea = new KAdjustingScrollArea(this);
38 * scrollArea->setSizeAdjustPolicy(QScrollArea::AdjustToContents);
39 * scrollArea->setFocusPolicy(Qt::NoFocus);
40 * scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
41 * scrollArea->setWidget(new QWidget);
42 *
43 * auto scrollAreaLayout = new QVBoxLayout(scrollArea->widget());
44 * \endcode
45 *
46 * \since 6.9
47 */
48class KWIDGETSADDONS_EXPORT KAdjustingScrollArea : public QScrollArea
49{
50 Q_OBJECT
51
52public:
53 /*!
54 * Creates a scroll area with a QWidget with QVBoxLayout that is flagged
55 * as resizable.
56 */
57 explicit KAdjustingScrollArea(QWidget *parent = nullptr);
58 ~KAdjustingScrollArea() override;
59
60 QSize minimumSizeHint() const override;
61
62 /*
63 * Reimplemented to remove the caching of the size/size hint of the
64 * widget and to add the horizontal size hint of the vertical scroll bar
65 * unless it is explicitly turned off.
66 */
67 QSize sizeHint() const override;
68
69 bool event(QEvent *event) override;
70
71private:
72 bool eventFilter(QObject *obj, QEvent *ev) override;
73};
74
75#endif // KADJUSTINGSCROLLAREA_H
76

source code of kwidgetsaddons/src/kadjustingscrollarea.h