File indexing completed on 2024-12-22 04:13:15

0001 /*
0002  *  SPDX-FileCopyrightText: 2011 Silvio Heinrich <plassy@web.de>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef H_KIS_WIDGET_CHOOSER_H_
0008 #define H_KIS_WIDGET_CHOOSER_H_
0009 
0010 #include <kritaui_export.h>
0011 #include <QList>
0012 #include <QIcon>
0013 #include <QFrame>
0014 
0015 class QToolButton;
0016 class QLabel;
0017 class QButtonGroup;
0018 
0019 class KRITAUI_EXPORT KisWidgetChooser: public QFrame
0020 {
0021     Q_OBJECT
0022     
0023     struct Data
0024     {
0025         Data(const QString& ID):
0026             id(ID), widget(0), label(0), chosen(false) { }
0027         Data(const Data& d):
0028             id(d.id), widget(d.widget), label(d.label), chosen(d.chosen) { }
0029         Data(const QString& ID, QWidget* w, QLabel* l):
0030             id(ID), widget(w), label(l), chosen(false) { }
0031             
0032         friend bool operator == (const Data& a, const Data& b) {
0033             return a.id == b.id;
0034         }
0035         
0036         QString  id;
0037         QWidget* widget;
0038         QLabel*  label;
0039         bool     chosen;
0040     };
0041     
0042     typedef QList<Data>::iterator       Iterator;
0043     typedef QList<Data>::const_iterator ConstIterator;
0044     
0045 public:
0046      KisWidgetChooser(int id, QWidget* parent=0);
0047     ~KisWidgetChooser() override;
0048     
0049     QWidget* chooseWidget(const QString& id);
0050     void     addLabelWidget(const QString& id, const QString& label, QWidget* widget);
0051     QWidget* getWidget(const QString& id) const;
0052     
0053     template<class TWidget>
0054     TWidget* addWidget(const QString& id, const QString& label = "") {
0055         if (id.isEmpty()) {
0056             return 0;
0057         }
0058 
0059         TWidget* widget = new TWidget();
0060         addLabelWidget(id, label, widget);
0061         return widget;
0062     }
0063     
0064     template<class TWidget>
0065     TWidget* getWidget(const QString& id) const {
0066         return dynamic_cast<TWidget*>(getWidget(id));
0067     }
0068 public Q_SLOTS:
0069 
0070     void showPopupWidget();
0071     void updateThemedIcons();
0072     
0073 private:
0074     void     removeWidget(const QString& id);
0075     QLayout* createPopupLayout();
0076     QLayout* createLayout();
0077     void     updateArrowIcon();
0078     
0079 protected Q_SLOTS:
0080     void slotButtonPressed();
0081     void slotWidgetChosen(int index);
0082 
0083     // QWidget interface
0084 protected:
0085     void changeEvent(QEvent *e) override;
0086 
0087 private:
0088     int           m_chooserid;
0089     QIcon         m_acceptIcon;
0090     QToolButton*  m_arrowButton;
0091     QButtonGroup* m_buttons;
0092     QFrame*       m_popup;
0093     QString       m_chosenID;
0094     QList<Data>   m_widgets;
0095 };
0096 
0097 #endif // H_KIS_WIDGET_CHOOSER_H_
0098