69daf33e504ac31c01887631ab5ca7db5a49a0ee
[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 ShoppingTreeItem *ShoppingTreeItem::parent()\r
78 {\r
79     return parentItem;\r
80 }\r
81 \r
82 bool ShoppingTreeItem::removeChildren(int position, int count)\r
83 {\r
84     if (position < 0 || position + count > childItems.size())\r
85         return false;\r
86 \r
87     for (int row = 0; row < count; ++row)\r
88         delete childItems.takeAt(position);\r
89 \r
90     return true;\r
91 }\r
92 \r
93 bool ShoppingTreeItem::setData(int column, const QVariant &value)\r
94 {\r
95     if (column < 0 || column >= itemData.size())\r
96         return false;\r
97 \r
98     itemData[column] = value;\r
99     return true;\r
100 }\r