OSDN Git Service

0fb87afda864c903e009a2dd4701e8bd47165e58
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / integration / designdocumentcontrollerview.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "designdocumentcontrollerview.h"
35 #include <rewriterview.h>
36 #include <basetexteditmodifier.h>
37 #include <metainfo.h>
38 #include <plaintexteditmodifier.h>
39
40 #include <QApplication>
41 #include <QPlainTextEdit>
42 #include <QClipboard>
43 #include <QDebug>
44
45 namespace QmlDesigner {
46
47 void DesignDocumentControllerView::nodeCreated(const ModelNode & /*createdNode*/) {}
48 void DesignDocumentControllerView::nodeAboutToBeRemoved(const ModelNode & /*removedNode*/) {}
49 void DesignDocumentControllerView::nodeRemoved(const ModelNode & /*removedNode*/, const NodeAbstractProperty & /*parentProperty*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {}
50 void DesignDocumentControllerView::nodeAboutToBeReparented(const ModelNode & /*node*/, const NodeAbstractProperty & /*newPropertyParent*/, const NodeAbstractProperty & /*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {}
51 void DesignDocumentControllerView::nodeReparented(const ModelNode & /*node*/, const NodeAbstractProperty & /*newPropertyParent*/, const NodeAbstractProperty & /*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {}
52 void DesignDocumentControllerView::nodeIdChanged(const ModelNode& /*node*/, const QString& /*newId*/, const QString& /*oldId*/) {}
53 void DesignDocumentControllerView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& /*propertyList*/) {}
54 void DesignDocumentControllerView::propertiesRemoved(const QList<AbstractProperty>& /*propertyList*/) {}
55 void DesignDocumentControllerView::variantPropertiesChanged(const QList<VariantProperty>& /*propertyList*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {}
56 void DesignDocumentControllerView::bindingPropertiesChanged(const QList<BindingProperty>& /*propertyList*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {}
57 void DesignDocumentControllerView::rootNodeTypeChanged(const QString & /*type*/, int /*majorVersion*/, int /*minorVersion*/) {}
58
59 void DesignDocumentControllerView::selectedNodesChanged(const QList<ModelNode> & /*selectedNodeList*/,
60                           const QList<ModelNode> & /*lastSelectedNodeList*/) {}
61
62 void DesignDocumentControllerView::nodeOrderChanged(const NodeListProperty & /*listProperty*/, const ModelNode & /*movedNode*/, int /*oldIndex*/) {}
63
64 void DesignDocumentControllerView::scriptFunctionsChanged(const ModelNode &/*node*/, const QStringList &/*scriptFunctionList*/)
65 {
66 }
67
68 void DesignDocumentControllerView::instancePropertyChange(const QList<QPair<ModelNode, QString> > &/*propertyList*/)
69 {
70 }
71
72 void DesignDocumentControllerView::instancesCompleted(const QVector<ModelNode> &/*completedNodeList*/)
73 {
74
75 }
76 void DesignDocumentControllerView::instanceInformationsChange(const QVector<ModelNode> &/*nodeList*/)
77 {
78
79 }
80
81 void DesignDocumentControllerView::instancesRenderImageChanged(const QVector<ModelNode> &/*nodeList*/)
82 {
83
84 }
85
86 void DesignDocumentControllerView::instancesPreviewImageChanged(const QVector<ModelNode> &/*nodeList*/)
87 {
88
89 }
90
91 void DesignDocumentControllerView::instancesChildrenChanged(const QVector<ModelNode> &/*nodeList*/)
92 {
93
94 }
95
96
97 void DesignDocumentControllerView::rewriterBeginTransaction()
98 {
99
100 }
101
102 void DesignDocumentControllerView::rewriterEndTransaction()
103 {
104 }
105
106 void DesignDocumentControllerView::actualStateChanged(const ModelNode &/*node*/)
107 {
108 }
109
110 void DesignDocumentControllerView::importsChanged(const QList<Import> &/*addedImports*/, const QList<Import> &/*removedImports*/)
111 {
112
113 }
114
115 static QStringList arrayToStringList(const QByteArray &byteArray)
116 {
117     QString str(QString::fromLatin1(byteArray));
118     return str.split('\n');
119 }
120
121 static QByteArray stringListToArray(const QStringList &stringList)
122 {
123     QString str;
124     foreach (const QString &subString, stringList)
125         str += subString + '\n';
126     return str.toLatin1();
127 }
128
129 void DesignDocumentControllerView::toClipboard() const
130 {
131     QClipboard *clipboard = QApplication::clipboard();
132
133     QMimeData *data = new QMimeData;
134
135     data->setText(toText());
136     QStringList imports;
137     foreach (const Import &import, model()->imports())
138         imports.append(import.toString());
139
140     data->setData("QmlDesigner::imports", stringListToArray(imports));
141     clipboard->setMimeData(data);
142 }
143
144 void DesignDocumentControllerView::fromClipboard()
145 {
146     QClipboard *clipboard = QApplication::clipboard();
147     fromText(clipboard->text());
148     QStringList imports = arrayToStringList(clipboard->mimeData()->data("QmlDesigner::imports"));
149 //    foreach (const QString &importString, imports) {
150 //        Import import(Import::createLibraryImport();
151 //        model()->addImport(import); //### imports
152 //    }
153 }
154
155
156 QString DesignDocumentControllerView::toText() const
157 {
158     QScopedPointer<Model> outputModel(Model::create("QtQuick.Rectangle", 1, 0, model()));
159     QPlainTextEdit textEdit;
160
161     QString imports;
162     foreach (const Import &import, model()->imports()) {
163         if (import.isFileImport())
164             imports += QLatin1String("import ") + QLatin1String("\"") + import.file() + QLatin1String("\"")+ QLatin1String(";\n");
165         else
166             imports += QLatin1String("import ") + import.url() + QLatin1String(" ") + import.version() + QLatin1String(";\n");
167     }
168
169     textEdit.setPlainText(imports +  QLatin1String("Item {\n}\n"));
170     NotIndentingTextEditModifier modifier(&textEdit);
171
172     QScopedPointer<RewriterView> rewriterView(new RewriterView(RewriterView::Amend, 0));
173     rewriterView->setCheckSemanticErrors(false);
174     rewriterView->setTextModifier(&modifier);
175     outputModel->attachView(rewriterView.data());
176
177     ModelMerger merger(rewriterView.data());
178
179     merger.replaceModel(rootModelNode());
180
181     ModelNode rewriterNode(rewriterView->rootModelNode());
182
183     //get the text of the root item without imports
184     return rewriterView->extractText(QList<ModelNode>() << rewriterNode).value(rewriterNode);
185 }
186
187 void DesignDocumentControllerView::fromText(QString text)
188 {
189     QScopedPointer<Model> inputModel(Model::create("QtQuick.Rectangle", 1, 0, model()));
190     inputModel->setFileUrl(model()->fileUrl());
191     QPlainTextEdit textEdit;
192     QString imports;
193     foreach (Import import, model()->imports())
194         imports += import.toString() + ";\n";
195
196     textEdit.setPlainText(imports + text);
197     NotIndentingTextEditModifier modifier(&textEdit);
198
199     QScopedPointer<RewriterView> rewriterView(new RewriterView(RewriterView::Amend, 0));
200     rewriterView->setCheckSemanticErrors(false);
201     rewriterView->setTextModifier(&modifier);
202     inputModel->attachView(rewriterView.data());
203
204     if (rewriterView->errors().isEmpty() && rewriterView->rootModelNode().isValid()) {
205         ModelMerger merger(this);
206         merger.replaceModel(rewriterView->rootModelNode());
207     }
208 }
209
210 }// namespace QmlDesigner