| 1 | /* |
| 2 | This file is part of the KDE project |
| 3 | SPDX-FileCopyrightText: 2015 David Edmundson <davidedmundson@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #ifndef KCOLLAPSIBLEGROUPBOX_H |
| 9 | #define KCOLLAPSIBLEGROUPBOX_H |
| 10 | |
| 11 | #include <QWidget> |
| 12 | #include <kwidgetsaddons_export.h> |
| 13 | #include <memory> |
| 14 | |
| 15 | /*! |
| 16 | * \class KCollapsibleGroupBox |
| 17 | * \inmodule KWidgetsAddons |
| 18 | * |
| 19 | * \brief A groupbox featuring a clickable header and arrow indicator that can be |
| 20 | * expanded and collapsed to reveal the contents. |
| 21 | * |
| 22 | * When expanded, the widget will resize to fit the sizeHint of child items. |
| 23 | * |
| 24 | * \since 5.16 |
| 25 | */ |
| 26 | class KWIDGETSADDONS_EXPORT KCollapsibleGroupBox : public QWidget |
| 27 | { |
| 28 | Q_OBJECT |
| 29 | |
| 30 | /*! |
| 31 | * \property KCollapsibleGroupBox::title |
| 32 | */ |
| 33 | Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) |
| 34 | |
| 35 | /*! |
| 36 | * \property KCollapsibleGroupBox::isExpanded |
| 37 | */ |
| 38 | Q_PROPERTY(bool expanded READ isExpanded WRITE setExpanded NOTIFY expandedChanged) |
| 39 | |
| 40 | public: |
| 41 | /*! |
| 42 | * |
| 43 | */ |
| 44 | explicit KCollapsibleGroupBox(QWidget *parent = nullptr); |
| 45 | ~KCollapsibleGroupBox() override; |
| 46 | |
| 47 | /*! |
| 48 | * Set the title that will be permanently shown at the top of the collapsing box |
| 49 | * Mnemonics are supported |
| 50 | */ |
| 51 | void setTitle(const QString &title); |
| 52 | |
| 53 | /*! |
| 54 | * The title |
| 55 | */ |
| 56 | QString title() const; |
| 57 | |
| 58 | /*! |
| 59 | * Set whether contents are shown |
| 60 | * |
| 61 | * The default is false until the user clicks |
| 62 | */ |
| 63 | void setExpanded(bool expanded); |
| 64 | |
| 65 | /*! |
| 66 | * Whether contents are shown |
| 67 | * During animations, this will reflect the target state at the end of the animation |
| 68 | */ |
| 69 | bool isExpanded() const; |
| 70 | |
| 71 | QSize sizeHint() const override; |
| 72 | QSize minimumSizeHint() const override; |
| 73 | |
| 74 | public Q_SLOTS: |
| 75 | /*! |
| 76 | * Expands if collapsed and vice versa |
| 77 | */ |
| 78 | void toggle(); |
| 79 | |
| 80 | /*! |
| 81 | * Equivalent to setExpanded(true) |
| 82 | */ |
| 83 | void expand(); |
| 84 | |
| 85 | /*! |
| 86 | * Equivalent to setExpanded(false) |
| 87 | */ |
| 88 | void collapse(); |
| 89 | |
| 90 | Q_SIGNALS: |
| 91 | /*! |
| 92 | * Emitted when the title is changed |
| 93 | */ |
| 94 | void titleChanged(); |
| 95 | |
| 96 | /*! |
| 97 | * Emitted when the widget expands or collapsed |
| 98 | */ |
| 99 | void expandedChanged(); |
| 100 | |
| 101 | protected: |
| 102 | void paintEvent(QPaintEvent *) override; |
| 103 | |
| 104 | bool event(QEvent *) override; |
| 105 | void mousePressEvent(QMouseEvent *) override; |
| 106 | void mouseMoveEvent(QMouseEvent *) override; |
| 107 | void leaveEvent(QEvent *) override; |
| 108 | void keyPressEvent(QKeyEvent *) override; |
| 109 | void resizeEvent(QResizeEvent *) override; |
| 110 | |
| 111 | private Q_SLOTS: |
| 112 | KWIDGETSADDONS_NO_EXPORT void overrideFocusPolicyOf(QWidget *widget); |
| 113 | |
| 114 | private: |
| 115 | std::unique_ptr<class KCollapsibleGroupBoxPrivate> const d; |
| 116 | |
| 117 | Q_DISABLE_COPY(KCollapsibleGroupBox) |
| 118 | }; |
| 119 | |
| 120 | #endif |
| 121 | |