2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2009-12-09 11:05:18 +01:00
|
|
|
#include "statusbarmanager.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2018-07-10 15:49:50 +02:00
|
|
|
#include "imode.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "mainwindow.h"
|
2017-07-03 16:26:26 +02:00
|
|
|
#include "minisplitter.h"
|
2018-07-10 15:49:50 +02:00
|
|
|
#include "modemanager.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2018-01-24 17:41:53 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
|
#include <QLabel>
|
2014-02-27 17:26:04 +01:00
|
|
|
#include <QResizeEvent>
|
|
|
|
|
#include <QSplitter>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QStatusBar>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2018-01-24 17:41:53 +01:00
|
|
|
namespace Core {
|
2014-02-27 17:26:04 +01:00
|
|
|
|
2018-01-24 17:41:53 +01:00
|
|
|
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;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2020-03-18 13:32:02 +01:00
|
|
|
/*
|
2018-07-10 15:49:50 +02:00
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-24 12:42:50 +01:00
|
|
|
static QWidget *createWidget(QWidget *parent)
|
2013-03-22 14:11:19 +01:00
|
|
|
{
|
2013-04-10 10:41:29 +02:00
|
|
|
QWidget *w = new QWidget(parent);
|
2013-03-22 14:11:19 +01:00
|
|
|
w->setLayout(new QHBoxLayout);
|
|
|
|
|
w->setVisible(true);
|
2019-08-29 10:36:01 +02:00
|
|
|
w->layout()->setContentsMargins(0, 0, 0, 0);
|
2013-03-22 14:11:19 +01:00
|
|
|
return w;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 17:41:53 +01:00
|
|
|
static void createStatusBarManager()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2018-01-24 12:42:50 +01:00
|
|
|
QStatusBar *bar = ICore::statusBar();
|
|
|
|
|
|
2014-02-27 17:26:04 +01:00
|
|
|
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);
|
|
|
|
|
|
2013-04-10 10:41:29 +02:00
|
|
|
QWidget *rightCornerWidget = createWidget(bar);
|
2014-02-27 17:26:04 +01:00
|
|
|
bar->insertPermanentWidget(1, rightCornerWidget);
|
2013-03-22 14:11:19 +01:00
|
|
|
m_statusBarWidgets.append(rightCornerWidget);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2020-05-26 14:48:38 +02:00
|
|
|
auto statusContext = new StatusBarContext(bar);
|
|
|
|
|
statusContext->setWidget(bar);
|
|
|
|
|
ICore::addContextObject(statusContext);
|
2018-07-10 15:49:50 +02:00
|
|
|
|
2022-12-07 16:25:11 +01:00
|
|
|
QObject::connect(ICore::instance(), &ICore::saveSettingsRequested, ICore::instance(), [] {
|
2018-01-24 17:41:53 +01:00
|
|
|
QSettings *s = ICore::settings();
|
|
|
|
|
s->beginGroup(QLatin1String(kSettingsGroup));
|
|
|
|
|
s->setValue(QLatin1String(kLeftSplitWidthKey), m_splitter->sizes().at(0));
|
|
|
|
|
s->endGroup();
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-07 16:25:11 +01:00
|
|
|
QObject::connect(ICore::instance(), &ICore::coreAboutToClose, statusContext, [statusContext] {
|
2020-05-26 14:48:38 +02:00
|
|
|
delete statusContext;
|
2018-01-24 17:41:53 +01:00
|
|
|
// This is the catch-all on rampdown. Individual items may
|
|
|
|
|
// have been removed earlier by destroyStatusBarWidget().
|
2022-10-07 14:46:06 +02:00
|
|
|
for (const QPointer<IContext> &context : std::as_const(m_contexts)) {
|
2018-01-24 17:41:53 +01:00
|
|
|
ICore::removeContextObject(context);
|
2018-02-16 14:52:05 +01:00
|
|
|
delete context;
|
|
|
|
|
}
|
2018-01-24 17:41:53 +01:00
|
|
|
m_contexts.clear();
|
|
|
|
|
});
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-24 17:41:53 +01:00
|
|
|
void StatusBarManager::addStatusBarWidget(QWidget *widget,
|
|
|
|
|
StatusBarPosition position,
|
|
|
|
|
const Context &ctx)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2018-01-24 17:41:53 +01:00
|
|
|
if (!m_splitter)
|
|
|
|
|
createStatusBarManager();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2018-01-24 17:41:53 +01:00
|
|
|
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);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2018-01-24 17:41:53 +01:00
|
|
|
auto context = new IContext;
|
|
|
|
|
context->setWidget(widget);
|
|
|
|
|
context->setContext(ctx);
|
|
|
|
|
m_contexts.append(context);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2018-01-24 17:41:53 +01:00
|
|
|
ICore::addContextObject(context);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-24 17:41:53 +01:00
|
|
|
void StatusBarManager::destroyStatusBarWidget(QWidget *widget)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2018-01-24 17:41:53 +01:00
|
|
|
QTC_ASSERT(widget, return);
|
2020-08-04 16:52:09 +02:00
|
|
|
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);
|
2018-01-24 17:41:53 +01:00
|
|
|
}
|
|
|
|
|
widget->setParent(nullptr);
|
|
|
|
|
delete widget;
|
2014-02-27 17:26:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2022-05-02 17:25:11 +02:00
|
|
|
const QList<int> sizes = m_splitter->sizes();
|
|
|
|
|
for (const int w : sizes)
|
2014-02-27 17:26:04 +01:00
|
|
|
sum += w;
|
|
|
|
|
m_splitter->setSizes(QList<int>() << leftSplitWidth << (sum - leftSplitWidth));
|
|
|
|
|
}
|
2018-01-24 17:41:53 +01:00
|
|
|
|
2018-07-10 15:49:50 +02:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 17:41:53 +01:00
|
|
|
} // Core
|