OSDN Git Service

adjust integer's signedness
[tjqt4port/tj2qt4.git] / taskjuggler / CoreAttributes.cpp
1 /*
2  * CoreAttributes.h - TaskJuggler
3  *
4  * Copyright (c) 2001, 2002, 2003, 2004, 2005 by Chris Schlaeger <cs@kde.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * $Id$
11  */
12
13 #include "CoreAttributes.h"
14 #include "CoreAttributesList.h"
15 #include "CustomAttributeDefinition.h"
16 #include "TextAttribute.h"
17 #include "ReferenceAttribute.h"
18
19 CoreAttributes::CoreAttributes(Project* p, const QString& i,
20                                const QString& n,
21                                CoreAttributes* parent_, const QString& df,
22                                uint dl) :
23     project(p),
24     id(i),
25     name(n),
26     parent(parent_),
27     definitionFile(df),
28     definitionLine(dl),
29     sequenceNo(0),
30     hierarchNo(0),
31     index(-1),
32     hierarchIndex(0),
33     sub(new CoreAttributesList()),
34     flags(),
35     customAttributes()
36 {
37     customAttributes.setAutoDelete(true);
38     if (parent_)
39         parent_->sub->append(this);
40 }
41
42 CoreAttributes::~CoreAttributes()
43 {
44     while (!sub->isEmpty())
45         delete sub->getFirst();
46     if (parent)
47         parent->sub->removeRef(this);
48     delete sub;
49 }
50
51 uint
52 CoreAttributes::treeLevel() const
53 {
54     uint tl = 0;
55     for (CoreAttributes* c = parent; c; c = c->parent)
56         tl++;
57     return tl;
58 }
59
60 CoreAttributesList
61 CoreAttributes::getSubList() const
62 {
63    return *sub;
64 }
65
66 CoreAttributesListIterator
67 CoreAttributes::getSubListIterator() const
68 {
69     return CoreAttributesListIterator(*sub);
70 }
71
72 bool
73 CoreAttributes::hasSubs() const
74 {
75     return !sub->isEmpty();
76 }
77
78 void
79 CoreAttributes::setHierarchNo(uint no)
80 {
81     hierarchNo = no;
82     uint hNo = 1;
83     for (CoreAttributesListIterator it(*sub); *it; ++it)
84         (*it)->setHierarchNo(hNo++);
85 }
86
87 QString
88 CoreAttributes::getHierarchNo() const
89 {
90     QString text;
91     const CoreAttributes* ca = this;
92     do
93     {
94         if (!text.isEmpty())
95             text = "." + text;
96         text = QString("%1").arg(ca->hierarchNo) + text;
97         ca = ca->getParent();
98     }
99     while (ca);
100     return text;
101 }
102
103 void
104 CoreAttributes::setHierarchIndex(uint no)
105 {
106     if (no == 0)
107     {
108         hierarchIndex = 0;
109         return;
110     }
111     /* If there is no parent, we take the passed number. */
112     if (!parent)
113     {
114         hierarchIndex = no;
115         return;
116     }
117
118     /* Find the highest hierarchIndex of all childs of this CAs parent. */
119     uint max = 0;
120     for (CoreAttributesListIterator it(*parent->sub); *it; ++it)
121         if ((*it)->hierarchIndex > max)
122             max = (*it)->hierarchIndex;
123
124     /* The index is then the highest found + 1. */
125     hierarchIndex = max + 1;
126 }
127
128 QString
129 CoreAttributes::getHierarchIndex() const
130 {
131     QString text;
132     const CoreAttributes* ca = this;
133     while (ca)
134     {
135         if (!text.isEmpty())
136             text = "." + text;
137         text = QString("%1").arg(ca->hierarchIndex) + text;
138         ca = ca->getParent();
139     }
140     return text;
141 }
142
143 QString
144 CoreAttributes::getHierarchLevel() const
145 {
146     return QString("%1").arg(treeLevel());
147 }
148
149 void
150 CoreAttributes::getFullName(QString& fullName) const
151 {
152     fullName = QString::null;
153     for (const CoreAttributes* c = this; c != 0; c = c->parent)
154         fullName = c->name + "." + fullName;
155     // Remove trailing dot.
156     fullName.remove(fullName.length() - 1, 1);
157 }
158
159 QString
160 CoreAttributes::getFullId() const
161 {
162     QString fullID = id;
163     for (const CoreAttributes* c = parent; c != 0; c = c->parent)
164         fullID = c->id + "." + fullID;
165     return fullID;
166 }
167
168 bool
169 CoreAttributes::hasSameAncestor(const CoreAttributes* c) const
170 {
171     if (c == 0)
172         return false;
173
174     CoreAttributes const* p1;
175     for (p1 = this; p1->parent; p1 = p1->parent)
176         ;
177     CoreAttributes const* p2;
178     for (p2 = c; p2->parent; p2 = p2->parent)
179         ;
180     return p1 == p2;
181 }
182
183 bool
184 CoreAttributes::isDescendantOf(const CoreAttributes* c) const
185 {
186     if (c == 0)
187         return false;
188
189     for (CoreAttributes const* p = this->parent; p; p = p->parent)
190         if (p == c)
191             return true;
192
193     return false;
194 }
195
196 bool
197 CoreAttributes::isParentOf(const CoreAttributes* c) const
198 {
199     if (!c)
200         return false;
201
202     for (CoreAttributes const* p = c->parent; p; p = p->parent)
203         if (p == this)
204             return true;
205
206     return false;
207 }
208
209 bool
210 CoreAttributes::isLeaf() const
211 {
212     return sub->isEmpty();
213 }
214
215 void
216 CoreAttributes::addCustomAttribute(const QString& id, CustomAttribute* ca)
217 {
218     customAttributes.insert(id, ca);
219 }
220
221 const CustomAttribute*
222 CoreAttributes::getCustomAttribute(const QString& id) const
223 {
224     return customAttributes[id];
225 }
226
227 void
228 CoreAttributes::inheritCustomAttributes
229 (const Q3Dict<CustomAttributeDefinition>& dict)
230 {
231     Q3DictIterator<CustomAttributeDefinition> cadi(dict);
232     for ( ; cadi.current(); ++cadi)
233     {
234         const CustomAttribute* custAttr;
235         if (cadi.current()->getInherit() &&
236                 (custAttr = parent->getCustomAttribute(cadi.currentKey())))
237         {
238             switch (custAttr->getType())
239             {
240             case CAT_Text:
241                 addCustomAttribute(cadi.currentKey(), new TextAttribute
242                                    (*(static_cast<const TextAttribute*>(custAttr))));
243                 break;
244             case CAT_Reference:
245                 addCustomAttribute(cadi.currentKey(), new ReferenceAttribute
246                                    (*(static_cast<const ReferenceAttribute*>(custAttr))));
247                 break;
248             default:
249                 qFatal("CoreAttributes::inheritCustomAttributes: "
250                        "Unknown CAT %d", custAttr->getType());
251                 break;
252             }
253         }
254     }
255 }
256