diff options
Diffstat (limited to 'src/subwindow')
-rw-r--r-- | src/subwindow/subwindow.cpp | 6 | ||||
-rw-r--r-- | src/subwindow/subwindow.h | 4 | ||||
-rw-r--r-- | src/subwindow/tabwidget.cpp | 49 | ||||
-rw-r--r-- | src/subwindow/tabwidget.h | 53 |
4 files changed, 61 insertions, 51 deletions
diff --git a/src/subwindow/subwindow.cpp b/src/subwindow/subwindow.cpp index a2d6138..60ea633 100644 --- a/src/subwindow/subwindow.cpp +++ b/src/subwindow/subwindow.cpp @@ -9,9 +9,9 @@ #include "subwindow.h" #include "browser.h" #include "configuration.h" -#include "webengine/webprofile.h" -#include "webengine/webprofilemanager.h" -#include "webengine/webview.h" +#include "webprofile.h" +#include "webprofilemanager.h" +#include "webview.h" #include <QAction> #include <QCloseEvent> #include <QHideEvent> diff --git a/src/subwindow/subwindow.h b/src/subwindow/subwindow.h index 80e8520..a7b4564 100644 --- a/src/subwindow/subwindow.h +++ b/src/subwindow/subwindow.h @@ -11,7 +11,7 @@ #include "smolbote/session.hpp" #include "tabwidget.h" -#include "webengine/webview.h" +#include "webview.h" #include <QMenu> #include <QUrl> #include <QWidget> @@ -29,7 +29,7 @@ public: explicit SubWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); explicit SubWindow(const Session::SubWindow &tab_data, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); - ~SubWindow() = default; + ~SubWindow() override = default; [[nodiscard]] Session::SubWindow serialize() const; [[nodiscard]] int currentTabIndex() const diff --git a/src/subwindow/tabwidget.cpp b/src/subwindow/tabwidget.cpp index 69f3b8a..1e1d939 100644 --- a/src/subwindow/tabwidget.cpp +++ b/src/subwindow/tabwidget.cpp @@ -8,29 +8,22 @@ #include "tabwidget.h" #include "browser.h" -#include "webengine/webview.h" +#include "subwindow.h" +#include "webprofile.h" +#include "webview.h" #include <QAction> #include <QContextMenuEvent> #include <QMenu> #include <QTabBar> -#include "webengine/webprofile.h" #include <QWebEngineHistory> -#include "subwindow.h" -inline WebView *createViewFromInfo(TabWidget::TabInformation &tab, SubWindow *parent) -{ - auto *view = new WebView(tab.profile, std::bind(&SubWindow::createView, parent, std::placeholders::_1), parent); - QDataStream stream(&tab.historyBuffer, QIODevice::ReadOnly); - stream >> *view->history(); - view->history()->goToItem(view->history()->itemAt(tab.historyIndex)); - return view; -} +const QLatin1String stylesheet("QTabBar::tab { width: 200px; }"); TabWidget::TabWidget(SubWindow *parent) : QTabWidget(parent) , m_parent(parent) { - setStyleSheet("QTabBar::tab { width: 200px; }"); + setStyleSheet(stylesheet); setTabsClosable(true); setTabBarAutoHide(true); @@ -80,29 +73,6 @@ TabWidget::~TabWidget() } } -int TabWidget::addTab(WebView *view) -{ - if(view == nullptr) { - return -1; - } - - const int idx = QTabWidget::addTab(view, view->title()); - connect(view, &WebView::titleChanged, [this, view](const QString &title) { - const int current_idx = indexOf(view); - if(current_idx != -1) { - setTabText(current_idx, title); - } - }); - connect(view, &WebView::iconChanged, [this, view](const QIcon &icon) { - const int current_idx = indexOf(view); - if(current_idx != -1) { - setTabIcon(current_idx, icon); - } - }); - tabBar()->setTabData(idx, QVariant::fromValue<SubWindow::TabData>(SubWindow::TabData{})); - return idx; -} - void TabWidget::removeTab(int index) { // deleting the widget automatically removes the tab? @@ -180,3 +150,12 @@ void TabWidget::mousePressEvent(QMouseEvent *event) QTabWidget::mousePressEvent(event); } + +WebView *TabWidget::createViewFromInfo(TabWidget::TabInformation &tab, SubWindow *parent) +{ + auto *view = new WebView(tab.profile, std::bind(&SubWindow::createView, parent, std::placeholders::_1), parent); + QDataStream stream(&tab.historyBuffer, QIODevice::ReadOnly); + stream >> *view->history(); + view->history()->goToItem(view->history()->itemAt(tab.historyIndex)); + return view; +} diff --git a/src/subwindow/tabwidget.h b/src/subwindow/tabwidget.h index de5e6fb..2a5f360 100644 --- a/src/subwindow/tabwidget.h +++ b/src/subwindow/tabwidget.h @@ -9,9 +9,17 @@ #ifndef SMOLBOTE_TABWIDGET_H #define SMOLBOTE_TABWIDGET_H -#include <QTabWidget> -#include <QQueue> #include <QBuffer> +#include <QQueue> +#include <QTabWidget> + +template <typename T> +concept c_WebView = requires(T *a) +{ + a->title(); + a->titleChanged(QString()); + a->iconChanged(QIcon()); +}; class QAction; class QMenu; @@ -24,19 +32,34 @@ class TabWidget : public QTabWidget Q_OBJECT public: - struct TabInformation - { - WebProfile *profile; - QString title; - int historyIndex; - QByteArray historyBuffer; - }; - explicit TabWidget(SubWindow *parent = nullptr); ~TabWidget() override; + template <c_WebView T> + int addTab(T *view) + { + if(view == nullptr) { + return -1; + } + + const int idx = QTabWidget::addTab(view, view->title()); + connect(view, &T::titleChanged, [this, view](const QString &title) { + const int current_idx = indexOf(view); + if(current_idx != -1) { + setTabText(current_idx, title); + } + }); + connect(view, &T::iconChanged, [this, view](const QIcon &icon) { + const int current_idx = indexOf(view); + if(current_idx != -1) { + setTabIcon(current_idx, icon); + } + }); + //tabBar()->setTabData(idx, QVariant::fromValue<SubWindow::TabData>(SubWindow::TabData{})); + return idx; + } + public slots: - int addTab(WebView *view); void removeTab(int index); int restoreLastTab(); @@ -47,6 +70,14 @@ protected: void mousePressEvent(QMouseEvent *event) override; private: + struct TabInformation { + WebProfile *profile; + QString title; + int historyIndex; + QByteArray historyBuffer; + }; + [[nodiscard]] WebView *createViewFromInfo(TabInformation &tab, SubWindow *parent); + SubWindow *m_parent; int current = -1; int previous = -1; |