Files
qt-creator/src/plugins/qmldesigner/components/toolbar/toolbar.cpp
Marco Bubke 48ad79ee1e Utils: Introduce UniqueObjectPtr
We very often want to remove a Qobject in a scope but because it has a
parent it can be deleted earlier. So using std::unique_ptr can lead to
double deletion. UniqueObjectPtr can track if a pointer is already
deleted. So no double deletion is possible but we still delete the
object in that scope. UniqueObjectPtr is based on std::unique_ptr but
uses a QPointer as internal pointer representation.

Because QPointer is not convertable you cannot cast from one type to
an other(QTBUG-112464). Because of that UniqueObjectInternalPointer
derives from QPointer and adds a conversion constructor.

Change-Id: I2c7707489f6db836cc5db2463efa8c33932b6455
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
2023-04-03 12:48:11 +00:00

137 lines
4.0 KiB
C++

// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#include "toolbar.h"
#include "toolbarbackend.h"
#include <studioquickwidget.h>
#include <theme.h>
#include <qmldesignerconstants.h>
#include <coreplugin/icore.h>
#include <utils/filepath.h>
#include <utils/qtcassert.h>
#include <QMainWindow>
#include <QQmlEngine>
#include <QStatusBar>
#include <QToolBar>
namespace QmlDesigner {
static Utils::FilePath propertyEditorResourcesPath()
{
#ifdef SHARE_QML_PATH
if (qEnvironmentVariableIsSet("LOAD_QML_FROM_SOURCE"))
return Utils::FilePath::fromString(QLatin1String(SHARE_QML_PATH) + "/propertyEditorQmlSources");
#endif
return Core::ICore::resourcePath("qmldesigner/propertyEditorQmlSources");
}
Utils::FilePath qmlSourcesStatusBarPath()
{
#ifdef SHARE_QML_PATH
if (qEnvironmentVariableIsSet("LOAD_QML_FROM_SOURCE"))
return Utils::FilePath::fromString(QLatin1String(SHARE_QML_PATH) + "/statusbar");
#endif
return Core::ICore::resourcePath("qmldesigner/statusbar");
}
Utils::FilePath qmlSourcesPath()
{
#ifdef SHARE_QML_PATH
if (qEnvironmentVariableIsSet("LOAD_QML_FROM_SOURCE"))
return Utils::FilePath::fromString(QLatin1String(SHARE_QML_PATH) + "/toolbar");
#endif
return Core::ICore::resourcePath("qmldesigner/toolbar");
}
Utils::UniqueObjectPtr<QToolBar> ToolBar::create()
{
if (!isVisible())
return nullptr;
ToolBarBackend::registerDeclarativeType();
auto window = Core::ICore::mainWindow();
//Core::ICore::statusBar()->hide();
auto toolBar = Utils::makeUniqueObjectPtr<QToolBar>();
toolBar->setObjectName("QDS-TOOLBAR");
toolBar->setContextMenuPolicy(Qt::PreventContextMenu);
toolBar->setFloatable(false);
toolBar->setMovable(false);
auto quickWidget = std::make_unique<StudioQuickWidget>();
quickWidget->setFixedHeight(48);
quickWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
quickWidget->setMinimumWidth(200);
quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
quickWidget->quickWidget()->setObjectName(Constants::OBJECT_NAME_TOP_TOOLBAR);
quickWidget->engine()->addImportPath(propertyEditorResourcesPath().toString() + "/imports");
Utils::FilePath qmlFilePath = qmlSourcesPath() / "Main.qml";
QTC_ASSERT(qmlFilePath.exists(), return nullptr);
Theme::setupTheme(quickWidget->engine());
quickWidget->setSource(QUrl::fromLocalFile(qmlFilePath.toFSPathString()));
toolBar->addWidget(quickWidget.release());
window->addToolBar(toolBar.get());
return toolBar;
}
Utils::UniqueObjectPtr<QWidget> ToolBar::createStatusBar()
{
if (!isVisible())
return nullptr;
ToolBarBackend::registerDeclarativeType();
auto quickWidget = Utils::makeUniqueObjectPtr<StudioQuickWidget>();
quickWidget->setFixedHeight(Theme::toolbarSize());
quickWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
quickWidget->setMinimumWidth(200);
quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
quickWidget->quickWidget()->setObjectName(Constants::OBJECT_NAME_STATUSBAR);
quickWidget->engine()->addImportPath(propertyEditorResourcesPath().toString() + "/imports");
Utils::FilePath qmlFilePath = qmlSourcesStatusBarPath().pathAppended("/Main.qml");
QTC_ASSERT(qmlFilePath.exists(), return nullptr);
Theme::setupTheme(quickWidget->engine());
quickWidget->setSource(QUrl::fromLocalFile(qmlFilePath.toFSPathString()));
for (QWidget *w : Core::ICore::statusBar()->findChildren<QWidget *>(Qt::FindDirectChildrenOnly)) {
w->hide();
}
Core::ICore::statusBar()->addPermanentWidget(quickWidget.get(), 100);
Core::ICore::statusBar()->setFixedHeight(Theme::toolbarSize());
return quickWidget;
}
bool ToolBar::isVisible()
{
QSettings *settings = Core::ICore::settings();
const QString qdsToolbarEntry = "QML/Designer/TopToolBar";
return settings->value(qdsToolbarEntry, false).toBool();
}
} // namespace QmlDesigner