File indexing completed on 2025-04-27 03:58:30

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-03-14
0007  * Description : A widget to host settings as expander box
0008  *
0009  * SPDX-FileCopyrightText: 2008-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  * SPDX-FileCopyrightText: 2008-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText:      2010 by Manuel Viet <contact at 13zenrv dot fr>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #ifndef DIGIKAM_DEXPANDER_BOX_H
0018 #define DIGIKAM_DEXPANDER_BOX_H
0019 
0020 // Qt includes
0021 
0022 #include <QObject>
0023 #include <QPixmap>
0024 #include <QLabel>
0025 #include <QWidget>
0026 #include <QScrollArea>
0027 #include <QFrame>
0028 
0029 // Local includes
0030 
0031 #include "digikam_export.h"
0032 
0033 class KConfigGroup;
0034 
0035 namespace Digikam
0036 {
0037 
0038 /**
0039  * A widget to show an horizontal or vertical line separator
0040  */
0041 class DIGIKAM_EXPORT DLineWidget : public QFrame
0042 {
0043     Q_OBJECT
0044 
0045 public:
0046 
0047     explicit DLineWidget(Qt::Orientation orientation, QWidget* const parent = nullptr);
0048     ~DLineWidget() override;
0049 };
0050 
0051 // -------------------------------------------------------------------------
0052 
0053 /**
0054  * A label to show text adjusted to widget size
0055  */
0056 class DIGIKAM_EXPORT DAdjustableLabel : public QLabel
0057 {
0058     Q_OBJECT
0059 
0060 public:
0061 
0062     explicit DAdjustableLabel(QWidget* const parent = nullptr);
0063     ~DAdjustableLabel() override;
0064 
0065     QSize minimumSizeHint() const override;
0066     QSize sizeHint()        const override;
0067 
0068     void setAlignment(Qt::Alignment align);
0069     void setElideMode(Qt::TextElideMode mode);
0070 
0071     QString adjustedText()  const;
0072 
0073 public Q_SLOTS:
0074 
0075     void setAdjustedText(const QString& text = QString());
0076 
0077 private:
0078 
0079     void resizeEvent(QResizeEvent*) override;
0080     void adjustTextToLabel();
0081 
0082     // Disabled methods from QLabel
0083     QString text() const { return QString(); }; ///< Use adjustedText() instead.
0084     void setText(const QString&) {};            ///< Use setAdjustedText(text) instead.
0085     void clear() {};                            ///< Use setdjustedText(QString()) instead.
0086 
0087 private:
0088 
0089     class Private;
0090     Private* const d;
0091 };
0092 
0093 // -------------------------------------------------------------------------
0094 
0095 class DIGIKAM_EXPORT DClickLabel : public QLabel
0096 {
0097     Q_OBJECT
0098 
0099 public:
0100 
0101     explicit DClickLabel(QWidget* const parent = nullptr);
0102     explicit DClickLabel(const QString& text, QWidget* const parent = nullptr);
0103     ~DClickLabel()                             override;
0104 
0105 Q_SIGNALS:
0106 
0107     /// Emitted when activated by left mouse click
0108     void leftClicked();
0109 
0110     /// Emitted when activated, by mouse or key press
0111     void activated();
0112 
0113 protected:
0114 
0115     void mousePressEvent(QMouseEvent* event)   override;
0116     void mouseReleaseEvent(QMouseEvent* event) override;
0117     void keyPressEvent(QKeyEvent* event)       override;
0118 };
0119 
0120 // -------------------------------------------------------------------------
0121 
0122 class DIGIKAM_EXPORT DSqueezedClickLabel : public DAdjustableLabel
0123 {
0124     Q_OBJECT
0125 
0126 public:
0127 
0128     explicit DSqueezedClickLabel(QWidget* const parent = nullptr);
0129     explicit DSqueezedClickLabel(const QString& text, QWidget* const parent = nullptr);
0130     ~DSqueezedClickLabel()                     override;
0131 
0132 Q_SIGNALS:
0133 
0134     void leftClicked();
0135     void activated();
0136 
0137 protected:
0138 
0139     void mousePressEvent(QMouseEvent* event)   override;
0140     void mouseReleaseEvent(QMouseEvent* event) override;
0141     void keyPressEvent(QKeyEvent* event)       override;
0142 };
0143 
0144 // -------------------------------------------------------------------------
0145 
0146 class DIGIKAM_EXPORT DArrowClickLabel : public QWidget
0147 {
0148     Q_OBJECT
0149 
0150 public:
0151 
0152     explicit DArrowClickLabel(QWidget* const parent = nullptr);
0153     ~DArrowClickLabel()                               override;
0154 
0155     void setArrowType(Qt::ArrowType arrowType);
0156     Qt::ArrowType arrowType()                   const;
0157 
0158     QSize sizeHint ()                           const override;
0159 
0160 Q_SIGNALS:
0161 
0162     void leftClicked();
0163 
0164 protected:
0165 
0166     void mousePressEvent(QMouseEvent* event)          override;
0167     void mouseReleaseEvent(QMouseEvent* event)        override;
0168     void paintEvent(QPaintEvent* event)               override;
0169 
0170 protected:
0171 
0172     Qt::ArrowType m_arrowType;
0173     int           m_size;
0174     int           m_margin;
0175 };
0176 
0177 // -------------------------------------------------------------------------
0178 
0179 class DIGIKAM_EXPORT DLabelExpander : public QWidget
0180 {
0181     Q_OBJECT
0182 
0183 public:
0184 
0185     explicit DLabelExpander(QWidget* const parent = nullptr);
0186     ~DLabelExpander()                 override;
0187 
0188     void setCheckBoxVisible(bool b);
0189     bool checkBoxIsVisible()    const;
0190 
0191     void setChecked(bool b);
0192     bool isChecked()            const;
0193 
0194     void setLineVisible(bool b);
0195     bool lineIsVisible()        const;
0196 
0197     void setText(const QString& txt);
0198     QString text()              const;
0199 
0200     void setButtonVisible(bool b);
0201     bool buttonIsVisible()      const;
0202     void setButtonIcon(const QIcon& icon);
0203 
0204     void setIcon(const QIcon &icon);
0205     QIcon icon()                const;
0206 
0207     void setWidget(QWidget* const widget);
0208     QWidget* widget()           const;
0209 
0210     void setExpanded(bool b);
0211     bool isExpanded()           const;
0212 
0213     void setExpandByDefault(bool b);
0214     bool isExpandByDefault()    const;
0215 
0216 Q_SIGNALS:
0217 
0218     void signalExpanded(bool);
0219     void signalToggled(bool);
0220     void signalButtonPressed();
0221 
0222 private Q_SLOTS:
0223 
0224     void slotToggleContainer();
0225 
0226 private:
0227 
0228     bool eventFilter(QObject* obj,
0229                      QEvent* ev)      override;
0230 
0231 private:
0232 
0233     class Private;
0234     Private* const d;
0235 };
0236 
0237 // -------------------------------------------------------------------------
0238 
0239 class DIGIKAM_EXPORT DExpanderBox : public QScrollArea
0240 {
0241     Q_OBJECT
0242 
0243 public:
0244 
0245     explicit DExpanderBox(QWidget* const parent = nullptr);
0246     ~DExpanderBox()                                   override;
0247 
0248     /**
0249      * Add DLabelExpander item at end of box layout with these settings :
0250      *  'w'               : the widget hosted by DLabelExpander.
0251      *  'pix'             : pixmap used as icon to item title.
0252      *  'txt'             : text used as item title.
0253      *  'objName'         : item object name used to read/save expanded settings to rc file.
0254      *  'expandBydefault' : item state by default (expanded or not).
0255      */
0256     void addItem(QWidget* const w, const QIcon &icon, const QString& txt,
0257                  const QString& objName, bool expandBydefault);
0258     void addItem(QWidget* const w, const QString& txt,
0259                  const QString& objName, bool expandBydefault);
0260 
0261     /**
0262      * Insert DLabelExpander item at box layout index with these settings :
0263      *  'w'               : the widget hosted by DLabelExpander.
0264      *  'pix'             : pixmap used as icon to item title.
0265      *  'txt'             : text used as item title.
0266      *  'objName'         : item object name used to read/save expanded settings to rc file.
0267      *  'expandBydefault' : item state by default (expanded or not).
0268      */
0269     void insertItem(int index, QWidget* const w, const QIcon &icon, const QString& txt,
0270                     const QString& objName, bool expandBydefault);
0271     void insertItem(int index, QWidget* const w, const QString& txt,
0272                     const QString& objName, bool expandBydefault);
0273 
0274     void removeItem(int index);
0275 
0276     void setCheckBoxVisible(int index, bool b);
0277     bool checkBoxIsVisible(int index)           const;
0278 
0279     void setChecked(int index, bool b);
0280     bool isChecked(int index)                   const;
0281 
0282     void setItemText(int index, const QString& txt);
0283     QString itemText (int index)                const;
0284 
0285     void setItemIcon(int index, const QIcon &icon);
0286     QIcon itemIcon(int index)                   const;
0287 
0288     void setButtonVisible(int index, bool b);
0289     bool buttonIsVisible(int index)             const;
0290     void setButtonIcon(int index, const QIcon& icon);
0291 
0292     void setItemToolTip(int index, const QString& tip);
0293     QString itemToolTip(int index)              const;
0294 
0295     void setItemEnabled(int index, bool enabled);
0296     bool isItemEnabled(int index)               const;
0297 
0298     void addStretch();
0299     void insertStretch(int index);
0300 
0301     void setItemExpanded(int index, bool b);
0302     bool isItemExpanded(int index)              const;
0303 
0304     int  count() const;
0305 
0306     DLabelExpander* widget(int index)           const;
0307     int indexOf(DLabelExpander* const widget)   const;
0308 
0309     virtual void readSettings(KConfigGroup& group);
0310     virtual void writeSettings(KConfigGroup& group);
0311 
0312 Q_SIGNALS:
0313 
0314     void signalItemExpanded(int index, bool b);
0315     void signalItemToggled(int index, bool b);
0316     void signalItemButtonPressed(int index);
0317 
0318 private Q_SLOTS:
0319 
0320     void slotItemExpanded(bool b);
0321     void slotItemToggled(bool b);
0322     void slotItemButtonPressed();
0323 
0324 private:
0325 
0326     class Private;
0327     Private* const d;
0328 };
0329 
0330 // -------------------------------------------------------------------------
0331 
0332 class DIGIKAM_EXPORT DExpanderBoxExclusive : public DExpanderBox
0333 {
0334     Q_OBJECT
0335 
0336 public:
0337 
0338     explicit DExpanderBoxExclusive(QWidget* const parent = nullptr);
0339     ~DExpanderBoxExclusive()   override;
0340 
0341     /**
0342      * Show one expander open at most
0343      */
0344     void setIsToolBox(bool b);
0345     bool isToolBox()    const;
0346 
0347 private Q_SLOTS:
0348 
0349     void slotItemExpanded(bool b);
0350 
0351 private:
0352 
0353     bool m_toolbox;
0354 };
0355 
0356 } // namespace Digikam
0357 
0358 #endif // DIGIKAM_DEXPANDER_BOX_H