File indexing completed on 2024-11-24 04:42:25

0001 /*
0002  *  packedlayout.h  -  layout to pack items into rows
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2007-2022 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 #pragma once
0009 
0010 #include <QLayout>
0011 #include <QList>
0012 
0013 
0014 /**
0015  *  The PackedLayout class packs a group of widgets into rows.
0016  *  The widgets are arranged according to the total width available.
0017  *
0018  *  @author David Jarvie <djarvie@kde.org>
0019  */
0020 class PackedLayout : public QLayout
0021 {
0022 public:
0023     /** Constructor.
0024      *  @param parent the parent widget
0025      *  @param alignment how to align the widgets horizontally within the layout
0026      */
0027     PackedLayout(QWidget* parent, Qt::Alignment alignment);
0028     explicit PackedLayout(Qt::Alignment alignment);
0029     ~PackedLayout() override;
0030     void setHorizontalSpacing(int spacing);
0031     void setVerticalSpacing(int spacing);
0032     int horizontalSpacing() const;
0033     int verticalSpacing() const;
0034 
0035     // Override QLayout methods
0036     bool             hasHeightForWidth() const override  { return true; }
0037     int              heightForWidth(int w) const override;
0038     int              count() const override  { return mItems.count(); }
0039     void             addItem(QLayoutItem* item) override;
0040     QLayoutItem*     itemAt(int index) const override;
0041     QLayoutItem*     takeAt(int index) override;
0042     void             setGeometry(const QRect& r) override;
0043     QSize            sizeHint() const  override { return minimumSize(); }
0044     QSize            minimumSize() const override;
0045     Qt::Orientations expandingDirections() const override  { return Qt::Vertical | Qt::Horizontal; }
0046     void             invalidate() override  { mWidthCached = mHeightCached = 0; }
0047 
0048 private:
0049     int              arrange(const QRect&, bool set) const;
0050     QRect            alignRect(const QRect& rect, const QRect& itemRect) const;
0051 
0052 
0053     QList<QLayoutItem*> mItems;
0054     Qt::Alignment       mAlignment;
0055     int                 mHorizontalSpacing {-1};   // default = not set
0056     int                 mVerticalSpacing {-1};     // default = not set
0057     mutable int         mWidthCached {0};
0058     mutable int         mHeightCached {0};
0059 };
0060 
0061 // vim: et sw=4: