File indexing completed on 2024-05-19 04:59:20

0001 /* ============================================================
0002 * TabManager plugin for Falkon
0003 * Copyright (C) 2013-2017  S. Razi Alavizadeh <s.r.alavizadeh@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #ifndef TABMANAGERWIDGET_H
0019 #define TABMANAGERWIDGET_H
0020 
0021 #include <QWidget>
0022 #include <QPointer>
0023 #include <QMultiHash>
0024 #include <QTreeWidgetItem>
0025 
0026 namespace Ui
0027 {
0028 class TabManagerWidget;
0029 }
0030 class QUrl;
0031 class QTreeWidgetItem;
0032 class BrowserWindow;
0033 class WebPage;
0034 class WebTab;
0035 class WebView;
0036 class TLDExtractor;
0037 
0038 class TabTreeWidget : public QTreeWidget
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043     TabTreeWidget(QWidget* parent = nullptr);
0044 
0045     Qt::DropActions supportedDropActions() const override;
0046     QStringList mimeTypes() const override;
0047     QMimeData* mimeData(const QList<QTreeWidgetItem*> &items) const override;
0048     bool dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action) override;
0049 
0050     void setEnableDragTabs(bool enable);
0051 
0052 Q_SIGNALS:
0053     void requestRefreshTree();
0054 
0055 };
0056 
0057 class TabManagerWidget : public QWidget
0058 {
0059     Q_OBJECT
0060 
0061 public:
0062     enum GroupType {
0063         GroupByWindow = 0,
0064         GroupByDomain = 1,
0065         GroupByHost = 2
0066     };
0067 
0068     explicit TabManagerWidget(BrowserWindow* mainClass, QWidget* parent = nullptr, bool defaultWidget = false);
0069     ~TabManagerWidget() override;
0070 
0071     void closeSelectedTabs(const QMultiHash<BrowserWindow*, WebTab*> &tabsHash);
0072     void detachSelectedTabs(const QMultiHash<BrowserWindow*, WebTab*> &tabsHash);
0073     bool bookmarkSelectedTabs(const QMultiHash<BrowserWindow*, WebTab*> &tabsHash);
0074     void unloadSelectedTabs(const QMultiHash<BrowserWindow*, WebTab*> &tabsHash);
0075 
0076     void setGroupType(GroupType type);
0077 
0078     static QString domainFromUrl(const QUrl &url, bool useHostName = false);
0079 
0080 public Q_SLOTS:
0081     void delayedRefreshTree(WebPage* p = nullptr);
0082     void changeGroupType();
0083 
0084 private:
0085     QTreeWidgetItem* groupByDomainName(bool useHostName = false);
0086     QTreeWidgetItem* groupByWindow();
0087     BrowserWindow* getWindow();
0088 
0089     Ui::TabManagerWidget* ui;
0090     QPointer<BrowserWindow> m_window;
0091     WebPage* m_webPage;
0092 
0093     bool m_isRefreshing;
0094     bool m_refreshBlocked;
0095     bool m_waitForRefresh;
0096     bool m_isDefaultWidget;
0097     GroupType m_groupType;
0098 
0099     QString m_filterText;
0100 
0101     static TLDExtractor* s_tldExtractor;
0102 
0103 private Q_SLOTS:
0104     void refreshTree();
0105     void processActions();
0106     void onItemActivated(QTreeWidgetItem* item, int column);
0107     bool isTabSelected();
0108     void customContextMenuRequested(const QPoint &pos);
0109     void filterChanged(const QString &filter, bool force = false);
0110     void filterBarClosed();
0111 
0112 protected:
0113     bool eventFilter(QObject* obj, QEvent* event) override;
0114 
0115 Q_SIGNALS:
0116     void showSideBySide();
0117     void groupTypeChanged(TabManagerWidget::GroupType);
0118 };
0119 
0120 class TabItem : public QObject, public QTreeWidgetItem
0121 {
0122     Q_OBJECT
0123 
0124 public:
0125     enum StateRole {
0126         ActiveOrCaptionRole = Qt::UserRole + 1,
0127         SavedRole = Qt::UserRole + 2
0128     };
0129 
0130     TabItem(QTreeWidget* treeWidget, bool supportDrag = true, bool isTab = true, QTreeWidgetItem* parent = nullptr, bool addToTree = true);
0131 
0132     BrowserWindow* window() const;
0133     void setBrowserWindow(BrowserWindow* window);
0134 
0135     WebTab* webTab() const;
0136     void setWebTab(WebTab* webTab);
0137 
0138     bool isTab() const;
0139 
0140 public Q_SLOTS:
0141     void updateIcon();
0142     void setTitle(const QString& title);
0143     void setIsActiveOrCaption(bool yes);
0144     void setIsSavedTab(bool yes);
0145 
0146 private:
0147     QTreeWidget* m_treeWidget;
0148     BrowserWindow* m_window;
0149     WebTab* m_webTab;
0150     bool m_isTab;
0151 };
0152 
0153 #endif // TABMANAGERWIDGET_H