File indexing completed on 2024-04-21 14:56:00

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2003 Stephan Binner <binner@kde.org>
0003     Copyright (C) 2003 Zack Rusin <zack@kde.org>
0004     Copyright (C) 2009 Urs Wolfer <uwolfer @ kde.org>
0005 
0006     This library is free software; you can redistribute it and/or
0007     modify it under the terms of the GNU Library General Public
0008     License as published by the Free Software Foundation; either
0009     version 2 of the License, or (at your option) any later version.
0010 
0011     This library is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014     Library General Public License for more details.
0015 
0016     You should have received a copy of the GNU Library General Public License
0017     along with this library; see the file COPYING.LIB.  If not, write to
0018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019     Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #ifndef KTABWIDGET_H
0023 #define KTABWIDGET_H
0024 
0025 #include <kdelibs4support_export.h>
0026 
0027 #include <QTabWidget>
0028 
0029 class QTab;
0030 
0031 /**
0032  * \brief A widget containing multiple tabs
0033  *
0034  * It extends the Qt QTabWidget, providing extra optionally features such as close buttons when you hover
0035  * over the icon in the tab, and also adds functionality such as responding to mouse wheel scroll events to switch
0036  * the active tab.
0037  *
0038  * It is recommended to use KTabWidget instead of QTabWidget unless you have a good reason not to.
0039  *
0040  * See also the QTabWidget documentation.
0041  *
0042  * \image html ktabwidget.png "KDE Tab Widget"
0043  */
0044 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KTabWidget : public QTabWidget //krazy:exclude=qclasses
0045 {
0046     Q_OBJECT
0047 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0048     Q_PROPERTY(bool tabReorderingEnabled READ isTabReorderingEnabled WRITE setTabReorderingEnabled)
0049     Q_PROPERTY(bool hoverCloseButton READ hoverCloseButton WRITE setHoverCloseButton)
0050     Q_PROPERTY(bool hoverCloseButtonDelayed READ hoverCloseButtonDelayed WRITE setHoverCloseButtonDelayed)
0051     Q_PROPERTY(bool closeButtonEnabled READ isCloseButtonEnabled WRITE setCloseButtonEnabled)
0052     Q_PROPERTY(bool tabCloseActivatePrevious READ tabCloseActivatePrevious WRITE setTabCloseActivatePrevious)
0053 #endif
0054     Q_PROPERTY(bool automaticResizeTabs READ automaticResizeTabs WRITE setAutomaticResizeTabs)
0055 
0056 public:
0057 
0058     /**
0059      * Creates a new tab widget.
0060      *
0061      * @param parent The parent widgets.
0062      * @param flags The Qt window flags @see QWidget.
0063      */
0064     KDELIBS4SUPPORT_DEPRECATED explicit KTabWidget(QWidget *parent = nullptr, Qt::WindowFlags flags = {});
0065 
0066     /**
0067      * Destroys the tab widget.
0068      */
0069     ~KTabWidget() override;
0070 
0071     /**
0072      * Set the tab of the given widget to \a color.
0073      * This is simply a convenience method for QTabBar::setTabTextColor.
0074      */
0075     void setTabTextColor(int index, const QColor &color);
0076 
0077     /**
0078      * Returns the tab color for the given widget.
0079      * This is simply a convenience method for QTabBar::tabTextColor.
0080      */
0081     QColor tabTextColor(int index) const;
0082 
0083     /**
0084      * Returns true if tab ordering with the middle mouse button
0085      * has been enabled.
0086      *
0087      * @deprecated Use QTabWidget::isMovable() instead.
0088      */
0089 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0090     KDELIBS4SUPPORT_DEPRECATED bool isTabReorderingEnabled() const;
0091 #endif
0092 
0093     /**
0094      * Returns true if the close button is shown on tabs
0095      * when mouse is hovering over them.
0096      *
0097      * @deprecated Use QTabWidget::tabsClosable() instead.
0098      */
0099 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0100     KDELIBS4SUPPORT_DEPRECATED bool hoverCloseButton() const;
0101 #endif
0102 
0103     /**
0104      * Returns true if the close button is shown on tabs
0105      * after a delay.
0106      *
0107      * @deprecated Use QTabWidget::setTabsClosable() instead.
0108      */
0109 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0110     KDELIBS4SUPPORT_DEPRECATED bool hoverCloseButtonDelayed() const;
0111 #endif
0112 
0113     /**
0114      * Returns true if the close button is shown on tabs.
0115      *
0116      * @since 4.1
0117      *
0118      * @deprecated Use QTabWidget::tabsClosable() instead.
0119      */
0120 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0121     KDELIBS4SUPPORT_DEPRECATED bool isCloseButtonEnabled() const;
0122 #endif
0123 
0124     /**
0125      * Returns true if closing the current tab activates the previous
0126      * actice tab instead of the one to the right.
0127      *
0128      * @deprecated Use tabBar()->selectionBehaviorOnRemove() instead.
0129      */
0130 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0131     KDELIBS4SUPPORT_DEPRECATED bool tabCloseActivatePrevious() const;
0132 #endif
0133 
0134     /**
0135      * Returns true if calling setTitle() will resize tabs
0136      * to the width of the tab bar.
0137      */
0138     bool automaticResizeTabs() const;
0139 
0140     /**
0141      * If \a hide is true, the tabbar is hidden along with any corner
0142      * widgets.
0143      *
0144      * @deprecated since 5.0, use tabBar()->hide() instead.
0145      */
0146 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0147     KDELIBS4SUPPORT_DEPRECATED void setTabBarHidden(bool hide);
0148 #endif
0149 
0150     /**
0151      * Returns true if the tabbar was hidden by a call to setTabBarHidden( true ).
0152      * Returns false if the widget itself is hidden, but no call to setTabBarHidden( true )
0153      * has been made.
0154      *
0155      * @deprecated since 5.0, use tabBar()->isHidden() instead.
0156      */
0157 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0158     KDELIBS4SUPPORT_DEPRECATED bool isTabBarHidden() const;
0159 #endif
0160 
0161     /**
0162       Reimplemented for internal reasons.
0163      *
0164     virtual void insertTab( QWidget *, const QString &, int index = -1 );
0165 
0166     *!
0167       Reimplemented for internal reasons.
0168      *
0169     virtual void insertTab( QWidget *child, const QIcon& iconset,
0170                             const QString &label, int index = -1 );
0171     *!
0172       Reimplemented for internal reasons.
0173     *
0174     virtual void insertTab( QWidget *, QTab *, int index = -1 );*/
0175 
0176     /**
0177      * Reimplemented for internal reasons.
0178      */
0179     QString tabText(int) const;   // but it's not virtual...
0180 
0181 #ifdef KDE3_SUPPORT
0182     /**
0183      * @deprecated use tabText
0184      */
0185 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0186     inline KDELIBS4SUPPORT_DEPRECATED QString label(int index) const
0187     {
0188         return tabText(index);
0189     }
0190 #endif
0191 
0192     /**
0193      * @deprecated use tabText
0194      */
0195 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0196     inline KDELIBS4SUPPORT_DEPRECATED QString tabLabel(QWidget *w) const
0197     {
0198         return tabText(indexOf(w));
0199     }
0200 #endif
0201 
0202     /**
0203      * @deprecated use setTabText
0204      */
0205 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0206     inline KDELIBS4SUPPORT_DEPRECATED void setTabLabel(QWidget *w, const QString &l)
0207     {
0208         setTabText(indexOf(w), l);
0209     }
0210 #endif
0211 #endif
0212 
0213     /**
0214      * Reimplemented for internal reasons.
0215      */
0216     void setTabText(int, const QString &);
0217 
0218     using QTabWidget::tabBar;
0219 
0220 public Q_SLOTS:
0221     /**
0222      * Move a widget's tab from first to second specified index and emit
0223      * signal movedTab( int, int ) afterwards.
0224      */
0225     virtual void moveTab(int, int);
0226 
0227     /**
0228      * Removes the widget, reimplemented for
0229      * internal reasons (keeping labels in sync).
0230      * @deprecated since 4.0
0231      */
0232     virtual QT_MOC_COMPAT void removePage(QWidget *w);
0233 
0234     /**
0235      * Removes the widget, reimplemented for
0236      * internal reasons (keeping labels in sync).
0237      */
0238     virtual void removeTab(int index); // but it's not virtual in QTabWidget...
0239 
0240     /**
0241      * If \a enable is true, tab reordering with middle button will be enabled.
0242      *
0243      * Note that once enabled you shouldn't rely on previously queried
0244      * currentPageIndex() or indexOf( QWidget * ) values anymore.
0245      *
0246      * You can connect to signal movedTab(int, int) which will notify
0247      * you from which index to which index a tab has been moved.
0248      *
0249      * @deprecated Use QTabWidget::setMovable() instead.
0250      *
0251      * Note, however, that QTabWidget::setMovable(true) disables
0252      * dragging tabs out of the KTabBar (e.g., dragging the tab
0253      * URL from Konqueror to another application)!
0254      */
0255 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0256     QT_MOC_COMPAT void setTabReorderingEnabled(bool enable);
0257 #endif
0258 
0259     /**
0260      * If \a enable is true, a close button will be shown on mouse hover
0261      * over tab icons which will emit signal closeRequest( QWidget * )
0262      * when pressed.
0263      *
0264      * @deprecated Use QTabWidget::setTabsClosable() instead.
0265      */
0266 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0267     QT_MOC_COMPAT void setHoverCloseButton(bool enable);
0268 #endif
0269 
0270     /**
0271      * If \a delayed is true, a close button will be shown on mouse hover
0272      * over tab icons after mouse double click delay else immediately.
0273      *
0274      * @deprecated Use QTabWidget::setTabsClosable() instead.
0275      */
0276 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0277     QT_MOC_COMPAT void setHoverCloseButtonDelayed(bool delayed);
0278 #endif
0279 
0280     /**
0281      * If enabled, a close button is available for each tab. The
0282      * signal KTabWidget::closeRequest() is emitted, if the close button
0283      * has been clicked.
0284      *
0285      * @since 4.1
0286      *
0287      * @deprecated Use QTabWidget::setTabsClosable() instead.
0288      */
0289 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0290     QT_MOC_COMPAT void setCloseButtonEnabled(bool);
0291 #endif
0292 
0293     /**
0294      * If \a previous is true, closing the current tab activates the
0295      * previous active tab instead of the one to the right.
0296      *
0297      * @deprecated Use tabBar()->setSelectionBehaviorOnRemove() instead.
0298      */
0299 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0300     QT_MOC_COMPAT void setTabCloseActivatePrevious(bool previous);
0301 #endif
0302 
0303     /**
0304      * If \a enable is true, tabs will be resized to the width of the tab bar.
0305      *
0306      * Does not work reliably with "QTabWidget* foo=new KTabWidget()" and if
0307      * you change tabs via the tabbar or by accessing tabs directly.
0308      */
0309     void setAutomaticResizeTabs(bool enable);
0310 
0311 Q_SIGNALS:
0312     /**
0313      * Connect to this and set accept to true if you can and want to decode the event.
0314      */
0315     void testCanDecode(const QDragMoveEvent *e, bool &accept /* result */);
0316 
0317     /**
0318      * Received an event in the empty space beside tabbar. Usually creates a new tab.
0319      * This signal is only possible after testCanDecode and positive accept result.
0320      */
0321     void receivedDropEvent(QDropEvent *);
0322 
0323     /**
0324      * Received an drop event on given widget's tab.
0325      * This signal is only possible after testCanDecode and positive accept result.
0326      */
0327     void receivedDropEvent(QWidget *, QDropEvent *);
0328 
0329     /**
0330      * Request to start a drag operation on the given tab.
0331      */
0332     void initiateDrag(QWidget *);
0333 
0334     /**
0335      * The right mouse button was pressed over empty space besides tabbar.
0336      */
0337     void contextMenu(const QPoint &);
0338 
0339     /**
0340      * The right mouse button was pressed over a widget.
0341      */
0342     void contextMenu(QWidget *, const QPoint &);
0343 
0344 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0345     /**
0346      * A tab was moved from first to second index. This signal is only
0347      * possible after you have called setTabReorderingEnabled( true ).
0348      */
0349     void movedTab(int, int);
0350 #endif
0351 
0352     /**
0353      * A double left mouse button click was performed over empty space besides tabbar.
0354      * The signal is emitted on the second press of the mouse button, before the release.
0355      */
0356     void mouseDoubleClick();
0357 
0358     /**
0359      * A double left mouse button click was performed over the widget.
0360      * The signal is emitted on the second press of the mouse button, before the release.
0361      */
0362     void mouseDoubleClick(QWidget *);
0363 
0364     /**
0365      * A middle mouse button click was performed over empty space besides tabbar.
0366      * The signal is emitted on the release of the mouse button.
0367      */
0368     void mouseMiddleClick();
0369 
0370     /**
0371      * A middle mouse button click was performed over the widget.
0372      * The signal is emitted on the release of the mouse button.
0373      */
0374     void mouseMiddleClick(QWidget *);
0375 
0376     /**
0377      * The close button of a widget's tab was clicked. This signal is
0378      * only possible after you have called setCloseButtonEnabled( true ).
0379      */
0380     void closeRequest(QWidget *);
0381 
0382 protected:
0383     void mouseDoubleClickEvent(QMouseEvent *) override;
0384     void mousePressEvent(QMouseEvent *) override;
0385     void mouseReleaseEvent(QMouseEvent *) override;
0386     void dragEnterEvent(QDragEnterEvent *) override;
0387     void dragMoveEvent(QDragMoveEvent *) override;
0388     void dropEvent(QDropEvent *) override;
0389     int tabBarWidthForMaxChars(int);
0390 #ifndef QT_NO_WHEELEVENT
0391     void wheelEvent(QWheelEvent *) override;
0392 #endif
0393     void resizeEvent(QResizeEvent *) override;
0394     void tabInserted(int) override;
0395     void tabRemoved(int) override;
0396 
0397     /**
0398      * @deprecated  This method has no effect and should not be called
0399      */
0400 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0401     KDELIBS4SUPPORT_DEPRECATED void currentChanged(int);
0402 #endif
0403 
0404 protected Q_SLOTS:
0405     virtual void receivedDropEvent(int, QDropEvent *);
0406     virtual void initiateDrag(int);
0407     virtual void contextMenu(int, const QPoint &);
0408     virtual void mouseDoubleClick(int);
0409     virtual void mouseMiddleClick(int);
0410     virtual void closeRequest(int);
0411 #ifndef QT_NO_WHEELEVENT
0412     virtual void wheelDelta(int);
0413 #endif
0414 
0415 private:
0416     class Private;
0417     Private *const d;
0418 
0419     Q_PRIVATE_SLOT(d, void slotTabMoved(int, int))
0420 };
0421 
0422 #endif