diff options
author | Sandro S. Andrade <sandroandrade@kde.org> | 2013-09-04 07:26:59 -0300 |
---|---|---|
committer | Sandro S. Andrade <sandroandrade@kde.org> | 2013-09-04 12:24:26 +0200 |
commit | df7fb2f660e151d0c5fbe62f90be9437ed190087 (patch) | |
tree | d001e89e61a00cd00e23b2b6a042bc55aed150d0 | |
parent | c9b9fb27ccfef6f23ce89eb7a54ed1ffa0bdd697 (diff) |
Add clone() method in template files
Change-Id: I345f997a2002bb97d4d243e4b2286865728292d8
Reviewed-by: Sandro S. Andrade <sandroandrade@kde.org>
-rw-r--r-- | examples/uml/nested-packages/main.cpp | 13 | ||||
-rw-r--r-- | scripts/templates/common.tmpl | 27 | ||||
-rw-r--r-- | scripts/templates/qclass.cpp | 11 | ||||
-rw-r--r-- | scripts/templates/qclass.h | 2 | ||||
-rw-r--r-- | src/modeling/qmodelingobject.h | 3 |
5 files changed, 54 insertions, 2 deletions
diff --git a/examples/uml/nested-packages/main.cpp b/examples/uml/nested-packages/main.cpp index d0b3c34a..d78c48f2 100644 --- a/examples/uml/nested-packages/main.cpp +++ b/examples/uml/nested-packages/main.cpp @@ -49,13 +49,23 @@ int main () c2->asQObject()->setObjectName("c2"); QUmlComment *c3 = new QUmlComment; c3->asQObject()->setObjectName("c3"); + c3->setBody("c3 body"); - c2->setBody("Teste"); + c1->setBody("c1 body"); + c2->setBody("c2 body"); c1->addOwnedComment(c2); c1->addOwnedComment(c3); qDebug() << "c2's parent:" << c2->asQObject()->parent()->objectName(); qDebug() << "c3's parent:" << c3->asQObject()->parent()->objectName(); + QUmlComment *c4 = dynamic_cast<QUmlComment *>(c1->clone()); + qDebug() << "c1 body:" << c4->body(); + foreach (QUmlComment *comment, c1->ownedComment()) + qDebug() << "c1 owned comment body:" << comment->body(); + qDebug() << "c4 body:" << c4->body(); + foreach (QUmlComment *comment, c4->ownedComment()) + qDebug() << "c4 owned comment body:" << comment->body(); + //c1->removeOwnedComment(c3); qDebug() << "c3's parent:" << c3->asQObject()->parent(); @@ -70,5 +80,6 @@ int main () qDebug() << c->objectName(); delete c1; + delete c4; //delete c3; } diff --git a/scripts/templates/common.tmpl b/scripts/templates/common.tmpl index 96d7b63c..442825ee 100644 --- a/scripts/templates/common.tmpl +++ b/scripts/templates/common.tmpl @@ -394,4 +394,29 @@ void Q${namespace}${originalClassName}Object::set${attributeName.remove("^Is")}( [%- IF forwardName != className -%][%- forwards.push("Q${namespace}${forwardName}") -%][%- END -%] [%- END -%] [%- END -%] -[%- END -%]
\ No newline at end of file +[%- END -%] +[%- MACRO GENERATE_CLONE(class, visitedClasses, redefinedProperties) BLOCK -%] +[%- FOREACH parent IN class.findnodes("generalization") -%] + [%- SET parentName = parent.findvalue("@general") -%] + [%- IF visitedClasses.grep("^${parentName}$").size == 0 -%] + [%- visitedClasses.push("${parentName}") -%] + [%- GENERATE_CLONE(xmi.findnodes("//packagedElement[@xmi:type=\"uml:Class\" and @name=\"${parentName}\"]"), visitedClasses, redefinedProperties) -%] + [%- END -%] +[%- END -%] +[%- FOREACH attribute = class.findnodes("ownedAttribute[@isDerived=\"false\" or not(@isDerived)]") -%] + [%- SET qtType = QT_TYPE(namespace, attribute) -%] + [%- SET attributeName = attribute.findvalue("@name") -%] + [%- SET className = class.findvalue("@name") -%] + [%- SET qtAttribute = QT_ATTRIBUTE(attribute) -%] + [%- NEXT IF redefinedProperties.grep("^${className}-${qtAttribute}$").size > 0 -%] + [%- IF qtType.match("QList|QSet") && qtType.match('\*') %] + foreach (${qtType.remove("QList<").remove("QSet<").remove(">").replace('\* ', '*')}element, ${attributeName}()) + c->add${attributeName.ucfirst}(dynamic_cast<${qtType.remove("QList<").remove("QSet<").remove(">").replace('\* ', '*')}>(element->clone())); + [%- ELSIF qtType.match('\*') %] + if (${attributeName}()) + c->set${attributeName.ucfirst}(dynamic_cast<${qtType.remove("QList<").remove("QSet<").remove(">").replace('\* ', '*')}>(${attributeName}()->clone())); + [%- ELSE %] + c->set${attributeName.ucfirst}(${attributeName}()); + [%- END -%] +[%- END %] +[%- END -%] diff --git a/scripts/templates/qclass.cpp b/scripts/templates/qclass.cpp index 8e755719..70ca0bab 100644 --- a/scripts/templates/qclass.cpp +++ b/scripts/templates/qclass.cpp @@ -135,6 +135,17 @@ Q${namespace}${className}::~Q${namespace}${className}() } [%- END %] } + +QModelingObject *Q${namespace}${className}::clone() const +{ + Q${namespace}${className} *c = new Q${namespace}${className}; +[%- visitedClasses = [] -%] +[%- redefinedProperties = [] -%] +[%- POPULATE_REDEFINED_PROPERTIES(class, visitedClasses, redefinedProperties) -%] +[%- visitedClasses = [] -%] +[%- GENERATE_CLONE(class, visitedClasses, redefinedProperties) %] + return c; +} [%- FOREACH attribute = class.findnodes("ownedAttribute") %] [%- IF loop.first %] diff --git a/scripts/templates/qclass.h b/scripts/templates/qclass.h index 4e736811..1a29d7aa 100644 --- a/scripts/templates/qclass.h +++ b/scripts/templates/qclass.h @@ -97,6 +97,8 @@ public: explicit Q${namespace}${className}(bool createQObject = true); [%- END %] virtual ~Q${namespace}${className}(); + + [% IF class.findvalue("@isAbstract") == "true" %]Q_DECL_HIDDEN [% END %]QModelingObject *clone() const; [% FOREACH attribute = class.findnodes("ownedAttribute") -%] [%- IF loop.first %] // Owned attributes diff --git a/src/modeling/qmodelingobject.h b/src/modeling/qmodelingobject.h index fba397e1..3ed1c94b 100644 --- a/src/modeling/qmodelingobject.h +++ b/src/modeling/qmodelingobject.h @@ -64,6 +64,9 @@ public: inline QObject *asQObject() { return _qObject; } bool deletingFromQObject; + + virtual QModelingObject *clone() const = 0; + protected: QModelingObject() : deletingFromQObject(false), _qObject(0) {} QPointer<QObject> _qObject; |