1 #include "BlockLibraryTree.h"
3 BlockLibraryTree::BlockLibraryTree() {
9 BlockLibraryTree::~BlockLibraryTree() {
13 void BlockLibraryTree::clear() {
15 for(int i=0;i<nbCategories;i++) {
16 delete tabCategories[i];
18 delete [] tabCategories;
22 void BlockLibraryTree::clearBlocks() {
24 for(int i=0;i<nbCategories;i++) {
25 tabCategories[i]->blocks.clear();
29 void BlockLibraryTree::addItem(QXmlAttributes &attributes)
32 if(tabCategories == NULL){
33 tabCategories = new BlockCategory* [1];
34 tabIdParent = new int[1];
37 BlockCategory** tmpTabCat = new BlockCategory* [nbCategories];
38 int* tmpTabParent = new int[nbCategories];
39 for(int i=0; i< nbCategories; i++){
40 tmpTabCat[i] = tabCategories[i];
41 tmpTabParent[i] = tabIdParent[i];
43 tabCategories = tmpTabCat;
44 tabIdParent = tmpTabParent;
47 QString name = attributes.value(0);
48 int id = attributes.value(1).toInt();
49 int idParent = attributes.value(2).toInt();
50 BlockCategory* cat = new BlockCategory(name,id);
51 tabCategories[id] = cat;
52 tabIdParent[id] = idParent;
55 bool BlockLibraryTree::initChildParent()
57 // initializing parent/childs
58 for(int i=0;i<nbCategories;i++) {
59 if (tabIdParent[i] != -1) {
60 if (tabIdParent[i] >= nbCategories) return false;
61 tabCategories[i]->setParent(tabCategories[tabIdParent[i]]);
62 tabCategories[tabIdParent[i]]->addChild(tabCategories[i]);
69 QDomElement BlockLibraryTree::save(QDomDocument &doc) {
73 /* NOTE : load() is the only way to initialize the tree.
74 It is done at the begining of the application, while reading
75 the configuration file.
76 elt MUST be the DOM element that corresponds to the tag <categories>
78 void BlockLibraryTree::load(QDomElement &elt) throw(Exception) {
80 if (elt.tagName() != "categories") throw(Exception(CONFIGFILE_CORRUPTED));
82 QString nbStr = elt.attribute("nb","none");
84 int nb = nbStr.toInt(&ok);
85 QDomNodeList list = elt.elementsByTagName("category");
86 nbCategories = list.size();
87 if (nb != nbCategories) throw(Exception(CONFIGFILE_CORRUPTED));
94 tabCategories = new BlockCategory* [nbCategories];
95 tabIdParent = new int[nbCategories];
96 BlockCategory* cat = NULL;
98 // creating all BlockCategory objects
99 for(int i=0;i<nbCategories;i++) {
100 QDomNode node = list.item(i);
101 QDomElement e = node.toElement();
102 name = e.attribute("name","none");
103 idStr = e.attribute("id","none");
104 idParentStr = e.attribute("parent","none");
105 id = idStr.toInt(&ok);
106 if ((!ok) || (id < 0) || (id >= nbCategories)) throw(Exception(CONFIGFILE_CORRUPTED));
107 idParent = idParentStr.toInt(&ok);
108 if ((!ok)|| (idParent < -1) || (idParent >= nbCategories)) throw(Exception(CONFIGFILE_CORRUPTED));
109 cat = new BlockCategory(name,id);
110 tabCategories[id] = cat;
111 tabIdParent[id] = idParent;
114 ok = initChildParent();
115 delete [] tabIdParent;
116 if (!ok) throw(Exception(CONFIGFILE_CORRUPTED));
119 BlockCategory* BlockLibraryTree::searchCategory(int id) {
121 if (tabCategories != NULL) {
122 if ((id>=0) && (id < nbCategories)) {
123 return tabCategories[id];
130 BlockCategory *BlockLibraryTree::getRoot() {
131 if (tabCategories != NULL) {
132 if (nbCategories > 0) {
133 return tabCategories[0];