aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/switchsplittabwidget.cpp
blob: e30f4180cfe5e13de67ce0ff7b01bce07747dbeb (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "switchsplittabwidget.h"
#include <theme.h>

#include <utils/utilsicons.h>
#include <utils/fileutils.h>

#include <QVector>
#include <QBoxLayout>
#include <QTabBar>
#include <QToolButton>
#include <QSplitter>
#include <QLayoutItem>
#include <QEvent>

namespace QmlDesigner {
SwitchSplitTabWidget::SwitchSplitTabWidget(QWidget *parent)
    : QWidget(parent)
    , m_splitter(new QSplitter)
    , m_tabBar(new QTabBar)
    , m_tabBarBackground(new QWidget)
{
    // setting object names for css
    setObjectName("backgroundWidget");
    m_splitter->setObjectName("centralTabWidget");
    m_splitter->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_splitter->setHandleWidth(0);

    QString sheet = QString::fromUtf8(Utils::FileReader::fetchQrc(":/qmldesigner/centerwidget.css"));
    m_tabBarBackground->setStyleSheet(Theme::replaceCssColors(sheet));

    m_tabBar->setObjectName("centralTabBar");
    m_tabBar->setShape(QTabBar::RoundedEast);
    m_tabBar->setDocumentMode(false);
    // add a faketab to have the possibility to unselect all tabs
    m_tabBar->addTab(QString());
    selectFakeTab();

    m_tabBarBackground->setObjectName("tabBarBackground");

    connect(m_tabBar, &QTabBar::tabBarClicked, [this] (int index) {
        if (index != -1)
            updateSplitterSizes(index - fakeTab);
    });

    setLayout(new QHBoxLayout);
    layout()->setContentsMargins(0, 0, 0, 0);
    layout()->setSpacing(0);
    layout()->addWidget(m_splitter);

    m_tabBarBackground->setLayout(new QVBoxLayout);
    m_tabBarBackground->layout()->setContentsMargins(0, 0, 0, 0);
    m_tabBarBackground->layout()->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
    m_tabBarBackground->layout()->addWidget(m_tabBar);

    auto horizontalButton = new QToolButton;
    horizontalButton->setObjectName("centralTabBar");
    horizontalButton->setIcon(Utils::Icon({{QLatin1String(":/qmldesigner/images/spliteditorvertically.png"),
                                            Utils::Theme::IconsBaseColor}}).icon());
    horizontalButton->setIconSize(QSize(8, 16));
    connect(horizontalButton, &QToolButton::clicked, [this] () {
        m_splitter->setOrientation(Qt::Vertical);
        updateSplitterSizes();
        selectFakeTab();
    });
    auto verticalButton = new QToolButton;
    verticalButton->setObjectName("centralTabBar");
    verticalButton->setIcon(Utils::Icon({{QLatin1String(":/qmldesigner/images/spliteditorhorizontally.png"),
                                          Utils::Theme::IconsBaseColor}}).icon());
    verticalButton->setIconSize(QSize(8, 16));
    connect(verticalButton, &QToolButton::clicked, [this] () {
        m_splitter->setOrientation(Qt::Horizontal);
        updateSplitterSizes();
        selectFakeTab();
    });

    m_tabBarBackground->layout()->addWidget(horizontalButton);
    m_tabBarBackground->layout()->addWidget(verticalButton);
    layout()->addWidget(m_tabBarBackground);
    updateSplitButtons();
}

int SwitchSplitTabWidget::count() const
{
    return m_splitter->count();
}

QWidget *SwitchSplitTabWidget::currentWidget() const
{
    QList<int> sizes = m_splitter->sizes();
    for (int i = 0; i < count(); ++i) {
        if (sizes.at(i) > 0 && m_splitter->widget(i)->hasFocus())
            return m_splitter->widget(i);
    }
    return nullptr;
}

void SwitchSplitTabWidget::updateSplitterSizes(int index)
{
    QVector<int> splitterSizes(m_splitter->count());
    int splitterFullSize = 0;
    bool isHorizontal = m_splitter->orientation() == Qt::Horizontal;
    for (int i = 0; i < m_splitter->count(); ++i) {
        auto widget = m_splitter->widget(i);
        splitterFullSize += isHorizontal ? widget->width() : widget->height();
    }

    if (index > -1) {
        // collapse all but the one at index
        splitterSizes.fill(0);
        splitterSizes.replace(index, splitterFullSize);
    } else {
        // distribute full size among enabled tabs
        int divisor = splitterSizes.count();
        for (int i = 0; i < m_splitter->count(); ++i) {
            if (!m_tabBar->isTabEnabled(i + fakeTab))
                --divisor;
        }

        int splitSize = splitterFullSize / divisor;
        for (int i = 0; i < m_splitter->count(); ++i)
            splitterSizes.replace(i, m_tabBar->isTabEnabled(i + fakeTab) ? splitSize : 0);
    }

    m_splitter->setSizes(splitterSizes.toList());
}

int SwitchSplitTabWidget::addTab(QWidget *w, const QString &label)
{
    m_splitter->addWidget(w);
    const int newIndex = m_tabBar->addTab(label);
    if (mode() == TabMode) {
        m_tabBar->setCurrentIndex(newIndex);
        updateSplitterSizes(newIndex - fakeTab);
    }
    if (mode() == SplitMode)
        updateSplitterSizes();
    updateSplitButtons();
    return newIndex;
}

QWidget *SwitchSplitTabWidget::takeTabWidget(const int index)
{
    if (index == -1 || index > count() - 1)
        return nullptr;
    QWidget *widget = m_splitter->widget(index);
    widget->setParent(nullptr);
    m_tabBar->removeTab(index + fakeTab);
    // TODO: set which mode and tab is the current one
    updateSplitButtons();
    return widget;
}

void SwitchSplitTabWidget::switchTo(QWidget *widget)
{
    if (widget == nullptr || currentWidget() == widget)
        return;
    const int widgetIndex = m_splitter->indexOf(widget);
    Q_ASSERT(widgetIndex != -1);
    if (mode() == TabMode) {
        updateSplitterSizes(widgetIndex);
        m_tabBar->setCurrentIndex(widgetIndex + fakeTab);
    }

    widget->setFocus();
}

void SwitchSplitTabWidget::updateSplitButtons()
{
    const bool isTabBarNecessary = count() > 1;
    m_tabBarBackground->setVisible(isTabBarNecessary);
}

void SwitchSplitTabWidget::selectFakeTab()
{
    m_tabBar->setCurrentIndex(0);
}

SwitchSplitTabWidget::Mode SwitchSplitTabWidget::mode() const
{
    const bool isTabBarNecessary = count() > 1;
    const int fakeTabPosition = 0;
    const int hasSelectedTab = m_tabBar->currentIndex() > fakeTabPosition;
    // Note: When splitting the view by dragging from the side of the view, SplitMode is not detected
    return (isTabBarNecessary && !hasSelectedTab) ? SplitMode : TabMode;
}

} // namespace QmlDesigner