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 kcolumnresizer.h KColumnResizer
24 *
25 * @short Maintains consistent column sizes across layouts
26 *
27 * KColumnResizer is a helper class which can force columns of different layouts
28 * to keep the same width. It is useful to keep label columns consistent.
29 *
30 * It works with QGridLayout and QFormLayout.
31 *
32 * @image html kcolumnresizer.png "left: without KColumnResizer - right: with KColumnResizer"
33 *
34 * Here is a typical example:
35 *
36 * @code
37 * void Window::createWidgets()
38 * {
39 * QVBoxLayout *layout = new QVBoxLayout(this);
40 *
41 * QGroupBox *box1 = new QGroupBox();
42 * // Fill box1
43 * // ...
44 * layout->addWidget(box1);
45 *
46 * QGroupBox *box2 = new QGroupBox();
47 * // Fill box2
48 * // ...
49 * layout->addWidget(box2);
50 *
51 * KColumnResizer *resizer = new KColumnResizer(this);
52 * resizer->addWidgetsFromLayout(box1->layout(), 0);
53 * resizer->addWidgetsFromLayout(box2->layout(), 0);
54 * }
55 * @endcode
56 *
57 * In this example box1 and box2 children can be organized using QGridLayout or
58 * QFormLayout, resizer will ensure the first columns of the two QGroupBox stay
59 * the same width.
60 *
61 * @author Aurélien Gâteau <agateau@kde.org>
62 *
63 * @since 5.1
64 */
65class KWIDGETSADDONS_EXPORT KColumnResizer : public QObject
66{
67 Q_OBJECT
68public:
69 /**
70 * Constructs a KColumnResizer.
71 */
72 explicit KColumnResizer(QObject *parent = nullptr);
73
74 ~KColumnResizer() override;
75
76 /**
77 * Add all widgets from @p layout which are in column @p column to the list
78 * of widgets to manage.
79 *
80 * @param layout The layout containing the widgets to add. KColumnResizer
81 * supports QGridLayout and QFormLayout.
82 * @param 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 * @param 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 * @param 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