#include "BlockLibraryTree.h"
-BlockLibraryTree::BlockLibraryTree() {
- tabCategories = NULL;
- tabIdParent = NULL;
+BlockLibraryTree::BlockLibraryTree() {
nbCategories = 0;
}
for(int i=0;i<nbCategories;i++) {
delete tabCategories[i];
- }
- delete [] tabCategories;
+ }
nbCategories = 0;
}
tabCategories[i]->blocks.clear();
}
}
-
+/*
void BlockLibraryTree::addItem(QXmlAttributes &attributes)
{
nbCategories++;
tabCategories[id] = cat;
tabIdParent[id] = idParent;
}
-
-bool BlockLibraryTree::initChildParent()
-{
+*/
+bool BlockLibraryTree::initChildParent() {
// initializing parent/childs
+ bool ok;
for(int i=0;i<nbCategories;i++) {
- if (tabIdParent[i] != -1) {
- if (tabIdParent[i] >= nbCategories) return false;
- tabCategories[i]->setParent(tabCategories[tabIdParent[i]]);
- tabCategories[tabIdParent[i]]->addChild(tabCategories[i]);
+ if (tabIdParent[i] >= 0) {
+ ok = false;
+ foreach (BlockCategory* cat, tabCategories) {
+ if (cat->getId() == tabIdParent[i]) {
+ tabCategories[i]->setParent(cat);
+ cat->addChild(tabCategories[i]);
+ ok = true;
+ break;
+ }
+ }
+ if (!ok) return false;
}
}
return true;
if (elt.tagName() != "categories") throw(Exception(CONFIGFILE_CORRUPTED));
- QString nbStr = elt.attribute("nb","none");
bool ok;
- int nb = nbStr.toInt(&ok);
QDomNodeList list = elt.elementsByTagName("category");
nbCategories = list.size();
- if (nb != nbCategories) throw(Exception(CONFIGFILE_CORRUPTED));
QString name;
int id;
QString idStr;
int idParent;
QString idParentStr;
- tabCategories = new BlockCategory* [nbCategories];
- tabIdParent = new int[nbCategories];
BlockCategory* cat = NULL;
// creating all BlockCategory objects
idStr = e.attribute("id","none");
idParentStr = e.attribute("parent","none");
id = idStr.toInt(&ok);
- if ((!ok) || (id < 0) || (id >= nbCategories)) throw(Exception(CONFIGFILE_CORRUPTED));
+ if ((!ok) || (id < 0) || (id >= 100)) throw(Exception(CONFIGFILE_CORRUPTED));
idParent = idParentStr.toInt(&ok);
- if ((!ok)|| (idParent < -1) || (idParent >= nbCategories)) throw(Exception(CONFIGFILE_CORRUPTED));
+ if ((!ok)|| (idParent < -1) || (idParent >= 100)) throw(Exception(CONFIGFILE_CORRUPTED));
cat = new BlockCategory(name,id);
- tabCategories[id] = cat;
- tabIdParent[id] = idParent;
+ tabCategories.append(cat);
+ tabIdParent.append(idParent);
}
+ cat = new BlockCategory("hidden",100);
+ tabCategories.append(cat);
+ tabIdParent.append(0);
ok = initChildParent();
- delete [] tabIdParent;
+
if (!ok) throw(Exception(CONFIGFILE_CORRUPTED));
}
BlockCategory* BlockLibraryTree::searchCategory(int id) {
- if (tabCategories != NULL) {
- if ((id>=0) && (id < nbCategories)) {
- return tabCategories[id];
- }
+ foreach(BlockCategory* cat, tabCategories) {
+ if (cat->getId() == id) return cat;
}
-
return NULL;
}
BlockCategory *BlockLibraryTree::getRoot() {
- if (tabCategories != NULL) {
- if (nbCategories > 0) {
- return tabCategories[0];
- }
- }
- return NULL;
+
+ return searchCategory(0);
}