~ubuntu-branches/debian/sid/calligraplan/sid

« back to all changes in this revision

Viewing changes to src/libs/kernel/kptrelation.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2018-02-01 18:20:19 UTC
  • Revision ID: package-import@ubuntu.com-20180201182019-1qo7qaim5wejm5k9
Tags: upstream-3.1.0
ImportĀ upstreamĀ versionĀ 3.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2001 Thomas zander <zander@kde.org>
 
3
   Copyright (C) 2004, 2012 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 "kptdebug.h"
 
25
 
 
26
#include <KoXmlReader.h>
 
27
 
 
28
#include <KLocalizedString>
 
29
 
 
30
#include <QStringList>
 
31
 
 
32
 
 
33
namespace KPlato
 
34
{
 
35
 
 
36
Relation::Relation(Node *parent, Node *child, Type type, Duration lag) {
 
37
    m_parent=parent;
 
38
    m_child=child;
 
39
    m_type=type;
 
40
    m_lag=lag;
 
41
    //debugPlan<<this;
 
42
}
 
43
 
 
44
Relation::Relation(Node *parent, Node *child, Type type) {
 
45
    m_parent=parent;
 
46
    m_child=child;
 
47
    m_type=type;
 
48
    m_lag=Duration();
 
49
    //debugPlan<<this;
 
50
}
 
51
 
 
52
Relation::Relation(Relation *rel) {
 
53
    m_parent=rel->parent();
 
54
    m_child=rel->child();
 
55
    m_type=rel->type();
 
56
    m_lag=rel->lag();
 
57
    //debugPlan<<this;
 
58
}
 
59
 
 
60
Relation::~Relation() {
 
61
    //debugPlan<<"("<<this<<") parent:"<<(m_parent ? m_parent->name():"none")<<" child:"<<(m_child ? m_child->name():"None");
 
62
    if (m_parent)
 
63
        m_parent->takeDependChildNode(this);
 
64
    if (m_child)
 
65
        m_child->takeDependParentNode(this);
 
66
}
 
67
 
 
68
void Relation::setType(Type type) {
 
69
    m_type=type;
 
70
}
 
71
 
 
72
void Relation::setType( const QString &type )
 
73
{
 
74
    int t = typeList().indexOf( type );
 
75
    if ( t == -1 ) {
 
76
        t = FinishStart;
 
77
    }
 
78
    m_type = static_cast<Type>( t );
 
79
}
 
80
 
 
81
QString Relation::typeToString( bool trans ) const
 
82
{
 
83
    return typeList( trans ).at( m_type );
 
84
}
 
85
 
 
86
QStringList Relation::typeList( bool trans )
 
87
{
 
88
    //NOTE: must match enum
 
89
    QStringList lst;
 
90
    lst << ( trans ? i18n( "Finish-Start" ) : "Finish-Start" );
 
91
    lst << ( trans ? i18n( "Finish-Finish" ) : "Finish-Finish" );
 
92
    lst << ( trans ? i18n( "Start-Start" ) : "Start-Start" );
 
93
    return lst;
 
94
}
 
95
 
 
96
void Relation::setParent( Node* node )
 
97
{
 
98
    m_parent = node;
 
99
}
 
100
 
 
101
void Relation::setChild( Node* node )
 
102
{
 
103
    m_child = node;
 
104
}
 
105
 
 
106
 
 
107
bool Relation::load(KoXmlElement &element, Project &project) {
 
108
    m_parent = project.findNode(element.attribute("parent-id"));
 
109
    if (m_parent == 0) {
 
110
        return false;
 
111
    }
 
112
    m_child = project.findNode(element.attribute("child-id"));
 
113
    if (m_child == 0) {
 
114
        return false;
 
115
    }
 
116
    if (m_child == m_parent) {
 
117
        debugPlan<<"child == parent";
 
118
        return false;
 
119
    }
 
120
    if (m_child == m_parent) {
 
121
        debugPlan<<"child == parent";
 
122
        return false;
 
123
    }
 
124
    if (!m_parent->legalToLink(m_child))
 
125
        return false;
 
126
        
 
127
    setType( element.attribute("type") );
 
128
 
 
129
    m_lag = Duration::fromString(element.attribute("lag"));
 
130
 
 
131
    if (!m_parent->addDependChildNode(this)) {
 
132
        errorPlan<<"Failed to add relation: Child="<<m_child->name()<<" parent="<<m_parent->name()<<endl;
 
133
        return false;
 
134
    }
 
135
    if (!m_child->addDependParentNode(this)) {
 
136
        m_parent->takeDependChildNode(this);
 
137
        errorPlan<<"Failed to add relation: Child="<<m_child->name()<<" parent="<<m_parent->name()<<endl;
 
138
        return false;
 
139
    }
 
140
 
 
141
    //debugPlan<<"Added relation: Child="<<m_child->name()<<" parent="<<m_parent->name();
 
142
    return true;
 
143
}
 
144
 
 
145
 
 
146
void Relation::save(QDomElement &element) const {
 
147
    QDomElement me = element.ownerDocument().createElement("relation");
 
148
    element.appendChild(me);
 
149
 
 
150
    me.setAttribute("parent-id", m_parent->id());
 
151
    me.setAttribute("child-id", m_child->id());
 
152
    QString type = "Finish-Start";
 
153
    switch (m_type) {
 
154
        case FinishStart:
 
155
            type = "Finish-Start";
 
156
            break;
 
157
        case FinishFinish:
 
158
            type = "Finish-Finish";
 
159
            break;
 
160
        case StartStart:
 
161
            type = "Start-Start";
 
162
            break;
 
163
        default:
 
164
            break;
 
165
    }
 
166
    me.setAttribute("type", type);
 
167
    me.setAttribute("lag", m_lag.toString());
 
168
}
 
169
 
 
170
#ifndef NDEBUG
 
171
void Relation::printDebug(const QByteArray& _indent) { 
 
172
    QString indent = _indent;
 
173
    indent += "  ";
 
174
    debugPlan<<indent<<"  Parent:"<<m_parent->name();
 
175
    debugPlan<<indent<<"  Child:"<<m_child->name();
 
176
    debugPlan<<indent<<"  Type:"<<m_type;
 
177
}
 
178
#endif
 
179
 
 
180
 
 
181
}  //KPlato namespace
 
182
 
 
183
QDebug operator<<( QDebug dbg, const KPlato::Relation *r )
 
184
{
 
185
    return dbg<<(*r);
 
186
}
 
187
 
 
188
QDebug operator<<( QDebug dbg, const KPlato::Relation &r )
 
189
{
 
190
    KPlato::Node *parent = r.parent();
 
191
    KPlato::Node *child = r.child();
 
192
    QString type = "FS";
 
193
    switch ( r.type() ) {
 
194
    case KPlato::Relation::StartStart: type = "SS"; break;
 
195
    case KPlato::Relation::FinishFinish: type = "FF"; break;
 
196
    default: break;
 
197
    }
 
198
 
 
199
    KPlato::Duration lag = r.lag();
 
200
    dbg<<"Relation["<<parent->name()<<"->"<<child->name()<<type;
 
201
    if ( lag != 0 ) {
 
202
        dbg<<lag.toString( KPlato::Duration::Format_HourFraction );
 
203
    }
 
204
    dbg <<']';
 
205
    return dbg;
 
206
}