comparison demos/browser/tabwidget.d @ 74:37caa90ce503

more porting
author mandel
date Fri, 22 May 2009 23:43:58 +0000
parents 7bfd46c330dc
children 0654fc9bac95
comparison
equal deleted inserted replaced
73:7bfd46c330dc 74:37caa90ce503
65 Tab bar with a few more features such as a context menu and shortcuts 65 Tab bar with a few more features such as a context menu and shortcuts
66 */ 66 */
67 class TabBar : public QTabBar 67 class TabBar : public QTabBar
68 { 68 {
69 mixin Signal!("newTab"); 69 mixin Signal!("newTab");
70 mixin Signal!("cloneTab", int index); 70 mixin Signal!("cloneTab", int /*index*);
71 mixin Signal!("closeTab", int index); 71 mixin Signal!("closeTab", int index*/);
72 mixin Signal!("closeOtherTabs", int index); 72 mixin Signal!("closeOtherTabs", int /*index*/);
73 mixin Signal!("reloadTab", int index); 73 mixin Signal!("reloadTab", int /*index*/);
74 mixin Signal!("reloadAllTabs"); 74 mixin Signal!("reloadAllTabs");
75 mixin Signal!("tabMoveRequested", int fromIndex, int toIndex); 75 mixin Signal!("tabMoveRequested", int /*fromIndex*/, int /*toIndex*/);
76 76
77 public: 77 public:
78 78
79 this(QWidget parent = null) 79 this(QWidget parent = null)
80 { 80 {
139 index = 10; 139 index = 10;
140 setCurrentIndex(index); 140 setCurrentIndex(index);
141 } 141 }
142 } 142 }
143 143
144 void cloneTab(); 144 void cloneTab()
145 { 145 {
146 if (QAction action = qobject_cast<QAction>(sender())) { 146 if (QAction action = qobject_cast<QAction>(sender())) {
147 int index = action.data().toInt(); 147 int index = action.data().toInt();
148 emit cloneTab(index); 148 emit cloneTab(index);
149 } 149 }
150 } 150 }
151 151
152 void closeTab(); 152 void closeTab()
153 { 153 {
154 if (QAction action = qobject_cast<QAction>(sender())) { 154 if (QAction action = qobject_cast<QAction>(sender())) {
155 int index = action.data().toInt(); 155 int index = action.data().toInt();
156 emit closeTab(index); 156 emit closeTab(index);
157 } 157 }
199 } 199 }
200 menu.addAction(tr("Reload All Tabs"), this, SIGNAL(reloadAllTabs())); 200 menu.addAction(tr("Reload All Tabs"), this, SIGNAL(reloadAllTabs()));
201 menu.exec(QCursor.pos()); 201 menu.exec(QCursor.pos());
202 } 202 }
203 203
204 private: 204 private:
205 QList<QShortcut> m_tabShortcuts; 205
206 friend class TabWidget; 206 QShortcut[] m_tabShortcuts;
207 207
208 QPoint m_dragStartPos; 208 QPoint m_dragStartPos;
209 int m_dragCurrentIndex; 209 int m_dragCurrentIndex;
210 } 210 }
211 211
233 m_root.triggered.connect(&this.rootTriggered); 233 m_root.triggered.connect(&this.rootTriggered);
234 root.destroyed.connect(&this.rootDestroyed); 234 root.destroyed.connect(&this.rootDestroyed);
235 root.setEnabled(false); 235 root.setEnabled(false);
236 } 236 }
237 237
238 QWebPage.WebAction webAction() const 238 QWebPage.WebAction webAction()
239 { 239 {
240 return m_webAction; 240 return m_webAction;
241 } 241 }
242 242
243 void addChild(QAction action) 243 void addChild(QAction action)
315 to proxy the actions. 315 to proxy the actions.
316 */ 316 */
317 class TabWidget : public QTabWidget 317 class TabWidget : public QTabWidget
318 { 318 {
319 // tab widget signals 319 // tab widget signals
320 mixin Singal!("loadPage", QString url); 320 mixin Singal!("loadPage", QString /*url*/);
321 mixin Singal!("tabsChanged"); 321 mixin Singal!("tabsChanged");
322 mixin Singal!("lastTabClosed"); 322 mixin Singal!("lastTabClosed");
323 323
324 // current tab signals 324 // current tab signals
325 mixin Singal!("setCurrentTitle", QString url); 325 mixin Singal!("setCurrentTitle", QString /*url*/);
326 mixin Singal!("showStatusBarMessage", QString message); 326 mixin Singal!("showStatusBarMessage", QString /*message*/);
327 mixin Singal!("linkHovered", QString link); 327 mixin Singal!("linkHovered", QString /*link*/);
328 mixin Singal!("loadProgress", int progress); 328 mixin Singal!("loadProgress", int /*progress*/);
329 mixin Singal!("geometryChangeRequested", QRect geometry); 329 mixin Singal!("geometryChangeRequested", QRect /*geometry*/);
330 mixin Singal!("menuBarVisibilityChangeRequested", bool visible); 330 mixin Singal!("menuBarVisibilityChangeRequested", bool /*visible*/);
331 mixin Singal!("statusBarVisibilityChangeRequested", bool visible); 331 mixin Singal!("statusBarVisibilityChangeRequested", bool /*visible*/);
332 mixin Singal!("toolBarVisibilityChangeRequested", bool visible); 332 mixin Singal!("toolBarVisibilityChangeRequested", bool /*visible*/);
333 mixin Singal!("printRequested", QWebFrame frame); 333 mixin Singal!("printRequested", QWebFrame /*frame*/);
334 334
335 public: 335 public:
336 336
337 this(QWidget parent = null) 337 this(QWidget parent = null)
338 { 338 {
416 if (!action) 416 if (!action)
417 return; 417 return;
418 m_actions.append(new WebActionMapper(action, webAction, this)); 418 m_actions.append(new WebActionMapper(action, webAction, this));
419 } 419 }
420 420
421 QAction newTabAction() const; 421 QAction newTabAction()
422 { 422 {
423 return m_newTabAction; 423 return m_newTabAction;
424 } 424 }
425 425
426 QAction closeTabAction() const; 426 QAction closeTabAction()
427 { 427 {
428 return m_closeTabAction; 428 return m_closeTabAction;
429 } 429 }
430 430
431 QAction recentlyClosedTabsAction() const; 431 QAction recentlyClosedTabsAction()
432 { 432 {
433 return m_recentlyClosedTabsAction; 433 return m_recentlyClosedTabsAction;
434 } 434 }
435 435
436 QAction nextTabAction() const 436 QAction nextTabAction()
437 { 437 {
438 return m_nextTabAction; 438 return m_nextTabAction;
439 } 439 }
440 440
441 QAction previousTabAction() const 441 QAction previousTabAction()
442 { 442 {
443 return m_previousTabAction; 443 return m_previousTabAction;
444 } 444 }
445 445
446 QWidget lineEditStack() const 446 QWidget lineEditStack()
447 { 447 {
448 return m_lineEdits; 448 return m_lineEdits;
449 } 449 }
450 450
451 QLineEdit currentLineEdit() const 451 QLineEdit currentLineEdit()
452 { 452 {
453 return lineEdit(m_lineEdits.currentIndex()); 453 return lineEdit(m_lineEdits.currentIndex());
454 } 454 }
455 455
456 WebView currentWebView() const 456 WebView currentWebView()
457 { 457 {
458 return webView(currentIndex()); 458 return webView(currentIndex());
459 } 459 }
460 460
461 WebView webView(int index) const 461 WebView webView(int index)
462 { 462 {
463 QWidget widget = this.widget(index); 463 QWidget widget = this.widget(index);
464 if (WebView webView = qobject_cast<WebView>(widget)) { 464 if (WebView webView = cast(WebView) widget) {
465 return webView; 465 return webView;
466 } else { 466 } else {
467 // optimization to delay creating the first webview 467 // optimization to delay creating the first webview
468 if (count() == 1) { 468 if (count() == 1) {
469 TabWidget that = const_cast<TabWidget>(this); 469 TabWidget that = const_cast<TabWidget>(this);
475 } 475 }
476 } 476 }
477 return 0; 477 return 0;
478 } 478 }
479 479
480 QLineEdit lineEdit(int index) const 480 QLineEdit lineEdit(int index)
481 { 481 {
482 UrlLineEdit urlLineEdit = qobject_cast<UrlLineEdit>(m_lineEdits.widget(index)); 482 UrlLineEdit urlLineEdit = qobject_cast<UrlLineEdit>(m_lineEdits.widget(index));
483 if (urlLineEdit) 483 if (urlLineEdit)
484 return urlLineEdit.lineEdit(); 484 return urlLineEdit.lineEdit();
485 return 0; 485 return 0;
486 } 486 }
487 487
488 int webViewIndex(WebView webView) const 488 int webViewIndex(WebView webView)
489 { 489 {
490 int index = indexOf(webView); 490 int index = indexOf(webView);
491 return index; 491 return index;
492 } 492 }
493 493
494 494
495 static const qint32 TabWidgetMagic = 0xaa; 495 static const qint32 TabWidgetMagic = 0xaa;
496 496
497 QByteArray saveState() const; 497 QByteArray saveState()
498 { 498 {
499 int version = 1; 499 int version = 1;
500 QByteArray data; 500 QByteArray data;
501 QDataStream stream(data, QIODevice.WriteOnly); 501 QDataStream stream(data, QIODevice.WriteOnly);
502 502
503 stream << qint32(TabWidgetMagic); 503 stream << qint32(TabWidgetMagic);
504 stream << qint32(version); 504 stream << qint32(version);
505 505
506 QStringList tabs; 506 QStringList tabs;
507 for (int i = 0; i < count(); ++i) { 507 for (int i = 0; i < count(); ++i) {
508 if (WebView tab = qobject_cast<WebView>(widget(i))) { 508 if (WebView tab = cast(WebView) widget(i))) {
509 tabs.append(tab.url().toString()); 509 tabs.append(tab.url().toString());
510 } else { 510 } else {
511 tabs.append(QString.null); 511 tabs.append(QString.null);
512 } 512 }
513 } 513 }
547 return true; 547 return true;
548 } 548 }
549 549
550 protected: 550 protected:
551 551
552 void mouseDoubleClickEvent(QMouseEvent event); 552 void mouseDoubleClickEvent(QMouseEvent event)
553 { 553 {
554 if (!childAt(event.pos()) 554 if (!childAt(event.pos())
555 // Remove the line below when QTabWidget does not have a one pixel frame 555 // Remove the line below when QTabWidget does not have a one pixel frame
556 && event.pos().y() < (tabBar().y() + tabBar().height())) { 556 && event.pos().y() < (tabBar().y() + tabBar().height())) {
557 newTab(); 557 newTab();
667 WebView tab = newTab(false); 667 WebView tab = newTab(false);
668 tab.setUrl(webView(index).url()); 668 tab.setUrl(webView(index).url());
669 } 669 }
670 670
671 // When index is -1 index chooses the current tab 671 // When index is -1 index chooses the current tab
672 void closeTab(int index = -1); 672 void closeTab(int index = -1)
673 { 673 {
674 if (index < 0) 674 if (index < 0)
675 index = currentIndex(); 675 index = currentIndex();
676 if (index < 0 || index >= count()) 676 if (index < 0 || index >= count())
677 return; 677 return;
720 for (int i = index - 1; i >= 0; --i) 720 for (int i = index - 1; i >= 0; --i)
721 closeTab(i); 721 closeTab(i);
722 } 722 }
723 723
724 // When index is -1 index chooses the current tab 724 // When index is -1 index chooses the current tab
725 void reloadTab(int index = -1); 725 void reloadTab(int index = -1)
726 { 726 {
727 if (index < 0) 727 if (index < 0)
728 index = currentIndex(); 728 index = currentIndex();
729 if (index < 0 || index >= count()) 729 if (index < 0 || index >= count())
730 return; 730 return;
731 731
732 QWidget widget = this.widget(index); 732 QWidget widget = this.widget(index);
733 if (WebView tab = qobject_cast<WebView>(widget)) 733 if (WebView tab = cast(WebView) widget)
734 tab.reload(); 734 tab.reload();
735 } 735 }
736 736
737 void reloadAllTabs(); 737 void reloadAllTabs()
738 { 738 {
739 for (int i = 0; i < count(); ++i) { 739 for (int i = 0; i < count(); ++i) {
740 QWidget tabWidget = widget(i); 740 QWidget tabWidget = widget(i);
741 if (WebView tab = qobject_cast<WebView>(tabWidget)) { 741 if (WebView tab = cast(WebView) tabWidget) {
742 tab.reload(); 742 tab.reload();
743 } 743 }
744 } 744 }
745 } 745 }
746 746
814 loadUrlInCurrentTab(url); 814 loadUrlInCurrentTab(url);
815 } 815 }
816 816
817 void webViewLoadStarted() 817 void webViewLoadStarted()
818 { 818 {
819 WebView webView = qobject_cast<WebView>(sender()); 819 WebView webView = cast(WebView) sender();
820 int index = webViewIndex(webView); 820 int index = webViewIndex(webView);
821 if (-1 != index) { 821 if (-1 != index) {
822 QIcon icon(QLatin1String(":loading.gif")); 822 QIcon icon(QLatin1String(":loading.gif"));
823 setTabIcon(index, icon); 823 setTabIcon(index, icon);
824 } 824 }
825 } 825 }
826 826
827 void webViewIconChanged() 827 void webViewIconChanged()
828 { 828 {
829 WebView webView = qobject_cast<WebView>(sender()); 829 WebView webView = cast(WebView) sender();
830 int index = webViewIndex(webView); 830 int index = webViewIndex(webView);
831 if (-1 != index) { 831 if (-1 != index) {
832 QIcon icon = BrowserApplication.instance().icon(webView.url()); 832 QIcon icon = BrowserApplication.instance().icon(webView.url());
833 setTabIcon(index, icon); 833 setTabIcon(index, icon);
834 } 834 }
835 } 835 }
836 836
837 void webViewTitleChanged(QString title) 837 void webViewTitleChanged(QString title)
838 { 838 {
839 WebView webView = qobject_cast<WebView>(sender()); 839 WebView webView = cast(WebView) sender();
840 int index = webViewIndex(webView); 840 int index = webViewIndex(webView);
841 if (-1 != index) { 841 if (-1 != index) {
842 setTabText(index, title); 842 setTabText(index, title);
843 } 843 }
844 if (currentIndex() == index) 844 if (currentIndex() == index)
846 BrowserApplication.historyManager().updateHistoryItem(webView.url(), title); 846 BrowserApplication.historyManager().updateHistoryItem(webView.url(), title);
847 } 847 }
848 848
849 void webViewUrlChanged(QUrl url) 849 void webViewUrlChanged(QUrl url)
850 { 850 {
851 WebView webView = qobject_cast<WebView>(sender()); 851 WebView webView = cast(WebView) sender();
852 int index = webViewIndex(webView); 852 int index = webViewIndex(webView);
853 if (-1 != index) { 853 if (-1 != index) {
854 m_tabBar.setTabData(index, url); 854 m_tabBar.setTabData(index, url);
855 } 855 }
856 emit tabsChanged(); 856 emit tabsChanged();
857 } 857 }
858 858
859 void lineEditReturnPressed() 859 void lineEditReturnPressed()
860 { 860 {
861 if (QLineEdit lineEdit = qobject_cast<QLineEdit*>(sender())) { 861 if (QLineEdit lineEdit = cast(QLineEdit) sender()) {
862 emit loadPage(lineEdit.text()); 862 emit loadPage(lineEdit.text());
863 if (m_lineEdits.currentWidget() == lineEdit) 863 if (m_lineEdits.currentWidget() == lineEdit)
864 currentWebView().setFocus(); 864 currentWebView().setFocus();
865 } 865 }
866 } 866 }
867 867
868 void windowCloseRequested() 868 void windowCloseRequested()
869 { 869 {
870 WebPage webPage = qobject_cast<WebPage>(sender()); 870 WebPage webPage = cast(WebPage) sender();
871 WebView webView = qobject_cast<WebView>(webPage.view()); 871 WebView webView = cast(WebView) webPage.view();
872 int index = webViewIndex(webView); 872 int index = webViewIndex(webView);
873 if (index >= 0) { 873 if (index >= 0) {
874 if (count() == 1) 874 if (count() == 1)
875 webView.webPage().mainWindow().close(); 875 webView.webPage().mainWindow().close();
876 else 876 else
893 QAction m_nextTabAction; 893 QAction m_nextTabAction;
894 QAction m_previousTabAction; 894 QAction m_previousTabAction;
895 895
896 QMenu m_recentlyClosedTabsMenu; 896 QMenu m_recentlyClosedTabsMenu;
897 static const int m_recentlyClosedTabsSize = 10; 897 static const int m_recentlyClosedTabsSize = 10;
898 QList<QUrl> m_recentlyClosedTabs; 898 QUrl[] m_recentlyClosedTabs;
899 QList<WebActionMapper> m_actions; 899 WebActionMapper[] m_actions;
900 900
901 QCompleter m_lineEditCompleter; 901 QCompleter m_lineEditCompleter;
902 QStackedWidget m_lineEdits; 902 QStackedWidget m_lineEdits;
903 TabBar m_tabBar; 903 TabBar m_tabBar;
904 } 904 }