OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / tests / auto / qml / qmldesigner / common / statichelpers.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 //static void testMessageOutput(QtMsgType type, const char *msg)
34 // {
35 //     switch (type) {
36 //     case QtDebugMsg:
37 //#ifdef QDEBUG_IN_TESTS
38 //         std::fprintf(stderr, "Debug: %s\n", msg);
39 //#endif // QDEBUG_IN_TESTS
40 //         break;
41 //     case QtWarningMsg:
42 //#ifdef WARNINGS_IN_TESTS
43 //         std::fprintf(stderr, "Warning: %s\n", msg);
44 //#endif // WARNINGS_IN_TESTS
45 //         break;
46 //     case QtCriticalMsg:
47 //         std::fprintf(stderr, "Critical: %s\n", msg);
48 //         break;
49 //     case QtFatalMsg:
50 //         std::fprintf(stderr, "Fatal: %s\n", msg);
51 //         break;
52 //     }
53 // }
54
55 static ModelNode addNodeListChild(const ModelNode &parentNode, const QString &typeName, int major, int minor, const QString &parentProperty)
56 {
57     ModelNode newNode = parentNode.view()->createModelNode(typeName, major, minor);
58     parentNode.nodeListProperty(parentProperty).reparentHere(newNode);
59     return newNode;
60 }
61
62 static ModelNode addNodeChild(const ModelNode &parentNode, const QString &typeName, int major, int minor, const QString &parentProperty)
63 {
64     ModelNode newNode = parentNode.view()->createModelNode(typeName, major, minor);
65     parentNode.nodeProperty(parentProperty).reparentHere(newNode);
66     return newNode;
67 }
68
69 static QString bareTemplate("import Qt 4.6\n"
70                             "Item { id: parentItem;"
71                             "  %1"
72                             "}");
73 static QString contentsTemplate(bareTemplate.arg("Text { id: textChild; x:10; y: 10; text: \"%1\"; %2 }"));
74
75
76 void printErrors(const QList<QDeclarativeError> &errors, const QString &fileName)
77 {
78     if (errors.isEmpty())
79         return;
80
81     qDebug() << "Error loading file \"" << fileName << "\":";
82
83     foreach (const QDeclarativeError &error, errors) {
84         qDebug() << error.line() << ":" << error.column() << ": " << error.description();
85     }
86 }
87
88 // TODO: this need to e updated for states
89 static bool compareProperty(const AbstractProperty &property1, const AbstractProperty &property2)
90 {
91     return (property1.name() == property2.name());
92 //            && (property1.value().type() == property2.value().type());
93 //            && (property1.value() == property2.value()));
94 }
95
96 // TODO: this need to e updated for states
97 static bool compareTree(const ModelNode &node1, const ModelNode &node2)
98 {
99     if (!node1.isValid() || !node2.isValid()) {
100         return false;
101     }
102
103     if (node1.type() != node2.type()) {
104         return false;
105     }
106
107     // Compare properties
108     {
109         const QList<AbstractProperty> propList1 = node1.properties();
110         const QList<AbstractProperty> propList2 = node2.properties();
111
112         QList<AbstractProperty>::const_iterator iter1 = propList1.constBegin();
113         QList<AbstractProperty>::const_iterator iter2 = propList2.constBegin();
114         for (;
115              iter1 != propList1.constEnd() && iter2 != propList2.constEnd();
116              iter1++, iter2++) {
117             if (!compareProperty(*iter1, *iter2))
118                 return false;
119         }
120
121         if (iter1 != propList1.constEnd() || iter2 != propList2.constEnd())
122             return false;
123     }
124
125     // Compare list of children
126     {
127         const QList<ModelNode> childList1 = node1.allDirectSubModelNodes();
128         const QList<ModelNode> childList2 = node2.allDirectSubModelNodes();
129
130         QList<ModelNode>::const_iterator iter1;
131         QList<ModelNode>::const_iterator iter2;
132         for (iter1 = childList1.constBegin(), iter2 = childList2.constBegin();
133              iter1 != childList1.constEnd() && iter2 != childList2.constEnd();
134              iter1++, iter2++) {
135             if (!compareTree((*iter1), (*iter2)))
136                 return false;
137         }
138
139         if (iter1 != childList1.constEnd() || iter2 != childList2.constEnd())
140             return false;
141     }
142     return true;
143 }
144
145 //void load(const QString &data, Model *&model, ByteArrayModifier *&modifier)
146 //{
147 //    model = 0;
148 //    QByteArray bytes = data.toLatin1();
149 //    QBuffer file(&bytes, 0);
150 //    QVERIFY(file.open(QIODevice::ReadOnly));
151 //    QList<QDeclarativeError> errors;
152 //    QString fileText(file.readAll());
153 //    modifier = ByteArrayModifier::create(QString(fileText));
154 //    model = Model::create(modifier, QUrl(), &errors);
155 //
156 //    if (!errors.isEmpty()) {
157 //        printErrors(errors, "<inline>");
158 //    }
159 //
160 //    file.close();
161 //}
162
163 //void reload(const QString &data, ByteArrayModifier *modifier)
164 //{
165 //    modifier->setText(data);
166 //}
167
168 //static Model* create(const QString& document)
169 //{
170 //    ByteArrayModifier* modifier = 0;
171 //    Model *model = 0;
172 //
173 //    load(document, model, modifier);
174 //
175 //    if (modifier && model)
176 //        modifier->setParent(model);
177 //
178 //    return model;
179 //}