2 * This file is part of family-shop-mgr.
\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
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
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
17 * Author: Unai IRIGOYEN
\r
22 #include "ShoppingTreeModel.h"
\r
24 #include "ShoppingTreeItem.h"
\r
27 ShoppingTreeModel::ShoppingTreeModel(const QString &xmlFileName,
\r
29 QAbstractItemModel(parent), m_document("ShoppingList")
\r
35 QFile file(xmlFileName);
\r
36 if(!file.open(QIODevice::ReadOnly))
\r
39 if(!m_document.setContent(&file, true, &error, &errLine, &errColumn))
\r
41 emit xmlParseError(error, errLine, errColumn);
\r
47 QDomElement root = m_document.documentElement();
\r
48 if(root.tagName() != "shoppingList" || !root.hasAttribute("version"))
\r
50 emit invalidDocument();
\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
59 rootItem = new ShoppingTreeItem(rootData);
\r
63 // upgrade document version if possible
\r
67 QDomElement child = root.firstChildElement("category");
\r
68 while(!child.isNull())
\r
70 // Parse all categories
\r
71 parseCategoryElement(child);
\r
72 child = child.nextSiblingElement("category");
\r
75 child = root.firstChildElement("item");
\r
76 while(!child.isNull())
\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
86 rootItem->child(rootItem->childCount() - 1)->setData(column, columnData[column]);
\r
91 void ShoppingTreeModel::parseCategoryElement(const QDomElement &element,
\r
92 ShoppingTreeItem *parentItem)
\r
95 parentItem = rootItem;
\r
100 QVector<QVariant> ShoppingTreeModel::getColumnsFromItemElement(const QDomElement &element)
\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
107 emit invalidDocument;
\r
111 QVector<QVariant> data;
\r
112 data << title << quantity << store;
\r