File indexing completed on 2024-04-21 04:58:21

0001 /*  This file is part of the KDE project
0002 
0003     SPDX-FileCopyrightText: 2002-2003 Konqueror Developers <konq-e@kde.org>
0004     SPDX-FileCopyrightText: 2002-2003 Douglas Hanley <douglash@caltech.edu>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef KONQTABS_H
0010 #define KONQTABS_H
0011 
0012 #include "konqframe.h"
0013 #include "konqframecontainer.h"
0014 
0015 #include "ktabwidget.h"
0016 #include <QKeyEvent>
0017 #include <QList>
0018 
0019 class QMenu;
0020 
0021 class KonqView;
0022 class KonqViewManager;
0023 class KonqFrameContainerBase;
0024 class KonqFrameContainer;
0025 class KConfig;
0026 class QToolButton;
0027 
0028 class NewTabToolButton;
0029 
0030 class KONQ_TESTS_EXPORT KonqFrameTabs : public KTabWidget, public KonqFrameContainerBase
0031 {
0032     Q_OBJECT
0033 
0034 public:
0035     KonqFrameTabs(QWidget *parent, KonqFrameContainerBase *parentContainer,
0036                   KonqViewManager *viewManager);
0037     ~KonqFrameTabs() override;
0038 
0039     bool accept(KonqFrameVisitor *visitor) override;
0040 
0041     void saveConfig(KConfigGroup &config, const QString &prefix, const KonqFrameBase::Options &options,
0042                             KonqFrameBase *docContainer, int id = 0, int depth = 0) override;
0043     void copyHistory(KonqFrameBase *other) override;
0044 
0045     const QList<KonqFrameBase *> &childFrameList() const
0046     {
0047         return m_childFrameList;
0048     }
0049 
0050     void setTitle(const QString &title, QWidget *sender) override;
0051     void setTabIcon(const QUrl &url, QWidget *sender) override;
0052 
0053     QWidget *asQWidget() override
0054     {
0055         return this;
0056     }
0057     KonqFrameBase::FrameType frameType() const override
0058     {
0059         return KonqFrameBase::Tabs;
0060     }
0061 
0062     void activateChild() override;
0063 
0064     /**
0065      * Insert a new frame into the container.
0066      */
0067     void insertChildFrame(KonqFrameBase *frame, int index = -1) override;
0068 
0069     /**
0070      * Call this before deleting one of our children.
0071      */
0072     void childFrameRemoved(KonqFrameBase *frame) override;
0073 
0074     void replaceChildFrame(KonqFrameBase *oldFrame, KonqFrameBase *newFrame) override;
0075 
0076     /**
0077      * Returns the tab at a given index
0078      * (same as widget(index), but casted to a KonqFrameBase)
0079      */
0080     KonqFrameBase *tabAt(int index) const;
0081 
0082     /**
0083      * Returns the current tab
0084      * (same as currentWidget(), but casted to a KonqFrameBase)
0085      */
0086     KonqFrameBase *currentTab() const;
0087 
0088     void moveTabBackward(int index);
0089     void moveTabForward(int index);
0090 
0091     void setLoading(KonqFrameBase *frame, bool loading);
0092 
0093     /**
0094      * Returns the tab index that contains (directly or indirectly) the frame @p frame,
0095      * or -1 if the frame is not in the tab widget.
0096      */
0097     int tabIndexContaining(KonqFrameBase *frame) const;
0098 
0099     /**
0100      * Implemented to catch MMB click when KonqSettings::mouseMiddleClickClosesTab()
0101      * returns true so that the tab can be properly closed without being activated
0102      * first.
0103      */
0104     bool eventFilter(QObject *, QEvent *) override;
0105 
0106 public Q_SLOTS:
0107     void slotCurrentChanged(int index);
0108     void setAlwaysTabbedMode(bool);
0109     void forceHideTabBar(bool force);
0110     void reparseConfiguration();
0111 
0112 Q_SIGNALS:
0113     void removeTabPopup();
0114     void openUrl(KonqView *view, const QUrl &url);
0115 
0116 private:
0117     void refreshSubPopupMenuTab();
0118     void updateTabBarVisibility();
0119     void initPopupMenu();
0120     /**
0121      * Returns the index position of the tab where the frame @p frame is, assuming that
0122      * it's the active frame in that tab,
0123      * or -1 if the frame is not in the tab widget or it's not active.
0124      */
0125     int tabWhereActive(KonqFrameBase *frame) const;
0126 
0127     /**
0128      * @brief Applies the user choice regarding the tab bar position
0129      *
0130      * If the option contains an invalid value, `North` will be used
0131      */
0132     void applyTabBarPositionOption();
0133 
0134 private Q_SLOTS:
0135     void slotContextMenu(const QPoint &);
0136     void slotContextMenu(QWidget *, const QPoint &);
0137     void slotCloseRequest(int);
0138     void slotMovedTab(int, int);
0139     void slotMouseMiddleClick();
0140     void slotMouseMiddleClick(QWidget *);
0141 
0142     void slotTestCanDecode(const QDragMoveEvent *e, bool &accept /* result */);
0143     void slotReceivedDropEvent(QDropEvent *);
0144     void slotInitiateDrag(QWidget *);
0145     void slotReceivedDropEvent(QWidget *, QDropEvent *);
0146     void slotSubPopupMenuTabActivated(QAction *);
0147 
0148 private:
0149     QList<KonqFrameBase *> m_childFrameList;
0150 
0151     KonqViewManager *m_pViewManager;
0152     QMenu *m_pPopupMenu;
0153     QMenu *m_pSubPopupMenuTab;
0154     QToolButton *m_rightWidget;
0155     NewTabToolButton *m_leftWidget;
0156     bool m_permanentCloseButtons;
0157     bool m_alwaysTabBar;
0158     bool m_forceHideTabBar;
0159     QMap<QString, QAction *> m_popupActions;
0160 };
0161 
0162 #include <QToolButton>
0163 
0164 class NewTabToolButton : public QToolButton // subclass with drag'n'drop functionality for links
0165 {
0166     Q_OBJECT
0167 public:
0168     NewTabToolButton(QWidget *parent)
0169         : QToolButton(parent)
0170     {
0171         setAcceptDrops(true);
0172     }
0173 
0174 Q_SIGNALS:
0175     void testCanDecode(const QDragMoveEvent *event, bool &accept);
0176     void receivedDropEvent(QDropEvent *event);
0177 
0178 protected:
0179     void dragEnterEvent(QDragEnterEvent *event) override
0180     {
0181         bool accept = false;
0182         emit testCanDecode(event, accept);
0183         if (accept) {
0184             event->acceptProposedAction();
0185         }
0186     }
0187 
0188     void dropEvent(QDropEvent *event) override
0189     {
0190         emit receivedDropEvent(event);
0191         event->acceptProposedAction();
0192     }
0193 };
0194 
0195 #endif // KONQTABS_H