OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / metainfo / metainfo.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 #include "metainfo.h"
34
35 #include "abstractproperty.h"
36 #include "modelnode.h"
37 #include "invalidmodelnodeexception.h"
38 #include "invalidargumentexception.h"
39 #include "metainfoparser.h"
40 #include "iwidgetplugin.h"
41
42 #include "model/variantparser.h"
43 #include "pluginmanager/widgetpluginmanager.h"
44
45 #include <QtDebug>
46 #include <QPair>
47 #include <QtAlgorithms>
48
49 enum {
50     debug = false
51 };
52
53 namespace QmlDesigner {
54 namespace Internal {
55
56 class MetaInfoPrivate
57 {
58     Q_DISABLE_COPY(MetaInfoPrivate)
59 public:
60     typedef QSharedPointer<MetaInfoPrivate> Pointer;
61     typedef QWeakPointer<MetaInfoPrivate> WeakPointer;
62
63
64     MetaInfoPrivate(MetaInfo *q);
65     void clear();
66
67     void initialize();
68
69     void parseXmlFiles();
70
71     QScopedPointer<ItemLibraryInfo> m_itemLibraryInfo;
72
73     MetaInfo *m_q;
74     bool m_isInitialized;
75 };
76
77 MetaInfoPrivate::MetaInfoPrivate(MetaInfo *q) :
78         m_itemLibraryInfo(new ItemLibraryInfo()),
79         m_q(q),
80         m_isInitialized(false)
81 {
82     if (!m_q->isGlobal())
83         m_itemLibraryInfo->setBaseInfo(MetaInfo::global().itemLibraryInfo());
84 }
85
86 void MetaInfoPrivate::clear()
87 {
88     m_itemLibraryInfo->clearEntries();
89     m_isInitialized = false;
90 }
91
92 void MetaInfoPrivate::initialize()
93 {
94     parseXmlFiles();
95     m_isInitialized = true;
96 }
97
98 QString static inline stripPrefix(const QString &typeName)
99 {
100     QStringList list = typeName.split('.');
101     if (list.count() == 2)
102         return list.last();
103     return typeName;
104 }
105
106 static inline bool isDepricatedQtType(const QString &typeName)
107 {
108     if (typeName.length() < 8)
109         return false;
110
111     return typeName.contains("Qt.");
112 }
113
114 void MetaInfoPrivate::parseXmlFiles()
115 {
116     Internal::WidgetPluginManager pluginManager;
117     foreach (const QString &pluginDir, m_q->s_pluginDirs)
118         pluginManager.addPath(pluginDir);
119     QList<IWidgetPlugin *> widgetPluginList = pluginManager.instances();
120     foreach (IWidgetPlugin *plugin, widgetPluginList) {
121         Internal::MetaInfoParser parser(*m_q);
122         parser.parseFile(plugin->metaInfo());
123     }
124 }
125
126 } // namespace Internal
127
128 using QmlDesigner::Internal::MetaInfoPrivate;
129
130 MetaInfo MetaInfo::s_global;
131 QStringList MetaInfo::s_pluginDirs;
132
133
134 /*!
135 \class QmlDesigner::MetaInfo
136 \ingroup CoreModel
137 \brief The MetaInfo class provides meta information about qml types and properties.
138 */
139
140 /*!
141   \brief Constructs a copy of the given meta info.
142   */
143 MetaInfo::MetaInfo(const MetaInfo &metaInfo) :
144         m_p(metaInfo.m_p)
145 {
146 }
147
148 /*!
149   \brief Creates a meta information object with just the qml types registered statically.
150   You almost always want to use Model::metaInfo() instead!
151
152   You almost certainly want to access the meta information for the model.
153
154   \see Model::metaInfo()
155   */
156 MetaInfo::MetaInfo() :
157         m_p(new MetaInfoPrivate(this))
158 {
159 }
160
161 MetaInfo::~MetaInfo()
162 {
163 }
164
165 /*!
166   \brief Assigns other to this meta information and returns a reference to this meta information.
167   */
168 MetaInfo& MetaInfo::operator=(const MetaInfo &other)
169 {
170     m_p = other.m_p;
171     return *this;
172 }
173
174 ItemLibraryInfo *MetaInfo::itemLibraryInfo() const
175 {
176     return m_p->m_itemLibraryInfo.data();
177 }
178
179 /*!
180   \brief Access to the global meta information object.
181   You almost always want to use Model::metaInfo() instead.
182
183   Internally all meta information objects share this "global" object
184   where static qml type information is stored.
185   */
186 MetaInfo MetaInfo::global()
187 {
188     if (!s_global.m_p->m_isInitialized) {
189         s_global.m_p = QSharedPointer<MetaInfoPrivate>(new MetaInfoPrivate(&s_global));
190         s_global.m_p->initialize();
191     }
192     return s_global;
193 }
194
195 /*!
196   \brief Clears the global meta information object.
197
198   This method should be called once on application shutdown to free static data structures.
199   */
200 void MetaInfo::clearGlobal()
201 {
202     if (s_global.m_p->m_isInitialized) {
203         s_global.m_p->clear();
204     }
205 }
206
207 void MetaInfo::setPluginPaths(const QStringList &paths)
208 {
209     s_pluginDirs = paths;
210 }
211
212 bool MetaInfo::isGlobal() const
213 {
214     return (this->m_p == s_global.m_p);
215 }
216
217 bool operator==(const MetaInfo &first, const MetaInfo &second)
218 {
219     return first.m_p == second.m_p;
220 }
221
222 bool operator!=(const MetaInfo &first, const MetaInfo &second)
223 {
224     return !(first == second);
225 }
226
227 } //namespace QmlDesigner