modeldocument.cpp 5.37 KB
Newer Older
1
/****************************************************************************
Jochen Becher's avatar
Jochen Becher committed
2
**
3
4
** Copyright (C) 2016 Jochen Becher
** Contact: https://www.qt.io/licensing/
Jochen Becher's avatar
Jochen Becher 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.
Jochen Becher's avatar
Jochen Becher committed
15
**
16
17
18
19
20
21
22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** 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.
Jochen Becher's avatar
Jochen Becher committed
23
24
25
26
27
28
29
30
31
32
**
****************************************************************************/

#include "modeldocument.h"

#include "modeleditor_constants.h"
#include "modeleditor_plugin.h"
#include "modelsmanager.h"
#include "extdocumentcontroller.h"

33
#include "qmt/config/configcontroller.h"
Jochen Becher's avatar
Jochen Becher committed
34
35
36
37
38
39
40
41
42
43
#include "qmt/infrastructure/ioexceptions.h"
#include "qmt/model_controller/modelcontroller.h"
#include "qmt/model/mdiagram.h"
#include "qmt/project_controller/projectcontroller.h"
#include "qmt/project/project.h"

#include <coreplugin/id.h>
#include <utils/fileutils.h>

#include <QFileInfo>
44
#include <QDir>
Jochen Becher's avatar
Jochen Becher committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

namespace ModelEditor {
namespace Internal {

class ModelDocument::ModelDocumentPrivate {
public:
    ExtDocumentController *documentController = 0;
};

ModelDocument::ModelDocument(QObject *parent)
    : Core::IDocument(parent),
      d(new ModelDocumentPrivate)
{
    setId(ModelEditor::Constants::MODEL_EDITOR_ID);
    setMimeType(QLatin1String(Constants::MIME_TYPE_MODEL));
}

ModelDocument::~ModelDocument()
{
    if (d->documentController)
65
        ModelEditorPlugin::modelsManager()->releaseModel(d->documentController);
Jochen Becher's avatar
Jochen Becher committed
66
67
68
69
70
71
72
73
    delete d;
}

Core::IDocument::OpenResult ModelDocument::open(QString *errorString, const QString &fileName,
                                                const QString &realFileName)
{
    Q_UNUSED(fileName);

74
75
    OpenResult result = load(errorString, realFileName);
    return result;
Jochen Becher's avatar
Jochen Becher committed
76
77
78
79
80
81
82
83
84
85
86
87
}

bool ModelDocument::save(QString *errorString, const QString &name, bool autoSave)
{
    if (!d->documentController) {
        *errorString = tr("No model loaded. Cannot save.");
        return false;
    }

    QString actualName = filePath().toString();
    if (!name.isEmpty())
        actualName = name;
88
    d->documentController->projectController()->setFileName(actualName);
Jochen Becher's avatar
Jochen Becher committed
89
    try {
90
        d->documentController->projectController()->save();
Jochen Becher's avatar
Jochen Becher committed
91
    } catch (const qmt::Exception &ex) {
92
        *errorString = ex.errorMessage();
Jochen Becher's avatar
Jochen Becher committed
93
94
95
        return false;
    }

Jochen Becher's avatar
Jochen Becher committed
96
    if (autoSave) {
97
        d->documentController->projectController()->setModified();
Jochen Becher's avatar
Jochen Becher committed
98
    } else {
99
        setFilePath(Utils::FileName::fromString(d->documentController->projectController()->project()->fileName()));
Jochen Becher's avatar
Jochen Becher committed
100
101
102
103
104
105
        emit changed();
    }

    return true;
}

Jochen Becher's avatar
Jochen Becher committed
106
107
108
109
110
bool ModelDocument::shouldAutoSave() const
{
    return isModified();
}

111
112
113
114
115
bool ModelDocument::isModified() const
{
    return d->documentController ? d->documentController->projectController()->isModified() : false;
}

Jochen Becher's avatar
Jochen Becher committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
bool ModelDocument::isSaveAsAllowed() const
{
    return true;
}

bool ModelDocument::reload(QString *errorString, Core::IDocument::ReloadFlag flag,
                           Core::IDocument::ChangeType type)
{
    if (flag == FlagIgnore)
        return true;
    if (type == TypePermissions) {
        emit changed();
        return true;
    }
    *errorString = tr("Cannot reload model file.");
    return false;
}

ExtDocumentController *ModelDocument::documentController() const
{
    return d->documentController;
}

139
Core::IDocument::OpenResult ModelDocument::load(QString *errorString, const QString &fileName)
Jochen Becher's avatar
Jochen Becher committed
140
{
141
142
143
144
145
    d->documentController = ModelEditorPlugin::modelsManager()->createModel(this);
    connect(d->documentController, &qmt::DocumentController::changed, this, &IDocument::changed);

    try {
        d->documentController->loadProject(fileName);
146
        setFilePath(Utils::FileName::fromString(d->documentController->projectController()->project()->fileName()));
147
    } catch (const qmt::FileNotFoundException &ex) {
148
        *errorString = ex.errorMessage();
149
150
        return OpenResult::ReadError;
    } catch (const qmt::Exception &ex) {
151
        *errorString = tr("Could not open \"%1\" for reading: %2.").arg(fileName).arg(ex.errorMessage());
152
        return OpenResult::CannotHandle;
Jochen Becher's avatar
Jochen Becher committed
153
154
    }

155
156
157
158
159
160
161
162
163
164
165
    QString configPath = d->documentController->projectController()->project()->configPath();
    if (!configPath.isEmpty()) {
        QString canonicalPath = QFileInfo(QDir(QFileInfo(fileName).path()).filePath(configPath)).canonicalFilePath();
        if (!canonicalPath.isEmpty()) {
            // TODO error output on reading definition files
            d->documentController->configController()->readStereotypeDefinitions(canonicalPath);
        } else {
            // TODO error output
        }
    }

Jochen Becher's avatar
Jochen Becher committed
166
    emit contentSet();
167
    return OpenResult::Success;
Jochen Becher's avatar
Jochen Becher committed
168
169
170
171
}

} // namespace Internal
} // namespace ModelEditor