OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / share / qtcreator / qml / qmldump / qmlstreamwriter.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 // WARNING: This code is shared with the qmlplugindump tool code in Qt.
34 //          Modifications to this file need to be applied there.
35
36 #include "qmlstreamwriter.h"
37
38 #include <QtCore/QBuffer>
39 #include <QtCore/QStringList>
40
41 QmlStreamWriter::QmlStreamWriter(QByteArray *array)
42     : m_indentDepth(0)
43     , m_pendingLineLength(0)
44     , m_maybeOneline(false)
45     , m_stream(new QBuffer(array))
46 {
47     m_stream->open(QIODevice::WriteOnly);
48 }
49
50 void QmlStreamWriter::writeStartDocument()
51 {
52 }
53
54 void QmlStreamWriter::writeEndDocument()
55 {
56 }
57
58 void QmlStreamWriter::writeLibraryImport(const QString &uri, int majorVersion, int minorVersion, const QString &as)
59 {
60     m_stream->write(QString("import %1 %2.%3").arg(uri, QString::number(majorVersion), QString::number(minorVersion)).toUtf8());
61     if (!as.isEmpty())
62         m_stream->write(QString(" as %1").arg(as).toUtf8());
63     m_stream->write("\n");
64 }
65
66 void QmlStreamWriter::writeStartObject(const QString &component)
67 {
68     flushPotentialLinesWithNewlines();
69     writeIndent();
70     m_stream->write(QString("%1 {").arg(component).toUtf8());
71     ++m_indentDepth;
72     m_maybeOneline = true;
73 }
74
75 void QmlStreamWriter::writeEndObject()
76 {
77     if (m_maybeOneline && !m_pendingLines.isEmpty()) {
78         --m_indentDepth;
79         for (int i = 0; i < m_pendingLines.size(); ++i) {
80             m_stream->write(" ");
81             m_stream->write(m_pendingLines.at(i).trimmed());
82             if (i != m_pendingLines.size() - 1)
83                 m_stream->write(";");
84         }
85         m_stream->write(" }\n");
86         m_pendingLines.clear();
87         m_pendingLineLength = 0;
88         m_maybeOneline = false;
89     } else {
90         if (m_maybeOneline)
91             flushPotentialLinesWithNewlines();
92         --m_indentDepth;
93         writeIndent();
94         m_stream->write("}\n");
95     }
96 }
97
98 void QmlStreamWriter::writeScriptBinding(const QString &name, const QString &rhs)
99 {
100     writePotentialLine(QString("%1: %2").arg(name, rhs).toUtf8());
101 }
102
103 void QmlStreamWriter::writeArrayBinding(const QString &name, const QStringList &elements)
104 {
105     flushPotentialLinesWithNewlines();
106     writeIndent();
107     m_stream->write(QString("%1: [\n").arg(name).toUtf8());
108     ++m_indentDepth;
109     for (int i = 0; i < elements.size(); ++i) {
110         writeIndent();
111         m_stream->write(elements.at(i).toUtf8());
112         if (i != elements.size() - 1) {
113             m_stream->write(",\n");
114         } else {
115             m_stream->write("\n");
116         }
117     }
118     --m_indentDepth;
119     writeIndent();
120     m_stream->write("]\n");
121 }
122
123 void QmlStreamWriter::write(const QString &data)
124 {
125     flushPotentialLinesWithNewlines();
126     m_stream->write(data.toUtf8());
127 }
128
129 void QmlStreamWriter::writeScriptObjectLiteralBinding(const QString &name, const QList<QPair<QString, QString> > &keyValue)
130 {
131     flushPotentialLinesWithNewlines();
132     writeIndent();
133     m_stream->write(QString("%1: {\n").arg(name).toUtf8());
134     ++m_indentDepth;
135     for (int i = 0; i < keyValue.size(); ++i) {
136         const QString key = keyValue.at(i).first;
137         const QString value = keyValue.at(i).second;
138         writeIndent();
139         m_stream->write(QString("%1: %2").arg(key, value).toUtf8());
140         if (i != keyValue.size() - 1) {
141             m_stream->write(",\n");
142         } else {
143             m_stream->write("\n");
144         }
145     }
146     --m_indentDepth;
147     writeIndent();
148     m_stream->write("}\n");
149 }
150
151 void QmlStreamWriter::writeIndent()
152 {
153     m_stream->write(QByteArray(m_indentDepth * 4, ' '));
154 }
155
156 void QmlStreamWriter::writePotentialLine(const QByteArray &line)
157 {
158     m_pendingLines.append(line);
159     m_pendingLineLength += line.size();
160     if (m_pendingLineLength >= 80) {
161         flushPotentialLinesWithNewlines();
162     }
163 }
164
165 void QmlStreamWriter::flushPotentialLinesWithNewlines()
166 {
167     if (m_maybeOneline)
168         m_stream->write("\n");
169     foreach (const QByteArray &line, m_pendingLines) {
170         writeIndent();
171         m_stream->write(line);
172         m_stream->write("\n");
173     }
174     m_pendingLines.clear();
175     m_pendingLineLength = 0;
176     m_maybeOneline = false;
177 }