| 1 |
/* |
| 2 |
* CoreAttributesList.cpp - TaskJuggler |
| 3 |
* |
| 4 |
* SPDX-FileCopyrightText: 2001, 2002, 2003, 2004 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 "CoreAttributesList.h" |
| 16 |
|
| 17 |
#include <QDebug> |
| 18 |
|
| 19 |
namespace TJ |
| 20 |
{ |
| 21 |
|
| 22 |
|
| 23 |
CoreAttributesList::~CoreAttributesList() |
| 24 |
{ |
| 25 |
if (autoDelete()) { |
| 26 |
setAutoDelete(false); |
| 27 |
while (!isEmpty()) { |
| 28 |
delete takeFirst(); |
| 29 |
} |
| 30 |
setAutoDelete(true); |
| 31 |
} |
| 32 |
// if (autoDelete()) |
| 33 |
// { |
| 34 |
// /* We need to make sure that the CoreAttributes are first removed from |
| 35 |
// * the list and then deleted. */ |
| 36 |
// setAutoDelete(false); |
| 37 |
// while (!isEmpty()) |
| 38 |
// { |
| 39 |
// CoreAttributes* tp = getFirst(); |
| 40 |
// removeRef(tp); |
| 41 |
// delete tp; |
| 42 |
// } |
| 43 |
// setAutoDelete(true); |
| 44 |
// } |
| 45 |
} |
| 46 |
|
| 47 |
void |
| 48 |
CoreAttributesList::setAutoDelete(bool on) |
| 49 |
{ |
| 50 |
m_autodelete = on; |
| 51 |
} |
| 52 |
|
| 53 |
bool |
| 54 |
CoreAttributesList::autoDelete() const |
| 55 |
{ |
| 56 |
return m_autodelete; |
| 57 |
} |
| 58 |
|
| 59 |
void |
| 60 |
CoreAttributesList::deleteContents() |
| 61 |
{ |
| 62 |
// Don't understand this code (danders) |
| 63 |
while (!isEmpty()) { |
| 64 |
for (CoreAttributesListIterator li(*this); *li; ++li) |
| 65 |
if ((*li)->getParent() == nullptr) |
| 66 |
{ |
| 67 |
delete *li; |
| 68 |
break; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
void |
| 74 |
CoreAttributesList::setSorting(int s, int level) |
| 75 |
{ |
| 76 |
if (level >=0 && level < maxSortingLevel) |
| 77 |
sorting[level] = s; |
| 78 |
else |
| 79 |
qFatal("CoreAttributesList::setSorting: level out of range: %d", |
| 80 |
level); |
| 81 |
} |
| 82 |
|
| 83 |
void |
| 84 |
CoreAttributesList::sort() |
| 85 |
{ |
| 86 |
QList<CoreAttributes*> lst = *this; |
| 87 |
clear(); |
| 88 |
QStringList s; for(int i = 0; i < lst.count(); ++i) s << lst.at(i)->getId(); |
| 89 |
//qDebug()<<"CoreAttributesList::sort:"<<s; |
| 90 |
while (!lst.isEmpty()) { |
| 91 |
inSort(lst.takeLast()); |
| 92 |
} |
| 93 |
s.clear(); for(int i = 0; i < lst.count(); ++i) s << lst.at(i)->getId(); |
| 94 |
//qDebug()<<"CoreAttributesList::sort: sorted"<<s; |
| 95 |
} |
| 96 |
|
| 97 |
void |
| 98 |
CoreAttributesList::createIndex(bool initial) |
| 99 |
{ |
| 100 |
/* In "initial" mode the sequenceNo is set. This should only be done once |
| 101 |
* for each list. In the other mode the index is set. This is most likely |
| 102 |
* called after the sorting criteria have been changed. */ |
| 103 |
int i = 1; |
| 104 |
if (initial) |
| 105 |
{ |
| 106 |
uint hNo = 1; |
| 107 |
for (int pos = 0; pos < count(); ++pos) |
| 108 |
{ |
| 109 |
CoreAttributes *a = at(pos); |
| 110 |
a->setSequenceNo(i); |
| 111 |
if (a->getParent() == nullptr) |
| 112 |
a->setHierarchNo(hNo++); |
| 113 |
} |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
sort(); |
| 118 |
for (int pos = 0; pos < count(); ++pos) |
| 119 |
{ |
| 120 |
CoreAttributes *a = at(pos); |
| 121 |
a->setIndex(i); |
| 122 |
// Reset all hierarchIndices to 0. |
| 123 |
a->setHierarchIndex(0); |
| 124 |
} |
| 125 |
// Then number them again. |
| 126 |
uint hNo = 1; |
| 127 |
for (int pos = 0; pos < count(); ++pos) |
| 128 |
{ |
| 129 |
CoreAttributes *a = at(pos); |
| 130 |
a->setHierarchIndex(hNo); |
| 131 |
if (a->getParent() == nullptr) |
| 132 |
hNo++; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
int |
| 138 |
CoreAttributesList::getIndex(const QString& id) const |
| 139 |
{ |
| 140 |
for (int pos = 0; pos < count(); ++pos) { |
| 141 |
CoreAttributes *a = at(pos); |
| 142 |
if (a->getId() == id) |
| 143 |
return a->getIndex(); |
| 144 |
} |
| 145 |
return -1; |
| 146 |
} |
| 147 |
|
| 148 |
uint |
| 149 |
CoreAttributesList::maxDepth() const |
| 150 |
{ |
| 151 |
uint md = 0; |
| 152 |
for (int pos = 0; pos < count(); ++pos) { |
| 153 |
CoreAttributes *a = at(pos); |
| 154 |
if (a->treeLevel() + 1 > md) |
| 155 |
md = a->treeLevel() + 1; |
| 156 |
} |
| 157 |
return md; |
| 158 |
} |
| 159 |
|
| 160 |
bool |
| 161 |
CoreAttributesList::isSupportedSortingCriteria(int sc) |
| 162 |
{ |
| 163 |
switch (sc) |
| 164 |
{ |
| 165 |
case SequenceUp: |
| 166 |
case SequenceDown: |
| 167 |
case TreeMode: |
| 168 |
case FullNameDown: |
| 169 |
case FullNameUp: |
| 170 |
case IndexUp: |
| 171 |
case IndexDown: |
| 172 |
case IdUp: |
| 173 |
case NameUp: |
| 174 |
case NameDown: |
| 175 |
return true; |
| 176 |
default: |
| 177 |
return false; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
int CoreAttributesList::inSort(CoreAttributes* attr) |
| 182 |
{ |
| 183 |
int i = 0; |
| 184 |
for (; i < count(); ++i) { |
| 185 |
int r = compareItems(attr, at(i)); |
| 186 |
if (r < 0) { |
| 187 |
break; |
| 188 |
} |
| 189 |
} |
| 190 |
insert(i, attr); |
| 191 |
return i; |
| 192 |
} |
| 193 |
|
| 194 |
int |
| 195 |
CoreAttributesList::compareItemsLevel(CoreAttributes* c1, CoreAttributes* c2, |
| 196 |
int level) |
| 197 |
{ |
| 198 |
if (level < 0 || level >= maxSortingLevel) |
| 199 |
return -1; |
| 200 |
|
| 201 |
switch (sorting[level]) |
| 202 |
{ |
| 203 |
case SequenceUp: |
| 204 |
return c1->getSequenceNo() == c2->getSequenceNo() ? 0 : |
| 205 |
c1->getSequenceNo() < c2->getSequenceNo() ? -1 : 1; |
| 206 |
case SequenceDown: |
| 207 |
return c1->getSequenceNo() == c2->getSequenceNo() ? 0 : |
| 208 |
c1->getSequenceNo() > c2->getSequenceNo() ? -1 : 1; |
| 209 |
case TreeMode: |
| 210 |
{ |
| 211 |
if (level == 0) |
| 212 |
return compareTreeItemsT(this, c1, c2); |
| 213 |
else |
| 214 |
return c1->getSequenceNo() < c2->getSequenceNo() ? -1 : 1; |
| 215 |
} |
| 216 |
case FullNameDown: |
| 217 |
{ |
| 218 |
QString fn1; |
| 219 |
c1->getFullName(fn1); |
| 220 |
QString fn2; |
| 221 |
c2->getFullName(fn2); |
| 222 |
return fn1.compare(fn2); |
| 223 |
} |
| 224 |
case FullNameUp: |
| 225 |
{ |
| 226 |
QString fn1; |
| 227 |
c1->getFullName(fn1); |
| 228 |
QString fn2; |
| 229 |
c2->getFullName(fn2); |
| 230 |
return fn2.compare(fn1); |
| 231 |
} |
| 232 |
case IndexUp: |
| 233 |
return c2->getIndex() == c1->getIndex() ? 0 : |
| 234 |
c2->getIndex() < c1->getIndex() ? -1 : 1; |
| 235 |
case IndexDown: |
| 236 |
return c1->getIndex() == c2->getIndex() ? 0 : |
| 237 |
c1->getIndex() > c2->getIndex() ? -1 : 1; |
| 238 |
case IdUp: |
| 239 |
return QString::compare(c1->getId(), c2->getId()); |
| 240 |
case IdDown: |
| 241 |
return QString::compare(c2->getId(), c1->getId()); |
| 242 |
case NameUp: |
| 243 |
return c1->getName().compare(c2->getName()); |
| 244 |
case NameDown: |
| 245 |
return c2->getName().compare(c1->getName()); |
| 246 |
default: |
| 247 |
qFatal("CoreAttributesList:compareItemsLevel: " |
| 248 |
"Please implement sorting for mode (%d/%d) in sub class!", |
| 249 |
sorting[level], level); |
| 250 |
} |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
int |
| 255 |
CoreAttributesList::compareItems(CoreAttributes* c1, CoreAttributes* c2) |
| 256 |
{ |
| 257 |
int res; |
| 258 |
for (int i = 0; i < CoreAttributesList::maxSortingLevel; ++i) |
| 259 |
if ((res = compareItemsLevel(c1, c2, i)) != 0) |
| 260 |
return res; |
| 261 |
return res; |
| 262 |
} |
| 263 |
|
| 264 |
//static |
| 265 |
QStringList CoreAttributesList::getSortCriteria() |
| 266 |
{ |
| 267 |
QStringList lst; |
| 268 |
lst << "SequenceUp" << "SequenceDown" |
| 269 |
<< "TreeMode" << "NameUp" << "NameDown" << "FullNameUp" |
| 270 |
<< "FullNameDown" << "IdUp" << "IdDown" << "IndexUp" << "IndexDown" |
| 271 |
<< "StatusUp" << "StatusDown" << "CompletedUp" << "CompletedDown" |
| 272 |
<< "PrioUp" << "PrioDown" |
| 273 |
<< "ResponsibleUp" << "ResponsibleDown" |
| 274 |
<< "MinEffortUp" << "MinEffortDown" |
| 275 |
<< "MaxEffortUp" << "MaxEffortDown" |
| 276 |
<< "RateUp" << "RateDown" |
| 277 |
<< "StartUp" << "StartDown" << "EndUp" << "EndDown" |
| 278 |
<< "CriticalnessUp" << "CriticalnessDown" |
| 279 |
<< "PathCriticalnessUp" << "PathCriticalnessDown" |
| 280 |
; |
| 281 |
return lst; |
| 282 |
} |
| 283 |
|
| 284 |
} // namespace TJ |
| 285 |
|
| 286 |
QDebug operator<<(QDebug dbg, const TJ::CoreAttributesList& lst) |
| 287 |
{ |
| 288 |
QStringList s; |
| 289 |
for (int i = 0; i < TJ::CoreAttributesList::maxSortingLevel; ++i) { |
| 290 |
s << TJ::CoreAttributesList::getSortCriteria().at(lst.getSorting(i)); |
| 291 |
} |
| 292 |
dbg.nospace() << "CoreAttributeList{sort: " << s.join("|") << " ("; |
| 293 |
for(int i = 0; i < lst.count(); ++i) { |
| 294 |
dbg << lst.at(i); |
| 295 |
if (i < lst.count() - 1) { |
| 296 |
dbg.nospace() << ','; |
| 297 |
} |
| 298 |
} |
| 299 |
dbg.nospace() << ")}"; |
| 300 |
return dbg; |
| 301 |
} |