aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/assetexporterplugin/assetexporterview.cpp
blob: 80ab776d852034eecaf0003d92c261b524955b70 (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
133
134
135
136
137
138
139
140
// Copyright (C) 2020 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 "assetexporterview.h"

#include "qmlitemnode.h"
#include "rewriterview.h"

#include "coreplugin/editormanager/editormanager.h"
#include "coreplugin/editormanager/ieditor.h"
#include "coreplugin/modemanager.h"
#include "coreplugin/coreconstants.h"

#include <QLoggingCategory>

namespace  {
Q_LOGGING_CATEGORY(loggerInfo, "qtc.designer.assetExportPlugin.view", QtInfoMsg)
Q_LOGGING_CATEGORY(loggerWarn, "qtc.designer.assetExportPlugin.view", QtWarningMsg)
//Q_LOGGING_CATEGORY(loggerError, "qtc.designer.assetExportPlugin.view", QtCriticalMsg)

static const int RetryIntervalMs = 500;
static const int MinRetry = 2;
}

namespace QmlDesigner {

AssetExporterView::AssetExporterView(QObject *parent) : AbstractView(parent),
    m_timer(this)
{
    m_timer.setInterval(RetryIntervalMs);
    // We periodically check if file is loaded.
    connect(&m_timer, &QTimer::timeout, this, &AssetExporterView::handleTimerTimeout);
}


bool AssetExporterView::loadQmlFile(const Utils::FilePath &path, uint timeoutSecs)
{
    qCDebug(loggerInfo) << "Load file" << path;
    if (loadingState() == LoadState::Busy)
        return false;

    setState(LoadState::Busy);
    m_retryCount = std::max(MinRetry, static_cast<int>((timeoutSecs * 1000) / RetryIntervalMs));
    m_currentEditor = Core::EditorManager::openEditor(path, Utils::Id(),
                                    Core::EditorManager::DoNotMakeVisible);
    Core::ModeManager::activateMode(Core::Constants::MODE_DESIGN);
    Core::ModeManager::setFocusToCurrentMode();
    m_timer.start();
    return true;
}

bool AssetExporterView::saveQmlFile(QString *error) const
{
    if (!m_currentEditor) {
        qCDebug(loggerWarn) << "Saving QML file failed. No editor.";
        return false;
    }
    return m_currentEditor->document()->save(error);
}

void AssetExporterView::modelAttached(Model *model)
{
    if (model->rewriterView() && !model->rewriterView()->errors().isEmpty())
        setState(LoadState::QmlErrorState);

    AbstractView::modelAttached(model);
}

void AssetExporterView::
instanceInformationsChanged(const QMultiHash<ModelNode, InformationName> &informationChangeHash)
{
    if (inErrorState() || loadingState() == LoadState::Loaded)
        return; // Already reached error or connected state.

    // We expect correct dimensions are available if the rootnode's
    // information change message is received.
    const auto nodes = informationChangeHash.keys();
    bool hasRootNode = std::any_of(nodes.begin(), nodes.end(), [](const ModelNode &n) {
        return n.isRootNode();
    });
    if (hasRootNode)
        handleMaybeDone();
}

void AssetExporterView::instancesPreviewImageChanged([[maybe_unused]] const QVector<ModelNode> &nodeList)
{
    emit previewChanged();
}

bool AssetExporterView::inErrorState() const
{
    return m_state == LoadState::Exausted || m_state == LoadState::QmlErrorState;
}

bool AssetExporterView::isLoaded() const
{
    return isAttached() && QmlItemNode(rootModelNode()).isValid();
}

void AssetExporterView::setState(AssetExporterView::LoadState state)
{
    if (state != m_state) {
        m_state = state;
        qCDebug(loggerInfo) << "Loading state changed" << m_state;
        if (inErrorState() || m_state == LoadState::Loaded) {
            m_timer.stop();
            // TODO: Send the loaded signal with a delay. The assumption that model attached and a
            // valid root object is enough to declare a QML file is ready is incorrect. A ideal
            // solution would be that the puppet notifies file ready signal.
            if (m_state == LoadState::Loaded)
                QTimer::singleShot(2000, this, &AssetExporterView::loadingFinished);
            else
                emit loadingError(m_state);
        }
    }
}

void AssetExporterView::handleMaybeDone()
{
    if (isLoaded())
        setState(LoadState::Loaded);
}

void AssetExporterView::handleTimerTimeout()
{
    if (!inErrorState() && loadingState() != LoadState::Loaded)
        handleMaybeDone();

    if (--m_retryCount < 0)
        setState(LoadState::Exausted);
}

}

QT_BEGIN_NAMESPACE
QDebug operator<<(QDebug os, const QmlDesigner::AssetExporterView::LoadState &s)
{
    os << static_cast<std::underlying_type<QmlDesigner::AssetExporterView::LoadState>::type>(s);
    return os;
}
QT_END_NAMESPACE