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