OOModel::Expression* CompoundObjectDescriptor::create(const QList<OOModel::Expression*>& operands)
{
	Q_ASSERT(operands.size() == 1);
	auto ilit = dynamic_cast<OOModel::IntegerLiteral*> (operands.first());
	Q_ASSERT(ilit);
	auto e = storedExpressions().value(ilit->value());
	SAFE_DELETE(ilit);
	Q_ASSERT(e);

	if (auto model = e->model())
	{
		auto beingModified = model->isBeingModified();
		auto oldModificationTarget = beingModified ? nullptr : model->modificationTarget();
		auto oldParent = e->parent();

		Q_ASSERT(e!=oldModificationTarget && !e->isAncestorOf(oldModificationTarget));

		if (!beingModified) oldParent->beginModification("extract expression");
		else model->changeModificationTarget(oldParent);
		auto replaceSuccessfull = oldParent->replaceChild(e, new OOModel::EmptyExpression());
		Q_ASSERT(replaceSuccessfull);

		if (!beingModified) oldParent->endModification();
		else model->changeModificationTarget(oldModificationTarget);
	}
	else
	{
		auto replaceSuccessfull = e->parent()->replaceChild(e, new OOModel::EmptyExpression());
		Q_ASSERT(replaceSuccessfull);
	}

	return e;
}
Example #2
0
Interaction::CommandResult* CCreateClass::create(Visualization::Item* /*source*/, Visualization::Item* target,
	const QString& name, const QStringList& attributes)
{
	auto pr = dynamic_cast<OOModel::Project*> (target->node());

	OOModel::Class* cl = nullptr;
	bool newModel = false;
	if (pr)
	{
		cl = new OOModel::Class();
		if (!name.isEmpty()) cl->setName(name);

		// Set visibility
		if (attributes.first() == "private" ) cl->setVisibility(OOModel::Visibility::PRIVATE);
		else if (attributes.first()  == "protected" ) cl->setVisibility(OOModel::Visibility::PROTECTED);
		else if (attributes.first()  == "public" ) cl->setVisibility(OOModel::Visibility::PUBLIC);
		else cl->setVisibility(OOModel::Visibility::DEFAULT);

		pr->beginModification("create class");
		pr->classes()->append(cl);
		pr->endModification();
	}
	else
	{
		newModel = true;
		auto model = new Model::Model();
		cl = dynamic_cast<OOModel::Class*> (model->createRoot("Class"));

		cl->beginModification("set project name");

		if (!name.isEmpty()) cl->setName(name);

		// Set visibility
		if (attributes.first() == "private" ) cl->setVisibility(OOModel::Visibility::PRIVATE);
		else if (attributes.first()  == "protected" ) cl->setVisibility(OOModel::Visibility::PROTECTED);
		else if (attributes.first()  == "public" ) cl->setVisibility(OOModel::Visibility::PUBLIC);
		else cl->setVisibility(OOModel::Visibility::DEFAULT);

		cl->endModification();

		target->scene()->addTopLevelItem( new Visualization::RootItem(cl) );
		target->scene()->listenToModel(model);
	}

	target->setUpdateNeeded(Visualization::Item::StandardUpdate);
		if (newModel) target->scene()->addPostEventAction(new Interaction::SetCursorEvent(target->scene(), cl,
				Interaction::SetCursorEvent::CursorDefault, true));
		else target->scene()->addPostEventAction(new Interaction::SetCursorEvent(target, cl,
				Interaction::SetCursorEvent::CursorDefault, true));

	return new Interaction::CommandResult();
}
Model::Node* ValueAtReturnVisitor::visitChildren(Model::Node* n)
{
    for (auto child : n->children())
    {
        auto newChild = visit(child);
        if (newChild != child)
        {
            auto model = n->model();
            model->beginModification(n, "replace child in visitor");
            n->replaceChild(child, newChild, false);
            model->endModification();
        }
    }

    return n;
}
Model::Node* ValueAtReturnVisitor::visitMethodCall(ValueAtReturnVisitor* v, OOModel::MethodCallExpression* call)
{
    if (ensuresMethod_ && call->ref()->target() == ensuresMethod_)
    {
        v->inEnsuresCall_ = true;
        v->visitChildren(call);
        v->inEnsuresCall_ = false;
        return call;
    }

    if (v->inEnsuresCall_ &&
            valueAtReturnMethod_ && call->ref()->target() == valueAtReturnMethod_ && call->arguments()->size() == 1)
    {
        v->inValueAtReturnCall_ = true;
        v->outReference_ = false;
        v->visitChildren(call);
        v->inValueAtReturnCall_ = false;

        if (!v->outReference_)
        {
            // Replace this method call by its arguments
            auto arg = call->arguments()->at(0);
            auto model = call->model();
            model->beginModification(call, "remove valueAtReturn call in visitor");
            call->arguments()->remove(0, true);
            model->endModification();
            ++v->numUnwrapped_;

            return arg;
        }
        v->outReference_ = false;
        return call;
    }

    return call;
}