1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "formeditormainview.h"
35 #include "formeditorview.h"
36 #include "formeditorwidget.h"
37 #include <QPixmapCache>
40 #include "zoomaction.h"
42 namespace QmlDesigner {
45 FormEditorMainView::FormEditorMainView()
46 : m_formMainEditorWidget(new FormEditorMainWidget(this))
48 QPixmapCache::setCacheLimit(1024 * 100);
51 FormEditorMainView::~FormEditorMainView()
55 if (m_formMainEditorWidget)
56 m_formMainEditorWidget->deleteLater();
59 void FormEditorMainView::modelAttached(Model *model)
61 AbstractView::modelAttached(model);
65 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
67 view->modelAttached(model);
70 m_formMainEditorWidget->setModel(model);
73 void FormEditorMainView::createSubView(const QmlModelState &state)
75 FormEditorView *subView = new FormEditorView(this);
76 subView->setCurrentState(state);
77 m_formEditorViewList.append(subView);
79 m_formMainEditorWidget->addWidget(subView->widget());
80 m_subWindowMap.insert(state, subView->widget());
82 subView->setZoomLevel(zoomAction()->zoomLevel());
83 connect(zoomAction(), SIGNAL(zoomLevelChanged(double)), subView, SLOT(setZoomLevel(double)));
86 void FormEditorMainView::removeSubView(const ModelState &state)
88 QWidget *subWindow = m_subWindowMap.take(state).data();
90 if (subWindow == m_formMainEditorWidget->currentWidget())
94 FormEditorView *editorView = 0;
95 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
96 if (view->modelState() == state) {
97 editorView = view.data();
101 Q_ASSERT(editorView);
102 m_formEditorViewList.removeOne(editorView);
106 static bool modelStateLessThan(const ModelState &firstState, const ModelState &secondState)
108 if (firstState.isBaseState())
111 return firstState.name().toLower() > secondState.name().toLower();
114 void FormEditorMainView::setupSubViews()
116 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
118 m_formEditorViewList.clear();
120 foreach(const QWeakPointer<QWidget> &view, m_subWindowMap.values())
122 m_subWindowMap.clear();
124 QList<ModelState> invertedStates(model()->modelStates());
125 qSort(invertedStates.begin(), invertedStates.end(), modelStateLessThan);
126 foreach(const ModelState &state, invertedStates)
127 createSubView(state);
130 void FormEditorMainView::resetViews()
132 m_subWindowMap.clear();
133 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
135 m_formEditorViewList.clear();
138 void FormEditorMainView::nodeCreated(const ModelNode &createdNode)
140 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
142 view->nodeCreated(createdNode);
146 void FormEditorMainView::modelAboutToBeDetached(Model *model)
148 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
150 view->modelAboutToBeDetached(model);
155 m_formMainEditorWidget->setModel(0);
157 AbstractView::modelAboutToBeDetached(model);
160 void FormEditorMainView::nodeAboutToBeRemoved(const ModelNode &removedNode)
162 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
164 view->nodeAboutToBeRemoved(removedNode);
168 void FormEditorMainView::propertiesAdded(const NodeState &state, const QList<NodeProperty>& propertyList)
170 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
172 view->propertiesAdded(state, propertyList);
176 void FormEditorMainView::propertiesAboutToBeRemoved(const NodeState &state, const QList<NodeProperty>& propertyList)
178 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
180 view->propertiesAboutToBeRemoved(state, propertyList);
184 void FormEditorMainView::propertyValuesChanged(const NodeState &state, const QList<NodeProperty>& propertyList)
186 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
188 view->propertyValuesChanged(state, propertyList);
192 void FormEditorMainView::nodeIdChanged(const ModelNode& node, const QString& newId, const QString& oldId)
194 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
196 view->nodeIdChanged(node, newId, oldId);
200 void FormEditorMainView::nodeReparented(const ModelNode &node, const ModelNode &oldParent, const ModelNode &newParent)
202 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
204 view->nodeReparented(node, oldParent, newParent);
208 FormEditorMainWidget *FormEditorMainView::widget() const
210 return m_formMainEditorWidget.data();
213 NodeInstanceView *FormEditorMainView::nodeInstanceView(const ModelState &modelState) const
215 foreach (const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
216 if (view->modelState() == modelState)
217 return view->nodeInstanceView();
223 void FormEditorMainView::selectedNodesChanged(const QList<ModelNode> &selectedNodeList,
224 const QList<ModelNode> &lastSelectedNodeList)
226 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
228 view->selectedNodesChanged(selectedNodeList, lastSelectedNodeList);
232 void FormEditorMainView::modelStateAboutToBeRemoved(const ModelState &modelState)
234 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
236 view->modelStateAboutToBeRemoved(modelState);
239 removeSubView(modelState);
242 void FormEditorMainView::modelStateAdded(const ModelState &modelState)
244 createSubView(modelState);
245 m_formEditorViewList.last()->modelAttached(model());
247 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
249 view->modelStateAdded(modelState);
253 void FormEditorMainView::nodeStatesAboutToBeRemoved(const QList<NodeState> &nodeStateList)
255 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
257 view->nodeStatesAboutToBeRemoved(nodeStateList);
261 void FormEditorMainView::nodeStatesAdded(const QList<NodeState> &nodeStateList)
263 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
265 view->nodeStatesAdded(nodeStateList);
269 void FormEditorMainView::setCurrentState(const QmlModelState &state)
271 Q_ASSERT(m_subWindowMap.contains(state));
272 m_formMainEditorWidget->setCurrentWidget(m_subWindowMap.value(state).data());
273 emit stateChanged(state);
276 ModelState FormEditorMainView::currentState() const
278 QWidget *currentWidget = m_formMainEditorWidget->currentWidget();
279 QMapIterator<ModelState, QWeakPointer<QWidget> > iter(m_subWindowMap);
280 while (iter.hasNext()) {
282 if (iter.value().data() == currentWidget) {
286 Q_ASSERT_X(0, Q_FUNC_INFO, "cannot find current state");
290 FormEditorMainView::EditorTool FormEditorMainView::currentTool() const
292 return m_currentTool;
295 void FormEditorMainView::setCurrentTool(FormEditorMainView::EditorTool tool)
297 if (m_currentTool == tool)
299 m_currentTool = tool;
302 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
303 view->changeToMoveTool();
307 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
308 view->changeToDragTool();
312 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
313 view->changeToSelectionTool();
317 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
318 view->changeToResizeTool();
322 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
323 view->changeToAnchorTool();
326 default: Q_ASSERT(0);
328 emit toolChanged(m_currentTool);
331 void FormEditorMainView::changeToDragTool()
333 setCurrentTool(DragTool);
337 void FormEditorMainView::changeToMoveTool()
339 setCurrentTool(MoveTool);
342 void FormEditorMainView::changeToMoveTool(const QPointF &/*beginPoint*/)
344 setCurrentTool(MoveTool);
347 void FormEditorMainView::changeToSelectionTool()
349 setCurrentTool(SelectTool);
352 void FormEditorMainView::changeToResizeTool()
354 setCurrentTool(ResizeTool);
357 void FormEditorMainView::changeToAnchorTool()
359 setCurrentTool(AnchorTool);
362 void FormEditorMainView::changeToTransformTools()
364 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList)
366 view->changeToTransformTools();
369 void FormEditorMainView::anchorsChanged(const NodeState &nodeState)
371 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
373 view->anchorsChanged(nodeState);
378 void FormEditorMainView::auxiliaryDataChanged(const ModelNode &node, const QString &name, const QVariant &data)
380 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
382 view->auxiliaryDataChanged(node, name, data);
386 void FormEditorMainView::nodeSlidedToIndex(const ModelNode &node, int newIndex, int oldIndex)
388 foreach(const QWeakPointer<FormEditorView> &view, m_formEditorViewList) {
390 view->nodeSlidedToIndex(node, newIndex, oldIndex);
394 ComponentAction *FormEditorMainView::componentAction() const
396 return m_formMainEditorWidget->componentAction();
399 ZoomAction *FormEditorMainView::zoomAction() const
401 return m_formMainEditorWidget->zoomAction();
404 } // namespace QmlDesigner