Commit 7d79f4f8 authored by Erik Verbruggen's avatar Erik Verbruggen
Browse files

Changed import reading to use the DifferenceHandler.

parent 2e0fa4fc
......@@ -57,7 +57,13 @@ class ModelToTextMerger
public:
ModelToTextMerger(RewriterView *reWriterView);
void applyChanges(); // Note: his method might throw exceptions, as the model works this way. So to handle rewriting failures, you will also need to catch any exception coming out.
/**
* Note: his method might throw exceptions, as the model works this way. So to
* handle rewriting failures, you will also need to catch any exception coming
* out.
*/
void applyChanges();
void nodeCreated(const ModelNode &createdNode);
void nodeRemoved(const ModelNode &removedNode, const NodeAbstractProperty &parentProperty, PropertyChangeFlags propertyChange);
......
......@@ -68,20 +68,33 @@ bool TextToModelMerger::isActive() const
return m_isActive;
}
void TextToModelMerger::setupImports(QmlDomDocument &doc)
void TextToModelMerger::setupImports(QmlDomDocument &doc,
DifferenceHandler &differenceHandler)
{
foreach (const Import &import, m_rewriterView->model()->imports())
m_rewriterView->model()->removeImport(import);
QSet<Import> existingImports = m_rewriterView->model()->imports();
foreach (const QmlDomImport &qmlImport, doc.imports()) {
if (qmlImport.type() == QmlDomImport::Library) {
Import import(Import::createLibraryImport(QUrl(qmlImport.uri()), qmlImport.version(), qmlImport.qualifier()));
m_rewriterView->model()->addImport(import);
}
if (qmlImport.type() == QmlDomImport::File) {
Import import(Import:: createFileImport(qmlImport.uri(), qmlImport.version(), qmlImport.qualifier()));
m_rewriterView->model()->addImport(import);
Import import(Import::createLibraryImport(QUrl(qmlImport.uri()),
qmlImport.version(),
qmlImport.qualifier()));
if (!existingImports.remove(import))
differenceHandler.modelMissesImport(m_rewriterView->model(),
import);
} else if (qmlImport.type() == QmlDomImport::File) {
Import import(Import:: createFileImport(qmlImport.uri(),
qmlImport.version(),
qmlImport.qualifier()));
if (!existingImports.remove(import))
differenceHandler.modelMissesImport(m_rewriterView->model(),
import);
}
}
foreach (const Import &import, existingImports)
differenceHandler.importAbsentInQMl(m_rewriterView->model(), import);
}
bool TextToModelMerger::load(const QByteArray &data, DifferenceHandler &differenceHandler)
......@@ -95,7 +108,7 @@ bool TextToModelMerger::load(const QByteArray &data, DifferenceHandler &differen
const bool success = doc.load(&engine, data, url);
if (success) {
setupImports(doc);
setupImports(doc, differenceHandler);
const QmlDomObject rootDomObject = doc.rootObject();
ModelNode modelRootNode = m_rewriterView->rootModelNode();
......@@ -370,6 +383,16 @@ QVariant TextToModelMerger::convertToVariant(const ModelNode &node, const QmlDom
}
}
void ModelValidator::modelMissesImport(Model *model, const Import &import)
{
Q_ASSERT(model->imports().contains(import));
}
void ModelValidator::importAbsentInQMl(Model *model, const Import &import)
{
Q_ASSERT(! model->imports().contains(import));
}
void ModelValidator::bindingExpressionsDiffer(BindingProperty &modelProperty, const QString &qmlBinding)
{
Q_ASSERT(modelProperty.expression() == qmlBinding);
......@@ -443,6 +466,16 @@ void ModelValidator::idsDiffer(ModelNode &modelNode, const QString &qmlId)
Q_ASSERT(0);
}
void ModelAmender::modelMissesImport(Model *model, const Import &import)
{
model->addImport(import);
}
void ModelAmender::importAbsentInQMl(Model *model, const Import &import)
{
model->removeImport(import);
}
void ModelAmender::bindingExpressionsDiffer(BindingProperty &modelProperty, const QString &qmlBinding)
{
modelProperty.toBindingProperty().setExpression(qmlBinding);
......
......@@ -31,6 +31,7 @@
#define TEXTTOMODELMERGER_H
#include "corelib_global.h"
#include "import.h"
#include "nodelistproperty.h"
#include "modelnode.h"
......@@ -53,7 +54,7 @@ public:
TextToModelMerger(RewriterView *reWriterView);
bool isActive() const;
void setupImports(QmlDomDocument &doc);
void setupImports(QmlDomDocument &doc, DifferenceHandler &differenceHandler);
bool load(const QByteArray &data, DifferenceHandler &differenceHandler);
protected:
......@@ -95,6 +96,8 @@ public:
virtual ~DifferenceHandler()
{}
virtual void modelMissesImport(Model *model, const Import &import) = 0;
virtual void importAbsentInQMl(Model *model, const Import &import) = 0;
virtual void bindingExpressionsDiffer(BindingProperty &modelProperty, const QString &qmlBinding) = 0;
virtual void shouldBeBindingProperty(AbstractProperty &modelProperty, const QString &qmlBinding) = 0;
virtual void shouldBeNodeListProperty(AbstractProperty &modelProperty, const QmlDomList &domList) = 0;
......@@ -120,6 +123,8 @@ public:
~ModelValidator()
{}
virtual void modelMissesImport(Model *model, const Import &import);
virtual void importAbsentInQMl(Model *model, const Import &import);
virtual void bindingExpressionsDiffer(BindingProperty &modelProperty, const QString &qmlBinding);
virtual void shouldBeBindingProperty(AbstractProperty &modelProperty, const QString &qmlBinding);
virtual void shouldBeNodeListProperty(AbstractProperty &modelProperty, const QmlDomList &domList);
......@@ -142,6 +147,8 @@ public:
~ModelAmender()
{}
virtual void modelMissesImport(Model *model, const Import &import);
virtual void importAbsentInQMl(Model *model, const Import &import);
virtual void bindingExpressionsDiffer(BindingProperty &modelProperty, const QString &qmlBinding);
virtual void shouldBeBindingProperty(AbstractProperty &modelProperty, const QString &qmlBinding);
virtual void shouldBeNodeListProperty(AbstractProperty &modelProperty, const QmlDomList &domList);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment