kplato

kptrelation.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 Thomas zander <zander@kde.org>
3 Copyright (C) 2004 Dag Andersen <danders@get2net.dk>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20#include "kptrelation.h"
21
22#include "kptnode.h"
23#include "kptproject.h"
24#include "kptcanvasitem.h"
25
26#include <tqcanvas.h>
27#include <tqdom.h>
28
29#include <kdebug.h>
30
31namespace KPlato
32{
33
34Relation::Relation(Node *parent, Node *child, Type type, Duration lag) {
35 m_parent=parent;
36 m_child=child;
37 m_type=type;
38 m_lag=lag;
39}
40
41Relation::Relation(Node *parent, Node *child, Type type) {
42 m_parent=parent;
43 m_child=child;
44 m_type=type;
45 m_lag=Duration();
46}
47
48Relation::Relation(Relation *rel) {
49 m_parent=rel->parent();
50 m_child=rel->child();
51 m_type=rel->type();
52 m_lag=rel->lag();
53}
54
56 //kdDebug()<<k_funcinfo<<"parent: "<<(m_parent ? m_parent->name():"none")<<" child: "<<(m_child ? m_child->name():"None")<<endl;
57 if (m_parent)
58 m_parent->takeDependChildNode(this);
59 if (m_child)
60 m_child->takeDependParentNode(this);
61}
62
63void Relation::setType(Type type) {
64 m_type=type;
65}
66
67
68bool Relation::load(TQDomElement &element, Project &project) {
69 m_parent = project.findNode(element.attribute("parent-id"));
70 if (m_parent == 0) {
71 return false;
72 }
73 m_child = project.findNode(element.attribute("child-id"));
74 if (m_child == 0) {
75 return false;
76 }
77 if (m_child == m_parent) {
78 kdDebug()<<k_funcinfo<<"child == parent"<<endl;
79 return false;
80 }
81 if (!m_parent->legalToLink(m_child))
82 return false;
83
84 TQString tr = element.attribute("type");
85 if ( tr == "Finish-Start" )
86 m_type = FinishStart;
87 else if ( tr == "Finish-Finish" )
88 m_type = FinishFinish;
89 else if ( tr == "Start-Start" )
90 m_type = StartStart;
91 else
92 m_type = FinishStart;
93
94 m_lag = Duration::fromString(element.attribute("lag"));
95
96 if (!m_parent->addDependChildNode(this)) {
97 kdError()<<k_funcinfo<<"Failed to add relation: Child="<<m_child->name()<<" parent="<<m_parent->name()<<endl;
98 return false;
99 }
100 if (!m_child->addDependParentNode(this)) {
101 m_parent->delDependChildNode(this, false/*do not delete*/);
102 kdError()<<k_funcinfo<<"Failed to add relation: Child="<<m_child->name()<<" parent="<<m_parent->name()<<endl;
103 return false;
104 }
105
106 //kdDebug()<<k_funcinfo<<"Added relation: Child="<<m_child->name()<<" parent="<<m_parent->name()<<endl;
107 return true;
108}
109
110
111void Relation::save(TQDomElement &element) const {
112 TQDomElement me = element.ownerDocument().createElement("relation");
113 element.appendChild(me);
114
115 me.setAttribute("parent-id", m_parent->id());
116 me.setAttribute("child-id", m_child->id());
117 TQString type = "Finish-Start";
118 switch (m_type) {
119 case FinishStart:
120 type = "Finish-Start";
121 break;
122 case FinishFinish:
123 type = "Finish-Finish";
124 break;
125 case StartStart:
126 type = "Start-Start";
127 break;
128 }
129 me.setAttribute("type", type);
130 me.setAttribute("lag", m_lag.toString());
131}
132
133#ifndef NDEBUG
134void Relation::printDebug(TQCString indent) {
135 indent += " ";
136 kdDebug()<<indent<<" Parent: "<<m_parent->name()<<endl;
137 kdDebug()<<indent<<" Child: "<<m_child->name()<<endl;
138 kdDebug()<<indent<<" Type: "<<m_type<<endl;
139}
140#endif
141
142} //KPlato namespace
virtual void addDependChildNode(Node *node, Relation::Type p=Relation::FinishStart)
Adds relation to both this node and address node.
Definition kptnode.cpp:183
void takeDependParentNode(Relation *rel)
Takes the relation rel from this node only.
Definition kptnode.cpp:295
void takeDependChildNode(Relation *rel)
Takes the relation rel from this node only.
Definition kptnode.cpp:236
virtual Node * findNode() const
Find the node with my id.
Definition kptnode.h:399
bool legalToLink(Node *node)
Check if this node can be linked to node.
Definition kptnode.cpp:559
virtual void addDependParentNode(Node *node, Relation::Type p=Relation::FinishStart)
Adds relation to both this node and node.
Definition kptnode.cpp:242
Node * parent() const
Definition kptrelation.h:72
const Duration & lag() const
returns the lag.
Definition kptrelation.h:66
virtual ~Relation()
When deleted the relation will remove itself from the parent- and child nodes lists.
Node * child() const
Definition kptrelation.h:76