1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2015 The Qt Company Ltd. 4 ** Contact: http://www.qt.io/licensing/ 5 ** 6 ** This file is part of the demonstration applications of the Qt Toolkit. 7 ** 8 ** $QT_BEGIN_LICENSE:LGPL21$ 9 ** Commercial License Usage 10 ** Licensees holding valid commercial Qt licenses may use this file in 11 ** accordance with the commercial license agreement provided with the 12 ** Software or, alternatively, in accordance with the terms contained in 13 ** a written agreement between you and The Qt Company. For licensing terms 14 ** and conditions see http://www.qt.io/terms-conditions. For further 15 ** information use the contact form at http://www.qt.io/contact-us. 16 ** 17 ** GNU Lesser General Public License Usage 18 ** Alternatively, this file may be used under the terms of the GNU Lesser 19 ** General Public License version 2.1 or version 3 as published by the Free 20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and 21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the 22 ** following information to ensure the GNU Lesser General Public License 23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and 24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 25 ** 26 ** As a special exception, The Qt Company gives you certain additional 27 ** rights. These rights are described in The Qt Company LGPL Exception 28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 29 ** 30 ** $QT_END_LICENSE$ 31 ** 32 ****************************************************************************/ 33 34 #ifndef TABWIDGET_H 35 #define TABWIDGET_H 36 37 #include <QtWidgets/QTabBar> 38 39 #include <QtWidgets/QShortcut> 40 /* 41 Tab bar with a few more features such as a context menu and shortcuts 42 */ 43 class TabBar : public QTabBar 44 { 45 Q_OBJECT 46 47 signals: 48 void newTab(); 49 void cloneTab(int index); 50 void closeTab(int index); 51 void closeOtherTabs(int index); 52 void reloadTab(int index); 53 void reloadAllTabs(); 54 void tabMoveRequested(int fromIndex, int toIndex); 55 56 public: 57 TabBar(QWidget *parent = 0); 58 59 protected: 60 void mousePressEvent(QMouseEvent* event); 61 void mouseMoveEvent(QMouseEvent* event); 62 63 private slots: 64 void selectTabAction(); 65 void cloneTab(); 66 void closeTab(); 67 void closeOtherTabs(); 68 void reloadTab(); 69 void contextMenuRequested(const QPoint &position); 70 71 private: 72 QList<QShortcut*> m_tabShortcuts; 73 friend class TabWidget; 74 75 QPoint m_dragStartPos; 76 int m_dragCurrentIndex; 77 }; 78 79 #include <QWebPage> 80 81 QT_BEGIN_NAMESPACE 82 class QAction; 83 QT_END_NAMESPACE 84 class WebView; 85 /*! 86 A proxy object that connects a single browser action 87 to one child webpage action at a time. 88 89 Example usage: used to keep the main window stop action in sync with 90 the current tabs webview's stop action. 91 */ 92 class WebActionMapper : public QObject 93 { 94 Q_OBJECT 95 96 public: 97 WebActionMapper(QAction *root, QWebPage::WebAction webAction, QObject *parent); 98 QWebPage::WebAction webAction() const; 99 void addChild(QAction *action); 100 void updateCurrent(QWebPage *currentParent); 101 102 private slots: 103 void rootTriggered(); 104 void childChanged(); 105 void rootDestroyed(); 106 void currentDestroyed(); 107 108 private: 109 QWebPage *m_currentParent; 110 QAction *m_root; 111 QWebPage::WebAction m_webAction; 112 }; 113 114 #include <QtCore/QUrl> 115 #include <QtWidgets/QTabWidget> 116 QT_BEGIN_NAMESPACE 117 class QCompleter; 118 class QLineEdit; 119 class QMenu; 120 class QStackedWidget; 121 QT_END_NAMESPACE 122 /*! 123 TabWidget that contains WebViews and a stack widget of associated line edits. 124 125 Connects up the current tab's signals to this class's signal and uses WebActionMapper 126 to proxy the actions. 127 */ 128 class TabWidget : public QTabWidget 129 { 130 Q_OBJECT 131 132 signals: 133 // tab widget signals 134 void loadPage(const QString &url); 135 void tabsChanged(); 136 void lastTabClosed(); 137 138 // current tab signals 139 void setCurrentTitle(const QString &url); 140 void showStatusBarMessage(const QString &message); 141 void linkHovered(const QString &link); 142 void loadProgress(int progress); 143 void geometryChangeRequested(const QRect &geometry); 144 void menuBarVisibilityChangeRequested(bool visible); 145 void statusBarVisibilityChangeRequested(bool visible); 146 void toolBarVisibilityChangeRequested(bool visible); 147 void printRequested(QWebFrame *frame); 148 149 public: 150 TabWidget(QWidget *parent = 0); 151 void clear(); 152 void addWebAction(QAction *action, QWebPage::WebAction webAction); 153 154 QAction *newTabAction() const; 155 QAction *closeTabAction() const; 156 QAction *recentlyClosedTabsAction() const; 157 QAction *nextTabAction() const; 158 QAction *previousTabAction() const; 159 160 QWidget *lineEditStack() const; 161 QLineEdit *currentLineEdit() const; 162 WebView *currentWebView() const; 163 WebView *webView(int index) const; 164 QLineEdit *lineEdit(int index) const; 165 int webViewIndex(WebView *webView) const; 166 167 QByteArray saveState() const; 168 bool restoreState(const QByteArray &state); 169 170 protected: 171 void mouseDoubleClickEvent(QMouseEvent *event); 172 void contextMenuEvent(QContextMenuEvent *event); 173 void mouseReleaseEvent(QMouseEvent *event); 174 175 public slots: 176 void loadUrlInCurrentTab(const QUrl &url); 177 WebView *newTab(bool makeCurrent = true); 178 void cloneTab(int index = -1); 179 void closeTab(int index = -1); 180 void closeOtherTabs(int index); 181 void reloadTab(int index = -1); 182 void reloadAllTabs(); 183 void nextTab(); 184 void previousTab(); 185 186 private slots: 187 void currentChanged(int index); 188 void aboutToShowRecentTabsMenu(); 189 void aboutToShowRecentTriggeredAction(QAction *action); 190 void webViewLoadStarted(); 191 void webViewIconChanged(); 192 void webViewTitleChanged(const QString &title); 193 void webViewUrlChanged(const QUrl &url); 194 void lineEditReturnPressed(); 195 void windowCloseRequested(); 196 void moveTab(int fromIndex, int toIndex); 197 198 private: 199 QAction *m_recentlyClosedTabsAction; 200 QAction *m_newTabAction; 201 QAction *m_closeTabAction; 202 QAction *m_nextTabAction; 203 QAction *m_previousTabAction; 204 205 QMenu *m_recentlyClosedTabsMenu; 206 static const int m_recentlyClosedTabsSize = 10; 207 QList<QUrl> m_recentlyClosedTabs; 208 QList<WebActionMapper*> m_actions; 209 210 QCompleter *m_lineEditCompleter; 211 QStackedWidget *m_lineEdits; 212 TabBar *m_tabBar; 213 }; 214 215 #endif // TABWIDGET_H 216 217