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
|
/****************************************************************************
**
** 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 "statusbarmanager.h"
#include "imode.h"
#include "mainwindow.h"
#include "minisplitter.h"
#include "modemanager.h"
#include <utils/qtcassert.h>
#include <QHBoxLayout>
#include <QLabel>
#include <QResizeEvent>
#include <QSplitter>
#include <QStatusBar>
namespace Core {
const char kSettingsGroup[] = "StatusBar";
const char kLeftSplitWidthKey[] = "LeftSplitWidth";
static QPointer<QSplitter> m_splitter;
static QList<QPointer<QWidget>> m_statusBarWidgets;
static QList<QPointer<IContext>> m_contexts;
/*
Context that always returns the context of the active's mode widget (if available).
*/
class StatusBarContext : public IContext
{
public:
StatusBarContext(QObject *parent);
Context context() const final;
};
static QWidget *createWidget(QWidget *parent)
{
QWidget *w = new QWidget(parent);
w->setLayout(new QHBoxLayout);
w->setVisible(true);
w->layout()->setContentsMargins(0, 0, 0, 0);
return w;
}
static void createStatusBarManager()
{
QStatusBar *bar = ICore::statusBar();
m_splitter = new NonResizingSplitter(bar);
bar->insertPermanentWidget(0, m_splitter, 10);
m_splitter->setChildrenCollapsible(false);
// first
QWidget *w = createWidget(m_splitter);
w->layout()->setContentsMargins(0, 0, 3, 0);
m_splitter->addWidget(w);
m_statusBarWidgets.append(w);
QWidget *w2 = createWidget(m_splitter);
w2->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
m_splitter->addWidget(w2);
// second
w = createWidget(w2);
w2->layout()->addWidget(w);
m_statusBarWidgets.append(w);
// third
w = createWidget(w2);
w2->layout()->addWidget(w);
m_statusBarWidgets.append(w);
static_cast<QBoxLayout *>(w2->layout())->addStretch(1);
QWidget *rightCornerWidget = createWidget(bar);
bar->insertPermanentWidget(1, rightCornerWidget);
m_statusBarWidgets.append(rightCornerWidget);
auto statusContext = new StatusBarContext(bar);
statusContext->setWidget(bar);
ICore::addContextObject(statusContext);
QObject::connect(ICore::instance(), &ICore::saveSettingsRequested, [] {
QSettings *s = ICore::settings();
s->beginGroup(QLatin1String(kSettingsGroup));
s->setValue(QLatin1String(kLeftSplitWidthKey), m_splitter->sizes().at(0));
s->endGroup();
});
QObject::connect(ICore::instance(), &ICore::coreAboutToClose, [statusContext] {
delete statusContext;
// This is the catch-all on rampdown. Individual items may
// have been removed earlier by destroyStatusBarWidget().
for (const QPointer<IContext> &context : qAsConst(m_contexts)) {
ICore::removeContextObject(context);
delete context;
}
m_contexts.clear();
});
}
void StatusBarManager::addStatusBarWidget(QWidget *widget,
StatusBarPosition position,
const Context &ctx)
{
if (!m_splitter)
createStatusBarManager();
QTC_ASSERT(widget, return);
QTC_CHECK(widget->parent() == nullptr); // We re-parent, so user code does need / should not set it.
m_statusBarWidgets.at(position)->layout()->addWidget(widget);
auto context = new IContext;
context->setWidget(widget);
context->setContext(ctx);
m_contexts.append(context);
ICore::addContextObject(context);
}
void StatusBarManager::destroyStatusBarWidget(QWidget *widget)
{
QTC_ASSERT(widget, return);
const auto it = std::find_if(m_contexts.begin(), m_contexts.end(),
[widget](const auto &context) { return context->widget() == widget; });
if (it != m_contexts.end()) {
delete *it;
m_contexts.erase(it);
}
widget->setParent(nullptr);
delete widget;
}
void StatusBarManager::restoreSettings()
{
QSettings *s = ICore::settings();
s->beginGroup(QLatin1String(kSettingsGroup));
int leftSplitWidth = s->value(QLatin1String(kLeftSplitWidthKey), -1).toInt();
s->endGroup();
if (leftSplitWidth < 0) {
// size first split after its sizeHint + a bit of buffer
leftSplitWidth = m_splitter->widget(0)->sizeHint().width();
}
int sum = 0;
const QList<int> sizes = m_splitter->sizes();
for (const int w : sizes)
sum += w;
m_splitter->setSizes(QList<int>() << leftSplitWidth << (sum - leftSplitWidth));
}
StatusBarContext::StatusBarContext(QObject *parent)
: IContext(parent)
{
}
Context StatusBarContext::context() const
{
IMode *currentMode = ModeManager::currentMode();
QWidget *modeWidget = currentMode ? currentMode->widget() : nullptr;
if (modeWidget) {
if (IContext *context = ICore::contextObject(modeWidget))
return context->context();
}
return Context();
}
} // Core
|