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
24 #include "ShoppingTreeItem.h"
\r
25 #include "ShoppingTreeModel.h"
\r
27 ShoppingTreeModel::ShoppingTreeModel(QString fileName, QObject *parent)
\r
28 : QAbstractItemModel(parent),m_fileName(fileName)
\r
30 connect(this, SIGNAL(documentChanged()), this, SLOT(save()));
\r
31 QFile file(m_fileName);
\r
32 if (!file.open(QIODevice::ReadOnly))
\r
34 if (!domDocument.setContent(&file)) {
\r
40 rootItemNode = domDocument.firstChildElement("shoppingList");
\r
41 rootItem = new ShoppingTreeItem(rootItemNode, 0);
\r
44 /*******************************************************************/
\r
45 ShoppingTreeModel::~ShoppingTreeModel()
\r
50 /*******************************************************************/
\r
51 int ShoppingTreeModel::columnCount(const QModelIndex &/*parent*/) const
\r
56 /*******************************************************************/
\r
57 QVariant ShoppingTreeModel::data(const QModelIndex &index, int role) const
\r
59 if (!index.isValid())
\r
62 if (role != Qt::DisplayRole)
\r
65 ShoppingTreeItem *item = static_cast<ShoppingTreeItem*>(index.internalPointer());
\r
67 QDomNode node = item->node();
\r
69 switch (index.column()) {
\r
71 return node.firstChildElement("title").text();
\r
73 if(node.toElement().nodeName() == "item")
\r
75 return node.firstChildElement("quantity").text();
\r
79 if(node.toElement().nodeName() == "item")
\r
81 return node.firstChildElement("store").text();
\r
85 if(node.toElement().nodeName() == "item")
\r
87 return (bool) node.firstChildElement("checked").text().toInt();
\r
95 /*******************************************************************/
\r
96 Qt::ItemFlags ShoppingTreeModel::flags(const QModelIndex &index) const
\r
98 if (!index.isValid())
\r
101 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
\r
104 /*******************************************************************/
\r
105 QVariant ShoppingTreeModel::headerData(int section, Qt::Orientation orientation,
\r
108 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
\r
111 return tr("Category/Item name");
\r
113 return tr("Quantity");
\r
115 return tr("Store");
\r
117 return tr("Checked");
\r
126 /*******************************************************************/
\r
127 QModelIndex ShoppingTreeModel::index(int row, int column, const QModelIndex &parent)
\r
130 if (!hasIndex(row, column, parent))
\r
131 return QModelIndex();
\r
133 ShoppingTreeItem *parentItem;
\r
135 if (!parent.isValid())
\r
136 parentItem = rootItem;
\r
138 parentItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());
\r
140 ShoppingTreeItem *childItem = parentItem->child(row);
\r
142 return createIndex(row, column, childItem);
\r
144 return QModelIndex();
\r
147 /*******************************************************************/
\r
148 QModelIndex ShoppingTreeModel::parent(const QModelIndex &child) const
\r
150 if (!child.isValid())
\r
151 return QModelIndex();
\r
153 ShoppingTreeItem *childItem = static_cast<ShoppingTreeItem*>(child.internalPointer());
\r
154 ShoppingTreeItem *parentItem = childItem->parent();
\r
156 if (!parentItem || parentItem == rootItem)
\r
157 return QModelIndex();
\r
159 return createIndex(parentItem->row(), 0, parentItem);
\r
162 /*******************************************************************/
\r
163 int ShoppingTreeModel::rowCount(const QModelIndex &parent) const
\r
165 if (parent.column() > 0)
\r
168 ShoppingTreeItem *parentItem;
\r
170 if (!parent.isValid())
\r
171 parentItem = rootItem;
\r
173 parentItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());
\r
175 int childCount = 0;
\r
176 for(QDomElement childElement = parentItem->node().firstChildElement();
\r
177 !childElement.isNull(); childElement = childElement.nextSiblingElement())
\r
179 if(childElement.nodeName() == "category" || childElement.nodeName() == "item")
\r
185 /*******************************************************************/
\r
186 bool ShoppingTreeModel::insertRows(int row, int count, const QModelIndex &parent, ItemType type)
\r
188 ShoppingTreeItem *parentItem;
\r
190 if (!parent.isValid())
\r
191 parentItem = rootItem;
\r
193 parentItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());
\r
195 for(int i = 0; i < count; i++)
\r
197 QDomElement newElement;
\r
198 QDomElement titleElement;
\r
199 titleElement.setTagName("title");
\r
200 if(type == category)
\r
202 newElement.setTagName("category");
\r
203 titleElement.setNodeValue(tr("new category"));
\r
204 newElement.appendChild(titleElement);
\r
205 parentItem->node().insertAfter(newElement, parentItem->child(row + i)->node());
\r
207 else if(type == item)
\r
209 newElement.setTagName("item");
\r
210 titleElement.setNodeValue(tr("new item"));
\r
211 newElement.appendChild(titleElement);
\r
212 QDomElement quantityElement;
\r
213 quantityElement.setTagName("quantity");
\r
214 newElement.appendChild(quantityElement);
\r
215 QDomElement storeElement;
\r
216 storeElement.setTagName("store");
\r
217 newElement.appendChild(storeElement);
\r
218 QDomElement checkedElement;
\r
219 checkedElement.setTagName("checked");
\r
220 checkedElement.setNodeValue("0");
\r
221 newElement.appendChild(checkedElement);
\r
222 QDomElement lastModifiedElement;
\r
223 lastModifiedElement.setTagName("lastModified");
\r
224 lastModifiedElement.setNodeValue(QDateTime::currentDateTime().toString("dd/MM/yyyy-hh:mm:ss"));
\r
225 newElement.appendChild(lastModifiedElement);
\r
226 parentItem->node().insertAfter(newElement, parentItem->child(row + i)->node());
\r
229 emit documentChanged();
\r
232 /*******************************************************************/
\r
233 ShoppingTreeModel::removeRows(int row, int count, const QModelIndex &parent)
\r
235 ShoppingTreeItem *parentItem;
\r
237 if (!parent.isValid())
\r
238 parentItem = rootItem;
\r
240 parentItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());
\r
242 for(int i = 0; i < count; i++)
\r
244 parentItem->node().removeChild(parentItem->child(i)->node());
\r
246 emit documentChanged();
\r
249 /*******************************************************************/
\r
250 ShoppingTreeModel::addCategory(QModelIndex &index)
\r
252 ShoppingTreeItem *selectedItem;
\r
254 if (!parent.isValid())
\r
255 selectedItem = rootItem;
\r
257 selectedItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());
\r
259 insertRows(selectedItem->row(), 1, parent(index), category);
\r
262 /*******************************************************************/
\r
263 ShoppingTreeModel::addItem(QModelIndex &index)
\r
265 ShoppingTreeItem *selectedItem;
\r
267 if (!parent.isValid())
\r
268 selectedItem = rootItem;
\r
270 selectedItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());
\r
272 insertRows(selectedItem->row(), 1, parent(index), item);
\r
275 /*******************************************************************/
\r
276 ShoppingTreeModel::removeElement(QModelIndex &index)
\r
278 ShoppingTreeItem *selectedItem;
\r
280 if (!parent.isValid())
\r
281 selectedItem = rootItem;
\r
283 selectedItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());
\r
285 removeRows(selectedItem->row(), 1, parent(index));
\r
288 /*******************************************************************/
\r
289 ShoppingTreeModel::save()
\r
291 QFile file(m_fileName);
\r
293 if (!file.open(QIODevice::WriteOnly))
\r
295 if (!file.write(domDocument.toString(4).toUtf8())) {
\r