-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamiclayout.cpp
executable file
·124 lines (98 loc) · 3.33 KB
/
dynamiclayout.cpp
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
#include "dynamiclayout.h"
#include "dynamicpage.h"
#include "renamedialog.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QStyle>
#include <QTabBar>
DynamicLayout::DynamicLayout(QWidget *parent)
: QWidget(parent) {
m_tabLayout = new QTabWidget(this);
connect(m_tabLayout, &QTabWidget::currentChanged, this, &DynamicLayout::pageChanged);
connect(m_tabLayout, &QTabWidget::tabBarDoubleClicked, this, &DynamicLayout::renamePageRequest);
auto *page = new DynamicPage(this);
connect(page, &DynamicPage::nameChanged, this, &DynamicLayout::renamePage);
m_tabLayout->addTab(page, "");
page->setName(tr("layout#%1").arg(m_index++));
addCloseButton(0);
auto *layout = new QHBoxLayout(this);
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_tabLayout);
setLayout(layout);
}
DynamicLayout::~DynamicLayout() = default;
void DynamicLayout::pageChanged(int index) {
m_tabLayout->setCornerWidget(nullptr);
auto count = m_tabLayout->count();
if (index == count - 1) {
auto *newPage = new DynamicPage(this);
connect(newPage, &DynamicPage::nameChanged, this, &DynamicLayout::renamePage);
m_tabLayout->addTab(newPage, "");
newPage->setName("+");
const auto pageName = tr("layout#%1").arg(m_index++);
auto *page = dynamic_cast<DynamicPage *>(m_tabLayout->widget(index));
if (nullptr != page) {
page->setName(pageName);
}
addCloseButton(index);
}
auto *page = dynamic_cast<DynamicPage *>(m_tabLayout->widget(index));
if (nullptr == page) {
return;
}
auto *newTool = page->toolBar();
m_tabLayout->setCornerWidget(newTool);
newTool->show();
}
void DynamicLayout::closePage() {
int index = -1;
for (int i = 0; i < m_tabLayout->count(); i++) {
if (sender() != m_tabLayout->tabBar()->tabButton(i, QTabBar::RightSide)) {
continue;
}
index = i;
break;
}
if (-1 == index) {
return;
}
auto *page = dynamic_cast<DynamicPage *>(m_tabLayout->widget(index));
if (nullptr != page) {
page->closePage();
}
if (m_tabLayout->count() >= 3 && index == m_tabLayout->count() - 2) {
m_tabLayout->setCurrentIndex(m_tabLayout->count() - 3);
}
m_tabLayout->removeTab(index);
}
void DynamicLayout::renamePageRequest(int index) {
auto *page = dynamic_cast<DynamicPage *>(m_tabLayout->widget(index));
if (nullptr == page) {
return;
}
page->renamePage();
}
void DynamicLayout::renamePage(const QString &name) {
int index = -1;
for (int i = 0; i < m_tabLayout->count(); i++) {
if (sender() != m_tabLayout->widget(i)) {
continue;
}
index = i;
break;
}
if (-1 == index) {
return;
}
m_tabLayout->setTabText(index, name);
}
void DynamicLayout::addCloseButton(int index) {
auto *closeButton = new QPushButton(this);
closeButton->setToolTip(tr("close layout"));
closeButton->setIcon(style()->standardIcon(QStyle::SP_TitleBarCloseButton));
closeButton->setFlat(true);
closeButton->setFixedWidth(16);
m_tabLayout->tabBar()->setTabButton(index, QTabBar::RightSide, closeButton);
connect(closeButton, &QPushButton::clicked, this, &DynamicLayout::closePage);
}