summaryrefslogtreecommitdiffstats
path: root/ui/qt/widgets/detachable_tabwidget.cpp
blob: caffd743f934a2bf09a1e1ca546f0cc12cf57098 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* @file
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <ui/qt/widgets/detachable_tabwidget.h>

#include <QStackedWidget>
#include <QBoxLayout>
#include <QEvent>
#include <QCloseEvent>
#include <QMouseEvent>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QStringList>
#include <QApplication>
#include <QDrag>
#include <QPixmap>
#include <QPainter>

DetachableTabWidget::DetachableTabWidget(QWidget *parent) :
    QTabWidget(parent)
{
    DragDropTabBar * tabBar = new DragDropTabBar(this);
    connect(tabBar, &DragDropTabBar::onDetachTab, this, &DetachableTabWidget::detachTab);
    connect(tabBar, &DragDropTabBar::onMoveTab, this, &DetachableTabWidget::moveTab);

    setMovable(false);

    setTabBar(tabBar);
}

void DetachableTabWidget::setTabBasename(QString newName) {
    _tabBasename = newName;
}

QString DetachableTabWidget::tabBasename() const {
    return _tabBasename;
}

void DetachableTabWidget::moveTab(int from, int to)
{
    QWidget * contentWidget = widget(from);
    QString text = tabText(from);

    removeTab(from);
    insertTab(to, contentWidget, text);
    setCurrentIndex(to);
}

void DetachableTabWidget::detachTab(int tabIdx, QPoint pos)
{
    QString name = tabText(tabIdx);

    QWidget * contentWidget = widget(tabIdx);
    
    /* For the widget to properly show in the dialog, it has to be
     * removed properly and unhidden. QTabWidget uses a QStackedWidget for
     * all parents of widgets. So we remove it from it's own parent and then
     * unhide it to show the widget in the dialog */
    QStackedWidget * par = qobject_cast<QStackedWidget *>(contentWidget->parent());
    if (!par)
        return;
    QRect contentWidgetRect = par->frameGeometry();
    par->removeWidget(contentWidget);
    contentWidget->setHidden(false);

    ToolDialog * detachedTab = new ToolDialog(contentWidget, parentWidget());
    detachedTab->setWindowModality(Qt::NonModal);
    detachedTab->setWindowTitle(_tabBasename + ": " + name);
    detachedTab->setObjectName(name);
    detachedTab->setGeometry(contentWidgetRect);
    connect(detachedTab, &ToolDialog::onCloseSignal, this, &DetachableTabWidget::attachTab);
    detachedTab->move(pos);
    detachedTab->show();
}

void DetachableTabWidget::attachTab(QWidget * content, QString name)
{
    content->setParent(this);

    int index = addTab(content, name);
    if (index > -1)
        setCurrentIndex(index);
}

ToolDialog::ToolDialog(QWidget *contentWidget, QWidget *parent, Qt::WindowFlags f) :
    QDialog(parent, f)
{
    _contentWidget = contentWidget;

    _contentWidget->setParent(this);
    QVBoxLayout * layout = new QVBoxLayout(this);
    layout->addWidget(_contentWidget);
    this->setLayout(layout);
}

bool ToolDialog::event(QEvent *event)
{
    /**
     * Capture a double click event on the dialog's window frame
     */
    if (event->type() == QEvent::NonClientAreaMouseButtonDblClick) {
        event->accept();
        close();
    }

    return QDialog::event(event);
}

void ToolDialog::closeEvent(QCloseEvent * /*event*/)
{
    emit onCloseSignal(_contentWidget, objectName());
}

DragDropTabBar::DragDropTabBar(QWidget *parent) :
    QTabBar(parent)
{
    setAcceptDrops(true);
    setElideMode(Qt::ElideRight);
    setSelectionBehaviorOnRemove(QTabBar::SelectLeftTab);

    _dragStartPos = QPoint();
    _dragDropPos = QPoint();
    _mouseCursor = QCursor();
    _dragInitiated = false;
}

void DragDropTabBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    event->accept();
    emit onDetachTab(tabAt(event->pos()), _mouseCursor.pos());
}

void DragDropTabBar::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
        _dragStartPos = event->pos();

    _dragDropPos = QPoint(0, 0);
    _dragInitiated = false;

    QTabBar::mousePressEvent(event);
}

void DragDropTabBar::mouseMoveEvent(QMouseEvent *event)
{
    if (!_dragStartPos.isNull() &&
            ((event->pos() - _dragStartPos).manhattanLength() > QApplication::startDragDistance()))
        _dragInitiated = true;

    if ((event->buttons() & Qt::LeftButton) && _dragInitiated) {
        QMouseEvent * finishMouseMove = new QMouseEvent(QEvent::MouseMove, event->pos(), QCursor::pos(), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
        QTabBar::mouseMoveEvent(finishMouseMove);

        QDrag * drag = new QDrag(this);
        QMimeData * mimeData = new QMimeData();
        mimeData->setData("action", "application/tab-detach");
        drag->setMimeData(mimeData);

        QWidget * original = parentWidget();
        if (qobject_cast<DetachableTabWidget *>(original)) {
            DetachableTabWidget * tabWidget = qobject_cast<DetachableTabWidget *>(original);
            original = tabWidget->widget(tabWidget->currentIndex());
        }
        QPixmap pixmap = original->grab();
        QPixmap targetPixmap = QPixmap(pixmap.size());
        targetPixmap.fill(Qt::transparent);

        QPainter painter(&targetPixmap);
        painter.setOpacity(0.85);
        painter.drawPixmap(0, 0, pixmap);
        painter.end();
        drag->setPixmap(targetPixmap);

        Qt::DropAction dropAction = drag->exec(Qt::MoveAction | Qt::CopyAction);
        if (dropAction == Qt::IgnoreAction) {
            event->accept();
            emit onDetachTab(tabAt(_dragStartPos), _mouseCursor.pos());
        } if (dropAction == Qt::MoveAction) {
            if (! _dragDropPos.isNull()) {
                event->accept();
                emit onMoveTab(tabAt(_dragStartPos), tabAt(_dragDropPos));
            }
        }
    } else
        QTabBar::mouseMoveEvent(event);
}

void DragDropTabBar::dragEnterEvent(QDragEnterEvent *event)
{
    const QMimeData * mimeData = event->mimeData();
    QStringList formats = mimeData->formats();

    if (formats.contains("action") && mimeData->data("action") == "application/tab-detach")
        event->acceptProposedAction();
}

void DragDropTabBar::dropEvent(QDropEvent *event)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    _dragDropPos = event->position().toPoint();
#else
    _dragDropPos = event->pos();
#endif
    QTabBar::dropEvent(event);
}