git-svn-id: file:///svnroot/family-shop-mgr@8 26eb2498-383b-47a6-be48-5d6f36779e85
[family-shop-mgr] / code / family-shop-mgr / ShoppingTreeModel.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 "ShoppingTreeModel.h"\r
23 \r
24 #include "ShoppingTreeItem.h"\r
25 #include <QFile>\r
26 \r
27 ShoppingTreeModel::ShoppingTreeModel(const QString &xmlFileName,\r
28                                      QObject *parent) :\r
29 QAbstractItemModel(parent), m_document("ShoppingList")\r
30 {\r
31     QString error;\r
32     int errLine;\r
33     int errColumn;\r
34 \r
35     QFile file(xmlFileName);\r
36     if(!file.open(QIODevice::ReadOnly))\r
37         return;\r
38     // Parse xml file\r
39     if(!m_document.setContent(&file, true, &error, &errLine, &errColumn))\r
40     {\r
41         emit xmlParseError(error, errLine, errColumn);\r
42         file.close(();\r
43         return;\r
44     }\r
45     file.close();\r
46 \r
47     QDomElement root = m_document.documentElement();\r
48     if(root.tagName() != "shoppingList" || !root.hasAttribute("version"))\r
49     {\r
50         emit invalidDocument();\r
51         return;\r
52     }\r
53     else if(root.attribute("version") == "1.0")\r
54         // set column titles\r
55         QVector<QVariant> rootData;\r
56         rootData << "Category/Item name"\r
57                 << "Quantity" << "Store";\r
58 \r
59         rootItem = new ShoppingTreeItem(rootData);\r
60     }\r
61     else\r
62     {\r
63         // upgrade document version if possible\r
64         ;\r
65     }\r
66 \r
67     QDomElement child = root.firstChildElement("category");\r
68     while(!child.isNull())\r
69     {\r
70         // Parse all categories\r
71         parseCategoryElement(child);\r
72         child = child.nextSiblingElement("category");\r
73     }\r
74 \r
75     child = root.firstChildElement("item");\r
76     while(!child.isNull())\r
77     {\r
78         // parse all items which don't have category\r
79         rootItem->insertChildren(\r
80                 rootItem->childCount(), 1,\r
81                 rootItem->columnCount());\r
82         QVector<QVariant> columnData =\r
83                 getColumnsFromItemElement(child);\r
84         for(int column = 0; column < columnData.size(); column++)\r
85         {\r
86             rootItem->child(rootItem->childCount() - 1)->setData(column, columnData[column]);\r
87         }\r
88     }\r
89 }\r
90 \r
91 void ShoppingTreeModel::parseCategoryElement(const QDomElement &element,\r
92                                              ShoppingTreeItem *parentItem)\r
93 {\r
94     if(!parentItem)\r
95         parentItem = rootItem;\r
96     // TODO\r
97 \r
98 }\r
99 \r
100 QVector<QVariant> ShoppingTreeModel::getColumnsFromItemElement(const QDomElement &element)\r
101 {\r
102     QString title = child.firstChildElement("title").text();\r
103     int quantity = child.firstChildElement("quantity").text().toInt();\r
104     QString store = child.firstChildElement("store").text();\r
105     if(title.isEmpty() || quantity < 0)\r
106     {\r
107         emit invalidDocument;\r
108         return;\r
109     }\r
110 \r
111     QVector<QVariant> data;\r
112     data << title << quantity << store;\r
113     return data;\r
114 }\r