45
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the demonstration applications of the Qt Toolkit.
|
|
7 **
|
|
8 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
10 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11 ** accordance with the Qt Commercial License Agreement provided with the
|
|
12 ** Software or, alternatively, in accordance with the terms contained in
|
|
13 ** a written agreement between you and Nokia.
|
|
14 **
|
|
15 ** GNU Lesser General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
17 ** General Public License version 2.1 as published by the Free Software
|
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
19 ** packaging of this file. Please review the following information to
|
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
22 **
|
|
23 ** In addition, as a special exception, Nokia gives you certain
|
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL
|
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
26 ** package.
|
|
27 **
|
|
28 ** GNU General Public License Usage
|
|
29 ** Alternatively, this file may be used under the terms of the GNU
|
|
30 ** General Public License version 3.0 as published by the Free Software
|
|
31 ** Foundation and appearing in the file LICENSE.GPL included in the
|
|
32 ** packaging of this file. Please review the following information to
|
|
33 ** ensure the GNU General Public License version 3.0 requirements will be
|
|
34 ** met: http://www.gnu.org/copyleft/gpl.html.
|
|
35 **
|
|
36 ** If you are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41
|
73
|
42 module tabwidget;
|
45
|
43
|
73
|
44 import qt.gui.QTabBar;
|
|
45 import qt.gui.QShortcut;
|
|
46 import qt.gui.QClipboard;
|
|
47 import qt.gui.QCompleter;
|
|
48 import qt.gui.QListView;
|
|
49 import qt.gui.QMenu;
|
|
50 import qt.gui.QMessageBox;
|
|
51 import qt.gui.QMouseEvent;
|
|
52 import qt.gui.QStackedWidget;
|
|
53 import qt.gui.QStyle;
|
|
54 import qt.gui.QToolButton;
|
45
|
55
|
73
|
56 import qt.core.QDebug;
|
45
|
57
|
|
58 import browserapplication;
|
|
59 import browsermainwindow;
|
|
60 import history;
|
|
61 import urllineedit;
|
|
62 import webview;
|
|
63
|
|
64 /*
|
73
|
65 Tab bar with a few more features such as a context menu and shortcuts
|
45
|
66 */
|
|
67 class TabBar : public QTabBar
|
|
68 {
|
73
|
69 mixin Signal!("newTab");
|
74
|
70 mixin Signal!("cloneTab", int /*index*);
|
|
71 mixin Signal!("closeTab", int index*/);
|
|
72 mixin Signal!("closeOtherTabs", int /*index*/);
|
|
73 mixin Signal!("reloadTab", int /*index*/);
|
73
|
74 mixin Signal!("reloadAllTabs");
|
74
|
75 mixin Signal!("tabMoveRequested", int /*fromIndex*/, int /*toIndex*/);
|
45
|
76
|
|
77 public:
|
73
|
78
|
|
79 this(QWidget parent = null)
|
|
80 {
|
|
81 super(parent);
|
|
82 setContextMenuPolicy(Qt.CustomContextMenu);
|
|
83 setAcceptDrops(true);
|
|
84 this.customContextMenuRequested.connect(&this.contextMenuRequested);
|
45
|
85
|
77
|
86 string alt = QLatin1String("Alt+%1");
|
73
|
87 for (int i = 1; i <= 10; ++i) {
|
|
88 int key = i;
|
|
89 if (key == 10)
|
|
90 key = 0;
|
|
91 QShortcut shortCut = new QShortcut(alt.arg(key), this);
|
77
|
92 m_tabShortcuts ~= shortCut;
|
73
|
93 shortCut.activated.connect(&this.selectTabAction);
|
|
94 }
|
|
95 setTabsClosable(true);
|
|
96 this.tabCloseRequested.connect(&this.closeTab);
|
|
97 setSelectionBehaviorOnRemove(QTabBar.SelectPreviousTab);
|
|
98 setMovable(true);
|
|
99 }
|
45
|
100
|
|
101 protected:
|
|
102
|
73
|
103 void mousePressEvent(QMouseEvent event)
|
|
104 {
|
|
105 if (event.button() == Qt.LeftButton)
|
|
106 m_dragStartPos = event.pos();
|
|
107 QTabBar.mousePressEvent(event);
|
|
108 }
|
45
|
109
|
73
|
110 void mouseMoveEvent(QMouseEvent event)
|
|
111 {
|
|
112 if (event.buttons() == Qt.LeftButton) {
|
|
113 int diffX = event.pos().x() - m_dragStartPos.x();
|
|
114 int diffY = event.pos().y() - m_dragStartPos.y();
|
|
115 if ((event.pos() - m_dragStartPos).manhattanLength() > QApplication.startDragDistance() && diffX < 3 && diffX > -3 && diffY < -10) {
|
|
116 QDrag drag = new QDrag(this);
|
|
117 QMimeData mimeData = new QMimeData;
|
77
|
118 QUrl[] urls;
|
73
|
119 int index = tabAt(event.pos());
|
|
120 QUrl url = tabData(index).toUrl();
|
77
|
121 urls ~= url;
|
73
|
122 mimeData.setUrls(urls);
|
|
123 mimeData.setText(tabText(index));
|
|
124 mimeData.setData(QLatin1String("action"), "tab-reordering");
|
|
125 drag.setMimeData(mimeData);
|
|
126 drag.exec();
|
|
127 }
|
|
128 }
|
|
129 QTabBar.mouseMoveEvent(event);
|
|
130 }
|
45
|
131
|
|
132 private:
|
73
|
133
|
|
134 void selectTabAction()
|
|
135 {
|
77
|
136 if (QShortcut shortCut = cast(QShortcut) signalSender()) {
|
73
|
137 int index = m_tabShortcuts.indexOf(shortCut);
|
|
138 if (index == 0)
|
|
139 index = 10;
|
|
140 setCurrentIndex(index);
|
|
141 }
|
|
142 }
|
|
143
|
74
|
144 void cloneTab()
|
73
|
145 {
|
77
|
146 if (QAction action = cast(QAction) signalSender()) {
|
73
|
147 int index = action.data().toInt();
|
77
|
148 cloneTab.emit(index);
|
73
|
149 }
|
|
150 }
|
|
151
|
74
|
152 void closeTab()
|
73
|
153 {
|
77
|
154 if (QAction action = cast(QAction) signalSender()) {
|
73
|
155 int index = action.data().toInt();
|
77
|
156 closeTab.emit(index);
|
73
|
157 }
|
|
158 }
|
|
159
|
|
160 void closeOtherTabs()
|
|
161 {
|
77
|
162 if (QAction action = cast(QAction) signalSender()) {
|
73
|
163 int index = action.data().toInt();
|
77
|
164 closeOtherTabs.emit(index);
|
73
|
165 }
|
|
166 }
|
45
|
167
|
73
|
168 void reloadTab()
|
|
169 {
|
77
|
170 if (QAction action = cast(QAction) signalSender()) {
|
73
|
171 int index = action.data().toInt();
|
77
|
172 reloadTab.emit(index);
|
73
|
173 }
|
|
174 }
|
|
175
|
|
176 void contextMenuRequested(QPoint position)
|
|
177 {
|
|
178 QMenu menu;
|
|
179 menu.addAction(tr("New &Tab"), this, SIGNAL(newTab()), QKeySequence.AddTab);
|
|
180 int index = tabAt(position);
|
|
181 if (-1 != index) {
|
|
182 QAction action = menu.addAction(tr("Clone Tab"), this, SLOT(cloneTab()));
|
|
183 action.setData(index);
|
|
184
|
|
185 menu.addSeparator();
|
|
186
|
|
187 action = menu.addAction(tr("&Close Tab"), this, SLOT(closeTab()), QKeySequence.Close);
|
|
188 action.setData(index);
|
|
189
|
|
190 action = menu.addAction(tr("Close &Other Tabs"), this, SLOT(closeOtherTabs()));
|
|
191 action.setData(index);
|
|
192
|
|
193 menu.addSeparator();
|
|
194
|
|
195 action = menu.addAction(tr("Reload Tab"), this, SLOT(reloadTab()), QKeySequence.Refresh);
|
|
196 action.setData(index);
|
|
197 } else {
|
|
198 menu.addSeparator();
|
|
199 }
|
|
200 menu.addAction(tr("Reload All Tabs"), this, SIGNAL(reloadAllTabs()));
|
|
201 menu.exec(QCursor.pos());
|
|
202 }
|
|
203
|
74
|
204 private:
|
|
205
|
|
206 QShortcut[] m_tabShortcuts;
|
73
|
207
|
|
208 QPoint m_dragStartPos;
|
|
209 int m_dragCurrentIndex;
|
45
|
210 }
|
|
211
|
|
212 import QtWebKit.QWebPage;
|
|
213
|
73
|
214 /*!
|
|
215 A proxy object that connects a single browser action
|
|
216 to one child webpage action at a time.
|
45
|
217
|
73
|
218 Example usage: used to keep the main window stop action in sync with
|
|
219 the current tabs webview's stop action.
|
|
220 */
|
45
|
221 class WebActionMapper : public QObject
|
|
222 {
|
73
|
223 public:
|
|
224
|
|
225 this(QAction root, QWebPage.WebAction webAction, QObject parent)
|
|
226 {
|
|
227 super(parent);
|
|
228 m_currentParent = 0;
|
|
229 m_root = root;
|
|
230 m_webAction = webAction;
|
|
231 if (!m_root)
|
|
232 return;
|
|
233 m_root.triggered.connect(&this.rootTriggered);
|
|
234 root.destroyed.connect(&this.rootDestroyed);
|
|
235 root.setEnabled(false);
|
|
236 }
|
|
237
|
74
|
238 QWebPage.WebAction webAction()
|
73
|
239 {
|
|
240 return m_webAction;
|
|
241 }
|
|
242
|
|
243 void addChild(QAction action)
|
|
244 {
|
|
245 if (!action)
|
|
246 return;
|
|
247 action.changed.connect(&this.childChanged);
|
|
248 }
|
|
249
|
|
250 void updateCurrent(QWebPage currentParent)
|
|
251 {
|
|
252 if (m_currentParent)
|
|
253 m_currentParent.destroyed.disconnect(&this.currentDestroyed);
|
45
|
254
|
73
|
255 m_currentParent = currentParent;
|
|
256 if (!m_root)
|
|
257 return;
|
|
258 if (!m_currentParent) {
|
|
259 m_root.setEnabled(false);
|
|
260 m_root.setChecked(false);
|
|
261 return;
|
|
262 }
|
|
263 QAction source = m_currentParent.action(m_webAction);
|
|
264 m_root.setChecked(source.isChecked());
|
|
265 m_root.setEnabled(source.isEnabled());
|
|
266 m_currentParent.destroyed.connect(&this.currentDestroyed);
|
|
267 }
|
|
268
|
|
269 private:
|
|
270
|
|
271 void rootTriggered()
|
|
272 {
|
|
273 if (m_currentParent) {
|
|
274 QAction gotoAction = m_currentParent.action(m_webAction);
|
|
275 gotoAction.trigger();
|
|
276 }
|
|
277 }
|
45
|
278
|
73
|
279 void childChanged()
|
|
280 {
|
77
|
281 if (QAction source = cast(QAction) signalSender()) {
|
73
|
282 if (m_root && m_currentParent && source.parent() == m_currentParent) {
|
|
283 m_root.setChecked(source.isChecked());
|
|
284 m_root.setEnabled(source.isEnabled());
|
|
285 }
|
|
286 }
|
|
287 }
|
45
|
288
|
73
|
289 void rootDestroyed()
|
|
290 {
|
|
291 m_root = 0;
|
|
292 }
|
|
293
|
|
294 void currentDestroyed()
|
|
295 {
|
|
296 updateCurrent(0);
|
|
297 }
|
|
298
|
|
299 private:
|
|
300
|
|
301 QWebPage m_currentParent;
|
|
302 QAction m_root;
|
|
303 QWebPage.WebAction m_webAction;
|
45
|
304 }
|
|
305
|
|
306
|
73
|
307 import qt.core.QUrl;
|
|
308 import qt.gui.QTabWidget;
|
45
|
309
|
|
310
|
|
311 /*!
|
73
|
312 TabWidget that contains WebViews and a stack widget of associated line edits.
|
45
|
313
|
73
|
314 Connects up the current tab's signals to this class's signal and uses WebActionMapper
|
|
315 to proxy the actions.
|
|
316 */
|
45
|
317 class TabWidget : public QTabWidget
|
|
318 {
|
73
|
319 // tab widget signals
|
77
|
320 mixin Singal!("loadPage", string /*url*/);
|
73
|
321 mixin Singal!("tabsChanged");
|
|
322 mixin Singal!("lastTabClosed");
|
45
|
323
|
73
|
324 // current tab signals
|
77
|
325 mixin Singal!("setCurrentTitle", string /*url*/);
|
|
326 mixin Singal!("showStatusBarMessage", string /*message*/);
|
|
327 mixin Singal!("linkHovered", string /*link*/);
|
74
|
328 mixin Singal!("loadProgress", int /*progress*/);
|
|
329 mixin Singal!("geometryChangeRequested", QRect /*geometry*/);
|
|
330 mixin Singal!("menuBarVisibilityChangeRequested", bool /*visible*/);
|
|
331 mixin Singal!("statusBarVisibilityChangeRequested", bool /*visible*/);
|
|
332 mixin Singal!("toolBarVisibilityChangeRequested", bool /*visible*/);
|
|
333 mixin Singal!("printRequested", QWebFrame /*frame*/);
|
45
|
334
|
|
335 public:
|
73
|
336
|
|
337 this(QWidget parent = null)
|
|
338 {
|
77
|
339 super(parent);
|
73
|
340 m_recentlyClosedTabsAction = 0;
|
|
341 m_newTabAction = 0;
|
|
342 m_closeTabAction = 0;
|
|
343 m_nextTabAction = 0;
|
|
344 m_previousTabAction = 0;
|
|
345 m_recentlyClosedTabsMenu = 0;
|
|
346 m_lineEditCompleter = 0;
|
|
347 m_lineEdits = 0;
|
|
348 m_tabBar = new TabBar(this);
|
|
349
|
|
350 setElideMode(Qt.ElideRight);
|
45
|
351
|
73
|
352 m_tabBar.newTab.connect(&this.newTab);
|
77
|
353 m_tabBar.closeTab.connect(&this.closeTab);
|
|
354 m_tabBar.cloneTab.connect(&this.cloneTab);
|
|
355 m_tabBar.closeOtherTabs.connect(&this.closeOtherTabs);
|
|
356 m_tabBar.reloadTab.connect(&this.reloadTab);
|
|
357 m_tabBar.reloadAllTabs.connect(&this.reloadAllTabs);
|
|
358 m_tabBar.tabMoved.connect(&this.moveTab);
|
73
|
359 setTabBar(m_tabBar);
|
|
360 setDocumentMode(true);
|
45
|
361
|
73
|
362 // Actions
|
|
363 m_newTabAction = new QAction(QIcon(QLatin1String(":addtab.png")), tr("New &Tab"), this);
|
|
364 m_newTabAction.setShortcuts(QKeySequence.AddTab);
|
|
365 m_newTabAction.setIconVisibleInMenu(false);
|
|
366 m_newTabAction.triggered.connect(&this.newTab);
|
|
367
|
|
368 m_closeTabAction = new QAction(QIcon(QLatin1String(":closetab.png")), tr("&Close Tab"), this);
|
|
369 m_closeTabAction.setShortcuts(QKeySequence.Close);
|
|
370 m_closeTabAction.setIconVisibleInMenu(false);
|
|
371 m_closeTabAction.triggered.connect(&this.closeTab);
|
45
|
372
|
73
|
373 m_nextTabAction = new QAction(tr("Show Next Tab"), this);
|
77
|
374 QKeySequence[] shortcuts;
|
|
375 shortcuts ~= QKeySequence(Qt.CTRL | Qt.Key_BraceRight);
|
|
376 shortcuts ~= QKeySequence(Qt.CTRL | Qt.Key_PageDown);
|
|
377 shortcuts ~= QKeySequence(Qt.CTRL | Qt.Key_BracketRight);
|
|
378 shortcuts ~= QKeySequence(Qt.CTRL | Qt.Key_Less);
|
73
|
379 m_nextTabAction.setShortcuts(shortcuts);
|
|
380 m_nextTabAction.triggered.connect(&this.nextTab);
|
45
|
381
|
73
|
382 m_previousTabAction = new QAction(tr("Show Previous Tab"), this);
|
|
383 shortcuts.clear();
|
77
|
384 shortcuts ~= QKeySequence(Qt.CTRL | Qt.Key_BraceLeft);
|
|
385 shortcuts ~= QKeySequence(Qt.CTRL | Qt.Key_PageUp);
|
|
386 shortcuts ~= QKeySequence(Qt.CTRL | Qt.Key_BracketLeft);
|
|
387 shortcuts ~= QKeySequence(Qt.CTRL | Qt.Key_Greater);
|
73
|
388 m_previousTabAction.setShortcuts(shortcuts);
|
|
389 m_previousTabAction.triggered.connect(&this.previousTab);
|
45
|
390
|
73
|
391 m_recentlyClosedTabsMenu = new QMenu(this);
|
|
392 m_recentlyClosedTabsMenu.aboutToShow.connect(&this.aboutToShowRecentTabsMenu);
|
|
393 m_recentlyClosedTabsMenu.triggered.connect(&this.aboutToShowRecentTriggeredAction);
|
|
394 m_recentlyClosedTabsAction = new QAction(tr("Recently Closed Tabs"), this);
|
|
395 m_recentlyClosedTabsAction.setMenu(m_recentlyClosedTabsMenu);
|
|
396 m_recentlyClosedTabsAction.setEnabled(false);
|
|
397
|
|
398 this.currentChanged.connect(&this.currentChanged);
|
45
|
399
|
73
|
400 m_lineEdits = new QStackedWidget(this);
|
|
401 }
|
45
|
402
|
73
|
403 void clear()
|
|
404 {
|
|
405 // clear the recently closed tabs
|
77
|
406 m_recentlyClosedTabs.length = 0;
|
73
|
407 // clear the line edit history
|
|
408 for (int i = 0; i < m_lineEdits.count(); ++i) {
|
|
409 QLineEdit qLineEdit = lineEdit(i);
|
|
410 qLineEdit.setText(qLineEdit.text());
|
|
411 }
|
|
412 }
|
45
|
413
|
73
|
414 void addWebAction(QAction action, QWebPage.WebAction webAction)
|
|
415 {
|
|
416 if (!action)
|
|
417 return;
|
|
418 m_actions.append(new WebActionMapper(action, webAction, this));
|
|
419 }
|
45
|
420
|
74
|
421 QAction newTabAction()
|
73
|
422 {
|
|
423 return m_newTabAction;
|
|
424 }
|
45
|
425
|
74
|
426 QAction closeTabAction()
|
73
|
427 {
|
|
428 return m_closeTabAction;
|
|
429 }
|
|
430
|
74
|
431 QAction recentlyClosedTabsAction()
|
73
|
432 {
|
|
433 return m_recentlyClosedTabsAction;
|
|
434 }
|
45
|
435
|
74
|
436 QAction nextTabAction()
|
73
|
437 {
|
|
438 return m_nextTabAction;
|
|
439 }
|
45
|
440
|
74
|
441 QAction previousTabAction()
|
73
|
442 {
|
|
443 return m_previousTabAction;
|
|
444 }
|
|
445
|
74
|
446 QWidget lineEditStack()
|
73
|
447 {
|
|
448 return m_lineEdits;
|
|
449 }
|
45
|
450
|
74
|
451 QLineEdit currentLineEdit()
|
73
|
452 {
|
|
453 return lineEdit(m_lineEdits.currentIndex());
|
|
454 }
|
45
|
455
|
74
|
456 WebView currentWebView()
|
73
|
457 {
|
|
458 return webView(currentIndex());
|
|
459 }
|
45
|
460
|
74
|
461 WebView webView(int index)
|
73
|
462 {
|
|
463 QWidget widget = this.widget(index);
|
74
|
464 if (WebView webView = cast(WebView) widget) {
|
73
|
465 return webView;
|
|
466 } else {
|
|
467 // optimization to delay creating the first webview
|
|
468 if (count() == 1) {
|
77
|
469 TabWidget that = cast(TabWidget) this;
|
73
|
470 that.setUpdatesEnabled(false);
|
|
471 that.newTab();
|
|
472 that.closeTab(0);
|
|
473 that.setUpdatesEnabled(true);
|
|
474 return currentWebView();
|
|
475 }
|
|
476 }
|
|
477 return 0;
|
|
478 }
|
45
|
479
|
74
|
480 QLineEdit lineEdit(int index)
|
73
|
481 {
|
77
|
482 UrlLineEdit urlLineEdit = cast(UrlLineEdit) m_lineEdits.widget(index);
|
73
|
483 if (urlLineEdit)
|
|
484 return urlLineEdit.lineEdit();
|
|
485 return 0;
|
|
486 }
|
45
|
487
|
74
|
488 int webViewIndex(WebView webView)
|
73
|
489 {
|
|
490 int index = indexOf(webView);
|
|
491 return index;
|
|
492 }
|
45
|
493
|
|
494
|
77
|
495 static const int TabWidgetMagic = 0xaa;
|
45
|
496
|
74
|
497 QByteArray saveState()
|
73
|
498 {
|
77
|
499 int version_ = 1;
|
73
|
500 QByteArray data;
|
|
501 QDataStream stream(data, QIODevice.WriteOnly);
|
45
|
502
|
77
|
503 stream << cast(int) TabWidgetMagic;
|
|
504 stream << cast(int) version_;
|
45
|
505
|
77
|
506 string[] tabs;
|
73
|
507 for (int i = 0; i < count(); ++i) {
|
77
|
508 if (WebView tab = cast(WebView) widget(i)) {
|
73
|
509 tabs.append(tab.url().toString());
|
|
510 } else {
|
77
|
511 tabs.append(null); //QString.null);
|
73
|
512 }
|
|
513 }
|
|
514 stream << tabs;
|
|
515 stream << currentIndex();
|
|
516 return data;
|
|
517 }
|
45
|
518
|
73
|
519 bool restoreState(QByteArray state)
|
|
520 {
|
|
521 int version_ = 1;
|
|
522 QByteArray sd = state;
|
|
523 QDataStream stream(sd, QIODevice.ReadOnly);
|
|
524 if (stream.atEnd())
|
|
525 return false;
|
45
|
526
|
77
|
527 int marker;
|
|
528 int v;
|
73
|
529 stream >> marker;
|
|
530 stream >> v;
|
|
531 if (marker != TabWidgetMagic || v != version_)
|
|
532 return false;
|
45
|
533
|
77
|
534 string[] openTabs;
|
73
|
535 stream >> openTabs;
|
45
|
536
|
77
|
537 for (int i = 0; i < openTabs.length; ++i) {
|
73
|
538 if (i != 0)
|
|
539 newTab();
|
77
|
540 loadPage(openTabs[i]);
|
73
|
541 }
|
45
|
542
|
73
|
543 int currentTab;
|
|
544 stream >> currentTab;
|
|
545 setCurrentIndex(currentTab);
|
45
|
546
|
73
|
547 return true;
|
|
548 }
|
45
|
549
|
|
550 protected:
|
|
551
|
74
|
552 void mouseDoubleClickEvent(QMouseEvent event)
|
73
|
553 {
|
|
554 if (!childAt(event.pos())
|
|
555 // Remove the line below when QTabWidget does not have a one pixel frame
|
|
556 && event.pos().y() < (tabBar().y() + tabBar().height())) {
|
|
557 newTab();
|
|
558 return;
|
|
559 }
|
|
560 QTabWidget.mouseDoubleClickEvent(event);
|
|
561 }
|
45
|
562
|
|
563
|
73
|
564 void contextMenuEvent(QContextMenuEvent event)
|
|
565 {
|
|
566 if (!childAt(event.pos())) {
|
|
567 m_tabBar.contextMenuRequested(event.pos());
|
|
568 return;
|
|
569 }
|
|
570 QTabWidget.contextMenuEvent(event);
|
|
571 }
|
45
|
572
|
73
|
573 void mouseReleaseEvent(QMouseEvent event)
|
|
574 {
|
|
575 if (event.button() == Qt.MidButton && !childAt(event.pos())
|
|
576 // Remove the line below when QTabWidget does not have a one pixel frame
|
|
577 && event.pos().y() < (tabBar().y() + tabBar().height())) {
|
77
|
578 auto url = new QUrl(QApplication.clipboard().text(QClipboard.Selection));
|
73
|
579 if (!url.isEmpty() && url.isValid() && !url.scheme().isEmpty()) {
|
|
580 WebView webView = newTab();
|
|
581 webView.setUrl(url);
|
|
582 }
|
|
583 }
|
|
584 }
|
45
|
585
|
73
|
586 public:
|
|
587
|
|
588 void loadUrlInCurrentTab(QUrl url)
|
|
589 {
|
|
590 WebView webView = currentWebView();
|
|
591 if (webView) {
|
|
592 webView.loadUrl(url);
|
|
593 webView.setFocus();
|
|
594 }
|
|
595 }
|
45
|
596
|
73
|
597 WebView newTab(bool makeCurrent = true)
|
|
598 {
|
|
599 // line edit
|
|
600 UrlLineEdit urlLineEdit = new UrlLineEdit;
|
|
601 QLineEdit lineEdit = urlLineEdit.lineEdit();
|
|
602 if (!m_lineEditCompleter && count() > 0) {
|
|
603 HistoryCompletionModel completionModel = new HistoryCompletionModel(this);
|
|
604 completionModel.setSourceModel(BrowserApplication.historyManager().historyFilterModel());
|
|
605 m_lineEditCompleter = new QCompleter(completionModel, this);
|
|
606 // Should this be in Qt by default?
|
|
607 QAbstractItemView popup = m_lineEditCompleter.popup();
|
77
|
608 QListView listView = cast(QListView) popup;
|
73
|
609 if (listView)
|
|
610 listView.setUniformItemSizes(true);
|
|
611 }
|
|
612 lineEdit.setCompleter(m_lineEditCompleter);
|
|
613 lineEdit.returnPressed.connect(&this.lineEditReturnPressed);
|
|
614 m_lineEdits.addWidget(urlLineEdit);
|
|
615 m_lineEdits.setSizePolicy(lineEdit.sizePolicy());
|
45
|
616
|
73
|
617 // optimization to delay creating the more expensive WebView, history, etc
|
|
618 if (count() == 0) {
|
|
619 QWidget emptyWidget = new QWidget;
|
|
620 QPalette p = emptyWidget.palette();
|
|
621 p.setColor(QPalette.Window, palette().color(QPalette.Base));
|
|
622 emptyWidget.setPalette(p);
|
|
623 emptyWidget.setAutoFillBackground(true);
|
|
624 this.currentChanged.disconnect(&this.currentChanged);
|
|
625 addTab(emptyWidget, tr("(Untitled)"));
|
|
626 this.currentChanged.connect(&this.currentChanged);
|
|
627 return 0;
|
|
628 }
|
45
|
629
|
73
|
630 // webview
|
|
631 WebView webView = new WebView;
|
|
632 urlLineEdit.setWebView(webView);
|
|
633 webView.loadStarted().connect(&this.webViewLoadStarted);
|
|
634 webView.loadFinished.connect(&this.webViewIconChanged);
|
|
635 webView.iconChanged.connect(&this.webViewIconChanged);
|
|
636 webView.titleChanged.connect(&this.webViewTitleChanged);
|
|
637 webView.urlChanged.connect(&this.webViewUrlChanged);
|
|
638 webView.page().windowCloseRequested.connect(&this.windowCloseRequested);
|
|
639 webView.page().geometryChangeRequested.connect(&this.geometryChangeRequested);
|
|
640 webView.page().printRequested.connect(&this.printRequested);
|
|
641 webView.page().menuBarVisibilityChangeRequested.connect(&this.menuBarVisibilityChangeRequested);
|
|
642 webView.page().statusBarVisibilityChangeRequested.connect(&this.statusBarVisibilityChangeRequested);
|
|
643 webView.page().toolBarVisibilityChangeRequested.connect(&this.toolBarVisibilityChangeRequested);
|
|
644 addTab(webView, tr("(Untitled)"));
|
|
645 if (makeCurrent)
|
|
646 setCurrentWidget(webView);
|
45
|
647
|
73
|
648 // webview actions
|
|
649 for (int i = 0; i < m_actions.count(); ++i) {
|
|
650 WebActionMapper mapper = m_actions[i];
|
|
651 mapper.addChild(webView.page().action(mapper.webAction()));
|
|
652 }
|
45
|
653
|
73
|
654 if (count() == 1)
|
|
655 currentChanged(currentIndex());
|
77
|
656 tabsChanged.emit();
|
73
|
657 return webView;
|
|
658 }
|
45
|
659
|
73
|
660 // When index is -1 index chooses the current tab
|
|
661 void cloneTab(int index = -1)
|
|
662 {
|
|
663 if (index < 0)
|
|
664 index = currentIndex();
|
|
665 if (index < 0 || index >= count())
|
|
666 return;
|
|
667 WebView tab = newTab(false);
|
|
668 tab.setUrl(webView(index).url());
|
|
669 }
|
45
|
670
|
73
|
671 // When index is -1 index chooses the current tab
|
74
|
672 void closeTab(int index = -1)
|
73
|
673 {
|
|
674 if (index < 0)
|
|
675 index = currentIndex();
|
|
676 if (index < 0 || index >= count())
|
|
677 return;
|
45
|
678
|
73
|
679 bool hasFocus = false;
|
|
680 if (WebView tab = webView(index)) {
|
|
681 if (tab.isModified()) {
|
|
682 QMessageBox closeConfirmation(tab);
|
|
683 closeConfirmation.setWindowFlags(Qt.Sheet);
|
|
684 closeConfirmation.setWindowTitle(tr("Do you really want to close this page?"));
|
|
685 closeConfirmation.setInformativeText(tr("You have modified this page and when closing it you would lose the modification.\n"
|
|
686 "Do you really want to close this page?\n"));
|
|
687 closeConfirmation.setIcon(QMessageBox.Question);
|
|
688 closeConfirmation.addButton(QMessageBox.Yes);
|
|
689 closeConfirmation.addButton(QMessageBox.No);
|
|
690 closeConfirmation.setEscapeButton(QMessageBox.No);
|
|
691 if (closeConfirmation.exec() == QMessageBox.No)
|
|
692 return;
|
|
693 }
|
|
694 hasFocus = tab.hasFocus();
|
45
|
695
|
73
|
696 m_recentlyClosedTabsAction.setEnabled(true);
|
77
|
697 m_recentlyClosedTabs = [tab.url()] ~ m_recentlyClosedTabs;
|
|
698 if (m_recentlyClosedTabs.length >= TabWidget.m_recentlyClosedTabsSize)
|
73
|
699 m_recentlyClosedTabs.removeLast();
|
|
700 }
|
|
701 QWidget lineEdit = m_lineEdits.widget(index);
|
|
702 m_lineEdits.removeWidget(lineEdit);
|
|
703 lineEdit.deleteLater();
|
|
704 QWidget webView = widget(index);
|
|
705 removeTab(index);
|
|
706 webView.deleteLater();
|
77
|
707 tabsChanged.emit();
|
73
|
708 if (hasFocus && count() > 0)
|
|
709 currentWebView().setFocus();
|
|
710 if (count() == 0)
|
77
|
711 lastTabClosed.emit();
|
73
|
712 }
|
45
|
713
|
73
|
714 void closeOtherTabs(int index)
|
|
715 {
|
|
716 if (-1 == index)
|
|
717 return;
|
|
718 for (int i = count() - 1; i > index; --i)
|
|
719 closeTab(i);
|
|
720 for (int i = index - 1; i >= 0; --i)
|
|
721 closeTab(i);
|
|
722 }
|
45
|
723
|
73
|
724 // When index is -1 index chooses the current tab
|
74
|
725 void reloadTab(int index = -1)
|
73
|
726 {
|
|
727 if (index < 0)
|
|
728 index = currentIndex();
|
|
729 if (index < 0 || index >= count())
|
|
730 return;
|
45
|
731
|
73
|
732 QWidget widget = this.widget(index);
|
74
|
733 if (WebView tab = cast(WebView) widget)
|
73
|
734 tab.reload();
|
|
735 }
|
45
|
736
|
74
|
737 void reloadAllTabs()
|
73
|
738 {
|
|
739 for (int i = 0; i < count(); ++i) {
|
|
740 QWidget tabWidget = widget(i);
|
74
|
741 if (WebView tab = cast(WebView) tabWidget) {
|
73
|
742 tab.reload();
|
|
743 }
|
|
744 }
|
|
745 }
|
|
746
|
|
747 void nextTab()
|
|
748 {
|
|
749 int next = currentIndex() + 1;
|
|
750 if (next == count())
|
|
751 next = 0;
|
|
752 setCurrentIndex(next);
|
|
753 }
|
45
|
754
|
73
|
755 void previousTab()
|
|
756 {
|
|
757 int next = currentIndex() - 1;
|
|
758 if (next < 0)
|
|
759 next = count() - 1;
|
|
760 setCurrentIndex(next);
|
|
761 }
|
45
|
762
|
|
763 private:
|
73
|
764
|
|
765 void currentChanged(int index)
|
|
766 {
|
|
767 WebView webView = this.webView(index);
|
|
768 if (!webView)
|
|
769 return;
|
|
770
|
|
771 assert(m_lineEdits.count() == count());
|
|
772
|
|
773 WebView oldWebView = this.webView(m_lineEdits.currentIndex());
|
|
774 if (oldWebView) {
|
|
775 oldWebView.statusBarMessage.disconnect(&this.showStatusBarMessage);
|
|
776 oldWebView.page().linkHovered.disconnect(&this.linkHovered);
|
|
777 oldWebView.loadProgress().disconnect(&this.loadProgress);
|
|
778 }
|
|
779
|
|
780 webView.statusBarMessage.connect(&this.showStatusBarMessage);
|
|
781 webView.page().linkHovered.connect(&this.linkHovered);
|
|
782 webView.loadProgress.connect(&this.loadProgress);
|
|
783
|
|
784 for (int i = 0; i < m_actions.count(); ++i) {
|
|
785 WebActionMapper mapper = m_actions[i];
|
|
786 mapper.updateCurrent(webView.page());
|
|
787 }
|
77
|
788 setCurrentTitle.emit(webView.title());
|
73
|
789 m_lineEdits.setCurrentIndex(index);
|
77
|
790 loadProgress.emit(webView.progress());
|
|
791 showStatusBarMessage.emit(webView.lastStatusBarText());
|
73
|
792 if (webView.url().isEmpty())
|
|
793 m_lineEdits.currentWidget().setFocus();
|
|
794 else
|
|
795 webView.setFocus();
|
|
796 }
|
|
797
|
|
798 void aboutToShowRecentTabsMenu()
|
|
799 {
|
|
800 m_recentlyClosedTabsMenu.clear();
|
77
|
801 for (int i = 0; i < m_recentlyClosedTabs.length; ++i) {
|
73
|
802 QAction action = new QAction(m_recentlyClosedTabsMenu);
|
77
|
803 action.setData(m_recentlyClosedTabs[i]);
|
|
804 QIcon icon = BrowserApplication.instance().icon(m_recentlyClosedTabs[i]);
|
73
|
805 action.setIcon(icon);
|
77
|
806 action.setText(m_recentlyClosedTabs[i].toString());
|
73
|
807 m_recentlyClosedTabsMenu.addAction(action);
|
|
808 }
|
|
809 }
|
|
810
|
|
811 void aboutToShowRecentTriggeredAction(QAction action)
|
|
812 {
|
|
813 QUrl url = action.data().toUrl();
|
|
814 loadUrlInCurrentTab(url);
|
|
815 }
|
|
816
|
|
817 void webViewLoadStarted()
|
|
818 {
|
77
|
819 WebView webView = cast(WebView) signalSender();
|
73
|
820 int index = webViewIndex(webView);
|
|
821 if (-1 != index) {
|
77
|
822 auto icon = new QIcon(QLatin1String(":loading.gif"));
|
73
|
823 setTabIcon(index, icon);
|
|
824 }
|
|
825 }
|
45
|
826
|
73
|
827 void webViewIconChanged()
|
|
828 {
|
77
|
829 WebView webView = cast(WebView) signalSender();
|
73
|
830 int index = webViewIndex(webView);
|
|
831 if (-1 != index) {
|
|
832 QIcon icon = BrowserApplication.instance().icon(webView.url());
|
|
833 setTabIcon(index, icon);
|
|
834 }
|
|
835 }
|
|
836
|
77
|
837 void webViewTitleChanged(string title)
|
73
|
838 {
|
77
|
839 WebView webView = cast(WebView) signalSender();
|
73
|
840 int index = webViewIndex(webView);
|
|
841 if (-1 != index) {
|
|
842 setTabText(index, title);
|
|
843 }
|
|
844 if (currentIndex() == index)
|
77
|
845 setCurrentTitle.emit(title);
|
73
|
846 BrowserApplication.historyManager().updateHistoryItem(webView.url(), title);
|
|
847 }
|
|
848
|
|
849 void webViewUrlChanged(QUrl url)
|
|
850 {
|
77
|
851 WebView webView = cast(WebView) signalSender();
|
73
|
852 int index = webViewIndex(webView);
|
|
853 if (-1 != index) {
|
|
854 m_tabBar.setTabData(index, url);
|
|
855 }
|
77
|
856 tabsChanged.emit();
|
73
|
857 }
|
45
|
858
|
73
|
859 void lineEditReturnPressed()
|
|
860 {
|
77
|
861 if (QLineEdit lineEdit = cast(QLineEdit) signalSender()) {
|
|
862 loadPage.emit(lineEdit.text());
|
73
|
863 if (m_lineEdits.currentWidget() == lineEdit)
|
|
864 currentWebView().setFocus();
|
|
865 }
|
|
866 }
|
|
867
|
|
868 void windowCloseRequested()
|
|
869 {
|
77
|
870 WebPage webPage = cast(WebPage) signalSender();
|
74
|
871 WebView webView = cast(WebView) webPage.view();
|
73
|
872 int index = webViewIndex(webView);
|
|
873 if (index >= 0) {
|
|
874 if (count() == 1)
|
|
875 webView.webPage().mainWindow().close();
|
|
876 else
|
|
877 closeTab(index);
|
|
878 }
|
|
879 }
|
|
880
|
|
881 void moveTab(int fromIndex, int toIndex)
|
|
882 {
|
|
883 QWidget lineEdit = m_lineEdits.widget(fromIndex);
|
|
884 m_lineEdits.removeWidget(lineEdit);
|
|
885 m_lineEdits.insertWidget(toIndex, lineEdit);
|
|
886 }
|
|
887
|
|
888 private:
|
|
889
|
|
890 QAction m_recentlyClosedTabsAction;
|
|
891 QAction m_newTabAction;
|
|
892 QAction m_closeTabAction;
|
|
893 QAction m_nextTabAction;
|
|
894 QAction m_previousTabAction;
|
|
895
|
|
896 QMenu m_recentlyClosedTabsMenu;
|
|
897 static const int m_recentlyClosedTabsSize = 10;
|
74
|
898 QUrl[] m_recentlyClosedTabs;
|
|
899 WebActionMapper[] m_actions;
|
73
|
900
|
|
901 QCompleter m_lineEditCompleter;
|
|
902 QStackedWidget m_lineEdits;
|
|
903 TabBar m_tabBar;
|
45
|
904 }
|