summaryrefslogtreecommitdiff
path: root/src/plugins/qmldesigner/components/toolbar/toolbar.cpp
blob: bc8625583af1af7eecc048961253f0f6a17e1193 (plain)
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
// 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");
}

void ToolBar::create()
{
    if (!isVisible())
        return;

    ToolBarBackend::registerDeclarativeType();

    auto window = Core::ICore::mainWindow();

    //Core::ICore::statusBar()->hide();

    auto toolBar = new QToolBar;
    toolBar->setObjectName("QDS-TOOLBAR");

    toolBar->setContextMenuPolicy(Qt::PreventContextMenu);

    toolBar->setFloatable(false);
    toolBar->setMovable(false);

    auto quickWidget = new StudioQuickWidget;

    quickWidget->setFixedHeight(48);
    quickWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    quickWidget->setMinimumWidth(200);
    quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);

    quickWidget->setObjectName(Constants::OBJECT_NAME_TOP_TOOLBAR);

    quickWidget->engine()->addImportPath(propertyEditorResourcesPath().toString() + "/imports");

    Utils::FilePath qmlFilePath = qmlSourcesPath() / "Main.qml";
    QTC_ASSERT(qmlFilePath.exists(), return);

    Theme::setupTheme(quickWidget->engine());

    quickWidget->setSource(QUrl::fromLocalFile(qmlFilePath.toFSPathString()));

    toolBar->addWidget(quickWidget);
    window->addToolBar(toolBar);
}

void ToolBar::createStatusBar()
{
    if (!isVisible())
        return;

    ToolBarBackend::registerDeclarativeType();

    auto quickWidget = new StudioQuickWidget;

    quickWidget->setFixedHeight(Theme::toolbarSize());
    quickWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    quickWidget->setMinimumWidth(200);
    quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);

    quickWidget->setObjectName(Constants::OBJECT_NAME_STATUSBAR);

    quickWidget->engine()->addImportPath(propertyEditorResourcesPath().toString() + "/imports");

    Utils::FilePath qmlFilePath = qmlSourcesStatusBarPath() + QStringLiteral("/Main.qml");
    QTC_ASSERT(qmlFilePath.exists(), return);

    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()->addWidget(quickWidget);
    Core::ICore::statusBar()->setFixedHeight(Theme::toolbarSize());
}

bool ToolBar::isVisible()
{
    QSettings *settings = Core::ICore::settings();
    const QString qdsToolbarEntry = "QML/Designer/TopToolBar";

    return settings->value(qdsToolbarEntry, false).toBool();
}

} // namespace QmlDesigner