66829bf15ea9234c1235cf144c8c9793f1efc588
[family-shop-mgr] / code / family-shop-mgr / ShoppingTreeModel.h
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 #ifndef SHOPPINGTREEMODEL_H\r
23 #define SHOPPINGTREEMODEL_H\r
24 \r
25 #include <QAbstractItemModel>\r
26 #include <QDomDocument>\r
27 #include <QModelIndex>\r
28 #include <QVector>\r
29 #include <QVariant>\r
30 #include <QHash>\r
31 \r
32 class ShoppingTreeItem;\r
33 \r
34 class ShoppingTreeModel : public QAbstractItemModel\r
35 {\r
36 Q_OBJECT\r
37 \r
38 public:\r
39     ShoppingTreeModel(const QString &xmlFileName, QObject *parent = 0);\r
40     ~ShoppingTreeModel();\r
41 \r
42     bool addCategory(QString name);\r
43     bool addSubCategory(QString name, int position,\r
44                         const QModelIndex &parent = QModelIndex());\r
45     bool addItem(QString name, int position,\r
46                  const QModelIndex &parent = QModelIndex());\r
47     bool removeCategoryOrItem(const QModelIndex &index = QModelIndex());\r
48 \r
49     QVariant data(const QModelIndex &index, int role) const;\r
50     QVariant headerData(int section, Qt::Orientation orientation,\r
51                         int role = Qt::DisplayRole) const;\r
52     QModelIndex index(int row, int column,\r
53                       const QModelIndex &parent = QModelIndex()) const;\r
54     QModelIndex parent(const QModelIndex &index) const;\r
55 \r
56     int rowCount(const QModelIndex &parent = QModelIndex()) const;\r
57     int columnCount(const QModelIndex &parent = QModelIndex()) const;\r
58 \r
59     Qt::ItemFlags flags(const QModelIndex &index) const;\r
60     bool setData(const QModelIndex &index, const QVariant &value,\r
61                  int role = Qt::EditRole);\r
62     bool setHeaderData(int section, Qt::Orientation orientation,\r
63                        const QVariant &value, int role = Qt::EditRole);\r
64 \r
65 private:\r
66     bool insertColumns(int position, int columns,\r
67                            const QModelIndex &parent = QModelIndex());\r
68     bool removeColumns(int position, int columns,\r
69                            const QModelIndex &parent = QModelIndex());\r
70     bool insertRows(int position, int rows,\r
71                     const QModelIndex &parent = QModelIndex());\r
72     bool removeRows(int position, int rows,\r
73                     const QModelIndex &parent = QModelIndex());\r
74 \r
75 signals:\r
76     void xmlParseError(QString error, int line, int column);\r
77     void invalidDocument();\r
78 \r
79 public slots:\r
80     void registerInsertedChild(ShoppingTreeItem *item);\r
81     void deleteRemovedChild(ShoppingTreeItem *item);\r
82 \r
83 protected:\r
84     void parseCategoryElement(const QDomElement &element,\r
85                               ShoppingTreeItem *parentItem = 0);\r
86     QVector<QVariant> getColumnsFromItemElement(const QDomElement &element);\r
87     bool updateDomElement(ShoppingTreeItem *item, int column);\r
88     void updateXmlFile() const;\r
89 \r
90 private:\r
91     ShoppingTreeItem *getItem(const QModelIndex &index) const;\r
92 \r
93     ShoppingTreeItem *rootItem;\r
94     QString m_xmlFileName;\r
95     QDomDocument m_document;\r
96     QHash<ShoppingTreeItem *, QDomElement> m_domElementForItem;\r
97 };\r
98 \r
99 #endif // SHOPPINGTREEMODEL_H\r