diff options
author | aqua <aqua@iserlohn-fortress.net> | 2022-09-18 09:57:29 +0300 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2022-09-18 09:57:29 +0300 |
commit | 1b1e327cb73491c17972a33a07c3c9beafe7f938 (patch) | |
tree | 57fa4bc121fc2e172db42288fec7cdd7ff95399c /src/panels | |
parent | Add Bookmarks toolbar (diff) | |
download | rekonq-1b1e327cb73491c17972a33a07c3c9beafe7f938.tar.xz |
Add Navigation and Tab toolbars
Diffstat (limited to 'src/panels')
-rw-r--r-- | src/panels/bookmarksmenu.cpp | 41 | ||||
-rw-r--r-- | src/panels/bookmarksmenu.hpp | 39 | ||||
-rw-r--r-- | src/panels/bookmarkstoolbar.cpp | 389 | ||||
-rw-r--r-- | src/panels/bookmarkstoolbar.hpp | 58 | ||||
-rw-r--r-- | src/panels/navigationtoolbar.cpp | 49 | ||||
-rw-r--r-- | src/panels/navigationtoolbar.hpp | 36 | ||||
-rw-r--r-- | src/panels/tabtoolbar.cpp | 49 | ||||
-rw-r--r-- | src/panels/tabtoolbar.hpp | 33 |
8 files changed, 694 insertions, 0 deletions
diff --git a/src/panels/bookmarksmenu.cpp b/src/panels/bookmarksmenu.cpp new file mode 100644 index 00000000..94615ff4 --- /dev/null +++ b/src/panels/bookmarksmenu.cpp @@ -0,0 +1,41 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-2.0-or-later + * Copyright (C) 2008-2013 by Andrea Diamantini <adjam7 at gmail dot com> + * Copyright (C) 2010 by Yoann Laissus <yoann dot laissus at gmail dot com> + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#include "bookmarksmenu.hpp" +#include "bookmarks/bookmarkstreemodel.hpp" + +BookmarksMenu::BookmarksMenu(QWidget *parent) : QMenu(parent) +{ + connect(this, &QMenu::aboutToShow, this, &BookmarksMenu::refill); +} + +void BookmarksMenu::refill() +{ + Q_CHECK_PTR(model); + + for (auto *a : actions) { + removeAction(a); + delete a; + } + actions.clear(); + + actions.append(addSeparator()); + + const auto *root = model->item(); + Q_CHECK_PTR(root); + for (int i = 0; i < root->childCount(); ++i) { + const auto *child = root->child(i); + if (child->type() != BookmarksTreeItem::Bookmark) continue; + auto *action = addAction(child->data(BookmarksTreeItem::Title).toString(), this, [this, child]() { + emit loadUrl(child->data(BookmarksTreeItem::Href).toUrl(), rekonq::CurrentTab); + }); + actions.append(action); + } +} diff --git a/src/panels/bookmarksmenu.hpp b/src/panels/bookmarksmenu.hpp new file mode 100644 index 00000000..f162244e --- /dev/null +++ b/src/panels/bookmarksmenu.hpp @@ -0,0 +1,39 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-2.0-or-later + * Copyright (C) 2008-2013 by Andrea Diamantini <adjam7 at gmail dot com> + * Copyright (C) 2010 by Yoann Laissus <yoann dot laissus at gmail dot com> + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#pragma once + +#include "rekonq.hpp" +#include <QMenu> + +class BookmarksTreeModel; +class BookmarksMenu : public QMenu { + Q_OBJECT + +public: + explicit BookmarksMenu(QWidget *parent = nullptr); + ~BookmarksMenu() override = default; + + void setModel(const BookmarksTreeModel *ptr) + { + Q_CHECK_PTR(ptr); + model = ptr; + } + +signals: + void loadUrl(const QUrl &url, rekonq::OpenType type); + +private slots: + void refill(); + +private: + const BookmarksTreeModel *model = nullptr; + QList<QAction *> actions; +}; diff --git a/src/panels/bookmarkstoolbar.cpp b/src/panels/bookmarkstoolbar.cpp new file mode 100644 index 00000000..7c3517ec --- /dev/null +++ b/src/panels/bookmarkstoolbar.cpp @@ -0,0 +1,389 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-2.0-or-later + * Copyright (C) 2008-2013 by Andrea Diamantini <adjam7 at gmail dot com> + * Copyright (C) 2010 by Yoann Laissus <yoann dot laissus at gmail dot com> + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#include "bookmarkstoolbar.hpp" +#include "bookmarks/bookmarkstreemodel.hpp" +#include <QShowEvent> + +BookmarkToolBar::BookmarkToolBar(QWidget *parent) : QToolBar(parent) +{ + /* +setContextMenuPolicy(Qt::CustomContextMenu); + +connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenu(QPoint))); +connect(BookmarkManager::self()->manager(), SIGNAL(changed(QString, QString)), this, SLOT(hideMenu())); + +setAcceptDrops(true); +installEventFilter(this); +setShortcutEnabled(false); + +if (isVisible()) { + BookmarkManager::self()->fillBookmarkBar(this); + m_filled = true; +} +*/ +} + +void BookmarkToolBar::showEvent(QShowEvent *event) +{ + Q_CHECK_PTR(model); + + clear(); + + const auto *root = model->item(); + Q_CHECK_PTR(root); + for (int i = 0; i < root->childCount(); ++i) { + const auto *child = root->child(i); + if (child->type() != BookmarksTreeItem::Bookmark) continue; + addAction(child->action(this, rekonq::CurrentTab)); + } + + event->accept(); +} + +/* +void BookmarkToolBar::contextMenu(const QPoint &point) +{ + KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface *>(actionAt(point)); + KBookmark bookmark = BookmarkManager::self()->manager()->toolbar(); + bool nullAction = true; + if (action) { + bookmark = action->bookmark(); + nullAction = false; + } + + BookmarksContextMenu menu(bookmark, BookmarkManager::self()->manager(), BookmarkManager::self()->owner(), nullAction); + + menu.exec(mapToGlobal(point)); +} + +void BookmarkToolBar::menuDisplayed() +{ + qApp->installEventFilter(this); + m_currentMenu = qobject_cast<KMenu *>(sender()); +} + +void BookmarkToolBar::menuHidden() +{ + qApp->removeEventFilter(this); + m_currentMenu = 0; +} + +void BookmarkToolBar::hideMenu() +{ + if (m_currentMenu) m_currentMenu->hide(); +} + +bool BookmarkToolBar::eventFilter(QObject *watched, QEvent *event) +{ + if (m_currentMenu && m_currentMenu->isVisible() && + !m_currentMenu->rect().contains(m_currentMenu->mapFromGlobal(QCursor::pos()))) { + // To switch root folders as in a menubar + + KBookmarkActionMenu *act = dynamic_cast<KBookmarkActionMenu *>(actionAt(mapFromGlobal(QCursor::pos()))); + + if (event->type() == QEvent::MouseMove && act && act->menu() != m_currentMenu) { + m_currentMenu->hide(); + QPoint pos = mapToGlobal(widgetForAction(act)->pos()); + act->menu()->popup(QPoint(pos.x(), pos.y() + widgetForAction(act)->height())); + } + else if (event->type() == QEvent::MouseButtonPress && act) { + m_currentMenu->hide(); + } + + return QObject::eventFilter(watched, event); + } + + switch (event->type()) { + case QEvent::Show: { + if (!m_filled) { + BookmarkManager::self()->fillBookmarkBar(this); + m_filled = true; + } + } break; + + case QEvent::ActionRemoved: { + QActionEvent *actionEvent = static_cast<QActionEvent *>(event); + if (actionEvent && actionEvent->action() != m_dropAction) { + QWidget *widget = widgetForAction(actionEvent->action()); + if (widget) { widget->removeEventFilter(this); } + } + } break; + + case QEvent::ParentChange: { + QActionEvent *actionEvent = static_cast<QActionEvent *>(event); + if (actionEvent && actionEvent->action() != m_dropAction) { + QWidget *widget = widgetForAction(actionEvent->action()); + if (widget) { widget->removeEventFilter(this); } + } + } break; + + case QEvent::DragEnter: { + QDragEnterEvent *dragEvent = static_cast<QDragEnterEvent *>(event); + if (dragEvent->mimeData()->hasFormat(BookmarkManager::bookmark_mime_type()) || + dragEvent->mimeData()->hasFormat("text/uri-list") || dragEvent->mimeData()->hasFormat("text/plain")) { + QFrame *dropIndicatorWidget = new QFrame(this); + dropIndicatorWidget->setFrameShape(QFrame::VLine); + m_dropAction = insertWidget(actionAt(dragEvent->pos()), dropIndicatorWidget); + + dragEvent->accept(); + } + } break; + + case QEvent::DragLeave: { + QDragLeaveEvent *dragEvent = static_cast<QDragLeaveEvent *>(event); + + if (m_checkedAction) { + m_checkedAction->setCheckable(false); + m_checkedAction->setChecked(false); + } + + delete m_dropAction; + m_dropAction = 0; + dragEvent->accept(); + } break; + + case QEvent::DragMove: { + QDragMoveEvent *dragEvent = static_cast<QDragMoveEvent *>(event); + if (dragEvent->mimeData()->hasFormat(BookmarkManager::bookmark_mime_type()) || + dragEvent->mimeData()->hasFormat("text/uri-list") || dragEvent->mimeData()->hasFormat("text/plain")) { + QAction *overAction = actionAt(dragEvent->pos()); + KBookmarkActionInterface *overActionBK = dynamic_cast<KBookmarkActionInterface *>(overAction); + QWidget *widgetAction = widgetForAction(overAction); + + if (overAction != m_dropAction && overActionBK && widgetAction && m_dropAction) { + removeAction(m_dropAction); + if (m_checkedAction) { + m_checkedAction->setCheckable(false); + m_checkedAction->setChecked(false); + } + + if (!overActionBK->bookmark().isGroup()) { + if ((dragEvent->pos().x() - widgetAction->pos().x()) > (widgetAction->width() / 2)) { + if (actions().count() > actions().indexOf(overAction) + 1) { + insertAction(actions().at(actions().indexOf(overAction) + 1), m_dropAction); + } + else { + addAction(m_dropAction); + } + } + else { + insertAction(overAction, m_dropAction); + } + } + else { + if ((dragEvent->pos().x() - widgetAction->pos().x()) >= (widgetAction->width() * 0.75)) { + if (actions().count() > actions().indexOf(overAction) + 1) { + insertAction(actions().at(actions().indexOf(overAction) + 1), m_dropAction); + } + else { + addAction(m_dropAction); + } + } + else if ((dragEvent->pos().x() - widgetAction->pos().x()) <= (widgetAction->width() * 0.25)) { + insertAction(overAction, m_dropAction); + } + else { + overAction->setCheckable(true); + overAction->setChecked(true); + m_checkedAction = overAction; + } + } + + dragEvent->accept(); + } + } + } break; + + case QEvent::Drop: { + QDropEvent *dropEvent = static_cast<QDropEvent *>(event); + KBookmark bookmark; + KBookmarkGroup root = BookmarkManager::self()->manager()->toolbar(); + + if (m_checkedAction) { + m_checkedAction->setCheckable(false); + m_checkedAction->setChecked(false); + } + + if (dropEvent->mimeData()->hasFormat(BookmarkManager::bookmark_mime_type())) { + QByteArray addresses = dropEvent->mimeData()->data(BookmarkManager::bookmark_mime_type()); + bookmark = BookmarkManager::self()->findByAddress(QString::fromLatin1(addresses.data())); + if (bookmark.isNull()) return false; + } + else if (dropEvent->mimeData()->hasFormat("text/uri-list")) { + // DROP is URL + QString url = dropEvent->mimeData()->urls().at(0).toString(); + WebWindow *w = qobject_cast<WebWindow *>(parent()); + QString title = url.contains(w->url().url()) ? w->title() : url; + bookmark = root.addBookmark(title, url); + } + else if (dropEvent->mimeData()->hasFormat("text/plain")) { + // DROP is TEXT + QString url = dropEvent->mimeData()->text(); + KUrl u(url); + if (u.isValid()) { + WebWindow *w = qobject_cast<WebWindow *>(parent()); + QString title = url.contains(w->url().url()) ? w->title() : url; + bookmark = root.addBookmark(title, url); + } + } + else { + return false; + } + + QAction *destAction = actionAt(dropEvent->pos()); + if (destAction && destAction == m_dropAction) { + if (actions().indexOf(m_dropAction) > 0) { destAction = actions().at(actions().indexOf(m_dropAction) - 1); } + else { + destAction = actions().at(1); + } + } + + if (destAction) { + KBookmarkActionInterface *destBookmarkAction = dynamic_cast<KBookmarkActionInterface *>(destAction); + QWidget *widgetAction = widgetForAction(destAction); + + if (destBookmarkAction && !destBookmarkAction->bookmark().isNull() && widgetAction && + bookmark.address() != destBookmarkAction->bookmark().address()) { + KBookmark destBookmark = destBookmarkAction->bookmark(); + + if (!destBookmark.isGroup()) { + if ((dropEvent->pos().x() - widgetAction->pos().x()) >= (widgetAction->width() / 2)) { + root.moveBookmark(bookmark, destBookmark); + } + else { + root.moveBookmark(bookmark, destBookmark.parentGroup().previous(destBookmark)); + } + } + else { + if ((dropEvent->pos().x() - widgetAction->pos().x()) >= (widgetAction->width() * 0.75)) { + root.moveBookmark(bookmark, destBookmark); + } + else if ((dropEvent->pos().x() - widgetAction->pos().x()) <= (widgetAction->width() * 0.25)) { + root.moveBookmark(bookmark, destBookmark.parentGroup().previous(destBookmark)); + } + else { + destBookmark.toGroup().addBookmark(bookmark); + } + } + + BookmarkManager::self()->emitChanged(); + } + } + else { + root.deleteBookmark(bookmark); + bookmark = root.addBookmark(bookmark); + if (dropEvent->pos().x() < widgetForAction(actions().first())->pos().x()) { + root.moveBookmark(bookmark, KBookmark()); + } + + BookmarkManager::self()->emitChanged(); + } + dropEvent->accept(); + } break; + + default: + break; + } + + QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); + + // These events need to be handled only for Bookmark actions and not the bar + if (watched != this && mouseEvent) { + switch (event->type()) { + case QEvent::MouseButtonPress: // drag handling + { + QPoint pos = mapFromGlobal(QCursor::pos()); + KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface *>(actionAt(pos)); + + if (action && mouseEvent->button() != Qt::MidButton) { + m_dragAction = actionAt(pos); + m_startDragPos = pos; + + // The menu is displayed only when the mouse button is released + if (action->bookmark().isGroup()) return true; + } + } break; + + case QEvent::MouseMove: { + int distance = (mapFromGlobal(QCursor::pos()) - m_startDragPos).manhattanLength(); + if (!m_currentMenu && distance >= QApplication::startDragDistance()) { startDrag(); } + } break; + + case QEvent::MouseButtonRelease: { + QPoint destPos = mapFromGlobal(QCursor::pos()); + int distance = (destPos - m_startDragPos).manhattanLength(); + KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface *>(actionAt(destPos)); + + if (action) { + if (action->bookmark().isGroup()) { + if (mouseEvent->button() == Qt::MidButton) { + BookmarkManager::self()->owner()->loadBookmarkFolder(action->bookmark()); + } + else if (distance < QApplication::startDragDistance()) { + KBookmarkActionMenu *menu = dynamic_cast<KBookmarkActionMenu *>(actionAt(m_startDragPos)); + QPoint actionPos = mapToGlobal(widgetForAction(menu)->pos()); + menu->menu()->popup(QPoint(actionPos.x(), actionPos.y() + widgetForAction(menu)->height())); + } + } + else { + if (!action->bookmark().isNull() && !action->bookmark().isSeparator()) { + if (mouseEvent->button() == Qt::MidButton) { + BookmarkManager::self()->owner()->loadBookmarkInNewTab(action->bookmark()); + } + } + } + } + } break; + + default: + break; + } + } + + return QObject::eventFilter(watched, event); +} + +void BookmarkToolBar::startDrag() +{ + KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface *>(m_dragAction); + if (action) { + QMimeData *mimeData = new QMimeData; + KBookmark bookmark = action->bookmark(); + + QByteArray address = bookmark.address().toLatin1(); + mimeData->setData(BookmarkManager::bookmark_mime_type(), address); + bookmark.populateMimeData(mimeData); + + QDrag *drag = new QDrag(this); + drag->setMimeData(mimeData); + + if (bookmark.isGroup()) { drag->setPixmap(KIcon(bookmark.icon()).pixmap(24, 24)); } + else { + drag->setPixmap(IconManager::self()->iconForUrl(action->bookmark().url()).pixmap(24, 24)); + } + + drag->start(Qt::MoveAction); + connect(drag, SIGNAL(destroyed()), this, SLOT(dragDestroyed())); + } +} + +void BookmarkToolBar::dragDestroyed() +{ + // A workaround to get rid of the checked state of the dragged action + if (m_dragAction) { + m_dragAction->setVisible(false); + m_dragAction->setVisible(true); + m_dragAction = 0; + } + delete m_dropAction; + m_dropAction = 0; +} +*/ diff --git a/src/panels/bookmarkstoolbar.hpp b/src/panels/bookmarkstoolbar.hpp new file mode 100644 index 00000000..6aaa750a --- /dev/null +++ b/src/panels/bookmarkstoolbar.hpp @@ -0,0 +1,58 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-2.0-or-later + * Copyright (C) 2008-2013 by Andrea Diamantini <adjam7 at gmail dot com> + * Copyright (C) 2010 by Yoann Laissus <yoann dot laissus at gmail dot com> + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#pragma once + +#include "rekonq.hpp" +#include <QToolBar> + +class BookmarksTreeModel; + +/** + * This class manage the bookmark toolbar. + * Some events from the toolbar are handled to allow the drag and drop + */ +class BookmarkToolBar : public QToolBar { + Q_OBJECT + +public: + explicit BookmarkToolBar(QWidget *parent = nullptr); + ~BookmarkToolBar() override = default; + + void setModel(BookmarksTreeModel *ptr) + { + Q_CHECK_PTR(ptr); + model = ptr; + } + +signals: + void loadUrl(const QUrl &url, rekonq::OpenType type); + +protected: + void showEvent(QShowEvent *event) override; + // bool eventFilter(QObject *watched, QEvent *event); + +private slots: + // void contextMenu(const QPoint &); + // void menuDisplayed(); + // void menuHidden(); + // void hideMenu(); + // void dragDestroyed(); + +private: + BookmarksTreeModel *model = nullptr; + void startDrag(); + + QPoint m_startDragPos; + QAction *m_dragAction = nullptr; + QAction *m_dropAction = nullptr; + QAction *m_checkedAction = nullptr; + bool m_filled = false; +}; diff --git a/src/panels/navigationtoolbar.cpp b/src/panels/navigationtoolbar.cpp new file mode 100644 index 00000000..ae55ac02 --- /dev/null +++ b/src/panels/navigationtoolbar.cpp @@ -0,0 +1,49 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#include "navigationtoolbar.hpp" + +NavigationToolBar::NavigationToolBar(QWidget *parent) : QToolBar(parent), urlBar(new UrlBar(this)) +{ + auto *backAction = addAction(QIcon::fromTheme("go-previous"), "Back", this, [this]() { m_view->back(); }); + backAction->setObjectName("actionBack"); + auto *forwardAction = addAction(QIcon::fromTheme("go-next"), "Forward", this, [this]() { m_view->forward(); }); + forwardAction->setObjectName("actionForward"); + auto *refreshAction = addAction(QIcon::fromTheme("view-refresh"), "Refresh", this, [this]() { m_view->refresh(); }); + refreshAction->setObjectName("actionRefresh"); + addAction(QIcon::fromTheme("go-home"), "Home", this, + [this]() { emit loadUrl(rekonq::HomePage, rekonq::CurrentTab); }); + // TODO homeAction->setObjectName("actionHome"); + + addWidget(urlBar); + + auto *actionFocusUrlBar = new QAction(parent); + actionFocusUrlBar->setShortcut({"F6"}); + connect(actionFocusUrlBar, &QAction::triggered, urlBar, [this]() { + urlBar->setFocus(); + urlBar->selectAll(); + }); + parent->addAction(actionFocusUrlBar); +} + +void NavigationToolBar::setCurrentView(RekonqView *view) +{ + Q_CHECK_PTR(view); + m_view = view; + + disconnect(urlChanged_conn); + disconnect(loadStarted_conn); + disconnect(loadProgress_conn); + disconnect(loadFinished_conn); + + urlChanged_conn = connect(view, &RekonqView::urlChanged, urlBar, &UrlBar::setUrl); + loadStarted_conn = connect(view, &RekonqView::loadStarted, urlBar, &UrlBar::loadStarted); + loadProgress_conn = connect(view, &RekonqView::loadProgress, urlBar, &UrlBar::loadProgress); + loadFinished_conn = connect(view, &RekonqView::loadFinished, urlBar, &UrlBar::loadFinished); + + urlBar->setCurrentView(view); +} diff --git a/src/panels/navigationtoolbar.hpp b/src/panels/navigationtoolbar.hpp new file mode 100644 index 00000000..eaf5b145 --- /dev/null +++ b/src/panels/navigationtoolbar.hpp @@ -0,0 +1,36 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#pragma once + +#include "rekonq.hpp" +#include "urlbar/urlbar.hpp" +#include <QToolBar> + +class RekonqView; +class NavigationToolBar : public QToolBar { + Q_OBJECT + +public: + explicit NavigationToolBar(QWidget *parent = nullptr); + ~NavigationToolBar() override = default; + +signals: + void loadUrl(rekonq::DefaultUrl url, rekonq::OpenType type = rekonq::CurrentTab); + +public slots: + void setCurrentView(RekonqView *view); + +private: + RekonqView *m_view; + UrlBar *urlBar; + + QMetaObject::Connection urlChanged_conn; + QMetaObject::Connection loadStarted_conn; + QMetaObject::Connection loadProgress_conn; + QMetaObject::Connection loadFinished_conn; +}; diff --git a/src/panels/tabtoolbar.cpp b/src/panels/tabtoolbar.cpp new file mode 100644 index 00000000..be3f1384 --- /dev/null +++ b/src/panels/tabtoolbar.cpp @@ -0,0 +1,49 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#include "tabtoolbar.hpp" + +TabToolBar::TabToolBar(QWidget *parent) : QToolBar(parent), tabs(new TabBar(this)) +{ + addWidget(tabs); + + auto *spacer = new QWidget(this); + spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + addWidget(spacer); + + auto *actionNewTab = parent->findChild<QAction *>("actionNewTab"); + Q_CHECK_PTR(actionNewTab); + addAction(QIcon::fromTheme("document-new"), "Add Tab", actionNewTab, &QAction::trigger); + + // currentViewChanged + connect(tabs, &QTabBar::currentChanged, this, [this](int index) { + if (index == -1) emit currentViewChanged(nullptr); + else { + auto view = tabs->view(index); + Q_CHECK_PTR(view); + emit currentViewChanged(view); + } + }); + + // removeView + connect(tabs, &QTabBar::tabCloseRequested, this, [this](int index) { emit tabClosed(tabs->removeTab(index)); }); +} + +int TabToolBar::addView(RekonqView *view) +{ + Q_CHECK_PTR(view); + return tabs->addTab(view); +} + +void TabToolBar::setCurrentView(RekonqView *view) { tabs->setCurrentView(view); } + +RekonqView *TabToolBar::currentView() const +{ + auto *view = tabs->view(tabs->currentIndex()); + Q_CHECK_PTR(view); + return view; +} diff --git a/src/panels/tabtoolbar.hpp b/src/panels/tabtoolbar.hpp new file mode 100644 index 00000000..1c61ed2a --- /dev/null +++ b/src/panels/tabtoolbar.hpp @@ -0,0 +1,33 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#pragma once + +#include "tabbar/tabbar.h" +#include <QToolBar> + +class TabToolBar : public QToolBar { + Q_OBJECT + +public: + explicit TabToolBar(QWidget *parent = nullptr); + ~TabToolBar() override = default; + + int addView(RekonqView *view); + void setCurrentView(RekonqView *view); + [[nodiscard]] RekonqView *currentView() const; + +signals: + void currentViewChanged(RekonqView *view); + void tabClosed(RekonqView *view); + +public slots: + void closeCurrentTab() { emit tabs->tabCloseRequested(tabs->currentIndex()); } + +private: + TabBar *tabs; +}; |