00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kptrelation.h"
00021
00022 #include "kptnode.h"
00023 #include "kptproject.h"
00024 #include "kptcanvasitem.h"
00025
00026 #include <qdom.h>
00027
00028 #include <kdebug.h>
00029
00030 namespace KPlato
00031 {
00032
00033 Relation::Relation(Node *parent, Node *child, Type type, Duration lag) {
00034 m_parent=parent;
00035 m_child=child;
00036 m_type=type;
00037 m_lag=lag;
00038
00039 }
00040
00041 Relation::Relation(Node *parent, Node *child, Type type) {
00042 m_parent=parent;
00043 m_child=child;
00044 m_type=type;
00045 m_lag=Duration();
00046
00047 }
00048
00049 Relation::Relation(Relation *rel) {
00050 m_parent=rel->parent();
00051 m_child=rel->child();
00052 m_type=rel->type();
00053 m_lag=rel->lag();
00054
00055 }
00056
00057 Relation::~Relation() {
00058
00059 if (m_parent)
00060 m_parent->takeDependChildNode(this);
00061 if (m_child)
00062 m_child->takeDependParentNode(this);
00063 }
00064
00065 void Relation::setType(Type type) {
00066 m_type=type;
00067 }
00068
00069
00070 bool Relation::load(QDomElement &element, Project &project) {
00071 m_parent = project.findNode(element.attribute("parent-id"));
00072 if (m_parent == 0) {
00073 return false;
00074 }
00075 m_child = project.findNode(element.attribute("child-id"));
00076 if (m_child == 0) {
00077 return false;
00078 }
00079 if (m_child == m_parent) {
00080 kDebug()<<k_funcinfo<<"child == parent"<<endl;
00081 return false;
00082 }
00083 if (m_child == m_parent) {
00084 kDebug()<<k_funcinfo<<"child == parent"<<endl;
00085 return false;
00086 }
00087 if (!m_parent->legalToLink(m_child))
00088 return false;
00089
00090 QString tr = element.attribute("type");
00091 if ( tr == "Finish-Start" )
00092 m_type = FinishStart;
00093 else if ( tr == "Finish-Finish" )
00094 m_type = FinishFinish;
00095 else if ( tr == "Start-Start" )
00096 m_type = StartStart;
00097 else
00098 m_type = FinishStart;
00099
00100 m_lag = Duration::fromString(element.attribute("lag"));
00101
00102 if (!m_parent->addDependChildNode(this)) {
00103 kError()<<k_funcinfo<<"Failed to add relation: Child="<<m_child->name()<<" parent="<<m_parent->name()<<endl;
00104 return false;
00105 }
00106 if (!m_child->addDependParentNode(this)) {
00107 m_parent->takeDependChildNode(this);
00108 kError()<<k_funcinfo<<"Failed to add relation: Child="<<m_child->name()<<" parent="<<m_parent->name()<<endl;
00109 return false;
00110 }
00111
00112
00113 return true;
00114 }
00115
00116
00117 void Relation::save(QDomElement &element) const {
00118 QDomElement me = element.ownerDocument().createElement("relation");
00119 element.appendChild(me);
00120
00121 me.setAttribute("parent-id", m_parent->id());
00122 me.setAttribute("child-id", m_child->id());
00123 QString type = "Finish-Start";
00124 switch (m_type) {
00125 case FinishStart:
00126 type = "Finish-Start";
00127 break;
00128 case FinishFinish:
00129 type = "Finish-Finish";
00130 break;
00131 case StartStart:
00132 type = "Start-Start";
00133 break;
00134 }
00135 me.setAttribute("type", type);
00136 me.setAttribute("lag", m_lag.toString());
00137 }
00138
00139 #ifndef NDEBUG
00140 void Relation::printDebug(QByteArray indent) {
00141 indent += " ";
00142 kDebug()<<indent<<" Parent: "<<m_parent->name()<<endl;
00143 kDebug()<<indent<<" Child: "<<m_child->name()<<endl;
00144 kDebug()<<indent<<" Type: "<<m_type<<endl;
00145 }
00146 #endif
00147
00148 }