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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <QtInstanceNotebook.hxx>
#include <QtInstanceNotebook.moc>
#include <vcl/qt/QtUtils.hxx>
#include <QtWidgets/QTabBar>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
// property on each tab's widget holding the accessible identifier of that tab
const char* const PROPERTY_TAB_PAGE_ID = "tab-page-id";
QtInstanceNotebook::QtInstanceNotebook(QTabWidget* pTabWidget)
: QtInstanceWidget(pTabWidget)
, m_pTabWidget(pTabWidget)
{
assert(m_pTabWidget);
if (m_pTabWidget->count())
m_sCurrentTabId = get_current_page_ident();
connect(m_pTabWidget, &QTabWidget::currentChanged, this,
&QtInstanceNotebook::currentTabChanged);
}
int QtInstanceNotebook::get_current_page() const
{
SolarMutexGuard g;
int nCurrentIndex = 0;
GetQtInstance().RunInMainThread([&] { nCurrentIndex = m_pTabWidget->currentIndex(); });
return nCurrentIndex;
}
int QtInstanceNotebook::get_page_index(const OUString& rIdent) const
{
SolarMutexGuard g;
const QString sId = toQString(rIdent);
int nIndex = -1;
GetQtInstance().RunInMainThread([&] {
for (int i = 0; i < m_pTabWidget->count(); ++i)
{
if (get_page_ident(i) == rIdent)
{
nIndex = i;
return;
}
}
});
return nIndex;
}
OUString QtInstanceNotebook::get_page_ident(int nPage) const
{
SolarMutexGuard g;
OUString sIdent;
GetQtInstance().RunInMainThread([&] {
QWidget* pPage = m_pTabWidget->widget(nPage);
assert(pPage);
QVariant aIdVariant = pPage->property(PROPERTY_TAB_PAGE_ID);
if (aIdVariant.canConvert<QString>())
sIdent = toOUString(aIdVariant.toString());
});
return sIdent;
}
OUString QtInstanceNotebook::get_current_page_ident() const
{
SolarMutexGuard g;
OUString sIdent;
GetQtInstance().RunInMainThread([&] { sIdent = get_page_ident(m_pTabWidget->currentIndex()); });
return sIdent;
}
void QtInstanceNotebook::set_current_page(int nPage)
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] { m_pTabWidget->setCurrentIndex(nPage); });
}
void QtInstanceNotebook::set_current_page(const OUString& rIdent)
{
set_current_page(get_page_index(rIdent));
}
void QtInstanceNotebook::remove_page(const OUString& rIdent)
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] { m_pTabWidget->removeTab(get_page_index(rIdent)); });
}
void QtInstanceNotebook::insert_page(const OUString& rIdent, const OUString& rLabel, int nPos)
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] {
QWidget* pPage = new QWidget;
pPage->setLayout(new QVBoxLayout);
pPage->setProperty(PROPERTY_TAB_PAGE_ID, toQString(rIdent));
m_pTabWidget->insertTab(nPos, pPage, toQString(rLabel));
});
}
void QtInstanceNotebook::set_tab_label_text(const OUString& rIdent, const OUString& rLabel)
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] {
const int nIndex = get_page_index(rIdent);
m_pTabWidget->setTabText(nIndex, toQString(rLabel));
});
}
OUString QtInstanceNotebook::get_tab_label_text(const OUString& rIdent) const
{
SolarMutexGuard g;
OUString sText;
GetQtInstance().RunInMainThread([&] {
const int nIndex = get_page_index(rIdent);
sText = toOUString(m_pTabWidget->tabText(nIndex));
});
return sText;
}
void QtInstanceNotebook::set_show_tabs(bool bShow)
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] { m_pTabWidget->tabBar()->setVisible(bShow); });
}
int QtInstanceNotebook::get_n_pages() const
{
SolarMutexGuard g;
int nCount = 0;
GetQtInstance().RunInMainThread([&] { nCount = m_pTabWidget->count(); });
return nCount;
}
weld::Container* QtInstanceNotebook::get_page(const OUString& rIdent) const
{
SolarMutexGuard g;
QWidget* pWidget = nullptr;
GetQtInstance().RunInMainThread([&] {
const int nIndex = get_page_index(rIdent);
pWidget = m_pTabWidget->widget(nIndex);
});
if (!pWidget)
return nullptr;
if (!m_aPageContainerInstances.contains(pWidget))
m_aPageContainerInstances.emplace(pWidget, std::make_unique<QtInstanceContainer>(pWidget));
return m_aPageContainerInstances.at(pWidget).get();
}
void QtInstanceNotebook::setTabIdAndLabel(QTabWidget& rTabWidget, int nPage, const OUString& rIdent,
const OUString& rLabel)
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] {
QWidget* pPage = rTabWidget.widget(nPage);
assert(pPage);
pPage->setProperty(PROPERTY_TAB_PAGE_ID, toQString(rIdent));
rTabWidget.setTabText(nPage, toQString(rLabel));
});
}
void QtInstanceNotebook::currentTabChanged()
{
SolarMutexGuard g;
if (!m_sCurrentTabId.isEmpty())
m_aLeavePageHdl.Call(m_sCurrentTabId);
m_sCurrentTabId = get_current_page_ident();
if (!m_sCurrentTabId.isEmpty())
m_aLeavePageHdl.Call(m_sCurrentTabId);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
|