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
28 ShoppingTreeModel::ShoppingTreeModel(const QString &xmlFileName,
\r
30 QAbstractItemModel(parent), m_document("ShoppingList")
\r
36 QFile file(xmlFileName);
\r
37 if(!file.open(QIODevice::ReadOnly))
\r
40 if(!m_document.setContent(&file, true, &error, &errLine, &errColumn))
\r
42 emit xmlParseError(error, errLine, errColumn);
\r
48 QDomElement root = m_document.documentElement();
\r
49 if(root.tagName() != "shoppingList" || !root.hasAttribute("version"))
\r
51 emit invalidDocument();
\r
54 else if(root.attribute("version") == "1.0")
\r
56 // set column titles
\r
57 QVector<QVariant> rootData;
\r
58 rootData << "Category/Item name"
\r
59 << "Quantity" << "Store";
\r
61 rootItem = new ShoppingTreeItem(rootData);
\r
65 // upgrade document version if possible
\r
69 QDomElement child = root.firstChildElement("category");
\r
70 while(!child.isNull())
\r
72 // Parse all categories
\r
73 parseCategoryElement(child);
\r
74 child = child.nextSiblingElement("category");
\r
77 child = root.firstChildElement("item");
\r
78 while(!child.isNull())
\r
80 // parse all items which don't have category
\r
81 rootItem->insertChildren(
\r
82 rootItem->childCount(), 1,
\r
83 rootItem->columnCount());
\r
84 QVector<QVariant> columnData =
\r
85 getColumnsFromItemElement(child);
\r
86 for(int column = 0; column < columnData.size(); column++)
\r
88 rootItem->child(rootItem->childCount() - 1)->setData(column, columnData[column]);
\r
90 m_domElementForItem.insert(rootItem->child(rootItem->childCount() - 1),
\r
95 ShoppingTreeModel::~ShoppingTreeModel()
\r
100 QVariant ShoppingTreeModel::data(const QModelIndex &index, int role) const
\r
102 if(!index.isValid())
\r
105 if(role != Qt::DisplayRole && role != Qt::EditRole)
\r
108 ShoppingTreeItem *item = getItem(index);
\r
109 return item->data(index.column());
\r
112 Qt::ItemFlags ShoppingTreeModel::flags(const QModelIndex &index) const
\r
114 if(!index.isValid())
\r
117 return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
\r
120 ShoppingTreeItem *ShoppingTreeModel::getItem(const QModelIndex &index) const
\r
122 if(index.isValid()){
\r
123 ShoppingTreeItem *item = static_cast<ShoppingTreeItem*>(index.internalPointer());
\r
124 if(item) return item;
\r
130 QVariant ShoppingTreeModel::headerData(int section, Qt::Orientation orientation,
\r
133 if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
\r
134 return rootItem->data(section);
\r
139 QModelIndex ShoppingTreeModel::index(int row, int column, const QModelIndex &parent) const
\r
141 if(parent.isValid() && parent.column() != 0)
\r
142 return QModelIndex();
\r
144 ShoppingTreeItem *parentItem = getItem(parent);
\r
146 ShoppingTreeItem *childItem = parentItem->child(row);
\r
148 return createIndex(row, column, childItem);
\r
150 return QModelIndex();
\r
153 bool ShoppingTreeModel::insertColumns(int position, int columns, const QModelIndex &parent)
\r
157 beginInsertColumns(parent, position, position + columns - 1);
\r
158 success = rootItem->insertColumns(position, columns);
\r
159 endInsertColumns();
\r
164 bool ShoppingTreeModel::insertRows(int position, int rows, const QModelIndex &parent)
\r
166 ShoppingTreeItem *parentItem = getItem(parent);
\r
169 beginInsertRows(parent, position, position + rows - 1);
\r
170 success = parentItem->insertChildren(position, rows, rootItem->columnCount());
\r
176 QModelIndex ShoppingTreeModel::parent(const QModelIndex &index) const
\r
178 if(!index.isValid())
\r
179 return QModelIndex();
\r
181 ShoppingTreeItem *childItem = getItem(index);
\r
182 ShoppingTreeItem *parentItem = childItem->parent();
\r
184 if(parentItem == rootItem)
\r
185 return QModelIndex();
\r
187 return createIndex(parentItem->childNumber(), 0, parentItem);
\r
190 bool ShoppingTreeModel::removeColumns(int position, int columns, const QModelIndex &parent)
\r
194 beginRemoveColumns(parent, position, position + columns - 1);
\r
195 success = rootItem->removeColumns(position, columns);
\r
196 endRemoveColumns();
\r
198 if(rootItem->columnCount() != 0)
\r
199 removeRows(0, rowCount());
\r
204 bool ShoppingTreeModel::removeRows(int position, int rows, const QModelIndex &parent)
\r
206 ShoppingTreeItem *parentItem = getItem(parent);
\r
209 beginRemoveRows(parent, position, position + rows - 1);
\r
210 success = parentItem->removeChildren(position, rows);
\r
216 int ShoppingTreeModel::rowCount(const QModelIndex &parent) const
\r
218 ShoppingTreeItem *parentItem = getItem(parent);
\r
220 return parentItem->childCount();
\r
223 int ShoppingTreeModel::columnCount(const QModelIndex &parent) const
\r
225 return rootItem->columnCount();
\r
227 bool ShoppingTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
\r
229 if(role != Qt::EditRole)
\r
232 ShoppingTreeItem *item = getItem(index);
\r
233 bool result = item->setData(index.column(),value);
\r
236 emit dataChanged(index, index);
\r
241 bool ShoppingTreeModel::setHeaderData(int section, Qt::Orientation orientation,
\r
242 const QVariant &value, int role)
\r
244 if(role != Qt::EditRole || orientation != Qt::Horizontal)
\r
247 bool result = rootItem->setData(section, value);
\r
250 emit headerDataChanged(orientation, section, section);
\r
255 void ShoppingTreeModel::parseCategoryElement(const QDomElement &element,
\r
256 ShoppingTreeItem *parentItem)
\r
258 // if parent is null then add category to root
\r
260 parentItem = rootItem;
\r
262 ShoppingTreeItem *item;
\r
263 QString title = element.firstChildElement("title").text();
\r
264 if(!title.isEmpty())
\r
266 parentItem->insertChildren(parentItem->childCount(), 1,
\r
267 rootItem->columnCount());
\r
269 parentItem->child(parentItem->childCount() - 1)->setData(0, title);
\r
270 m_domElementForItem.insert(parentItem->child(parentItem->childCount() - 1),
\r
272 item = parentItem->child(parentItem->childCount() - 1);
\r
276 emit invalidDocument();
\r
280 // add each sub category and item to the tree
\r
281 QDomElement child = element.firstChildElement();
\r
282 while(!child.isNull())
\r
284 if(child.tagName() == "category")
\r
286 parseCategoryElement(child, parentItem);
\r
288 else if(child.tagName() == "item")
\r
290 item->insertChildren(
\r
291 item->childCount(), 1,
\r
292 rootItem->columnCount());
\r
293 QVector<QVariant> columnData =
\r
294 getColumnsFromItemElement(child);
\r
295 for(int column = 0; column < columnData.size(); column++)
\r
297 item->child(item->childCount() - 1)->setData(column, columnData[column]);
\r
299 m_domElementForItem.insert(item->child(item->childCount() - 1),
\r
304 emit invalidDocument();
\r
308 child = child.nextSiblingElement();
\r
312 QVector<QVariant> ShoppingTreeModel::getColumnsFromItemElement(const QDomElement &element)
\r
314 QVector<QVariant> data;
\r
315 QString title = element.firstChildElement("title").text();
\r
316 int quantity = element.firstChildElement("quantity").text().toInt();
\r
317 QString store = element.firstChildElement("store").text();
\r
318 if(title.isEmpty() || quantity < 0)
\r
320 emit invalidDocument();
\r
324 data << title << quantity << store;
\r