1 #include "BlockLibraryTree.h"
3 BlockLibraryTree::BlockLibraryTree() {
7 BlockLibraryTree::~BlockLibraryTree() {
11 void BlockLibraryTree::clear() {
13 for(int i=0;i<nbCategories;i++) {
14 delete tabCategories[i];
19 void BlockLibraryTree::clearBlocks() {
21 for(int i=0;i<nbCategories;i++) {
22 tabCategories[i]->blocks.clear();
26 void BlockLibraryTree::addItem(QXmlAttributes &attributes)
29 if(tabCategories == NULL){
30 tabCategories = new BlockCategory* [1];
31 tabIdParent = new int[1];
34 BlockCategory** tmpTabCat = new BlockCategory* [nbCategories];
35 int* tmpTabParent = new int[nbCategories];
36 for(int i=0; i< nbCategories; i++){
37 tmpTabCat[i] = tabCategories[i];
38 tmpTabParent[i] = tabIdParent[i];
40 tabCategories = tmpTabCat;
41 tabIdParent = tmpTabParent;
44 QString name = attributes.value(0);
45 int id = attributes.value(1).toInt();
46 int idParent = attributes.value(2).toInt();
47 BlockCategory* cat = new BlockCategory(name,id);
48 tabCategories[id] = cat;
49 tabIdParent[id] = idParent;
52 bool BlockLibraryTree::initChildParent() {
53 // initializing parent/childs
55 for(int i=0;i<nbCategories;i++) {
56 if (tabIdParent[i] >= 0) {
58 foreach (BlockCategory* cat, tabCategories) {
59 if (cat->getId() == tabIdParent[i]) {
60 tabCategories[i]->setParent(cat);
61 cat->addChild(tabCategories[i]);
66 if (!ok) return false;
73 QDomElement BlockLibraryTree::save(QDomDocument &doc) {
77 /* NOTE : load() is the only way to initialize the tree.
78 It is done at the begining of the application, while reading
79 the configuration file.
80 elt MUST be the DOM element that corresponds to the tag <categories>
82 void BlockLibraryTree::load(QDomElement &elt) throw(Exception) {
84 if (elt.tagName() != "categories") throw(Exception(CONFIGFILE_CORRUPTED));
87 QDomNodeList list = elt.elementsByTagName("category");
88 nbCategories = list.size();
95 BlockCategory* cat = NULL;
97 // creating all BlockCategory objects
98 for(int i=0;i<nbCategories;i++) {
99 QDomNode node = list.item(i);
100 QDomElement e = node.toElement();
101 name = e.attribute("name","none");
102 idStr = e.attribute("id","none");
103 idParentStr = e.attribute("parent","none");
104 id = idStr.toInt(&ok);
105 if ((!ok) || (id < 0) || (id >= 100)) throw(Exception(CONFIGFILE_CORRUPTED));
106 idParent = idParentStr.toInt(&ok);
107 if ((!ok)|| (idParent < -1) || (idParent >= 100)) throw(Exception(CONFIGFILE_CORRUPTED));
108 cat = new BlockCategory(name,id);
109 tabCategories.append(cat);
110 tabIdParent.append(idParent);
112 cat = new BlockCategory("hidden",100);
113 tabCategories.append(cat);
114 tabIdParent.append(0);
116 ok = initChildParent();
118 if (!ok) throw(Exception(CONFIGFILE_CORRUPTED));
121 BlockCategory* BlockLibraryTree::searchCategory(int id) {
123 foreach(BlockCategory* cat, tabCategories) {
124 if (cat->getId() == id) return cat;
129 BlockCategory *BlockLibraryTree::getRoot() {
131 return searchCategory(0);