aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/widgets/tabwidget.cpp
blob: 21476cd5cc376877fa0c4bae5d7aec5f0c88f095 (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
112
113
114
115
116
117
118
119
120
121
/*
 * 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/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "tabwidget.h"
#include "webengine/webview.h"
#include <QAction>
#include <QContextMenuEvent>
#include <QMenu>
#include <QTabBar>

TabWidget::TabWidget(QWidget *parent)
    : QTabWidget(parent)
{
    setStyleSheet("QTabBar::tab { width: 200px; }");

    setTabsClosable(true);
    setTabBarAutoHide(true);
    setElideMode(Qt::ElideRight);
    setMovable(true);

    connect(this, &TabWidget::tabCloseRequested, this, &TabWidget::deleteTab);

    // general actions
    closeTab_action = new QAction(this);
    closeTab_action->setShortcut(QKeySequence("Ctrl+X"));
    connect(closeTab_action, &QAction::triggered, this, [this]() {
        this->deleteTab(this->currentIndex());
    });
    addAction(closeTab_action);

    leftTab_action = new QAction(this);
    leftTab_action->setShortcut(QKeySequence("Ctrl+O"));
    connect(leftTab_action, &QAction::triggered, this, [this]() {
        this->setCurrentIndex(qMax(0, this->currentIndex() - 1));
    });
    addAction(leftTab_action);

    rightTab_action = new QAction(this);
    rightTab_action->setShortcut(QKeySequence("Ctrl+P"));
    connect(rightTab_action, &QAction::triggered, this, [this]() {
        this->setCurrentIndex(qMin(this->currentIndex() + 1, this->count() - 1));
    });
    addAction(rightTab_action);

    // context menu
    tabContextMenu = new QMenu(this);
    auto *closeTab = tabContextMenu->addAction(tr("Close Tab"));
    connect(closeTab, &QAction::triggered, this, [this]() {
        deleteTab(this->tabBar()->tabAt(mapFromGlobal(tabContextMenu->pos())));
    });

    auto *closeTabsLeft = tabContextMenu->addAction(tr("Close Tabs left"));
    connect(closeTabsLeft, &QAction::triggered, this, [this]() {
        int idx = this->tabBar()->tabAt(mapFromGlobal(tabContextMenu->pos()));
        for(int i = idx - 1; i >= 0; --i) {
            deleteTab(i);
        }
    });

    auto *closeTabsRight = tabContextMenu->addAction(tr("Close Tabs right"));
    connect(closeTabsRight, &QAction::triggered, this, [this]() {
        int idx = this->tabBar()->tabAt(mapFromGlobal(tabContextMenu->pos()));
        for(int i = count() - 1; i > idx; --i) {
            deleteTab(i);
        }
    });
}

TabWidget::~TabWidget()
{
    for(int i = count() - 1; i >= 0; i--) {
        delete widget(i);
    }
}

int TabWidget::addTab(WebView *view)
{
    Q_CHECK_PTR(view);

    int idx = QTabWidget::addTab(view, view->title());
    connect(view, &WebView::titleChanged, [this, view](const QString &title) {
        int idx = this->indexOf(view);
        Q_ASSERT(idx != -1);

        this->setTabText(idx, title);
    });
    connect(view, &WebView::iconChanged, [this, view](const QIcon &icon) {
        int idx = this->indexOf(view);
        Q_ASSERT(idx != -1);

        this->setTabIcon(idx, icon);
    });

    return idx;
}

void TabWidget::deleteTab(int index)
{
    // deleting the widget automatically removes the tab?
    if(count() > 1) {
        widget(index)->deleteLater();
        removeTab(index);
    } else
        parentWidget()->close();
}

void TabWidget::contextMenuEvent(QContextMenuEvent *event)
{
    // check if the context menu was called on a tab
    int tabIndex = tabBar()->tabAt(event->pos());
    if(tabIndex < 0) {
        return;
    }

    tabContextMenu->exec(event->globalPos());
}