git-svn-id: file:///svnroot/family-shop-mgr@3 26eb2498-383b-47a6-be48-5d6f36779e85
[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 "ShoppingTreeItem.h"\r
23 \r
24 ShoppingTreeItem::ShoppingTreeItem(const QVector<QVariant> &data, ShoppingTreeItem *parent)\r
25 {\r
26     parentItem = parent;\r
27     itemData = data;\r
28 }\r
29 \r
30 ShoppingTreeItem::~ShoppingTreeItem()\r
31 {\r
32     qDeleteAll(childItems);\r
33 }\r
34 \r
35 ShoppingTreeItem *ShoppingTreeItem::child(int number)\r
36 {\r
37     return childItems.value(number);\r
38 }\r
39 \r
40 int ShoppingTreeItem::childCount() const\r
41 {\r
42     return childItems.count();\r
43 }\r
44 \r
45 int ShoppingTreeItem::childNumber() const\r
46 {\r
47     if (parentItem)\r
48         return parentItem->childItems.indexOf(const_cast<ShoppingTreeItem*>(this));\r
49 \r
50     return 0;\r
51 }\r
52 \r
53 int ShoppingTreeItem::columnCount() const\r
54 {\r
55     return itemData.count();\r
56 }\r
57 \r
58 QVariant ShoppingTreeItem::data(int column) const\r
59 {\r
60     return itemData.value(column);\r
61 }\r
62 \r
63 bool ShoppingTreeItem::insertChildren(int position, int count, int columns)\r
64 {\r
65     if (position < 0 || position > childItems.size())\r
66         return false;\r
67 \r
68     for (int row = 0; row < count; ++row) {\r
69         QVector<QVariant> data(columns);\r
70         ShoppingTreeItem *item = new ShoppingTreeItem(data, this);\r
71         childItems.insert(position, item);\r
72     }\r
73 \r
74     return true;\r
75 }\r
76 \r
77 bool ShoppingTreeItem::insertColumns(int position, int columns)\r
78 {\r
79     if (position < 0 || position > itemData.size())\r
80         return false;\r
81 \r
82     for (int column = 0; column < columns; ++column)\r
83         itemData.insert(position, QVariant());\r
84 \r
85     foreach (ShoppingTreeItem *child, childItems)\r
86         child->insertColumns(position, columns);\r
87 \r
88     return true;\r
89 }\r
90 \r
91 ShoppingTreeItem *ShoppingTreeItem::parent()\r
92 {\r
93     return parentItem;\r
94 }\r
95 \r
96 bool ShoppingTreeItem::removeChildren(int position, int count)\r
97 {\r
98     if (position < 0 || position + count > childItems.size())\r
99         return false;\r
100 \r
101     for (int row = 0; row < count; ++row)\r
102         delete childItems.takeAt(position);\r
103 \r
104     return true;\r
105 }\r
106 \r
107 bool ShoppingTreeItem::removeColumns(int position, int columns)\r
108 {\r
109     if (position < 0 || position + columns > itemData.size())\r
110         return false;\r
111 \r
112     for (int column = 0; column < columns; ++column)\r
113         itemData.remove(position);\r
114 \r
115     foreach (ShoppingTreeItem *child, childItems)\r
116         child->removeColumns(position, columns);\r
117 \r
118     return true;\r
119 }\r
120 \r
121 bool ShoppingTreeItem::setData(int column, const QVariant &value)\r
122 {\r
123     if (column < 0 || column >= itemData.size())\r
124         return false;\r
125 \r
126     itemData[column] = value;\r
127     return true;\r
128 }\r