| 1 |
/* |
| 2 |
* CoreAttributes.h - TaskJuggler |
| 3 |
* |
| 4 |
* SPDX-FileCopyrightText: 2001, 2002, 2003, 2004, 2005 Chris Schlaeger <[email protected]> |
| 5 |
* SPDX-FileCopyrightText: 2011 Dag Andersen <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of version 2 of the GNU General Public License as |
| 9 |
* published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* $Id$ |
| 12 |
*/ |
| 13 |
|
| 14 |
// clazy:excludeall=qstring-arg |
| 15 |
#include "CoreAttributes.h" |
| 16 |
#include "CoreAttributesList.h" |
| 17 |
#include "CustomAttributeDefinition.h" |
| 18 |
// #include "TextAttribute.h" |
| 19 |
// #include "ReferenceAttribute.h" |
| 20 |
#include "Task.h" |
| 21 |
|
| 22 |
namespace TJ |
| 23 |
{ |
| 24 |
|
| 25 |
|
| 26 |
CoreAttributes::CoreAttributes(Project* p, const QString& i, |
| 27 |
const QString& n, |
| 28 |
CoreAttributes* parent_, const QString& df, |
| 29 |
uint dl) : |
| 30 |
project(p), |
| 31 |
id(i), |
| 32 |
name(n), |
| 33 |
parent(parent_), |
| 34 |
definitionFile(df), |
| 35 |
definitionLine(dl), |
| 36 |
sequenceNo(0), |
| 37 |
hierarchNo(0), |
| 38 |
index(-1), |
| 39 |
hierarchIndex(0), |
| 40 |
sub(new CoreAttributesList()), |
| 41 |
flags(), |
| 42 |
customAttributes() |
| 43 |
{ |
| 44 |
// customAttributes.setAutoDelete(true); |
| 45 |
if (parent_) |
| 46 |
parent_->sub->append(this); |
| 47 |
|
| 48 |
// qDebug()<<"CoreAttributes:"<<this; |
| 49 |
} |
| 50 |
|
| 51 |
CoreAttributes::~CoreAttributes() |
| 52 |
{ |
| 53 |
//qDebug()<<"~CoreAttributes:"<<this; |
| 54 |
while (!sub->isEmpty()) |
| 55 |
delete sub->takeFirst(); |
| 56 |
if (parent && parent->sub->contains(this)) |
| 57 |
parent->sub->removeAt(parent->sub->indexOf(this)); |
| 58 |
delete sub; |
| 59 |
qDeleteAll(customAttributes); |
| 60 |
} |
| 61 |
|
| 62 |
uint |
| 63 |
CoreAttributes::treeLevel() const |
| 64 |
{ |
| 65 |
uint tl = 0; |
| 66 |
for (CoreAttributes* c = parent; c; c = c->parent) |
| 67 |
tl++; |
| 68 |
return tl; |
| 69 |
} |
| 70 |
|
| 71 |
CoreAttributesList |
| 72 |
CoreAttributes::getSubList() const |
| 73 |
{ |
| 74 |
return *sub; |
| 75 |
} |
| 76 |
|
| 77 |
CoreAttributesListIterator |
| 78 |
CoreAttributes::getSubListIterator() const |
| 79 |
{ |
| 80 |
return CoreAttributesListIterator(*sub); |
| 81 |
} |
| 82 |
|
| 83 |
bool |
| 84 |
CoreAttributes::hasSubs() const |
| 85 |
{ |
| 86 |
return !sub->isEmpty(); |
| 87 |
} |
| 88 |
|
| 89 |
void |
| 90 |
CoreAttributes::setHierarchNo(uint no) |
| 91 |
{ |
| 92 |
hierarchNo = no; |
| 93 |
uint hNo = 1; |
| 94 |
for (CoreAttributes *a : std::as_const(*sub)) { |
| 95 |
a->setHierarchNo(hNo++); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
QString |
| 100 |
CoreAttributes::getHierarchNo() const |
| 101 |
{ |
| 102 |
QString text; |
| 103 |
const CoreAttributes* ca = this; |
| 104 |
do |
| 105 |
{ |
| 106 |
if (!text.isEmpty()) |
| 107 |
text.prepend(QLatin1Char('.')); |
| 108 |
text = QString("%1").arg(ca->hierarchNo) + text; |
| 109 |
ca = ca->getParent(); |
| 110 |
} |
| 111 |
while (ca); |
| 112 |
return text; |
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
CoreAttributes::setHierarchIndex(uint no) |
| 117 |
{ |
| 118 |
if (no == 0) |
| 119 |
{ |
| 120 |
hierarchIndex = 0; |
| 121 |
return; |
| 122 |
} |
| 123 |
/* If there is no parent, we take the passed number. */ |
| 124 |
if (!parent) |
| 125 |
{ |
| 126 |
hierarchIndex = no; |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
/* Find the highest hierarchIndex of all children of this CAs parent. */ |
| 131 |
uint max = 0; |
| 132 |
const auto lst = *parent->sub; |
| 133 |
for (CoreAttributes *a : lst) { |
| 134 |
if (a->hierarchIndex > max) |
| 135 |
max = a->hierarchIndex; |
| 136 |
} |
| 137 |
/* The index is then the highest found + 1. */ |
| 138 |
hierarchIndex = max + 1; |
| 139 |
} |
| 140 |
|
| 141 |
QString |
| 142 |
CoreAttributes::getHierarchIndex() const |
| 143 |
{ |
| 144 |
QString text; |
| 145 |
const CoreAttributes* ca = this; |
| 146 |
while (ca) |
| 147 |
{ |
| 148 |
if (!text.isEmpty()) |
| 149 |
text.prepend(QLatin1Char('.')); |
| 150 |
text = QString("%1").arg(ca->hierarchIndex) + text; |
| 151 |
ca = ca->getParent(); |
| 152 |
} |
| 153 |
return text; |
| 154 |
} |
| 155 |
|
| 156 |
QString |
| 157 |
CoreAttributes::getHierarchLevel() const |
| 158 |
{ |
| 159 |
return QString("%1").arg(treeLevel()); |
| 160 |
} |
| 161 |
|
| 162 |
void |
| 163 |
CoreAttributes::getFullName(QString& fullName) const |
| 164 |
{ |
| 165 |
fullName.clear(); |
| 166 |
for (const CoreAttributes* c = this; c != nullptr; c = c->parent) |
| 167 |
fullName = c->name + QLatin1Char('.') + fullName; |
| 168 |
// Remove trailing dot. |
| 169 |
fullName.remove(fullName.length() - 1, 1); |
| 170 |
} |
| 171 |
|
| 172 |
QString |
| 173 |
CoreAttributes::getFullId() const |
| 174 |
{ |
| 175 |
QString fullID = id; |
| 176 |
for (const CoreAttributes* c = parent; c != nullptr; c = c->parent) |
| 177 |
fullID = c->id + QLatin1Char('.') + fullID; |
| 178 |
return fullID; |
| 179 |
} |
| 180 |
|
| 181 |
bool |
| 182 |
CoreAttributes::hasSameAncestor(const CoreAttributes* c) const |
| 183 |
{ |
| 184 |
if (c == nullptr) |
| 185 |
return false; |
| 186 |
|
| 187 |
CoreAttributes const* p1; |
| 188 |
for (p1 = this; p1->parent; p1 = p1->parent) |
| 189 |
; |
| 190 |
CoreAttributes const* p2; |
| 191 |
for (p2 = c; p2->parent; p2 = p2->parent) |
| 192 |
; |
| 193 |
return p1 == p2; |
| 194 |
} |
| 195 |
|
| 196 |
bool |
| 197 |
CoreAttributes::isDescendantOf(const CoreAttributes* c) const |
| 198 |
{ |
| 199 |
if (c == nullptr) |
| 200 |
return false; |
| 201 |
|
| 202 |
for (CoreAttributes const* p = this->parent; p; p = p->parent) |
| 203 |
if (p == c) |
| 204 |
return true; |
| 205 |
|
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
bool |
| 210 |
CoreAttributes::isParentOf(const CoreAttributes* c) const |
| 211 |
{ |
| 212 |
if (!c) |
| 213 |
return false; |
| 214 |
|
| 215 |
for (CoreAttributes const* p = c->parent; p; p = p->parent) |
| 216 |
if (p == this) |
| 217 |
return true; |
| 218 |
|
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
bool |
| 223 |
CoreAttributes::isLeaf() const |
| 224 |
{ |
| 225 |
return sub->isEmpty(); |
| 226 |
} |
| 227 |
|
| 228 |
void |
| 229 |
CoreAttributes::addCustomAttribute(const QString& id, CustomAttribute* ca) |
| 230 |
{ |
| 231 |
customAttributes.insert(id, ca); |
| 232 |
} |
| 233 |
|
| 234 |
const CustomAttribute* |
| 235 |
CoreAttributes::getCustomAttribute(const QString& id) const |
| 236 |
{ |
| 237 |
return customAttributes[id]; |
| 238 |
} |
| 239 |
|
| 240 |
void |
| 241 |
CoreAttributes::inheritCustomAttributes |
| 242 |
(const QMap<QString, CustomAttributeDefinition*>& dict) |
| 243 |
{ |
| 244 |
QMap<QString, CustomAttributeDefinition*>::const_iterator cadi = dict.constBegin(); |
| 245 |
for (; cadi != dict.constEnd(); ++cadi) |
| 246 |
{ |
| 247 |
const CustomAttribute* custAttr; |
| 248 |
if (cadi.value()->getInherit() && |
| 249 |
(custAttr = parent->getCustomAttribute(cadi.key()))) |
| 250 |
{ |
| 251 |
switch (custAttr->getType()) |
| 252 |
{ |
| 253 |
case CAT_Text: |
| 254 |
/* addCustomAttribute(cadi.key(), new TextAttribute |
| 255 |
(*(static_cast<const TextAttribute*>(custAttr)))); |
| 256 |
break;*/ |
| 257 |
case CAT_Reference: |
| 258 |
/* addCustomAttribute(cadi.key(), new ReferenceAttribute |
| 259 |
(*(static_cast<const ReferenceAttribute*>(custAttr)))); |
| 260 |
break;*/ |
| 261 |
default: |
| 262 |
qFatal("CoreAttributes::inheritCustomAttributes: " |
| 263 |
"Unknown CAT %d", custAttr->getType()); |
| 264 |
break; |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
} // namespace TJ |
| 271 |
|
| 272 |
QDebug operator<<(QDebug dbg, const TJ::CoreAttributes* t) |
| 273 |
{ |
| 274 |
if (t == nullptr) { |
| 275 |
return dbg << (void*)t; |
| 276 |
} |
| 277 |
return operator<<(dbg, *t); |
| 278 |
} |
| 279 |
|
| 280 |
QDebug operator<<(QDebug dbg, const TJ::CoreAttributes& t) |
| 281 |
{ |
| 282 |
switch (t.getType()) { |
| 283 |
case CA_Task: dbg << "Task[" << t.getName() << "]"; break; |
| 284 |
case CA_Resource: dbg << "Resource[" << t.getName() << "]"; break; |
| 285 |
case CA_Account: dbg << "Account[" << t.getName() << "]"; break; |
| 286 |
case CA_Shift: dbg << "Shift[" << t.getName() << "]"; break; |
| 287 |
case CA_Scenario: dbg << "Scenario[" << t.getName() << "]"; break; |
| 288 |
default: dbg << "CoreAttribute[" << t.getName() << "]"; break; |
| 289 |
} |
| 290 |
return dbg; |
| 291 |
} |