ae21ccc9b6113d2c9949feea81ec3d7195273902
[family-shop-mgr] / code / family-shop-mgr / ShoppingTreeItem.cpp
1 /*\r
2  * This file is part of family-shop-mgr.\r
3  *\r
4  * family-shop-mgr is free software: you can redistribute it and/or modify\r
5  * it under the terms of the GNU General Public License as published by\r
6  * the Free Software Foundation, either version 3 of the License, or\r
7  * (at your option) any later version.\r
8  *\r
9  * family-shop-mgr is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with family-shop-mgr.  If not, see <http://www.gnu.org/licenses/>.\r
16  *\r
17  * Author: Unai IRIGOYEN\r
18  * Date: 12/07/2009\r
19  *\r
20  */\r
21 \r
22 #include <QtXml>\r
23 \r
24 #include "ShoppingTreeItem.h"\r
25 \r
26 \r
27 DomItem::DomItem(QDomNode &node, int row, DomItem *parent)\r
28 {\r
29     domNode = node;\r
30     rowNumber = row;\r
31     parentItem = parent;\r
32 }\r
33 \r
34 /*******************************************************************/\r
35 DomItem::~DomItem()\r
36 {\r
37     QHash<int,DomItem*>::iterator it;\r
38     for (it = childItems.begin(); it != childItems.end(); ++it)\r
39         delete it.value();\r
40 }\r
41 \r
42 /*******************************************************************/\r
43 QDomNode DomItem::node() const\r
44 {\r
45     return domNode;\r
46 }\r
47 \r
48 /*******************************************************************/\r
49 DomItem *DomItem::parent()\r
50 {\r
51     return parentItem;\r
52 }\r
53 \r
54 /*******************************************************************/\r
55 DomItem *DomItem::child(int i)\r
56 {\r
57     if (childItems.contains(i))\r
58         return childItems[i];\r
59 \r
60     QDomNodeList childNodesList = domNode.childNodes();\r
61     QList<QDomNode> childElementsList;\r
62     QDomNode childNode;\r
63     for(int j = 0; !(childNode = childNodesList.at(j)).isNull(); j++)\r
64     {\r
65         if(childNode.isElement())\r
66         {\r
67             if(childNode.toElement().nodeName() == "category" ||\r
68                childNode.toElement().nodeName() == "item")\r
69                 childElementsList.append(childNode);\r
70         }\r
71     }\r
72     if (i >= 0 && i < childElementsList.count()) {\r
73         childNode = childElementsList.at(i);\r
74         DomItem *childItem = new DomItem(childNode, i, this);\r
75         childItems[i] = childItem;\r
76         return childItem;\r
77     }\r
78     return 0;\r
79 }\r
80 \r
81 /*******************************************************************/\r
82 int DomItem::row()\r
83 {\r
84     return rowNumber;\r
85 }\r