OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / propertyeditor / declarativewidgetview.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2009 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 "declarativewidgetview.h"
34
35 #include <qdeclarative.h>
36 #include <QDeclarativeItem>
37 #include <QDeclarativeEngine>
38 #include <QDeclarativeContext>
39 #include <QPointer>
40
41 namespace QmlDesigner {
42
43 class DeclarativeWidgetViewPrivate
44 {
45 public:
46     DeclarativeWidgetViewPrivate(DeclarativeWidgetView *view)
47         : q(view), root(0), component(0) {}
48     ~DeclarativeWidgetViewPrivate() { delete root; }
49     void execute();
50
51     DeclarativeWidgetView *q;
52
53     QPointer<QWidget> root;
54     QUrl source;
55     QDeclarativeEngine engine;
56     QDeclarativeComponent *component;
57 };
58
59 void DeclarativeWidgetViewPrivate::execute()
60 {
61     if (root) {
62         delete root;
63         root = 0;
64     }
65     if (component) {
66         delete component;
67         component = 0;
68     }
69     if (!source.isEmpty()) {
70         component = new QDeclarativeComponent(&engine, source, q);
71         if (!component->isLoading()) {
72             q->continueExecute();
73         } else {
74             QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), q, SLOT(continueExecute()));
75         }
76     }
77 }
78
79 DeclarativeWidgetView::DeclarativeWidgetView(QWidget *parent) :
80     QWidget(parent), d(new DeclarativeWidgetViewPrivate(this))
81 {
82 }
83
84 DeclarativeWidgetView::~DeclarativeWidgetView()
85 {
86     delete d;
87 }
88
89 QUrl DeclarativeWidgetView::source() const
90 {
91     return d->source;
92 }
93
94 void DeclarativeWidgetView::setSource(const QUrl& url)
95 {
96     d->source = url;
97     d->execute();
98 }
99
100 QDeclarativeEngine* DeclarativeWidgetView::engine()
101 {
102    return &d->engine;
103 }
104
105 QWidget *DeclarativeWidgetView::rootWidget() const
106 {
107     return d->root;
108 }
109
110 QDeclarativeContext* DeclarativeWidgetView::rootContext()
111 {
112    return d->engine.rootContext();
113 }
114
115 DeclarativeWidgetView::Status DeclarativeWidgetView::status() const
116 {
117     if (!d->component)
118         return DeclarativeWidgetView::Null;
119
120     return DeclarativeWidgetView::Status(d->component->status());
121 }
122
123
124 void DeclarativeWidgetView::continueExecute()
125 {
126
127     disconnect(d->component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), this, SLOT(continueExecute()));
128
129     if (d->component->isError()) {
130         QList<QDeclarativeError> errorList = d->component->errors();
131         foreach (const QDeclarativeError &error, errorList) {
132             qWarning() << error;
133         }
134         emit statusChanged(status());
135         return;
136     }
137
138     QObject *obj = d->component->create();
139
140     if(d->component->isError()) {
141         QList<QDeclarativeError> errorList = d->component->errors();
142         foreach (const QDeclarativeError &error, errorList) {
143             qWarning() << error;
144         }
145         emit statusChanged(status());
146         return;
147     }
148
149     setRootWidget(qobject_cast<QWidget *>(obj));
150     emit statusChanged(status());
151 }
152
153 void DeclarativeWidgetView::setRootWidget(QWidget *widget)
154 {
155     if (d->root == widget)
156         return;
157
158     window()->setAttribute(Qt::WA_OpaquePaintEvent, false);
159     window()->setAttribute(Qt::WA_NoSystemBackground, false);
160     widget->setParent(this);
161     if (isVisible()) {
162         widget->setVisible(true);
163     }
164     resize(widget->size());
165     d->root = widget;
166
167     if (d->root) {
168         QSize initialSize = d->root->size();
169         if (initialSize != size()) {
170             resize(initialSize);
171         }
172     }
173 }
174
175 } //QmlDesigner