File indexing completed on 2024-04-28 15:32:00

0001 /*
0002     This file is part of the KDE frameworks
0003     SPDX-FileCopyrightText: 2014 Aurélien Gâteau <agateau@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 #ifndef KCOLUMNRESIZER_H
0008 #define KCOLUMNRESIZER_H
0009 
0010 #include <kwidgetsaddons_export.h>
0011 
0012 #include <QFormLayout>
0013 #include <QObject>
0014 
0015 #include <memory>
0016 
0017 class QEvent;
0018 class QGridLayout;
0019 class QLayout;
0020 class QWidget;
0021 
0022 /**
0023  * @class KColumnResizer kcolumnresizer.h KColumnResizer
0024  *
0025  * @short Maintains consistent column sizes across layouts
0026  *
0027  * KColumnResizer is a helper class which can force columns of different layouts
0028  * to keep the same width. It is useful to keep label columns consistent.
0029  *
0030  * It works with QGridLayout and QFormLayout.
0031  *
0032  * @image html kcolumnresizer.png "left: without KColumnResizer - right: with KColumnResizer"
0033  *
0034  * Here is a typical example:
0035  *
0036  * @code
0037  *  void Window::createWidgets()
0038  *  {
0039  *      QVBoxLayout *layout = new QVBoxLayout(this);
0040  *
0041  *      QGroupBox *box1 = new QGroupBox();
0042  *      // Fill box1
0043  *      // ...
0044  *      layout->addWidget(box1);
0045  *
0046  *      QGroupBox *box2 = new QGroupBox();
0047  *      // Fill box2
0048  *      // ...
0049  *      layout->addWidget(box2);
0050  *
0051  *      KColumnResizer *resizer = new KColumnResizer(this);
0052  *      resizer->addWidgetsFromLayout(box1->layout(), 0);
0053  *      resizer->addWidgetsFromLayout(box2->layout(), 0);
0054  *  }
0055  * @endcode
0056  *
0057  * In this example box1 and box2 children can be organized using QGridLayout or
0058  * QFormLayout, resizer will ensure the first columns of the two QGroupBox stay
0059  * the same width.
0060  *
0061  * @author Aurélien Gâteau <agateau@kde.org>
0062  *
0063  * @since 5.1
0064  */
0065 class KWIDGETSADDONS_EXPORT KColumnResizer : public QObject
0066 {
0067     Q_OBJECT
0068 public:
0069     /**
0070      * Constructs a KColumnResizer.
0071      */
0072     explicit KColumnResizer(QObject *parent = nullptr);
0073 
0074     ~KColumnResizer() override;
0075 
0076     /**
0077      * Add all widgets from @p layout which are in column @p column to the list
0078      * of widgets to manage.
0079      *
0080      * @param layout The layout containing the widgets to add. KColumnResizer
0081      * supports QGridLayout and QFormLayout.
0082      * @param column The column number which contains the widgets. If layout is
0083      * a QFormLayout, column should not be higher than QFormLayout::SpanningRole
0084      */
0085     void addWidgetsFromLayout(QLayout *layout, int column = 0);
0086 
0087     /**
0088      * Add a single widget to the list of widgets whose width is monitored.
0089      *
0090      * It is more common to use addWidgetsFromLayout(), but adding single
0091      * widgets can be useful if you want to keep a single button the same width
0092      * as a column in a layout.
0093      *
0094      * @param widget The widget to add
0095      */
0096     void addWidget(QWidget *widget);
0097 
0098     /**
0099      * Remove a widget previously added by addWidget or addWidgetsFromLayout.
0100      *
0101      * @param widget The widget to remove
0102      */
0103     void removeWidget(QWidget *widget);
0104 
0105 protected:
0106     bool eventFilter(QObject *, QEvent *event) override;
0107 
0108 private:
0109     std::unique_ptr<class KColumnResizerPrivate> const d;
0110     Q_DISABLE_COPY(KColumnResizer)
0111 };
0112 
0113 #endif /* KCOLUMNRESIZER_H */