viewmanager.cpp 9.17 KB
Newer Older
Nikolai Kosjar's avatar
Nikolai Kosjar committed
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Nikolai Kosjar's avatar
Nikolai Kosjar committed
5 6 7 8 9 10 11
**
** 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
12 13 14
** 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.
Nikolai Kosjar's avatar
Nikolai Kosjar committed
15
**
16 17
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
18 19 20 21 22
** 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.
Nikolai Kosjar's avatar
Nikolai Kosjar committed
23 24 25
**
****************************************************************************/

Marco Bubke's avatar
Marco Bubke committed
26 27
#include "viewmanager.h"

28 29
#ifndef QMLDESIGNER_TEST

30 31 32 33 34 35 36 37 38 39 40 41
#include <rewriterview.h>
#include <nodeinstanceview.h>
#include <itemlibraryview.h>
#include <navigatorview.h>
#include <stateseditorview.h>
#include <formeditorview.h>
#include <propertyeditorview.h>
#include <componentview.h>
#include <debugview.h>
#include <importmanagerview.h>
#include <designeractionmanagerview.h>

Marco Bubke's avatar
Marco Bubke committed
42
#include "componentaction.h"
43 44
#include "designmodewidget.h"
#include "crumblebar.h"
Marco Bubke's avatar
Marco Bubke committed
45 46

#include <qmldesigner/qmldesignerplugin.h>
47
#include <utils/algorithm.h>
Marco Bubke's avatar
Marco Bubke committed
48 49 50

namespace QmlDesigner {

51 52 53 54 55 56 57 58 59 60 61 62 63 64
class ViewManagerData
{
public:
    QmlModelState savedState;
    Internal::DebugView debugView;
    ComponentView componentView;
    FormEditorView formEditorView;
    ItemLibraryView itemLibraryView;
    NavigatorView navigatorView;
    PropertyEditorView propertyEditorView;
    StatesEditorView statesEditorView;
    NodeInstanceView nodeInstanceView;
    DesignerActionManagerView designerActionManagerView;

65
    QList<QPointer<AbstractView> > additionalViews;
66 67
};

68 69 70 71 72

static CrumbleBar *crumbleBar() {
    return QmlDesignerPlugin::instance()->mainWidget()->crumbleBar();
}

Marco Bubke's avatar
Marco Bubke committed
73
ViewManager::ViewManager()
74
    : d(new ViewManagerData)
Marco Bubke's avatar
Marco Bubke committed
75 76 77
{
}

78 79
ViewManager::~ViewManager()
{
80
    foreach (const QPointer<AbstractView> &view, d->additionalViews)
81
        delete view.data();
82 83

    delete d;
84 85
}

Marco Bubke's avatar
Marco Bubke committed
86 87 88 89 90 91 92
DesignDocument *ViewManager::currentDesignDocument() const
{
    return QmlDesignerPlugin::instance()->documentManager().currentDesignDocument();
}

void ViewManager::attachNodeInstanceView()
{
93
    setNodeInstanceViewKit(currentDesignDocument()->currentKit());
94
    currentModel()->setNodeInstanceView(&d->nodeInstanceView);
Marco Bubke's avatar
Marco Bubke committed
95 96
}

97
void ViewManager::attachRewriterView()
Marco Bubke's avatar
Marco Bubke committed
98 99 100
{
    if (currentDesignDocument()->rewriterView()) {
        currentModel()->setRewriterView(currentDesignDocument()->rewriterView());
101
        currentDesignDocument()->rewriterView()->reactivateTextMofifierChangeSignals();
Marco Bubke's avatar
Marco Bubke committed
102 103 104 105 106 107 108 109 110 111 112 113 114
    }
}

void ViewManager::detachRewriterView()
{
    if (currentDesignDocument()->rewriterView()) {
        currentDesignDocument()->rewriterView()->deactivateTextMofifierChangeSignals();
        currentModel()->setRewriterView(0);
    }
}

void ViewManager::switchStateEditorViewToBaseState()
{
115 116 117
    if (d->statesEditorView.isAttached()) {
        d->savedState = d->statesEditorView.currentState();
        d->statesEditorView.setCurrentState(d->statesEditorView.baseState());
Marco Bubke's avatar
Marco Bubke committed
118 119 120 121 122
    }
}

void ViewManager::switchStateEditorViewToSavedState()
{
123 124
    if (d->savedState.isValid() && d->statesEditorView.isAttached())
        d->statesEditorView.setCurrentState(d->savedState);
Marco Bubke's avatar
Marco Bubke committed
125 126 127 128
}

void ViewManager::resetPropertyEditorView()
{
129
    d->propertyEditorView.resetView();
Marco Bubke's avatar
Marco Bubke committed
130 131
}

132
void ViewManager::registerFormEditorToolTakingOwnership(AbstractCustomTool *tool)
133
{
134
    d->formEditorView.registerTool(tool);
135 136
}

137
void ViewManager::registerViewTakingOwnership(AbstractView *view)
138
{
139
    d->additionalViews.append(view);
140 141
}

Marco Bubke's avatar
Marco Bubke committed
142 143 144
void ViewManager::detachViewsExceptRewriterAndComponetView()
{
    switchStateEditorViewToBaseState();
145
    detachAdditionalViews();
146 147 148 149 150 151
    currentModel()->detachView(&d->designerActionManagerView);
    currentModel()->detachView(&d->formEditorView);
    currentModel()->detachView(&d->navigatorView);
    currentModel()->detachView(&d->itemLibraryView);
    currentModel()->detachView(&d->statesEditorView);
    currentModel()->detachView(&d->propertyEditorView);
152

153 154
    if (d->debugView.isAttached())
        currentModel()->detachView(&d->debugView);
155

Marco Bubke's avatar
Marco Bubke committed
156 157 158 159 160
    currentModel()->setNodeInstanceView(0);
}

void ViewManager::attachItemLibraryView()
{
161
    setItemLibraryViewResourcePath(currentDesignDocument()->fileName().toFileInfo().absolutePath());
162
    currentModel()->attachView(&d->itemLibraryView);
Marco Bubke's avatar
Marco Bubke committed
163 164
}

165 166
void ViewManager::attachAdditionalViews()
{
167
    foreach (const QPointer<AbstractView> &view, d->additionalViews)
168 169 170 171 172
        currentModel()->attachView(view.data());
}

void ViewManager::detachAdditionalViews()
{
173
    foreach (const QPointer<AbstractView> &view, d->additionalViews)
174 175 176
        currentModel()->detachView(view.data());
}

Marco Bubke's avatar
Marco Bubke committed
177 178
void ViewManager::attachComponentView()
{
179 180 181
    documentModel()->attachView(&d->componentView);
    QObject::connect(d->componentView.action(), SIGNAL(currentComponentChanged(ModelNode)), currentDesignDocument(), SLOT(changeToSubComponent(ModelNode)));
    QObject::connect(d->componentView.action(), SIGNAL(changedToMaster()), currentDesignDocument(), SLOT(changeToMaster()));
Marco Bubke's avatar
Marco Bubke committed
182 183 184 185
}

void ViewManager::detachComponentView()
{
186 187
    QObject::disconnect(d->componentView.action(), SIGNAL(currentComponentChanged(ModelNode)), currentDesignDocument(), SLOT(changeToSubComponent(ModelNode)));
    QObject::disconnect(d->componentView.action(), SIGNAL(changedToMaster()), currentDesignDocument(), SLOT(changeToMaster()));
188

189
    documentModel()->detachView(&d->componentView);
Marco Bubke's avatar
Marco Bubke committed
190 191 192 193
}

void ViewManager::attachViewsExceptRewriterAndComponetView()
{
194 195
    if (QmlDesignerPlugin::instance()->settings().value(
            DesignerSettingsKey::ENABLE_DEBUGVIEW).toBool())
196
        currentModel()->attachView(&d->debugView);
197

Marco Bubke's avatar
Marco Bubke committed
198
    attachNodeInstanceView();
199 200
    currentModel()->attachView(&d->formEditorView);
    currentModel()->attachView(&d->navigatorView);
Marco Bubke's avatar
Marco Bubke committed
201
    attachItemLibraryView();
202 203 204
    currentModel()->attachView(&d->statesEditorView);
    currentModel()->attachView(&d->propertyEditorView);
    currentModel()->attachView(&d->designerActionManagerView);
205
    attachAdditionalViews();
Marco Bubke's avatar
Marco Bubke committed
206 207 208 209 210
    switchStateEditorViewToSavedState();
}

void ViewManager::setItemLibraryViewResourcePath(const QString &resourcePath)
{
211
    d->itemLibraryView.setResourcePath(resourcePath);
Marco Bubke's avatar
Marco Bubke committed
212 213 214 215
}

void ViewManager::setComponentNode(const ModelNode &componentNode)
{
216
    d->componentView.setComponentNode(componentNode);
Marco Bubke's avatar
Marco Bubke committed
217 218
}

219 220
void ViewManager::setComponentViewToMaster()
{
221
    d->componentView.setComponentToMaster();
222 223
}

224
void ViewManager::setNodeInstanceViewKit(ProjectExplorer::Kit *kit)
Marco Bubke's avatar
Marco Bubke committed
225
{
226
    d->nodeInstanceView.setKit(kit);
Marco Bubke's avatar
Marco Bubke committed
227 228
}

229
QList<WidgetInfo> ViewManager::widgetInfos()
Marco Bubke's avatar
Marco Bubke committed
230
{
231
    QList<WidgetInfo> widgetInfoList;
Marco Bubke's avatar
Marco Bubke committed
232

233 234 235 236 237 238 239
    widgetInfoList.append(d->formEditorView.widgetInfo());
    widgetInfoList.append(d->itemLibraryView.widgetInfo());
    widgetInfoList.append(d->navigatorView.widgetInfo());
    widgetInfoList.append(d->propertyEditorView.widgetInfo());
    widgetInfoList.append(d->statesEditorView.widgetInfo());
    if (d->debugView.hasWidget())
        widgetInfoList.append(d->debugView.widgetInfo());
240

241
    foreach (const QPointer<AbstractView> &abstractView, d->additionalViews) {
242 243 244 245
        if (abstractView && abstractView->hasWidget())
            widgetInfoList.append(abstractView->widgetInfo());
    }

246 247 248
    Utils::sort(widgetInfoList, [](const WidgetInfo &firstWidgetInfo, const WidgetInfo &secondWidgetInfo) {
        return firstWidgetInfo.placementPriority < secondWidgetInfo.placementPriority;
    });
249 250

    return widgetInfoList;
Marco Bubke's avatar
Marco Bubke committed
251 252
}

253
void ViewManager::disableWidgets()
Marco Bubke's avatar
Marco Bubke committed
254
{
255 256
    foreach (const WidgetInfo &widgetInfo, widgetInfos())
        widgetInfo.widget->setEnabled(false);
Marco Bubke's avatar
Marco Bubke committed
257 258
}

259
void ViewManager::enableWidgets()
Marco Bubke's avatar
Marco Bubke committed
260
{
261 262
    foreach (const WidgetInfo &widgetInfo, widgetInfos())
        widgetInfo.widget->setEnabled(true);
Marco Bubke's avatar
Marco Bubke committed
263 264
}

265
void ViewManager::pushFileOnCrumbleBar(const QString &fileName)
Marco Bubke's avatar
Marco Bubke committed
266
{
267
    crumbleBar()->pushFile(fileName);
Marco Bubke's avatar
Marco Bubke committed
268 269
}

270
void ViewManager::pushInFileComponentOnCrumbleBar(const ModelNode &modelNode)
Marco Bubke's avatar
Marco Bubke committed
271
{
272
    crumbleBar()->pushInFileComponent(modelNode);
Marco Bubke's avatar
Marco Bubke committed
273 274 275 276
}

void ViewManager::nextFileIsCalledInternally()
{
277
    crumbleBar()->nextFileIsCalledInternally();
Marco Bubke's avatar
Marco Bubke committed
278 279
}

280
NodeInstanceView *ViewManager::nodeInstanceView() const
Marco Bubke's avatar
Marco Bubke committed
281
{
282
    return &d->nodeInstanceView;
Marco Bubke's avatar
Marco Bubke committed
283
}
284

285
QWidgetAction *ViewManager::componentViewAction() const
286
{
287
    return d->componentView.action();
288 289
}

290 291
DesignerActionManager &ViewManager::designerActionManager()
{
292
    return d->designerActionManagerView.designerActionManager();
293 294 295 296
}

const DesignerActionManager &ViewManager::designerActionManager() const
{
297
    return d->designerActionManagerView.designerActionManager();
298 299
}

Marco Bubke's avatar
Marco Bubke committed
300 301 302 303 304 305 306 307 308 309 310
Model *ViewManager::currentModel() const
{
    return currentDesignDocument()->currentModel();
}

Model *ViewManager::documentModel() const
{
    return currentDesignDocument()->documentModel();
}

} // namespace QmlDesigner
311 312

#endif //QMLDESIGNER_TEST