1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the tools applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qmlstreamwriter.h"
44 #include <QtCore/QBuffer>
45 #include <QtCore/QStringList>
47 QmlStreamWriter::QmlStreamWriter(QByteArray *array)
49 , m_pendingLineLength(0)
50 , m_maybeOneline(false)
51 , m_stream(new QBuffer(array))
53 m_stream->open(QIODevice::WriteOnly);
56 void QmlStreamWriter::writeStartDocument()
60 void QmlStreamWriter::writeEndDocument()
64 void QmlStreamWriter::writeLibraryImport(const QString &uri, int majorVersion, int minorVersion, const QString &as)
66 m_stream->write(QString("import %1 %2.%3").arg(uri, QString::number(majorVersion), QString::number(minorVersion)).toUtf8());
68 m_stream->write(QString(" as %1").arg(as).toUtf8());
69 m_stream->write("\n");
72 void QmlStreamWriter::writeStartObject(const QString &component)
74 flushPotentialLinesWithNewlines();
76 m_stream->write(QString("%1 {").arg(component).toUtf8());
78 m_maybeOneline = true;
81 void QmlStreamWriter::writeEndObject()
83 if (m_maybeOneline && !m_pendingLines.isEmpty()) {
85 for (int i = 0; i < m_pendingLines.size(); ++i) {
87 m_stream->write(m_pendingLines.at(i).trimmed());
88 if (i != m_pendingLines.size() - 1)
91 m_stream->write(" }\n");
92 m_pendingLines.clear();
93 m_pendingLineLength = 0;
94 m_maybeOneline = false;
96 flushPotentialLinesWithNewlines();
99 m_stream->write("}\n");
103 void QmlStreamWriter::writeScriptBinding(const QString &name, const QString &rhs)
105 writePotentialLine(QString("%1: %2").arg(name, rhs).toUtf8());
108 void QmlStreamWriter::writeArrayBinding(const QString &name, const QStringList &elements)
110 flushPotentialLinesWithNewlines();
113 // try to use a single line
115 singleLine += QString("%1: [").arg(name);
116 for (int i = 0; i < elements.size(); ++i) {
117 singleLine += elements.at(i);
118 if (i != elements.size() - 1)
119 singleLine += QLatin1String(", ");
121 singleLine += QLatin1String("]\n");
122 if (singleLine.size() + m_indentDepth * 4 < 80) {
123 m_stream->write(singleLine.toUtf8());
128 m_stream->write(QString("%1: [\n").arg(name).toUtf8());
130 for (int i = 0; i < elements.size(); ++i) {
132 m_stream->write(elements.at(i).toUtf8());
133 if (i != elements.size() - 1) {
134 m_stream->write(",\n");
136 m_stream->write("\n");
141 m_stream->write("]\n");
144 void QmlStreamWriter::write(const QString &data)
146 flushPotentialLinesWithNewlines();
147 m_stream->write(data.toUtf8());
150 void QmlStreamWriter::writeScriptObjectLiteralBinding(const QString &name, const QList<QPair<QString, QString> > &keyValue)
152 flushPotentialLinesWithNewlines();
154 m_stream->write(QString("%1: {\n").arg(name).toUtf8());
156 for (int i = 0; i < keyValue.size(); ++i) {
157 const QString key = keyValue.at(i).first;
158 const QString value = keyValue.at(i).second;
160 m_stream->write(QString("%1: %2").arg(key, value).toUtf8());
161 if (i != keyValue.size() - 1) {
162 m_stream->write(",\n");
164 m_stream->write("\n");
169 m_stream->write("}\n");
172 void QmlStreamWriter::writeIndent()
174 m_stream->write(QByteArray(m_indentDepth * 4, ' '));
177 void QmlStreamWriter::writePotentialLine(const QByteArray &line)
179 m_pendingLines.append(line);
180 m_pendingLineLength += line.size();
181 if (m_pendingLineLength >= 80) {
182 flushPotentialLinesWithNewlines();
186 void QmlStreamWriter::flushPotentialLinesWithNewlines()
189 m_stream->write("\n");
190 foreach (const QByteArray &line, m_pendingLines) {
192 m_stream->write(line);
193 m_stream->write("\n");
195 m_pendingLines.clear();
196 m_pendingLineLength = 0;
197 m_maybeOneline = false;