OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / formeditor / formeditormainview.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 "formeditormainview.h"
34
35 #include "formeditorview.h"
36 #include "formeditorwidget.h"
37 #include <QPixmapCache>
38 #include <QtDebug>
39
40 #include "zoomaction.h"
41
42 namespace QmlDesigner {
43
44
45 FormEditorMainView::FormEditorMainView()
46    : m_formMainEditorWidget(new FormEditorMainWidget(this))
47 {
48     QPixmapCache::setCacheLimit(1024 * 100);
49 }
50
51 FormEditorMainView::~FormEditorMainView()
52 {
53     resetViews();
54
55     if (m_formMainEditorWidget)
56         m_formMainEditorWidget->deleteLater();
57 }
58
59 void FormEditorMainView::modelAttached(Model *model)
60 {
61     AbstractView::modelAttached(model);
62
63     setupSubViews();
64
65     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
66         if (view)
67             view->modelAttached(model);
68     }
69
70     m_formMainEditorWidget->setModel(model);
71 }
72
73 void FormEditorMainView::createSubView(const QmlModelState &state)
74 {
75     FormEditorView *subView = new FormEditorView(this);
76     subView->setCurrentState(state);
77     m_formEditorViewList.append(subView);
78
79     m_formMainEditorWidget->addWidget(subView->widget());
80     m_subWindowMap.insert(state, subView->widget());
81
82     subView->setZoomLevel(zoomAction()->zoomLevel());
83     connect(zoomAction(), SIGNAL(zoomLevelChanged(double)), subView, SLOT(setZoomLevel(double)));
84 }
85
86 void FormEditorMainView::removeSubView(const ModelState &state)
87 {
88     QWidget *subWindow = m_subWindowMap.take(state).data();
89     Q_ASSERT(subWindow);
90     if (subWindow == m_formMainEditorWidget->currentWidget())
91         setCurrentState();
92     delete subWindow;
93
94     FormEditorView *editorView = 0;
95     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
96         if (view->modelState() == state) {
97             editorView = view.data();
98             break;
99         }
100     }
101     Q_ASSERT(editorView);
102     m_formEditorViewList.removeOne(editorView);
103     delete editorView;
104 }
105
106 static bool modelStateLessThan(const ModelState &firstState, const ModelState &secondState)
107  {
108     if (firstState.isBaseState())
109         return false;
110
111     return firstState.name().toLower() > secondState.name().toLower();
112  }
113
114 void FormEditorMainView::setupSubViews()
115 {
116     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
117         delete view.data();
118     m_formEditorViewList.clear();
119
120     foreach(const QWeakPointer<QWidget> &view, m_subWindowMap.values())
121         delete view.data();
122     m_subWindowMap.clear();
123
124     QList<ModelState> invertedStates(model()->modelStates());
125     qSort(invertedStates.begin(), invertedStates.end(), modelStateLessThan);
126     foreach(const ModelState &state, invertedStates)
127         createSubView(state);
128 }
129
130 void FormEditorMainView::resetViews()
131 {
132     m_subWindowMap.clear();
133     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
134         delete view.data();
135     m_formEditorViewList.clear();
136 }
137
138 void FormEditorMainView::nodeCreated(const ModelNode &createdNode)
139 {
140     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
141         if (view)
142             view->nodeCreated(createdNode);
143     }
144 }
145
146 void FormEditorMainView::modelAboutToBeDetached(Model *model)
147 {
148     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
149         if (view)
150             view->modelAboutToBeDetached(model);
151     }
152
153     resetViews();
154
155     m_formMainEditorWidget->setModel(0);
156
157     AbstractView::modelAboutToBeDetached(model);
158 }
159
160 void FormEditorMainView::nodeAboutToBeRemoved(const ModelNode &removedNode)
161 {
162     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
163         if (view)
164             view->nodeAboutToBeRemoved(removedNode);
165     }
166 }
167
168 void FormEditorMainView::propertiesAdded(const NodeState &state, const QList<NodeProperty>& propertyList)
169 {
170     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
171         if (view)
172             view->propertiesAdded(state, propertyList);
173     }
174 }
175
176 void FormEditorMainView::propertiesAboutToBeRemoved(const NodeState &state, const QList<NodeProperty>& propertyList)
177 {
178     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
179         if (view)
180             view->propertiesAboutToBeRemoved(state, propertyList);
181     }
182 }
183
184 void FormEditorMainView::propertyValuesChanged(const NodeState &state, const QList<NodeProperty>& propertyList)
185 {
186     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
187         if (view)
188             view->propertyValuesChanged(state, propertyList);
189     }
190 }
191
192 void FormEditorMainView::nodeIdChanged(const ModelNode& node, const QString& newId, const QString& oldId)
193 {
194     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
195         if (view)
196             view->nodeIdChanged(node, newId, oldId);
197     }
198 }
199
200 void FormEditorMainView::nodeReparented(const ModelNode &node, const ModelNode &oldParent, const ModelNode &newParent)
201 {
202     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
203         if (view)
204             view->nodeReparented(node, oldParent, newParent);
205     }
206 }
207
208 FormEditorMainWidget *FormEditorMainView::widget() const
209 {
210     return m_formMainEditorWidget.data();
211 }
212
213 NodeInstanceView *FormEditorMainView::nodeInstanceView(const ModelState &modelState) const
214 {
215     foreach (const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
216         if (view->modelState() == modelState)
217             return view->nodeInstanceView();
218     }
219
220     return 0;
221 }
222
223 void FormEditorMainView::selectedNodesChanged(const QList<ModelNode> &selectedNodeList,
224                                           const QList<ModelNode> &lastSelectedNodeList)
225 {
226     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
227         if (view)
228             view->selectedNodesChanged(selectedNodeList, lastSelectedNodeList);
229     }
230 }
231
232 void FormEditorMainView::modelStateAboutToBeRemoved(const ModelState &modelState)
233 {
234     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
235         if (view)
236             view->modelStateAboutToBeRemoved(modelState);
237     }
238
239     removeSubView(modelState);
240 }
241
242 void FormEditorMainView::modelStateAdded(const ModelState &modelState)
243 {
244     createSubView(modelState);
245     m_formEditorViewList.last()->modelAttached(model());
246
247     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
248         if (view)
249             view->modelStateAdded(modelState);
250     }
251 }
252
253 void FormEditorMainView::nodeStatesAboutToBeRemoved(const QList<NodeState> &nodeStateList)
254 {
255     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
256         if (view)
257             view->nodeStatesAboutToBeRemoved(nodeStateList);
258     }
259 }
260
261 void FormEditorMainView::nodeStatesAdded(const QList<NodeState> &nodeStateList)
262 {
263     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
264         if (view)
265             view->nodeStatesAdded(nodeStateList);
266     }
267 }
268
269 void FormEditorMainView::setCurrentState(const QmlModelState &state)
270 {
271     Q_ASSERT(m_subWindowMap.contains(state));
272     m_formMainEditorWidget->setCurrentWidget(m_subWindowMap.value(state).data());
273     emit stateChanged(state);
274 }
275
276 ModelState FormEditorMainView::currentState() const
277 {
278     QWidget *currentWidget = m_formMainEditorWidget->currentWidget();
279     QMapIterator<ModelState, QWeakPointer<QWidget> > iter(m_subWindowMap);
280     while (iter.hasNext()) {
281         iter.next();
282         if (iter.value().data() == currentWidget) {
283             return iter.key();
284         }
285     }
286     Q_ASSERT_X(0, Q_FUNC_INFO, "cannot find current state");
287     return ModelState();
288 }
289
290 FormEditorMainView::EditorTool FormEditorMainView::currentTool() const
291 {
292     return m_currentTool;
293 }
294
295 void FormEditorMainView::setCurrentTool(FormEditorMainView::EditorTool tool)
296 {
297     if (m_currentTool == tool)
298         return;
299     m_currentTool = tool;
300     switch (tool) {
301     case MoveTool: {
302         foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
303             view->changeToMoveTool();
304         break;
305     }
306     case DragTool: {
307         foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
308             view->changeToDragTool();
309         break;
310     }
311     case SelectTool: {
312         foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
313             view->changeToSelectionTool();
314         break;
315     }
316     case ResizeTool: {
317         foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
318             view->changeToResizeTool();
319         break;
320     }
321     case AnchorTool: {
322         foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
323             view->changeToAnchorTool();
324         break;
325     }
326     default: Q_ASSERT(0);
327     }
328     emit toolChanged(m_currentTool);
329 }
330
331 void FormEditorMainView::changeToDragTool()
332 {
333     setCurrentTool(DragTool);
334 }
335
336
337 void FormEditorMainView::changeToMoveTool()
338 {
339     setCurrentTool(MoveTool);
340 }
341
342 void FormEditorMainView::changeToMoveTool(const QPointF &/*beginPoint*/)
343 {
344     setCurrentTool(MoveTool);
345 }
346
347 void FormEditorMainView::changeToSelectionTool()
348 {
349     setCurrentTool(SelectTool);
350 }
351
352 void FormEditorMainView::changeToResizeTool()
353 {
354     setCurrentTool(ResizeTool);
355 }
356
357 void FormEditorMainView::changeToAnchorTool()
358 {
359     setCurrentTool(AnchorTool);
360 }
361
362 void FormEditorMainView::changeToTransformTools()
363 {
364     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
365         if (view)
366             view->changeToTransformTools();
367 }
368
369 void FormEditorMainView::anchorsChanged(const NodeState &nodeState)
370 {
371     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
372         if (view)
373             view->anchorsChanged(nodeState);
374     }
375 }
376
377
378 void FormEditorMainView::auxiliaryDataChanged(const ModelNode &node, const QString &name, const QVariant &data)
379 {
380      foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
381         if (view)
382             view->auxiliaryDataChanged(node, name, data);
383     }
384 }
385
386 void FormEditorMainView::nodeSlidedToIndex(const ModelNode &node, int newIndex, int oldIndex)
387 {
388     foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
389         if (view)
390             view->nodeSlidedToIndex(node, newIndex, oldIndex);
391     }
392 }
393
394 ComponentAction *FormEditorMainView::componentAction() const
395 {
396     return m_formMainEditorWidget->componentAction();
397 }
398
399 ZoomAction *FormEditorMainView::zoomAction() const
400 {
401     return m_formMainEditorWidget->zoomAction();
402 }
403
404 } // namespace QmlDesigner