File indexing completed on 2024-04-28 03:59:09

0001 /*
0002     SPDX-FileCopyrightText: 2001, 2002, 2003 Joseph Wenninger <jowenn@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef _KMultitabbar_h_
0008 #define _KMultitabbar_h_
0009 
0010 #include <QPushButton>
0011 #include <QString>
0012 #include <memory>
0013 
0014 #include <kwidgetsaddons_export.h>
0015 
0016 class QPixmap;
0017 class QMenu;
0018 class QStyleOptionToolButton;
0019 
0020 class KMultiTabBarInternal;
0021 
0022 /**
0023  * @class KMultiTabBar kmultitabbar.h KMultiTabBar
0024  *
0025  * A Widget for horizontal and vertical tabs.
0026  * (Note that in Qt4, QTabBar can be vertical as well)
0027  *
0028  * It is possible to add normal buttons to the top/left
0029  * The handling if only one tab at a time or multiple tabs
0030  * should be raisable is left to the "user".
0031  *
0032  * \image html kmultitabbar.png "KMultiTabBar Widget"
0033  *
0034  * @author Joseph Wenninger
0035  */
0036 class KWIDGETSADDONS_EXPORT KMultiTabBar : public QWidget
0037 {
0038     Q_OBJECT
0039     Q_PROPERTY(KMultiTabBarPosition position READ position WRITE setPosition)
0040     Q_PROPERTY(KMultiTabBarStyle tabStyle READ tabStyle WRITE setStyle)
0041 public:
0042     enum KMultiTabBarPosition { Left, Right, Top, Bottom };
0043     Q_ENUM(KMultiTabBarPosition)
0044 
0045     /**
0046      * The list of available styles for KMultiTabBar
0047      */
0048     enum KMultiTabBarStyle {
0049         VSNET = 0, ///< Visual Studio .Net like, always shows icon, only show the text of active tabs
0050         KDEV3ICON = 2, ///< KDevelop 3 like, always shows the text and icons
0051         STYLELAST = 0xffff,
0052     };
0053     Q_ENUM(KMultiTabBarStyle)
0054 
0055     /**
0056      * Create a KMultiTabBar with Left as KMultiTabBar position.
0057      * @param parent The parent of the widget.
0058      * @since 5.24
0059      */
0060     explicit KMultiTabBar(QWidget *parent = nullptr);
0061 
0062     explicit KMultiTabBar(KMultiTabBarPosition pos, QWidget *parent = nullptr);
0063     ~KMultiTabBar() override;
0064 
0065     /**
0066      * append  a new button to the button area. The button can later on be accessed with button(ID)
0067      * eg for connecting signals to it
0068      * @param icon a icon for the button
0069      * @param id an arbitrary ID value. It will be emitted in the clicked signal for identifying the button
0070      *  if more than one button is connected to a signals.
0071      * @param popup A popup menu which should be displayed if the button is clicked
0072      * @param not_used_yet will be used for a popup text in the future
0073      * @since 5.13
0074      */
0075     int appendButton(const QIcon &icon, int id = -1, QMenu *popup = nullptr, const QString &not_used_yet = QString());
0076 
0077     /**
0078      * remove a button with the given ID
0079      */
0080     void removeButton(int id);
0081 
0082     /**
0083      * append a new tab to the tab area. It can be accessed lateron with tabb(id);
0084      * @param icon a icon for the tab
0085      * @param id an arbitrary ID which can be used later on to identify the tab
0086      * @param text if a mode with text is used it will be the tab text, otherwise a mouse over hint
0087      * @since 5.13
0088      */
0089     int appendTab(const QIcon &icon, int id = -1, const QString &text = QString());
0090 
0091     /**
0092      * remove a tab with a given ID
0093      */
0094     void removeTab(int id);
0095     /**
0096      * set a tab to "raised"
0097      * @param id The ID of the tab to manipulate
0098      * @param state true == activated/raised, false == not active
0099      */
0100     void setTab(int id, bool state);
0101     /**
0102      * return the state of a tab, identified by its ID
0103      */
0104     bool isTabRaised(int id) const;
0105     /**
0106      * get a pointer to a button within the button area identified by its ID
0107      */
0108     class KMultiTabBarButton *button(int id) const;
0109 
0110     /**
0111      * get a pointer to a tab within the tab area, identified by its ID
0112      */
0113     class KMultiTabBarTab *tab(int id) const;
0114 
0115     /**
0116      * set the real position of the widget.
0117      * @param pos if the mode is horizontal, only use top, bottom, if it is vertical use left or right
0118      */
0119     void setPosition(KMultiTabBarPosition pos);
0120 
0121     /**
0122      * get the tabbar position.
0123      * @return position
0124      */
0125     KMultiTabBarPosition position() const;
0126 
0127     /**
0128      * set the display style of the tabs
0129      */
0130     void setStyle(KMultiTabBarStyle style);
0131 
0132     /**
0133      * get the display style of the tabs
0134      * @return display style
0135      */
0136     KMultiTabBarStyle tabStyle() const;
0137 
0138 protected:
0139     friend class KMultiTabBarButton;
0140     virtual void fontChange(const QFont &);
0141     void paintEvent(class QPaintEvent *) override;
0142     void updateSeparator();
0143 
0144 private:
0145     std::unique_ptr<class KMultiTabBarPrivate> const d;
0146 };
0147 
0148 /**
0149  * @class KMultiTabBarButton kmultitabbar.h KMultiTabBarButton
0150  *
0151  * Use KMultiTabBar::appendButton to append a button, which creates a KMultiTabBarButton instance
0152  */
0153 class KWIDGETSADDONS_EXPORT KMultiTabBarButton : public QPushButton
0154 {
0155     Q_OBJECT
0156 public:
0157     int id() const;
0158     ~KMultiTabBarButton() override;
0159 
0160 public Q_SLOTS:
0161     void setText(const QString &text);
0162 
0163 Q_SIGNALS:
0164     /**
0165      * this is emitted if  the button is clicked
0166      * @param id    the ID identifying the button
0167      */
0168     void clicked(int id);
0169 protected Q_SLOTS:
0170     virtual void slotClicked();
0171 
0172 protected:
0173     void hideEvent(class QHideEvent *) override;
0174     void showEvent(class QShowEvent *) override;
0175     void paintEvent(class QPaintEvent *) override;
0176 
0177     /**
0178      * Should not be created directly. Use KMultiTabBar::appendButton
0179      */
0180     KMultiTabBarButton(const QIcon &icon, const QString &, int id, QWidget *parent);
0181 
0182 private:
0183     friend class KMultiTabBar;
0184 
0185     int m_id;
0186     std::unique_ptr<class KMultiTabBarButtonPrivate> const d;
0187 };
0188 
0189 /**
0190  * @class KMultiTabBarTab kmultitabbar.h KMultiTabBarTab
0191  *
0192  * Use KMultiTabBar::appendTab to append a tab, which creates a KMultiTabBarTab instance
0193  */
0194 class KWIDGETSADDONS_EXPORT KMultiTabBarTab : public KMultiTabBarButton
0195 {
0196     Q_OBJECT
0197 public:
0198     ~KMultiTabBarTab() override;
0199     QSize sizeHint() const override;
0200     QSize minimumSizeHint() const override;
0201 
0202 public Q_SLOTS:
0203     /**
0204      * this is used internally, but can be used by the user, if (s)he wants to
0205      * It the according call of KMultiTabBar is invoked though this modifications will be overwritten
0206      */
0207     void setPosition(KMultiTabBar::KMultiTabBarPosition);
0208 
0209     /**
0210      * this is used internally, but can be used by the user, if (s)he wants to
0211      * It the according call of KMultiTabBar is invoked though this modifications will be overwritten
0212      */
0213     void setStyle(KMultiTabBar::KMultiTabBarStyle);
0214 
0215     /**
0216      * set the active state of the tab
0217      * @param  state true==active false==not active
0218      */
0219     void setState(bool state);
0220 
0221 public:
0222     using KMultiTabBarButton::setIcon;
0223 
0224 protected:
0225     void paintEvent(QPaintEvent *) override;
0226 
0227 private:
0228     KMultiTabBar::KMultiTabBarPosition m_position;
0229     KMultiTabBar::KMultiTabBarStyle m_style;
0230 
0231     KWIDGETSADDONS_NO_EXPORT void computeMargins(int *hMargin, int *vMargin) const;
0232     KWIDGETSADDONS_NO_EXPORT QSize computeSizeHint(bool withText) const;
0233     KWIDGETSADDONS_NO_EXPORT bool shouldDrawText() const;
0234     KWIDGETSADDONS_NO_EXPORT bool isVertical() const;
0235 
0236     KWIDGETSADDONS_NO_EXPORT void initStyleOption(QStyleOptionToolButton *opt) const;
0237 
0238     friend class KMultiTabBarInternal;
0239     /**
0240      * This class should never be created except with the appendTab call of KMultiTabBar
0241      */
0242     KWIDGETSADDONS_NO_EXPORT
0243     KMultiTabBarTab(const QIcon &icon, const QString &, int id, QWidget *parent, KMultiTabBar::KMultiTabBarPosition pos, KMultiTabBar::KMultiTabBarStyle style);
0244 
0245     std::unique_ptr<class KMultiTabBarTabPrivate> const d;
0246 };
0247 
0248 #endif