PageRenderTime 50ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/plugins/qmldesigner/designercore/model/model.cpp

https://bitbucket.org/kyanha/qt-creator
C++ | 1879 lines | 1408 code | 372 blank | 99 comment | 213 complexity | c8c8630cb6a844440b3cf23654fa3868 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of Qt Creator.
  7. **
  8. ** Commercial License Usage
  9. ** Licensees holding valid commercial Qt licenses may use this file in
  10. ** accordance with the commercial license agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and Digia. For licensing terms and
  13. ** conditions see http://qt.digia.com/licensing. For further information
  14. ** use the contact form at http://qt.digia.com/contact-us.
  15. **
  16. ** GNU Lesser General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU Lesser
  18. ** General Public License version 2.1 as published by the Free Software
  19. ** Foundation and appearing in the file LICENSE.LGPL included in the
  20. ** packaging of this file. Please review the following information to
  21. ** ensure the GNU Lesser General Public License version 2.1 requirements
  22. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  23. **
  24. ** In addition, as a special exception, Digia gives you certain additional
  25. ** rights. These rights are described in the Digia Qt LGPL Exception
  26. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  27. **
  28. ****************************************************************************/
  29. #include <model.h>
  30. #include <modelnode.h>
  31. #include "internalnode_p.h"
  32. #include "invalidpropertyexception.h"
  33. #include "invalidargumentexception.h"
  34. #include <QFile>
  35. #include <QByteArray>
  36. #include <QWeakPointer>
  37. #include <QFileInfo>
  38. #include <QUndoStack>
  39. #include <QXmlStreamReader>
  40. #include <QDebug>
  41. #include <QPlainTextEdit>
  42. #include <QHashIterator>
  43. #include "abstractview.h"
  44. #include "nodeinstanceview.h"
  45. #include "metainfo.h"
  46. #include "nodemetainfo.h"
  47. #include "model_p.h"
  48. #include "subcomponentmanager.h"
  49. #include "internalproperty.h"
  50. #include "internalnodelistproperty.h"
  51. #include "internalnodeabstractproperty.h"
  52. #include "invalidmodelnodeexception.h"
  53. #include "invalidmodelstateexception.h"
  54. #include "invalidslideindexexception.h"
  55. #include "abstractproperty.h"
  56. #include "variantproperty.h"
  57. #include "bindingproperty.h"
  58. #include "nodeabstractproperty.h"
  59. #include "nodelistproperty.h"
  60. #include "rewritertransaction.h"
  61. #include "rewriterview.h"
  62. #include "rewritingexception.h"
  63. #include "invalididexception.h"
  64. /*!
  65. \defgroup CoreModel
  66. */
  67. /*!
  68. \class QmlDesigner::Model
  69. \ingroup CoreModel
  70. \brief This is the facade for the abstract model data.
  71. All write access is running through this interface
  72. The Model is the central place to access a qml files data (see e.g. rootNode() ) and meta data (see metaInfo() ).
  73. Components that want to be informed about changes in the model can register a subclass of AbstractView via attachView().
  74. \see QmlDesigner::ModelNode, QmlDesigner::AbstractProperty, QmlDesigner::AbstractView
  75. */
  76. namespace QmlDesigner {
  77. namespace Internal {
  78. ModelPrivate::ModelPrivate(Model *model) :
  79. m_q(model),
  80. m_writeLock(false),
  81. m_internalIdCounter(1)
  82. {
  83. m_rootInternalNode = createNode("QtQuick.Item", 1, 0, PropertyListType(), PropertyListType(), QString(), ModelNode::NodeWithoutSource,true);
  84. m_acutalStateNode = m_rootInternalNode;
  85. }
  86. ModelPrivate::~ModelPrivate()
  87. {
  88. detachAllViews();
  89. }
  90. void ModelPrivate::detachAllViews()
  91. {
  92. foreach (const QWeakPointer<AbstractView> &view, m_viewList)
  93. detachView(view.data(), true);
  94. m_viewList.clear();
  95. if (m_nodeInstanceView) {
  96. m_nodeInstanceView->modelAboutToBeDetached(m_q);
  97. m_nodeInstanceView.clear();
  98. }
  99. if (m_rewriterView) {
  100. m_rewriterView->modelAboutToBeDetached(m_q);
  101. m_rewriterView.clear();
  102. }
  103. }
  104. Model *ModelPrivate::create(QString type, int major, int minor, Model *metaInfoPropxyModel)
  105. {
  106. Model *model = new Model;
  107. model->d->m_metaInfoProxyModel = metaInfoPropxyModel;
  108. model->d->rootNode()->setType(type);
  109. model->d->rootNode()->setMajorVersion(major);
  110. model->d->rootNode()->setMinorVersion(minor);
  111. return model;
  112. }
  113. void ModelPrivate::changeImports(const QList<Import> &toBeAddedImportList, const QList<Import> &toBeRemovedImportList)
  114. {
  115. QList<Import> removedImportList;
  116. foreach (const Import &import, toBeRemovedImportList) {
  117. if (m_imports.contains(import)) {
  118. removedImportList.append(import);
  119. m_imports.removeOne(import);
  120. }
  121. }
  122. QList<Import> addedImportList;
  123. foreach (const Import &import, toBeAddedImportList) {
  124. if (!m_imports.contains(import)) {
  125. addedImportList.append(import);
  126. m_imports.append(import);
  127. }
  128. }
  129. if (!removedImportList.isEmpty() || !addedImportList.isEmpty())
  130. notifyImportsChanged(addedImportList, removedImportList);
  131. }
  132. void ModelPrivate::notifyImportsChanged(const QList<Import> &addedImports, const QList<Import> &removedImports)
  133. {
  134. bool resetModel = false;
  135. QString description;
  136. try {
  137. if (rewriterView())
  138. rewriterView()->importsChanged(addedImports, removedImports);
  139. } catch (RewritingException &e) {
  140. description = e.description();
  141. resetModel = true;
  142. }
  143. NodeMetaInfo::clearCache();
  144. if (nodeInstanceView())
  145. nodeInstanceView()->importsChanged(addedImports, removedImports);
  146. foreach (const QWeakPointer<AbstractView> &view, m_viewList)
  147. view->importsChanged(addedImports, removedImports);
  148. if (resetModel)
  149. resetModelByRewriter(description);
  150. }
  151. QUrl ModelPrivate::fileUrl() const
  152. {
  153. return m_fileUrl;
  154. }
  155. void ModelPrivate::setFileUrl(const QUrl &fileUrl)
  156. {
  157. QUrl oldPath = m_fileUrl;
  158. if (oldPath != fileUrl) {
  159. m_fileUrl = fileUrl;
  160. foreach (const QWeakPointer<AbstractView> &view, m_viewList)
  161. view->fileUrlChanged(oldPath, fileUrl);
  162. }
  163. }
  164. InternalNode::Pointer ModelPrivate::createNode(const QString &typeString,
  165. int majorVersion,
  166. int minorVersion,
  167. const QList<QPair<QString, QVariant> > &propertyList,
  168. const QList<QPair<QString, QVariant> > &auxPropertyList,
  169. const QString &nodeSource,
  170. ModelNode::NodeSourceType nodeSourceType,
  171. bool isRootNode)
  172. {
  173. if (typeString.isEmpty())
  174. throw InvalidArgumentException(__LINE__, __FUNCTION__, __FILE__, tr("invalid type"));
  175. qint32 internalId = 0;
  176. if (!isRootNode)
  177. internalId = m_internalIdCounter++;
  178. InternalNode::Pointer newInternalNodePointer = InternalNode::create(typeString, majorVersion, minorVersion, internalId);
  179. newInternalNodePointer->setNodeSourceType(nodeSourceType);
  180. typedef QPair<QString, QVariant> PropertyPair;
  181. foreach (const PropertyPair &propertyPair, propertyList) {
  182. newInternalNodePointer->addVariantProperty(propertyPair.first);
  183. newInternalNodePointer->variantProperty(propertyPair.first)->setValue(propertyPair.second);
  184. }
  185. foreach (const PropertyPair &propertyPair, auxPropertyList) {
  186. newInternalNodePointer->setAuxiliaryData(propertyPair.first, propertyPair.second);
  187. }
  188. m_nodeSet.insert(newInternalNodePointer);
  189. m_internalIdNodeHash.insert(newInternalNodePointer->internalId(), newInternalNodePointer);
  190. if (!nodeSource.isNull())
  191. newInternalNodePointer->setNodeSource(nodeSource);
  192. notifyNodeCreated(newInternalNodePointer);
  193. return newInternalNodePointer;
  194. }
  195. void ModelPrivate::removeNodeFromModel(const InternalNodePointer &node)
  196. {
  197. Q_ASSERT(!node.isNull());
  198. node->resetParentProperty();
  199. if (!node->id().isEmpty())
  200. m_idNodeHash.remove(node->id());
  201. node->setValid(false);
  202. m_nodeSet.remove(node);
  203. m_internalIdNodeHash.remove(node->internalId());
  204. }
  205. void ModelPrivate::removeAllSubNodes(const InternalNode::Pointer &node)
  206. {
  207. foreach (const InternalNodePointer &subNode, node->allSubNodes()) {
  208. removeNodeFromModel(subNode);
  209. }
  210. }
  211. void ModelPrivate::removeNode(const InternalNode::Pointer &node)
  212. {
  213. Q_ASSERT(!node.isNull());
  214. AbstractView::PropertyChangeFlags propertyChangeFlags = AbstractView::NoAdditionalChanges;
  215. notifyNodeAboutToBeRemoved(node);
  216. InternalNodeAbstractProperty::Pointer oldParentProperty(node->parentProperty());
  217. removeAllSubNodes(node);
  218. removeNodeFromModel(node);
  219. InternalNode::Pointer parentNode;
  220. QString parentPropertyName;
  221. if (oldParentProperty) {
  222. parentNode = oldParentProperty->propertyOwner();
  223. parentPropertyName = oldParentProperty->name();
  224. }
  225. if (oldParentProperty && oldParentProperty->isEmpty()) {
  226. removePropertyWithoutNotification(oldParentProperty);
  227. propertyChangeFlags |= AbstractView::EmptyPropertiesRemoved;
  228. }
  229. notifyNodeRemoved(node, parentNode, parentPropertyName, propertyChangeFlags);
  230. }
  231. InternalNode::Pointer ModelPrivate::rootNode() const
  232. {
  233. return m_rootInternalNode;
  234. }
  235. MetaInfo ModelPrivate::metaInfo() const
  236. {
  237. return m_metaInfo;
  238. }
  239. void ModelPrivate::setMetaInfo(const MetaInfo &metaInfo)
  240. {
  241. m_metaInfo = metaInfo;
  242. }
  243. void ModelPrivate::changeNodeId(const InternalNode::Pointer& internalNodePointer, const QString &id)
  244. {
  245. const QString oldId = internalNodePointer->id();
  246. internalNodePointer->setId(id);
  247. if (!oldId.isEmpty())
  248. m_idNodeHash.remove(oldId);
  249. if (!id.isEmpty())
  250. m_idNodeHash.insert(id, internalNodePointer);
  251. try {
  252. notifyNodeIdChanged(internalNodePointer, id, oldId);
  253. } catch (RewritingException &e) {
  254. throw InvalidIdException(__LINE__, __FUNCTION__, __FILE__, id, e.description());
  255. }
  256. }
  257. void ModelPrivate::checkPropertyName(const QString &propertyName)
  258. {
  259. if (propertyName.isEmpty()) {
  260. Q_ASSERT_X(propertyName.isEmpty(), Q_FUNC_INFO, "empty property name");
  261. throw InvalidPropertyException(__LINE__, __FUNCTION__, __FILE__, "<empty property name>");
  262. }
  263. if (propertyName == "id") {
  264. Q_ASSERT_X(propertyName != "id", Q_FUNC_INFO, "cannot add property id");
  265. throw InvalidPropertyException(__LINE__, __FUNCTION__, __FILE__, propertyName);
  266. }
  267. }
  268. void ModelPrivate::notifyAuxiliaryDataChanged(const InternalNodePointer &internalNode, const QString &name, const QVariant &data)
  269. {
  270. bool resetModel = false;
  271. QString description;
  272. try {
  273. if (rewriterView()) {
  274. ModelNode node(internalNode, model(), rewriterView());
  275. rewriterView()->auxiliaryDataChanged(node, name, data);
  276. }
  277. } catch (RewritingException &e) {
  278. description = e.description();
  279. resetModel = true;
  280. }
  281. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  282. Q_ASSERT(view != 0);
  283. ModelNode node(internalNode, model(), view.data());
  284. view->auxiliaryDataChanged(node, name, data);
  285. }
  286. if (nodeInstanceView()) {
  287. ModelNode node(internalNode, model(), nodeInstanceView());
  288. nodeInstanceView()->auxiliaryDataChanged(node, name, data);
  289. }
  290. if (resetModel)
  291. resetModelByRewriter(description);
  292. }
  293. void ModelPrivate::notifyNodeSourceChanged(const InternalNodePointer &internalNode, const QString &newNodeSource)
  294. {
  295. bool resetModel = false;
  296. QString description;
  297. try {
  298. if (rewriterView()) {
  299. ModelNode node(internalNode, model(), rewriterView());
  300. rewriterView()->nodeSourceChanged(node, newNodeSource);
  301. }
  302. } catch (RewritingException &e) {
  303. description = e.description();
  304. resetModel = true;
  305. }
  306. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  307. Q_ASSERT(view != 0);
  308. ModelNode node(internalNode, model(), view.data());
  309. view->nodeSourceChanged(node, newNodeSource);
  310. }
  311. if (nodeInstanceView()) {
  312. ModelNode node(internalNode, model(), nodeInstanceView());
  313. nodeInstanceView()->nodeSourceChanged(node, newNodeSource);
  314. }
  315. if (resetModel)
  316. resetModelByRewriter(description);
  317. }
  318. void ModelPrivate::notifyRootNodeTypeChanged(const QString &type, int majorVersion, int minorVersion)
  319. {
  320. bool resetModel = false;
  321. QString description;
  322. try {
  323. if (rewriterView())
  324. rewriterView()->rootNodeTypeChanged(type, majorVersion, minorVersion);
  325. } catch (RewritingException &e) {
  326. description = e.description();
  327. resetModel = true;
  328. }
  329. if (nodeInstanceView())
  330. nodeInstanceView()->rootNodeTypeChanged(type, majorVersion, minorVersion);
  331. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  332. Q_ASSERT(view != 0);
  333. view->rootNodeTypeChanged(type, majorVersion, minorVersion);
  334. }
  335. if (resetModel)
  336. resetModelByRewriter(description);
  337. }
  338. void ModelPrivate::notifyInstancePropertyChange(const QList<QPair<ModelNode, QString> > &propertyPairList)
  339. {
  340. // no need to notify the rewriter or the instance view
  341. typedef QPair<ModelNode, QString> ModelNodePropertyPair;
  342. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  343. Q_ASSERT(view != 0);
  344. QList<QPair<ModelNode, QString> > adaptedPropertyList;
  345. foreach (const ModelNodePropertyPair &propertyPair, propertyPairList) {
  346. ModelNodePropertyPair newPair(ModelNode(propertyPair.first.internalNode(), model(), view.data()), propertyPair.second);
  347. adaptedPropertyList.append(newPair);
  348. }
  349. view->instancePropertyChange(adaptedPropertyList);
  350. }
  351. }
  352. void ModelPrivate::notifyInstancesCompleted(const QVector<ModelNode> &nodeVector)
  353. {
  354. bool resetModel = false;
  355. QString description;
  356. QVector<Internal::InternalNode::Pointer> internalVector(toInternalNodeVector(nodeVector));
  357. try {
  358. if (rewriterView())
  359. rewriterView()->instancesCompleted(toModelNodeVector(internalVector, rewriterView()));
  360. } catch (RewritingException &e) {
  361. description = e.description();
  362. resetModel = true;
  363. }
  364. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  365. Q_ASSERT(view != 0);
  366. view->instancesCompleted(toModelNodeVector(internalVector, view.data()));
  367. }
  368. if (nodeInstanceView())
  369. nodeInstanceView()->instancesCompleted(toModelNodeVector(internalVector, nodeInstanceView()));
  370. if (resetModel)
  371. resetModelByRewriter(description);
  372. }
  373. QMultiHash<ModelNode, InformationName> convertModelNodeInformationHash(const QMultiHash<ModelNode, InformationName> &informationChangeHash, AbstractView *view)
  374. {
  375. QMultiHash<ModelNode, InformationName> convertedModelNodeInformationHash;
  376. QHashIterator<ModelNode, InformationName> hashIterator(informationChangeHash);
  377. while (hashIterator.hasNext()) {
  378. hashIterator.next();
  379. convertedModelNodeInformationHash.insert(ModelNode(hashIterator.key(), view), hashIterator.value());
  380. }
  381. return convertedModelNodeInformationHash;
  382. }
  383. void ModelPrivate::notifyInstancesInformationsChange(const QMultiHash<ModelNode, InformationName> &informationChangeHash)
  384. {
  385. bool resetModel = false;
  386. QString description;
  387. try {
  388. if (rewriterView())
  389. rewriterView()->instanceInformationsChange(convertModelNodeInformationHash(informationChangeHash, rewriterView()));
  390. } catch (RewritingException &e) {
  391. description = e.description();
  392. resetModel = true;
  393. }
  394. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  395. Q_ASSERT(view != 0);
  396. view->instanceInformationsChange(convertModelNodeInformationHash(informationChangeHash, view.data()));
  397. }
  398. if (nodeInstanceView())
  399. nodeInstanceView()->instanceInformationsChange(convertModelNodeInformationHash(informationChangeHash, nodeInstanceView()));
  400. if (resetModel)
  401. resetModelByRewriter(description);
  402. }
  403. void ModelPrivate::notifyInstancesRenderImageChanged(const QVector<ModelNode> &nodeVector)
  404. {
  405. bool resetModel = false;
  406. QString description;
  407. QVector<Internal::InternalNode::Pointer> internalVector(toInternalNodeVector(nodeVector));
  408. try {
  409. if (rewriterView())
  410. rewriterView()->instancesRenderImageChanged(toModelNodeVector(internalVector, rewriterView()));
  411. } catch (RewritingException &e) {
  412. description = e.description();
  413. resetModel = true;
  414. }
  415. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  416. Q_ASSERT(view != 0);
  417. view->instancesRenderImageChanged(toModelNodeVector(internalVector, view.data()));
  418. }
  419. if (nodeInstanceView())
  420. nodeInstanceView()->instancesRenderImageChanged(toModelNodeVector(internalVector, nodeInstanceView()));
  421. if (resetModel)
  422. resetModelByRewriter(description);
  423. }
  424. void ModelPrivate::notifyInstancesPreviewImageChanged(const QVector<ModelNode> &nodeVector)
  425. {
  426. bool resetModel = false;
  427. QString description;
  428. QVector<Internal::InternalNode::Pointer> internalVector(toInternalNodeVector(nodeVector));
  429. try {
  430. if (rewriterView())
  431. rewriterView()->instancesPreviewImageChanged(toModelNodeVector(internalVector, rewriterView()));
  432. } catch (RewritingException &e) {
  433. description = e.description();
  434. resetModel = true;
  435. }
  436. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  437. Q_ASSERT(view != 0);
  438. view->instancesPreviewImageChanged(toModelNodeVector(internalVector, view.data()));
  439. }
  440. if (nodeInstanceView())
  441. nodeInstanceView()->instancesPreviewImageChanged(toModelNodeVector(internalVector, nodeInstanceView()));
  442. if (resetModel)
  443. resetModelByRewriter(description);
  444. }
  445. void ModelPrivate::notifyInstancesChildrenChanged(const QVector<ModelNode> &nodeVector)
  446. {
  447. bool resetModel = false;
  448. QString description;
  449. QVector<Internal::InternalNode::Pointer> internalVector(toInternalNodeVector(nodeVector));
  450. try {
  451. if (rewriterView())
  452. rewriterView()->instancesChildrenChanged(toModelNodeVector(internalVector, rewriterView()));
  453. } catch (RewritingException &e) {
  454. description = e.description();
  455. resetModel = true;
  456. }
  457. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  458. Q_ASSERT(view != 0);
  459. view->instancesChildrenChanged(toModelNodeVector(internalVector, view.data()));
  460. }
  461. if (nodeInstanceView())
  462. nodeInstanceView()->instancesChildrenChanged(toModelNodeVector(internalVector, nodeInstanceView()));
  463. if (resetModel)
  464. resetModelByRewriter(description);
  465. }
  466. void ModelPrivate::notifyActualStateChanged(const ModelNode &node)
  467. {
  468. bool resetModel = false;
  469. QString description;
  470. m_acutalStateNode = node.internalNode();
  471. try {
  472. if (rewriterView())
  473. rewriterView()->actualStateChanged(ModelNode(node.internalNode(), model(), rewriterView()));
  474. } catch (RewritingException &e) {
  475. description = e.description();
  476. resetModel = true;
  477. }
  478. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  479. Q_ASSERT(view != 0);
  480. view->actualStateChanged(ModelNode(node.internalNode(), model(), view.data()));
  481. }
  482. if (nodeInstanceView())
  483. nodeInstanceView()->actualStateChanged(ModelNode(node.internalNode(), model(), nodeInstanceView()));
  484. if (resetModel)
  485. resetModelByRewriter(description);
  486. }
  487. void ModelPrivate::notifyRewriterBeginTransaction()
  488. {
  489. bool resetModel = false;
  490. QString description;
  491. try {
  492. if (rewriterView())
  493. rewriterView()->rewriterBeginTransaction();
  494. } catch (RewritingException &e) {
  495. description = e.description();
  496. resetModel = true;
  497. }
  498. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  499. Q_ASSERT(view != 0);
  500. view->rewriterBeginTransaction();
  501. }
  502. if (nodeInstanceView())
  503. nodeInstanceView()->rewriterBeginTransaction();
  504. if (resetModel)
  505. resetModelByRewriter(description);
  506. }
  507. void ModelPrivate::notifyRewriterEndTransaction()
  508. {
  509. bool resetModel = false;
  510. QString description;
  511. try {
  512. if (rewriterView())
  513. rewriterView()->rewriterEndTransaction();
  514. } catch (RewritingException &e) {
  515. description = e.description();
  516. resetModel = true;
  517. }
  518. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  519. Q_ASSERT(view != 0);
  520. view->rewriterEndTransaction();
  521. }
  522. if (nodeInstanceView())
  523. nodeInstanceView()->rewriterEndTransaction();
  524. if (resetModel)
  525. resetModelByRewriter(description);
  526. }
  527. void ModelPrivate::notifyInstanceToken(const QString &token, int number, const QVector<ModelNode> &nodeVector)
  528. {
  529. bool resetModel = false;
  530. QString description;
  531. QVector<Internal::InternalNode::Pointer> internalVector(toInternalNodeVector(nodeVector));
  532. try {
  533. if (rewriterView())
  534. rewriterView()->instancesToken(token, number, toModelNodeVector(internalVector, rewriterView()));
  535. } catch (RewritingException &e) {
  536. description = e.description();
  537. resetModel = true;
  538. }
  539. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  540. Q_ASSERT(view != 0);
  541. view->instancesToken(token, number, toModelNodeVector(internalVector, view.data()));
  542. }
  543. if (nodeInstanceView())
  544. nodeInstanceView()->instancesToken(token, number, toModelNodeVector(internalVector, nodeInstanceView()));
  545. if (resetModel)
  546. resetModelByRewriter(description);
  547. }
  548. void ModelPrivate::notifyCustomNotification(const AbstractView *senderView, const QString &identifier, const QList<ModelNode> &nodeList, const QList<QVariant> &data)
  549. {
  550. bool resetModel = false;
  551. QString description;
  552. QList<Internal::InternalNode::Pointer> internalList(toInternalNodeList(nodeList));
  553. try {
  554. if (rewriterView())
  555. rewriterView()->customNotification(senderView, identifier, toModelNodeList(internalList, rewriterView()), data);
  556. } catch (RewritingException &e) {
  557. description = e.description();
  558. resetModel = true;
  559. }
  560. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  561. Q_ASSERT(view != 0);
  562. view->customNotification(senderView, identifier, toModelNodeList(internalList, view.data()), data);
  563. }
  564. if (nodeInstanceView())
  565. nodeInstanceView()->customNotification(senderView, identifier, toModelNodeList(internalList, nodeInstanceView()), data);
  566. if (resetModel)
  567. resetModelByRewriter(description);
  568. }
  569. void ModelPrivate::notifyPropertiesRemoved(const QList<PropertyPair> &propertyPairList)
  570. {
  571. bool resetModel = false;
  572. QString description;
  573. try {
  574. if (rewriterView()) {
  575. QList<AbstractProperty> propertyList;
  576. foreach (const PropertyPair &propertyPair, propertyPairList) {
  577. AbstractProperty newProperty(propertyPair.second, propertyPair.first, model(), rewriterView());
  578. propertyList.append(newProperty);
  579. }
  580. rewriterView()->propertiesRemoved(propertyList);
  581. }
  582. } catch (RewritingException &e) {
  583. description = e.description();
  584. resetModel = true;
  585. }
  586. if (nodeInstanceView()) {
  587. QList<AbstractProperty> propertyList;
  588. foreach (const PropertyPair &propertyPair, propertyPairList) {
  589. AbstractProperty newProperty(propertyPair.second, propertyPair.first, model(), nodeInstanceView());
  590. propertyList.append(newProperty);
  591. }
  592. nodeInstanceView()->propertiesRemoved(propertyList);
  593. }
  594. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  595. QList<AbstractProperty> propertyList;
  596. Q_ASSERT(view != 0);
  597. foreach (const PropertyPair &propertyPair, propertyPairList) {
  598. AbstractProperty newProperty(propertyPair.second, propertyPair.first, model(), view.data());
  599. propertyList.append(newProperty);
  600. }
  601. view->propertiesRemoved(propertyList);
  602. }
  603. if (resetModel)
  604. resetModelByRewriter(description);
  605. }
  606. void ModelPrivate::notifyPropertiesAboutToBeRemoved(const QList<InternalProperty::Pointer> &internalPropertyList)
  607. {
  608. bool resetModel = false;
  609. QString description;
  610. try {
  611. if (rewriterView()) {
  612. QList<AbstractProperty> propertyList;
  613. foreach (const InternalProperty::Pointer &property, internalPropertyList) {
  614. AbstractProperty newProperty(property->name(), property->propertyOwner(), model(), rewriterView());
  615. propertyList.append(newProperty);
  616. }
  617. rewriterView()->propertiesAboutToBeRemoved(propertyList);
  618. }
  619. } catch (RewritingException &e) {
  620. description = e.description();
  621. resetModel = true;
  622. }
  623. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  624. QList<AbstractProperty> propertyList;
  625. Q_ASSERT(view != 0);
  626. foreach (const InternalProperty::Pointer &property, internalPropertyList) {
  627. AbstractProperty newProperty(property->name(), property->propertyOwner(), model(), view.data());
  628. propertyList.append(newProperty);
  629. }
  630. try {
  631. view->propertiesAboutToBeRemoved(propertyList);
  632. } catch (RewritingException &e) {
  633. description = e.description();
  634. resetModel = true;
  635. }
  636. }
  637. if (nodeInstanceView()) {
  638. QList<AbstractProperty> propertyList;
  639. foreach (const InternalProperty::Pointer &property, internalPropertyList) {
  640. AbstractProperty newProperty(property->name(), property->propertyOwner(), model(), nodeInstanceView());
  641. propertyList.append(newProperty);
  642. }
  643. nodeInstanceView()->propertiesAboutToBeRemoved(propertyList);
  644. }
  645. if (resetModel)
  646. resetModelByRewriter(description);
  647. }
  648. void ModelPrivate::setAuxiliaryData(const InternalNode::Pointer& node, const QString &name, const QVariant &data)
  649. {
  650. node->setAuxiliaryData(name, data);
  651. notifyAuxiliaryDataChanged(node, name,data);
  652. }
  653. void ModelPrivate::resetModelByRewriter(const QString &description)
  654. {
  655. if (rewriterView())
  656. rewriterView()->resetToLastCorrectQml();
  657. throw RewritingException(__LINE__, __FUNCTION__, __FILE__, description, rewriterView()->textModifierContent());
  658. }
  659. void ModelPrivate::attachView(AbstractView *view)
  660. {
  661. if (m_viewList.contains(view))
  662. return;
  663. m_viewList.append(view);
  664. view->modelAttached(m_q);
  665. }
  666. void ModelPrivate::detachView(AbstractView *view, bool notifyView)
  667. {
  668. if (notifyView)
  669. view->modelAboutToBeDetached(m_q);
  670. m_viewList.removeOne(view);
  671. }
  672. void ModelPrivate::notifyNodeCreated(const InternalNode::Pointer &newInternalNodePointer)
  673. {
  674. Q_ASSERT(newInternalNodePointer->isValid());
  675. bool resetModel = false;
  676. QString description;
  677. try {
  678. if (rewriterView()) {
  679. ModelNode createdNode(newInternalNodePointer, model(), rewriterView());
  680. rewriterView()->nodeCreated(createdNode);
  681. }
  682. } catch (RewritingException &e) {
  683. description = e.description();
  684. resetModel = true;
  685. }
  686. if (nodeInstanceView()) {
  687. ModelNode createdNode(newInternalNodePointer, model(), nodeInstanceView());
  688. nodeInstanceView()->nodeCreated(createdNode);
  689. }
  690. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  691. Q_ASSERT(view != 0);
  692. ModelNode createdNode(newInternalNodePointer, model(), view.data());
  693. view->nodeCreated(createdNode);
  694. }
  695. if (resetModel)
  696. resetModelByRewriter(description);
  697. }
  698. void ModelPrivate::notifyNodeAboutToBeRemoved(const InternalNode::Pointer &nodePointer)
  699. {
  700. bool resetModel = false;
  701. QString description;
  702. try {
  703. if (rewriterView()) {
  704. ModelNode node(nodePointer, model(), rewriterView());
  705. rewriterView()->nodeAboutToBeRemoved(node);
  706. }
  707. } catch (RewritingException &e) {
  708. description = e.description();
  709. resetModel = true;
  710. }
  711. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  712. Q_ASSERT(view != 0);
  713. ModelNode node(nodePointer, model(), view.data());
  714. view->nodeAboutToBeRemoved(node);
  715. }
  716. if (nodeInstanceView()) {
  717. ModelNode node(nodePointer, model(), nodeInstanceView());
  718. nodeInstanceView()->nodeAboutToBeRemoved(node);
  719. }
  720. if (resetModel)
  721. resetModelByRewriter(description);
  722. }
  723. void ModelPrivate::notifyNodeRemoved(const InternalNodePointer &nodePointer, const InternalNodePointer &parentNodePointer, const QString &parentPropertyName, AbstractView::PropertyChangeFlags propertyChange)
  724. {
  725. bool resetModel = false;
  726. QString description;
  727. try {
  728. if (rewriterView()) {
  729. ModelNode node(nodePointer, model(), rewriterView());
  730. NodeAbstractProperty parentProperty(parentPropertyName, parentNodePointer, model(), rewriterView());
  731. rewriterView()->nodeRemoved(node, parentProperty, propertyChange);
  732. }
  733. } catch (RewritingException &e) {
  734. description = e.description();
  735. resetModel = true;
  736. }
  737. if (nodeInstanceView()) {
  738. ModelNode node(nodePointer, model(), nodeInstanceView());
  739. NodeAbstractProperty parentProperty(parentPropertyName, parentNodePointer, model(), nodeInstanceView());
  740. nodeInstanceView()->nodeRemoved(node, parentProperty, propertyChange);
  741. }
  742. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  743. Q_ASSERT(view != 0);
  744. ModelNode node(nodePointer, model(), view.data());
  745. NodeAbstractProperty parentProperty(parentPropertyName, parentNodePointer, model(), view.data());
  746. view->nodeRemoved(node, parentProperty, propertyChange);
  747. }
  748. if (resetModel)
  749. resetModelByRewriter(description);
  750. }
  751. void ModelPrivate::notifyNodeIdChanged(const InternalNode::Pointer& nodePointer, const QString& newId, const QString& oldId)
  752. {
  753. bool resetModel = false;
  754. QString description;
  755. try {
  756. if (rewriterView()) {
  757. ModelNode node(nodePointer, model(), rewriterView());
  758. rewriterView()->nodeIdChanged(node, newId, oldId);
  759. }
  760. } catch (RewritingException &e) {
  761. description = e.description();
  762. resetModel = true;
  763. }
  764. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  765. Q_ASSERT(view != 0);
  766. ModelNode node(nodePointer, model(), view.data());
  767. view->nodeIdChanged(node, newId, oldId);
  768. }
  769. if (nodeInstanceView()) {
  770. ModelNode node(nodePointer, model(), nodeInstanceView());
  771. nodeInstanceView()->nodeIdChanged(node, newId, oldId);
  772. }
  773. if (resetModel)
  774. resetModelByRewriter(description);
  775. }
  776. void ModelPrivate::notifyBindingPropertiesChanged(const QList<InternalBindingPropertyPointer> &internalBropertyList, AbstractView::PropertyChangeFlags propertyChange)
  777. {
  778. bool resetModel = false;
  779. QString description;
  780. try {
  781. if (rewriterView()) {
  782. QList<BindingProperty> propertyList;
  783. foreach (const InternalBindingPropertyPointer &bindingProperty, internalBropertyList) {
  784. propertyList.append(BindingProperty(bindingProperty->name(), bindingProperty->propertyOwner(), model(), rewriterView()));
  785. }
  786. rewriterView()->bindingPropertiesChanged(propertyList, propertyChange);
  787. }
  788. } catch (RewritingException &e) {
  789. description = e.description();
  790. resetModel = true;
  791. }
  792. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  793. Q_ASSERT(view != 0);
  794. QList<BindingProperty> propertyList;
  795. foreach (const InternalBindingPropertyPointer &bindingProperty, internalBropertyList) {
  796. propertyList.append(BindingProperty(bindingProperty->name(), bindingProperty->propertyOwner(), model(), view.data()));
  797. }
  798. view->bindingPropertiesChanged(propertyList, propertyChange);
  799. }
  800. if (nodeInstanceView()) {
  801. QList<BindingProperty> propertyList;
  802. foreach (const InternalBindingPropertyPointer &bindingProperty, internalBropertyList) {
  803. propertyList.append(BindingProperty(bindingProperty->name(), bindingProperty->propertyOwner(), model(), nodeInstanceView()));
  804. }
  805. nodeInstanceView()->bindingPropertiesChanged(propertyList, propertyChange);
  806. }
  807. if (resetModel)
  808. resetModelByRewriter(description);
  809. }
  810. void ModelPrivate::notifyScriptFunctionsChanged(const InternalNodePointer &internalNodePointer, const QStringList &scriptFunctionList)
  811. {
  812. bool resetModel = false;
  813. QString description;
  814. try {
  815. if (rewriterView()) {
  816. ModelNode node(internalNodePointer, model(), rewriterView());
  817. rewriterView()->scriptFunctionsChanged(node, scriptFunctionList);
  818. }
  819. } catch (RewritingException &e) {
  820. description = e.description();
  821. resetModel = true;
  822. }
  823. if (nodeInstanceView()) {
  824. ModelNode node(internalNodePointer, model(), nodeInstanceView());
  825. nodeInstanceView()->scriptFunctionsChanged(node, scriptFunctionList);
  826. }
  827. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  828. Q_ASSERT(view != 0);
  829. ModelNode node(internalNodePointer, model(), view.data());
  830. view->scriptFunctionsChanged(node, scriptFunctionList);
  831. }
  832. if (resetModel)
  833. resetModelByRewriter(description);
  834. }
  835. void ModelPrivate::notifyVariantPropertiesChanged(const InternalNodePointer &internalNodePointer, const QStringList& propertyNameList, AbstractView::PropertyChangeFlags propertyChange)
  836. {
  837. bool resetModel = false;
  838. QString description;
  839. try {
  840. if (rewriterView()) {
  841. QList<VariantProperty> propertyList;
  842. foreach (const QString &propertyName, propertyNameList) {
  843. Q_ASSERT(internalNodePointer->hasProperty(propertyName));
  844. Q_ASSERT(internalNodePointer->property(propertyName)->isVariantProperty());
  845. VariantProperty property(propertyName, internalNodePointer, model(), rewriterView());
  846. propertyList.append(property);
  847. }
  848. ModelNode node(internalNodePointer, model(), rewriterView());
  849. rewriterView()->variantPropertiesChanged(propertyList, propertyChange);
  850. }
  851. } catch (RewritingException &e) {
  852. description = e.description();
  853. resetModel = true;
  854. }
  855. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  856. QList<VariantProperty> propertyList;
  857. Q_ASSERT(view != 0);
  858. foreach (const QString &propertyName, propertyNameList) {
  859. Q_ASSERT(internalNodePointer->hasProperty(propertyName));
  860. Q_ASSERT(internalNodePointer->property(propertyName)->isVariantProperty());
  861. VariantProperty property(propertyName, internalNodePointer, model(), view.data());
  862. propertyList.append(property);
  863. }
  864. ModelNode node(internalNodePointer, model(), view.data());
  865. view->variantPropertiesChanged(propertyList, propertyChange);
  866. }
  867. if (nodeInstanceView()) {
  868. QList<VariantProperty> propertyList;
  869. foreach (const QString &propertyName, propertyNameList) {
  870. Q_ASSERT(internalNodePointer->hasProperty(propertyName));
  871. Q_ASSERT(internalNodePointer->property(propertyName)->isVariantProperty());
  872. VariantProperty property(propertyName, internalNodePointer, model(), nodeInstanceView());
  873. propertyList.append(property);
  874. }
  875. ModelNode node(internalNodePointer, model(), nodeInstanceView());
  876. nodeInstanceView()->variantPropertiesChanged(propertyList, propertyChange);
  877. }
  878. if (resetModel)
  879. resetModelByRewriter(description);
  880. }
  881. void ModelPrivate::notifyNodeAboutToBeReparent(const InternalNodePointer &internalNodePointer, const InternalNodeAbstractPropertyPointer &newPropertyParent, const InternalNodePointer &oldParent, const QString &oldPropertyName, AbstractView::PropertyChangeFlags propertyChange)
  882. {
  883. bool resetModel = false;
  884. QString description;
  885. try {
  886. if (rewriterView()) {
  887. NodeAbstractProperty newProperty;
  888. NodeAbstractProperty oldProperty;
  889. if (!oldPropertyName.isEmpty() && oldParent->isValid())
  890. oldProperty = NodeAbstractProperty(oldPropertyName, oldParent, model(), rewriterView());
  891. if (!newPropertyParent.isNull())
  892. newProperty = NodeAbstractProperty(newPropertyParent, model(), rewriterView());
  893. ModelNode node(internalNodePointer, model(), rewriterView());
  894. rewriterView()->nodeAboutToBeReparented(node, newProperty, oldProperty, propertyChange);
  895. }
  896. } catch (RewritingException &e) {
  897. description = e.description();
  898. resetModel = true;
  899. }
  900. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  901. NodeAbstractProperty newProperty;
  902. NodeAbstractProperty oldProperty;
  903. Q_ASSERT(!view.isNull());
  904. if (!oldPropertyName.isEmpty() && oldParent->isValid())
  905. oldProperty = NodeAbstractProperty(oldPropertyName, oldParent, model(), view.data());
  906. if (!newPropertyParent.isNull())
  907. newProperty = NodeAbstractProperty(newPropertyParent, model(), view.data());
  908. ModelNode node(internalNodePointer, model(), view.data());
  909. view->nodeAboutToBeReparented(node, newProperty, oldProperty, propertyChange);
  910. }
  911. if (nodeInstanceView()) {
  912. NodeAbstractProperty newProperty;
  913. NodeAbstractProperty oldProperty;
  914. if (!oldPropertyName.isEmpty() && oldParent->isValid())
  915. oldProperty = NodeAbstractProperty(oldPropertyName, oldParent, model(), nodeInstanceView());
  916. if (!newPropertyParent.isNull())
  917. newProperty = NodeAbstractProperty(newPropertyParent, model(), nodeInstanceView());
  918. ModelNode node(internalNodePointer, model(), nodeInstanceView());
  919. nodeInstanceView()->nodeAboutToBeReparented(node, newProperty, oldProperty, propertyChange);
  920. }
  921. if (resetModel)
  922. resetModelByRewriter(description);
  923. }
  924. void ModelPrivate::notifyNodeReparent(const InternalNode::Pointer &internalNodePointer, const InternalNodeAbstractProperty::Pointer &newPropertyParent, const InternalNodePointer &oldParent, const QString &oldPropertyName, AbstractView::PropertyChangeFlags propertyChange)
  925. {
  926. bool resetModel = false;
  927. QString description;
  928. try {
  929. if (rewriterView()) {
  930. NodeAbstractProperty newProperty;
  931. NodeAbstractProperty oldProperty;
  932. if (!oldPropertyName.isEmpty() && oldParent->isValid())
  933. oldProperty = NodeAbstractProperty(oldPropertyName, oldParent, model(), rewriterView());
  934. if (!newPropertyParent.isNull())
  935. newProperty = NodeAbstractProperty(newPropertyParent, model(), rewriterView());
  936. ModelNode node(internalNodePointer, model(), rewriterView());
  937. rewriterView()->nodeReparented(node, newProperty, oldProperty, propertyChange);
  938. }
  939. } catch (RewritingException &e) {
  940. description = e.description();
  941. resetModel = true;
  942. }
  943. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  944. NodeAbstractProperty newProperty;
  945. NodeAbstractProperty oldProperty;
  946. Q_ASSERT(!view.isNull());
  947. if (!oldPropertyName.isEmpty() && oldParent->isValid())
  948. oldProperty = NodeAbstractProperty(oldPropertyName, oldParent, model(), view.data());
  949. if (!newPropertyParent.isNull())
  950. newProperty = NodeAbstractProperty(newPropertyParent, model(), view.data());
  951. ModelNode node(internalNodePointer, model(), view.data());
  952. view->nodeReparented(node, newProperty, oldProperty, propertyChange);
  953. }
  954. if (nodeInstanceView()) {
  955. NodeAbstractProperty newProperty;
  956. NodeAbstractProperty oldProperty;
  957. if (!oldPropertyName.isEmpty() && oldParent->isValid())
  958. oldProperty = NodeAbstractProperty(oldPropertyName, oldParent, model(), nodeInstanceView());
  959. if (!newPropertyParent.isNull())
  960. newProperty = NodeAbstractProperty(newPropertyParent, model(), nodeInstanceView());
  961. ModelNode node(internalNodePointer, model(), nodeInstanceView());
  962. nodeInstanceView()->nodeReparented(node, newProperty, oldProperty, propertyChange);
  963. }
  964. if (resetModel)
  965. resetModelByRewriter(description);
  966. }
  967. void ModelPrivate::notifyNodeOrderChanged(const InternalNodeListPropertyPointer &internalListPropertyPointer,
  968. const InternalNode::Pointer &internalNodePointer,
  969. int oldIndex)
  970. {
  971. bool resetModel = false;
  972. QString description;
  973. try {
  974. if (rewriterView())
  975. rewriterView()->nodeOrderChanged(NodeListProperty(internalListPropertyPointer, model(), rewriterView()),
  976. ModelNode(internalNodePointer, model(), rewriterView()),
  977. oldIndex);
  978. } catch (RewritingException &e) {
  979. description = e.description();
  980. resetModel = true;
  981. }
  982. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  983. Q_ASSERT(!view.isNull());
  984. view->nodeOrderChanged(NodeListProperty(internalListPropertyPointer, model(), view.data()),
  985. ModelNode(internalNodePointer, model(), view.data()),
  986. oldIndex);
  987. }
  988. if (nodeInstanceView())
  989. nodeInstanceView()->nodeOrderChanged(NodeListProperty(internalListPropertyPointer, model(), nodeInstanceView()),
  990. ModelNode(internalNodePointer, model(), nodeInstanceView()),
  991. oldIndex);
  992. if (resetModel)
  993. resetModelByRewriter(description);
  994. }
  995. void ModelPrivate::setSelectedNodes(const QList<InternalNode::Pointer> &selectedNodeList)
  996. {
  997. QList<InternalNode::Pointer> sortedSelectedList(selectedNodeList);
  998. QMutableListIterator<InternalNode::Pointer> iterator(sortedSelectedList);
  999. while (iterator.hasNext()) {
  1000. InternalNode::Pointer node(iterator.next());
  1001. if (!node->isValid())
  1002. iterator.remove();
  1003. }
  1004. sortedSelectedList = sortedSelectedList.toSet().toList();
  1005. qSort(sortedSelectedList);
  1006. if (sortedSelectedList == m_selectedNodeList)
  1007. return;
  1008. const QList<InternalNode::Pointer> lastSelectedNodeList = m_selectedNodeList;
  1009. m_selectedNodeList = sortedSelectedList;
  1010. changeSelectedNodes(sortedSelectedList, lastSelectedNodeList);
  1011. }
  1012. void ModelPrivate::clearSelectedNodes()
  1013. {
  1014. const QList<InternalNode::Pointer> lastSelectedNodeList = m_selectedNodeList;
  1015. m_selectedNodeList.clear();
  1016. changeSelectedNodes(m_selectedNodeList, lastSelectedNodeList);
  1017. }
  1018. QList<ModelNode> ModelPrivate::toModelNodeList(const QList<InternalNode::Pointer> &nodeList, AbstractView *view) const
  1019. {
  1020. QList<ModelNode> newNodeList;
  1021. foreach (const Internal::InternalNode::Pointer &node, nodeList)
  1022. newNodeList.append(ModelNode(node, model(), view));
  1023. return newNodeList;
  1024. }
  1025. QVector<ModelNode> ModelPrivate::toModelNodeVector(const QVector<InternalNode::Pointer> &nodeVector, AbstractView *view) const
  1026. {
  1027. QVector<ModelNode> newNodeVector;
  1028. foreach (const Internal::InternalNode::Pointer &node, nodeVector)
  1029. newNodeVector.append(ModelNode(node, model(), view));
  1030. return newNodeVector;
  1031. }
  1032. QList<Internal::InternalNode::Pointer> ModelPrivate::toInternalNodeList(const QList<ModelNode> &nodeList) const
  1033. {
  1034. QList<Internal::InternalNode::Pointer> newNodeList;
  1035. foreach (const ModelNode &node, nodeList)
  1036. newNodeList.append(node.internalNode());
  1037. return newNodeList;
  1038. }
  1039. QVector<Internal::InternalNode::Pointer> ModelPrivate::toInternalNodeVector(const QVector<ModelNode> &nodeVector) const
  1040. {
  1041. QVector<Internal::InternalNode::Pointer> newNodeVector;
  1042. foreach (const ModelNode &node, nodeVector)
  1043. newNodeVector.append(node.internalNode());
  1044. return newNodeVector;
  1045. }
  1046. void ModelPrivate::changeSelectedNodes(const QList<InternalNode::Pointer> &newSelectedNodeList,
  1047. const QList<InternalNode::Pointer> &oldSelectedNodeList)
  1048. {
  1049. foreach (const QWeakPointer<AbstractView> &view, m_viewList) {
  1050. Q_ASSERT(view != 0);
  1051. view->selectedNodesChanged(toModelNodeList(newSelectedNodeList, view.data()), toModelNodeList(oldSelectedNodeList, view.data()));
  1052. }
  1053. }
  1054. QList<InternalNode::Pointer> ModelPrivate::selectedNodes() const
  1055. {
  1056. foreach (const InternalNode::Pointer &node, m_selectedNodeList) {
  1057. if (!node->isValid())
  1058. throw new InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
  1059. }
  1060. return m_selectedNodeList;
  1061. }
  1062. void ModelPrivate::selectNode(const InternalNode::Pointer &node)
  1063. {
  1064. if (selectedNodes().contains(node))
  1065. return;
  1066. QList<InternalNode::Pointer> selectedNodeList(selectedNodes());
  1067. selectedNodeList += node;
  1068. setSelectedNodes(selectedNodeList);
  1069. }
  1070. void ModelPrivate::deselectNode(const InternalNode::Pointer &node)
  1071. {
  1072. QList<InternalNode::Pointer> selectedNodeList(selectedNodes());
  1073. bool isRemoved = selectedNodeList.removeOne(node);
  1074. if (!isRemoved)
  1075. return;
  1076. setSelectedNodes(selectedNodeList);
  1077. }
  1078. void ModelPrivate::removePropertyWithoutNotification(const InternalPropertyPointer &property)
  1079. {
  1080. if (property->isNodeAbstractProperty()) {
  1081. foreach (const InternalNode::Pointer & internalNode, property->toNodeAbstractProperty()->allSubNodes())
  1082. removeNodeFromModel(internalNode);
  1083. }
  1084. property->remove();
  1085. }
  1086. static QList<PropertyPair> toPropertyPairList(const QList<InternalProperty::Pointer> &propertyList)
  1087. {
  1088. QList<PropertyPair> propertyPairList;
  1089. foreach (const InternalProperty::Pointer &property, propertyList)
  1090. propertyPairList.append(qMakePair(property->propertyOwner(), property->name()));
  1091. return propertyPairList;
  1092. }
  1093. void ModelPrivate::removeProperty(const InternalProperty::Pointer &property)
  1094. {
  1095. notifyPropertiesAboutToBeRemoved(QList<InternalProperty::Pointer>() << property);
  1096. QList<PropertyPair> propertyPairList = toPropertyPairList(QList<InternalProperty::Pointer>() << property);
  1097. removePropertyWithoutNotification(property);
  1098. notifyPropertiesRemoved(propertyPairList);
  1099. }
  1100. void ModelPrivate::setBindingProperty(const InternalNode::Pointer &internalNode, const QString &name, const QString &expression)
  1101. {
  1102. AbstractView::PropertyChangeFlags propertyChange = AbstractView::NoAdditionalChanges;
  1103. if (!internalNode->hasProperty(name)) {
  1104. internalNode->addBindingProperty(name);
  1105. propertyChange = AbstractView::PropertiesAdded;
  1106. }
  1107. InternalBindingProperty::Pointer bindingProperty = internalNode->bindingProperty(name);
  1108. bindingProperty->setExpression(expression);
  1109. notifyBindingPropertiesChanged(QList<InternalBindingPropertyPointer>() << bindingProperty, propertyChange);
  1110. }
  1111. void ModelPrivate::setVariantProperty(const InternalNode::Pointer &internalNode, const QString &name, const QVariant &value)
  1112. {
  1113. AbstractView::PropertyChangeFlags propertyChange = AbstractView::NoAdditionalChanges;
  1114. if (!internalNode->hasProperty(name)) {
  1115. internalNode->addVariantProperty(name);
  1116. propertyChange = AbstractView::PropertiesAdded;
  1117. }
  1118. internalNode->variantProperty(name)->setValue(value);
  1119. internalNode->variantProperty(name)->resetDynamicTypeName();
  1120. notifyVariantPropertiesChanged(internalNode, QStringList() << name, propertyChange);
  1121. }
  1122. void ModelPrivate::setDynamicVariantProperty(const InternalNodePointer &internalNode, const QString &name, const QString &dynamicPropertyType, const QVariant &value)
  1123. {
  1124. AbstractView::PropertyChangeFlags propertyChange = AbstractView::NoAdditionalChanges;
  1125. if (!internalNode->hasProperty(name)) {
  1126. internalNode->addVariantProperty(name);
  1127. propertyChange = AbstractView::PropertiesAdded;
  1128. }
  1129. internalNode->variantProperty(name)->setDynamicValue(dynamicPropertyType, value);
  1130. notifyVariantPropertiesChanged(internalNode, QStringList() << name, propertyChange);
  1131. }
  1132. void ModelPrivate::setDynamicBindingProperty(const InternalNodePointer &internalNode, const QString &name, const QString &dynamicPropertyType, const QString &expression)
  1133. {
  1134. AbstractView::PropertyChangeFlags propertyChange = AbstractView::NoAdditionalChanges;
  1135. if (!internalNode->hasProperty(name)) {
  1136. internalNode->addBindingProperty(name);
  1137. propertyChange = AbstractView::PropertiesAdded;
  1138. }
  1139. InternalBindingProperty::Pointer bindingProperty = internalNode->bindingProperty(name);
  1140. bindingProperty->setDynamicExpression(dynamicPropertyType, expression);
  1141. notifyBindingPropertiesChanged(QList<InternalBindingPropertyPointer>() << bindingProperty, propertyChange);
  1142. }
  1143. void ModelPrivate::reparentNode(const InternalNode::Pointer &newParentNode, const QString &name, const InternalNode::Pointer &node, bool list)
  1144. {
  1145. AbstractView::PropertyChangeFlags propertyChange = AbstractView::NoAdditionalChanges;
  1146. if (!newParentNode->hasProperty(name)) {
  1147. if (list)
  1148. newParentNode->addNodeListProperty(name);
  1149. else
  1150. newParentNode->addNodeProperty(name);
  1151. propertyChange |= AbstractView::PropertiesAdded;
  1152. }
  1153. InternalNodeAbstractProperty::Pointer oldParentProperty(node->parentProperty());
  1154. InternalNode::Pointer oldParentNode;
  1155. QString oldParentPropertyName;
  1156. if (oldParentProperty && oldParentProperty->isValid()) {
  1157. oldParentNode = node->parentProperty()->propertyOwner();
  1158. oldParentPropertyName = node->parentProperty()->name();
  1159. }
  1160. InternalNodeAbstractProperty::Pointer newParentProperty(newParentNode->nodeAbstractProperty(name));
  1161. Q_ASSERT(!newParentProperty.isNull());
  1162. notifyNodeAboutToBeReparent(node, newParentProperty, oldParentNode, oldParentPropertyName, propertyChange);
  1163. if (newParentProperty)
  1164. node->setParentProperty(newParentProperty);
  1165. if (oldParentProperty && oldParentProperty->isValid() && oldParentProperty->isEmpty()) {
  1166. removePropertyWithoutNotification(oldParentProperty);
  1167. propertyChange |= AbstractView::EmptyPropertiesRemoved;
  1168. }
  1169. notifyNodeReparent(node, newParentProperty, oldParentNode, oldParentPropertyName, propertyChange);
  1170. }
  1171. void ModelPrivate::clearParent(const InternalNodePointer &node)
  1172. {
  1173. InternalNodeAbstractProperty::Pointer oldParentProperty(node->parentProperty());
  1174. InternalNode::Pointer oldParentNode;
  1175. QString oldParentPropertyName;
  1176. if (oldParentProperty->isValid()) {
  1177. oldParentNode = node->parentProperty()->propertyOwner();
  1178. oldParentPropertyName = node->parentProperty()->name();
  1179. }
  1180. node->resetParentProperty();
  1181. notifyNodeReparent(node, InternalNodeAbstractProperty::Pointer(), oldParentNode, oldParentPropertyName, AbstractView::NoAdditionalChanges);
  1182. }
  1183. void ModelPrivate::changeRootNodeType(const QString &type, int majorVersion, int minorVersion)
  1184. {
  1185. Q_ASSERT(!rootNode().isNull());
  1186. rootNode()->setType(type);
  1187. rootNode()->setMajorVersion(majorVersion);
  1188. rootNode()->setMinorVersion(minorVersion);
  1189. notifyRootNodeTypeChanged(type, majorVersion, minorVersion);
  1190. }
  1191. void ModelPrivate::setScriptFunctions(const InternalNode::Pointer &internalNode, const QStringList &scriptFunctionList)
  1192. {
  1193. internalNode->setScriptFunctions(scriptFunctionList);
  1194. notifyScriptFunctionsChanged(internalNode, scriptFunctionList);
  1195. }
  1196. void ModelPrivate::setNodeSource(const InternalNodePointer &internalNode, const QString &nodeSource)
  1197. {
  1198. internalNode->setNodeSource(nodeSource);
  1199. notifyNodeSourceChanged(internalNode, nodeSource);
  1200. }
  1201. void ModelPrivate::changeNodeOrder(const InternalNode::Pointer &internalParentNode, const QString &listPropertyName, int from, int to)
  1202. {
  1203. InternalNodeListProperty::Pointer nodeList(internalParentNode->nodeListProperty(listPropertyName));
  1204. Q_ASSERT(!nodeList.isNull());
  1205. nodeList->slide(from, to);
  1206. const InternalNodePointer internalNode = nodeList->nodeList().at(to);
  1207. notifyNodeOrderChanged(nodeList, internalNode, from);
  1208. }
  1209. void ModelPrivate::setRewriterView(RewriterView *rewriterView)
  1210. {
  1211. if (rewriterView == m_rewriterView.data())
  1212. return;
  1213. Q_ASSERT(!(rewriterView && m_rewriterView));
  1214. m_rewriterView = rewriterView;
  1215. if (rewriterView)
  1216. rewriterView->modelAttached(model());
  1217. else if (m_rewriterView)
  1218. m_rewriterView->modelAboutToBeDetached(model());
  1219. }
  1220. RewriterView *ModelPrivate::rewriterView() const
  1221. {
  1222. return m_rewriterView.data();
  1223. }
  1224. void ModelPrivate::setNodeInstanceView(NodeInstanceView *nodeInstanceView)
  1225. {
  1226. if (nodeInstanceView == m_nodeInstanceView.data())
  1227. return;
  1228. if (m_nodeInstanceView)
  1229. m_nodeInstanceView->modelAboutToBeDetached(m_q);
  1230. m_nodeInstanceView = nodeInstanceView;
  1231. if (nodeInstanceView)
  1232. nodeInstanceView->modelAttached(m_q);
  1233. }
  1234. NodeInstanceView *ModelPrivate::nodeInstanceView() const
  1235. {
  1236. return m_nodeInstanceView.data();
  1237. }
  1238. InternalNodePointer ModelPrivate::nodeForId(const QString &id) const
  1239. {
  1240. return m_idNodeHash.value(id);
  1241. }
  1242. bool ModelPrivate::hasId(const QString &id) const
  1243. {
  1244. return m_idNodeHash.contains(id);
  1245. }
  1246. InternalNodePointer ModelPrivate::nodeForInternalId(qint32 internalId) const
  1247. {
  1248. return m_internalIdNodeHash.value(internalId);
  1249. }
  1250. bool ModelPrivate::hasNodeForInternalId(qint32 internalId) const
  1251. {
  1252. return m_internalIdNodeHash.contains(internalId);
  1253. }
  1254. QList<InternalNodePointer> ModelPrivate::allNodes() const
  1255. {
  1256. // the item must be ordered!
  1257. QList<InternalNodePointer> nodeList;
  1258. if (m_rootInternalNode.isNull() || !m_rootInternalNode->isValid())
  1259. return nodeList;
  1260. nodeList.append(m_rootInternalNode);
  1261. nodeList.append(m_rootInternalNode->allSubNodes());
  1262. nodeList.append((m_nodeSet - nodeList.toSet()).toList());
  1263. return nodeList;
  1264. }
  1265. bool ModelPrivate::isWriteLocked() const
  1266. {
  1267. return m_writeLock;
  1268. }
  1269. InternalNode::Pointer ModelPrivate::actualStateNode() const
  1270. {
  1271. return m_acutalStateNode;
  1272. }
  1273. WriteLocker::WriteLocker(ModelPrivate *model)
  1274. : m_model(model)
  1275. {
  1276. Q_ASSERT(model);
  1277. if (m_model->m_writeLock)
  1278. qWarning() << "QmlDesigner: Misbehaving view calls back to model!!!";
  1279. // FIXME: Enable it again
  1280. Q_ASSERT(!m_model->m_writeLock);
  1281. model->m_writeLock = true;
  1282. }
  1283. WriteLocker::WriteLocker(Model *model)
  1284. : m_model(model->d)
  1285. {
  1286. Q_ASSERT(model->d);
  1287. if (m_model->m_writeLock)
  1288. qWarning() << "QmlDesigner: Misbehaving view calls back to model!!!";
  1289. // FIXME: Enable it again
  1290. Q_ASSERT(!m_model->m_writeLock);
  1291. m_model->m_writeLock = true;
  1292. }
  1293. WriteLocker::~WriteLocker()
  1294. {
  1295. if (!m_model->m_writeLock)
  1296. qWarning() << "QmlDesigner: Misbehaving view calls back to model!!!";
  1297. // FIXME: Enable it again
  1298. Q_ASSERT(m_model->m_writeLock);
  1299. m_model->m_writeLock = false;
  1300. }
  1301. } //namespace internal
  1302. Model::Model() :
  1303. QObject(),
  1304. d(new Internal::ModelPrivate(this))
  1305. {
  1306. }
  1307. Model::~Model()
  1308. {
  1309. delete d;
  1310. }
  1311. Model *Model::create(QString type, int major, int minor, Model *metaInfoPropxyModel)
  1312. {
  1313. return Internal::ModelPrivate::create(type, major, minor, metaInfoPropxyModel);
  1314. }
  1315. QList<Import> Model::imports() const
  1316. {
  1317. return d->imports();
  1318. }
  1319. void Model::changeImports(const QList<Import> &importsToBeAdded, const QList<Import> &importsToBeRemoved)
  1320. {
  1321. d->changeImports(importsToBeAdded, importsToBeRemoved);
  1322. }
  1323. static bool compareVersions(const QString &version1, const QString &version2, bool allowHigherVersion)
  1324. {
  1325. if (version1 == version2)
  1326. return true;
  1327. if (!allowHigherVersion)
  1328. return false;
  1329. QStringList version1List = version1.split('.');
  1330. QStringList version2List = version2.split('.');
  1331. if (version1List.count() == 2 && version2List.count() == 2) {
  1332. bool ok;
  1333. int major1 = version1List.first().toInt(&ok);
  1334. if (!ok)
  1335. return false;
  1336. int major2 = version2List.first().toInt(&ok);
  1337. if (!ok)
  1338. return false;
  1339. if (major1 >= major2) {
  1340. int minor1 = version1List.last().toInt(&ok);
  1341. if (!ok)
  1342. return false;
  1343. int minor2 = version2List.last().toInt(&ok);
  1344. if (!ok)
  1345. return false;
  1346. if (minor1 >= minor2)
  1347. return true;
  1348. }
  1349. }
  1350. return false;
  1351. }
  1352. bool Model::hasImport(const Import &import, bool ignoreAlias, bool allowHigherVersion)
  1353. {
  1354. if (imports().contains(import))
  1355. return true;
  1356. if (!ignoreAlias)
  1357. return false;
  1358. foreach (const Import &existingImport, imports()) {
  1359. if (existingImport.isFileImport() && import.isFileImport())
  1360. if (existingImport.file() == import.file() && compareVersions(existingImport.version(), import.version(), allowHigherVersion))
  1361. return true;
  1362. if (existingImport.isLibraryImport() && import.isLibraryImport())
  1363. if (existingImport.url() == import.url() && compareVersions(existingImport.version(), import.version(), allowHigherVersion))
  1364. return true;
  1365. }
  1366. return false;
  1367. }
  1368. QString Model::pathForImport(const Import &import)
  1369. {
  1370. if (!rewriterView())
  1371. return QString();
  1372. return rewriterView()->pathForImport(import);
  1373. }
  1374. RewriterView *Model::rewriterView() const
  1375. {
  1376. return d->rewriterView();
  1377. }
  1378. /*!
  1379. \brief Returns the model that is used for metainfo
  1380. \return Return itself if not other metaInfoProxyModel does exist
  1381. */
  1382. Model *Model::metaInfoProxyModel()
  1383. {
  1384. if (d->m_metaInfoProxyModel)
  1385. return d->m_metaInfoProxyModel->metaInfoProxyModel();
  1386. else
  1387. return this;
  1388. }
  1389. #if 0
  1390. /*!
  1391. \brief Creates a new empty model
  1392. \param uiFilePath path to the ui file
  1393. \param[out] errorMessage returns a error message
  1394. \return new created model
  1395. */
  1396. Model *Model::create(const QString &rootType)
  1397. {
  1398. return Internal::ModelPrivate::create(rootType);
  1399. }
  1400. #endif
  1401. Model *Model::masterModel() const
  1402. {
  1403. return d->m_masterModel.data();
  1404. }
  1405. void Model::setMasterModel(Model *model)
  1406. {
  1407. d->m_masterModel = model;
  1408. }
  1409. /*!
  1410. \brief Returns the URL against which relative URLs within the model should be resolved.
  1411. \return The base URL.
  1412. */
  1413. QUrl Model::fileUrl() const
  1414. {
  1415. return d->fileUrl();
  1416. }
  1417. /*!
  1418. \brief Sets the URL against which relative URLs within the model should be resolved.
  1419. \param url the base URL, i.e. the qml file path.
  1420. */
  1421. void Model::setFileUrl(const QUrl &url)
  1422. {
  1423. Internal::WriteLocker locker(d);
  1424. d->setFileUrl(url);
  1425. }
  1426. /*!
  1427. \brief Returns list of QML types available within the model.
  1428. */
  1429. const MetaInfo Model::metaInfo() const
  1430. {
  1431. return d->metaInfo();
  1432. }
  1433. bool Model::hasNodeMetaInfo(const QString &typeName, int majorVersion, int minorVersion)
  1434. {
  1435. return NodeMetaInfo(metaInfoProxyModel(), typeName, majorVersion, minorVersion).isValid();
  1436. }
  1437. NodeMetaInfo Model::metaInfo(const QString &typeName, int majorVersion, int minorVersion)
  1438. {
  1439. return NodeMetaInfo(metaInfoProxyModel(), typeName, majorVersion, minorVersion);
  1440. }
  1441. /*!
  1442. \brief Returns list of QML types available within the model.
  1443. */
  1444. MetaInfo Model::metaInfo()
  1445. {
  1446. return d->metaInfo();
  1447. }
  1448. /*! \name Undo Redo Interface
  1449. here you can find a facade to the internal undo redo framework
  1450. */
  1451. /*! \name View related functions
  1452. */
  1453. //\{
  1454. /*!
  1455. \brief Attaches a view to the model
  1456. Registers a "view" that from then on will be informed about changes to the model. Different views
  1457. will always be informed in the order in which they registered to the model.
  1458. The view is informed that it has been registered within the model by a call to AbstractView::modelAttached .
  1459. \param view The view object to register. Must be not null.
  1460. \see detachView
  1461. */
  1462. void Model::attachView(AbstractView *view)
  1463. {
  1464. // Internal::WriteLocker locker(d);
  1465. RewriterView *rewriterView = qobject_cast<RewriterView*>(view);
  1466. if (rewriterView) {
  1467. d->setRewriterView(rewriterView);
  1468. return;
  1469. }
  1470. NodeInstanceView *nodeInstanceView = qobject_cast<NodeInstanceView*>(view);
  1471. if (nodeInstanceView) {
  1472. d->setNodeInstanceView(nodeInstanceView);
  1473. return;
  1474. }
  1475. d->attachView(view);
  1476. }
  1477. /*!
  1478. \brief Detaches a view to the model
  1479. \param view The view to unregister. Must be not null.
  1480. \param emitDetachNotify If set to NotifyView (the default), AbstractView::modelAboutToBeDetached() will be called
  1481. \see attachView
  1482. */
  1483. void Model::detachView(AbstractView *view, ViewNotification emitDetachNotify)
  1484. {
  1485. // Internal::WriteLocker locker(d);
  1486. bool emitNotify = (emitDetachNotify == NotifyView);
  1487. RewriterView *rewriterView = qobject_cast<RewriterView*>(view);
  1488. if (rewriterView) {
  1489. d->setRewriterView(0);
  1490. return;
  1491. }
  1492. NodeInstanceView *nodeInstanceView = qobject_cast<NodeInstanceView*>(view);
  1493. if (nodeInstanceView) {
  1494. d->setNodeInstanceView(0);
  1495. return;
  1496. }
  1497. d->detachView(view, emitNotify);
  1498. }
  1499. }