2 * CoreAttributes.h - TaskJuggler
4 * Copyright (c) 2001, 2002, 2003, 2004, 2005 by Chris Schlaeger <cs@kde.org>
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.
13 #include "CoreAttributes.h"
14 #include "CoreAttributesList.h"
15 #include "CustomAttributeDefinition.h"
16 #include "TextAttribute.h"
17 #include "ReferenceAttribute.h"
19 CoreAttributes::CoreAttributes(Project* p, const QString& i,
21 CoreAttributes* parent_, const QString& df,
33 sub(new CoreAttributesList()),
37 customAttributes.setAutoDelete(true);
39 parent_->sub->append(this);
42 CoreAttributes::~CoreAttributes()
44 while (!sub->isEmpty())
45 delete sub->getFirst();
47 parent->sub->removeRef(this);
52 CoreAttributes::treeLevel() const
55 for (CoreAttributes* c = parent; c; c = c->parent)
61 CoreAttributes::getSubList() const
66 CoreAttributesListIterator
67 CoreAttributes::getSubListIterator() const
69 return CoreAttributesListIterator(*sub);
73 CoreAttributes::hasSubs() const
75 return !sub->isEmpty();
79 CoreAttributes::setHierarchNo(uint no)
83 for (CoreAttributesListIterator it(*sub); *it; ++it)
84 (*it)->setHierarchNo(hNo++);
88 CoreAttributes::getHierarchNo() const
91 const CoreAttributes* ca = this;
96 text = QString("%1").arg(ca->hierarchNo) + text;
104 CoreAttributes::setHierarchIndex(uint no)
111 /* If there is no parent, we take the passed number. */
118 /* Find the highest hierarchIndex of all childs of this CAs parent. */
120 for (CoreAttributesListIterator it(*parent->sub); *it; ++it)
121 if ((*it)->hierarchIndex > max)
122 max = (*it)->hierarchIndex;
124 /* The index is then the highest found + 1. */
125 hierarchIndex = max + 1;
129 CoreAttributes::getHierarchIndex() const
132 const CoreAttributes* ca = this;
137 text = QString("%1").arg(ca->hierarchIndex) + text;
138 ca = ca->getParent();
144 CoreAttributes::getHierarchLevel() const
146 return QString("%1").arg(treeLevel());
150 CoreAttributes::getFullName(QString& fullName) const
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);
160 CoreAttributes::getFullId() const
163 for (const CoreAttributes* c = parent; c != 0; c = c->parent)
164 fullID = c->id + "." + fullID;
169 CoreAttributes::hasSameAncestor(const CoreAttributes* c) const
174 CoreAttributes const* p1;
175 for (p1 = this; p1->parent; p1 = p1->parent)
177 CoreAttributes const* p2;
178 for (p2 = c; p2->parent; p2 = p2->parent)
184 CoreAttributes::isDescendantOf(const CoreAttributes* c) const
189 for (CoreAttributes const* p = this->parent; p; p = p->parent)
197 CoreAttributes::isParentOf(const CoreAttributes* c) const
202 for (CoreAttributes const* p = c->parent; p; p = p->parent)
210 CoreAttributes::isLeaf() const
212 return sub->isEmpty();
216 CoreAttributes::addCustomAttribute(const QString& id, CustomAttribute* ca)
218 customAttributes.insert(id, ca);
221 const CustomAttribute*
222 CoreAttributes::getCustomAttribute(const QString& id) const
224 return customAttributes[id];
228 CoreAttributes::inheritCustomAttributes
229 (const Q3Dict<CustomAttributeDefinition>& dict)
231 Q3DictIterator<CustomAttributeDefinition> cadi(dict);
232 for ( ; cadi.current(); ++cadi)
234 const CustomAttribute* custAttr;
235 if (cadi.current()->getInherit() &&
236 (custAttr = parent->getCustomAttribute(cadi.currentKey())))
238 switch (custAttr->getType())
241 addCustomAttribute(cadi.currentKey(), new TextAttribute
242 (*(static_cast<const TextAttribute*>(custAttr))));
245 addCustomAttribute(cadi.currentKey(), new ReferenceAttribute
246 (*(static_cast<const ReferenceAttribute*>(custAttr))));
249 qFatal("CoreAttributes::inheritCustomAttributes: "
250 "Unknown CAT %d", custAttr->getType());