From 4eedf60d76a047f63b0991eee0b623e9be854c76 Mon Sep 17 00:00:00 2001 From: Aqua-sama <aqua@iserlohn-fortress.net> Date: Thu, 13 Dec 2018 12:49:28 +0100 Subject: MainWindow: rework menu bar Split off menu bar into its own class out of MainWindow Menu bar now has a 'Find in menus' function --- src/subwindow/subwindow.cpp | 38 ++++++++++++---------- src/subwindow/subwindow.h | 3 ++ src/subwindow/tabwidget.cpp | 78 +++++++++++++++++++-------------------------- src/subwindow/tabwidget.h | 5 +-- 4 files changed, 60 insertions(+), 64 deletions(-) (limited to 'src/subwindow') diff --git a/src/subwindow/subwindow.cpp b/src/subwindow/subwindow.cpp index 0844f1f..4beba49 100644 --- a/src/subwindow/subwindow.cpp +++ b/src/subwindow/subwindow.cpp @@ -61,18 +61,6 @@ SubWindow::SubWindow(const Configuration *config, QWidget *parent, Qt::WindowFla menu->insertSeparator(firstAction); } - // new tab button - auto *newTab_button = new QToolButton(this); - newTab_button->setIcon(style()->standardIcon(QStyle::SP_FileIcon)); - newTab_button->setToolTip(tr("Add tab")); - newTab_button->setShortcut(QKeySequence(config->value<QString>("subwindow.shortcuts.new").value())); - connect(newTab_button, &QToolButton::clicked, this, [=]() { - auto index = addTab(WebProfile::defaultProfile()->newtab()); - tabWidget->setCurrentIndex(index); - }); - newTab_button->setMenu(tabWidget->createTabMenu(newTab_button)); - tabWidget->setCornerWidget(newTab_button, Qt::TopRightCorner); - // general actions auto *closeTab_shortcut = new QShortcut(QKeySequence(config->value<QString>("subwindow.shortcuts.close").value()), this); connect(closeTab_shortcut, &QShortcut::activated, this, [=]() { @@ -159,8 +147,10 @@ int SubWindow::tabCount() const void SubWindow::setProfile(WebProfile *profile) { - if(profile == nullptr) + if(profile == nullptr) { + setProfile(WebProfile::defaultProfile()); return; + } this->m_profile = profile; for(int i = 0; i < tabWidget->count(); ++i) { @@ -178,13 +168,29 @@ int SubWindow::addTab(const QUrl &url, WebProfile *profile) { Q_CHECK_PTR(m_profile); - auto *view = new WebView((profile == nullptr) ? m_profile : profile, this); - if(!url.isEmpty()) + auto *_profile = (profile == nullptr) ? m_profile : profile; + + auto *view = new WebView(_profile, this); + if(url.isEmpty()) + view->load(_profile->newtab()); + else view->load(url); + return tabWidget->addTab(view); } void SubWindow::setCurrentTab(int index) { - tabWidget->setCurrentIndex(index); + if(index > 0) + tabWidget->setCurrentIndex(index); +} + +int SubWindow::restoreLastTab() +{ + return tabWidget->restoreLastTab(); +} + +void SubWindow::restoreTabMenu(QMenu *menu) +{ + tabWidget->restoreTabMenu(menu); } diff --git a/src/subwindow/subwindow.h b/src/subwindow/subwindow.h index 3d70c98..1fc0097 100644 --- a/src/subwindow/subwindow.h +++ b/src/subwindow/subwindow.h @@ -40,6 +40,9 @@ public slots: int addTab(const QUrl &url = QUrl(), WebProfile *profile = nullptr); void setCurrentTab(int index); + int restoreLastTab(); + void restoreTabMenu(QMenu *menu); + private: WebProfile *m_profile; TabWidget *tabWidget; diff --git a/src/subwindow/tabwidget.cpp b/src/subwindow/tabwidget.cpp index 942a364..c635aee 100644 --- a/src/subwindow/tabwidget.cpp +++ b/src/subwindow/tabwidget.cpp @@ -31,7 +31,7 @@ TabWidget::TabWidget(QWidget *parent) setStyleSheet("QTabBar::tab { width: 200px; }"); setTabsClosable(true); - //setTabBarAutoHide(true); + setTabBarAutoHide(true); setElideMode(Qt::ElideRight); setMovable(true); @@ -75,51 +75,6 @@ TabWidget::~TabWidget() } } -QMenu *TabWidget::createTabMenu(QWidget *parent) -{ - auto *menu = new QMenu(parent); - - auto *reopenTabAction = menu->addAction(tr("Reopen last tab")); - connect(reopenTabAction, &QAction::triggered, this, [this]() { - if(!m_closedTabs.isEmpty()) { - TabInformation tab = m_closedTabs.takeLast(); - addTab(createViewFromInfo(tab, this)); - } - }); - - auto *tabHistoryMenu = menu->addMenu(tr("Tab History")); - connect(tabHistoryMenu, &QMenu::aboutToShow, this, [this, tabHistoryMenu]() { - tabHistoryMenu->clear(); - for(int i = 0; i < m_closedTabs.count(); ++i) { - auto *openAction = tabHistoryMenu->addAction(m_closedTabs.at(i).title); - - connect(openAction, &QAction::triggered, this, [this, i]() { - TabInformation tab = m_closedTabs.takeAt(i); - addTab(createViewFromInfo(tab, this)); - }); - } - - tabHistoryMenu->addSeparator(); - - auto *clearTabHistory = tabHistoryMenu->addAction(tr("Clear")); - connect(clearTabHistory, &QAction::triggered, this, [this]() { - m_closedTabs.clear(); - }); - }); - - connect(menu, &QMenu::aboutToShow, this, [this, reopenTabAction, tabHistoryMenu]() { - if(m_closedTabs.isEmpty()) { - reopenTabAction->setEnabled(false); - tabHistoryMenu->setEnabled(false); - } else { - reopenTabAction->setEnabled(true); - tabHistoryMenu->setEnabled(true); - } - }); - - return menu; -} - int TabWidget::addTab(WebView *view) { Q_ASSERT_X(view != nullptr, "TabWidget::addTab", "Tried to add null view"); @@ -165,6 +120,37 @@ void TabWidget::deleteTab(int index) parentWidget()->close(); } +int TabWidget::restoreLastTab() +{ + if(!m_closedTabs.isEmpty()) { + TabInformation tab = m_closedTabs.takeLast(); + return addTab(createViewFromInfo(tab, this)); + } + return -1; +} + +void TabWidget::restoreTabMenu(QMenu *menu) +{ + if(m_closedTabs.isEmpty()) + return; + + for(int i = 0; i < m_closedTabs.count(); ++i) { + auto *openAction = menu->addAction(m_closedTabs.at(i).title); + + connect(openAction, &QAction::triggered, this, [this, i]() { + TabInformation tab = m_closedTabs.takeAt(i); + addTab(createViewFromInfo(tab, this)); + }); + } + + menu->addSeparator(); + + auto *clearTabHistory = menu->addAction(tr("Clear")); + connect(clearTabHistory, &QAction::triggered, this, [this]() { + m_closedTabs.clear(); + }); +} + void TabWidget::contextMenuEvent(QContextMenuEvent *event) { // check if the context menu was called on a tab diff --git a/src/subwindow/tabwidget.h b/src/subwindow/tabwidget.h index 48690e8..ef1930d 100644 --- a/src/subwindow/tabwidget.h +++ b/src/subwindow/tabwidget.h @@ -34,12 +34,13 @@ public: explicit TabWidget(QWidget *parent = nullptr); ~TabWidget() override; - QMenu *createTabMenu(QWidget *parent = nullptr); - public slots: int addTab(WebView *view); void deleteTab(int index); + int restoreLastTab(); + void restoreTabMenu(QMenu *menu); + protected: void contextMenuEvent(QContextMenuEvent *event) override; void mousePressEvent(QMouseEvent *event) override; -- cgit v1.2.1