aboutsummaryrefslogtreecommitdiff
path: root/src/subwindow/subwindow.h
blob: 88f39859ee6c94fa7ecd9bbc3b05f452c7db99fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#ifndef SMOLBOTE_SUBWINDOW_H
#define SMOLBOTE_SUBWINDOW_H

#include "session.hpp"
#include "tabwidget.h"
#include "webengine/webview.h"
#include <QMenu>
#include <QUrl>
#include <QWidget>

class WebProfile;
class SubWindow : public QWidget
{
    Q_OBJECT

public:
    struct TabData {
        bool closeLocked = false;
        bool refreshLocked = false;
    };

    explicit SubWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
    explicit SubWindow(const Session::SubWindow &data, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
    ~SubWindow() = default;
    [[nodiscard]] Session::SubWindow serialize() const;

    [[nodiscard]] int currentTabIndex() const
    {
        return tabWidget->currentIndex();
    }

    [[nodiscard]] WebView *currentView()
    {
        return qobject_cast<WebView *>(tabWidget->currentWidget());
    }

    [[nodiscard]] WebView *view(int index) const
    {
        return qobject_cast<WebView *>(tabWidget->widget(index));
    }

    [[nodiscard]] int tabCount() const
    {
        return tabWidget->count();
    }

    void setProfile(WebProfile *profile);
    [[nodiscard]] WebProfile *profile() const
    {
        return m_profile;
    }

    void setTabData(TabData &data, int index);
    [[nodiscard]] TabData tabData(int index) const;

signals:
    void currentViewChanged(WebView *view);
    void showStatusMessage(const QString &message, int timeout = 0);
    void visibilityChanged(bool isVisible);
    void aboutToClose();

public slots:
    WebView *createView(QWebEnginePage::WebWindowType type);

    int addTab(const QUrl &url = QUrl(), WebProfile *profile = nullptr);
    int addTab(const Session::WebView &data);
    void closeTab(int index)
    {
        tabWidget->removeTab(index);
    }

    void setCurrentTab(int index)
    {
        if(index >= 0)
            tabWidget->setCurrentIndex(index);
    }
    void moveTab(int from, int to);

    int restoreLastTab()
    {
        return tabWidget->restoreLastTab();
    }
    void restoreTabMenu(QMenu *menu)
    {
        tabWidget->restoreTabMenu(menu);
    }

protected:
    void closeEvent(QCloseEvent *event);
    void hideEvent(QHideEvent *event);
    void showEvent(QShowEvent *event);

private:
    WebProfile *m_profile;
    TabWidget *tabWidget;

    QMetaObject::Connection titleConnection;
    QMetaObject::Connection linkHoveredConnection;
};

Q_DECLARE_METATYPE(SubWindow::TabData)

#endif // SMOLBOTE_SUBWINDOW_H