1/*
2 This file is part of the KDE frameworks
3 SPDX-FileCopyrightText: 2014 Aurélien Gâteau <agateau@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7#ifndef KCOLUMNRESIZER_H
8#define KCOLUMNRESIZER_H
9
10#include <kwidgetsaddons_export.h>
11
12#include <QFormLayout>
13#include <QObject>
14
15#include <memory>
16
17class QEvent;
18class QGridLayout;
19class QLayout;
20class QWidget;
21
22/*!
23 * \class KColumnResizer
24 * \inmodule KWidgetsAddons
25 *
26 * \brief Maintains consistent column sizes across layouts.
27 *
28 * KColumnResizer is a helper class which can force columns of different layouts
29 * to keep the same width. It is useful to keep label columns consistent.
30 *
31 * It works with QGridLayout and QFormLayout.
32 *
33 * \image kcolumnresizer.png "left: without KColumnResizer - right: with KColumnResizer"
34 *
35 * Here is a typical example:
36 *
37 * \code
38 * void Window::createWidgets()
39 * {
40 * QVBoxLayout *layout = new QVBoxLayout(this);
41 *
42 * QGroupBox *box1 = new QGroupBox();
43 * // Fill box1
44 * // ...
45 * layout->addWidget(box1);
46 *
47 * QGroupBox *box2 = new QGroupBox();
48 * // Fill box2
49 * // ...
50 * layout->addWidget(box2);
51 *
52 * KColumnResizer *resizer = new KColumnResizer(this);
53 * resizer->addWidgetsFromLayout(box1->layout(), 0);
54 * resizer->addWidgetsFromLayout(box2->layout(), 0);
55 * }
56 * \endcode
57 *
58 * In this example box1 and box2 children can be organized using QGridLayout or
59 * QFormLayout, resizer will ensure the first columns of the two QGroupBox stay
60 * the same width.
61 *
62 * \since 5.1
63 */
64class KWIDGETSADDONS_EXPORT KColumnResizer : public QObject
65{
66 Q_OBJECT
67public:
68 /*!
69 * Constructs a KColumnResizer.
70 */
71 explicit KColumnResizer(QObject *parent = nullptr);
72
73 ~KColumnResizer() override;
74
75 /*!
76 * Add all widgets from \a layout which are in column \a column to the list
77 * of widgets to manage.
78 *
79 * \a layout The layout containing the widgets to add. KColumnResizer
80 * supports QGridLayout and QFormLayout.
81 *
82 * \a column The column number which contains the widgets. If layout is
83 * a QFormLayout, column should not be higher than QFormLayout::SpanningRole
84 */
85 void addWidgetsFromLayout(QLayout *layout, int column = 0);
86
87 /*!
88 * Add a single widget to the list of widgets whose width is monitored.
89 *
90 * It is more common to use addWidgetsFromLayout(), but adding single
91 * widgets can be useful if you want to keep a single button the same width
92 * as a column in a layout.
93 *
94 * \a widget The widget to add
95 */
96 void addWidget(QWidget *widget);
97
98 /*!
99 * Remove a widget previously added by addWidget or addWidgetsFromLayout.
100 *
101 * \a widget The widget to remove
102 */
103 void removeWidget(QWidget *widget);
104
105protected:
106 bool eventFilter(QObject *, QEvent *event) override;
107
108private:
109 std::unique_ptr<class KColumnResizerPrivate> const d;
110 Q_DISABLE_COPY(KColumnResizer)
111};
112
113#endif /* KCOLUMNRESIZER_H */
114

source code of kwidgetsaddons/src/kcolumnresizer.h