git-svn-id: file:///svnroot/family-shop-mgr@34 26eb2498-383b-47a6-be48-5d6f36779e85
authoronil <u.irigoyen@gmail.com>
Fri, 28 May 2010 23:15:29 +0000 (23:15 +0000)
committeronil <u.irigoyen@gmail.com>
Fri, 28 May 2010 23:15:29 +0000 (23:15 +0000)
code/family-shop-mgr/family-shop-mgr.pro
code/family-shop-mgr/family-shop-mgr.pro.user
code/family-shop-mgr/main.cpp
code/family-shop-mgr/model/ShoppingTreeItem.cpp [new file with mode: 0644]
code/family-shop-mgr/model/ShoppingTreeItem.h [new file with mode: 0644]
code/family-shop-mgr/model/ShoppingTreeModel.cpp [new file with mode: 0644]
code/family-shop-mgr/model/ShoppingTreeModel.h [new file with mode: 0644]

index c9ccb3b..c9eaa1b 100644 (file)
@@ -4,19 +4,27 @@
 QT += xml
 TARGET = build/family-shop-mgr
 TEMPLATE = app
+INCLUDEPATH += model \
+    GUI \
+    sync \
+
 SOURCES += main.cpp \
-    FamilyShoppingManagerMainWindow.cpp \
-    ShoppingTreeModel.cpp \
-    ShoppingTreeItem.cpp \
-    GoShoppingView.cpp \
-    ListManagerView.cpp
-HEADERS += FamilyShoppingManagerMainWindow.h \
-    ShoppingTreeModel.h \
-    ShoppingTreeItem.h \
-    GoShoppingView.h \
-    ListManagerView.h \
+    GUI/FamilyShoppingManagerMainWindow.cpp \
+    GUI/GoShoppingView.cpp \
+    GUI/ListManagerView.cpp \
+    model/ShoppingTreeModel.cpp \
+    model/ShoppingTreeItem.cpp \
+
+HEADERS += GUI/FamilyShoppingManagerMainWindow.h \
+    GUI/GoShoppingView.h \
+    GUI/ListManagerView.h \
+    model/ShoppingTreeModel.h \
+    model/ShoppingTreeItem.h \
 
 TRANSLATIONS = localization/family-shop-mgr_fr.ts \
     localization/family-shop-mgr_es.ts \
     localization/family-shop-mgr_nl.ts \
     localization/family-shop-mgr_eu.ts
+
+MOC_DIR = tmp
+OBJECTS_DIR = tmp
index 6b78c17..ad776c1 100644 (file)
     <value type="QString">KDE_SESSION_UID=1000</value>
     <value type="QString">KDE_SESSION_VERSION=4</value>
     <value type="QString">LANG=fr_FR.UTF-8</value>
-    <value type="QString">LANGUAGE=</value>
     <value type="QString">LD_LIBRARY_PATH=/usr/lib/qtcreator:</value>
     <value type="QString">LOGNAME=onil</value>
     <value type="QString">PATH=/usr/bin:/home/onil/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games</value>
     <value type="QString">KDE_SESSION_UID=1000</value>
     <value type="QString">KDE_SESSION_VERSION=4</value>
     <value type="QString">LANG=fr_FR.UTF-8</value>
-    <value type="QString">LANGUAGE=</value>
     <value type="QString">LD_LIBRARY_PATH=/usr/lib/qtcreator:</value>
     <value type="QString">LOGNAME=onil</value>
     <value type="QString">PATH=/usr/bin:/home/onil/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games</value>
index a602825..9c6e026 100644 (file)
@@ -20,7 +20,7 @@
  */
 
 #include <QtGui/QApplication>
-#include "FamilyShoppingManagerMainWindow.h"
+#include <FamilyShoppingManagerMainWindow.h>
 
 /*******************************************************************/
 int main(int argc, char *argv[])
diff --git a/code/family-shop-mgr/model/ShoppingTreeItem.cpp b/code/family-shop-mgr/model/ShoppingTreeItem.cpp
new file mode 100644 (file)
index 0000000..6e473e7
--- /dev/null
@@ -0,0 +1,85 @@
+/*\r
+ * This file is part of family-shop-mgr.\r
+ *\r
+ * family-shop-mgr is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * family-shop-mgr is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with family-shop-mgr.  If not, see <http://www.gnu.org/licenses/>.\r
+ *\r
+ * Author: Unai IRIGOYEN\r
+ * Date: 12/07/2009\r
+ *\r
+ */\r
+\r
+#include <QtXml>\r
+\r
+#include "ShoppingTreeItem.h"\r
+\r
+\r
+ShoppingTreeItem::ShoppingTreeItem(QDomNode &node, int row, ShoppingTreeItem *parent)\r
+{\r
+    domNode = node;\r
+    rowNumber = row;\r
+    parentItem = parent;\r
+}\r
+\r
+/*******************************************************************/\r
+ShoppingTreeItem::~ShoppingTreeItem()\r
+{\r
+    QHash<int,ShoppingTreeItem*>::iterator it;\r
+    for (it = childItems.begin(); it != childItems.end(); ++it)\r
+        delete it.value();\r
+}\r
+\r
+/*******************************************************************/\r
+QDomNode ShoppingTreeItem::node() const\r
+{\r
+    return domNode;\r
+}\r
+\r
+/*******************************************************************/\r
+ShoppingTreeItem *ShoppingTreeItem::parent()\r
+{\r
+    return parentItem;\r
+}\r
+\r
+/*******************************************************************/\r
+ShoppingTreeItem *ShoppingTreeItem::child(int i)\r
+{\r
+    if (childItems.contains(i))\r
+        return childItems[i];\r
+\r
+    QDomNodeList childNodesList = domNode.childNodes();\r
+    QList<QDomNode> childElementsList;\r
+    QDomNode childNode;\r
+    for(int j = 0; !(childNode = childNodesList.at(j)).isNull(); j++)\r
+    {\r
+        if(childNode.isElement())\r
+        {\r
+            if(childNode.toElement().nodeName() == "category" ||\r
+               childNode.toElement().nodeName() == "item")\r
+                childElementsList.append(childNode);\r
+        }\r
+    }\r
+    if (i >= 0 && i < childElementsList.count()) {\r
+        childNode = childElementsList.at(i);\r
+        ShoppingTreeItem *childItem = new ShoppingTreeItem(childNode, i, this);\r
+        childItems[i] = childItem;\r
+        return childItem;\r
+    }\r
+    return 0;\r
+}\r
+\r
+/*******************************************************************/\r
+int ShoppingTreeItem::row()\r
+{\r
+    return rowNumber;\r
+}\r
diff --git a/code/family-shop-mgr/model/ShoppingTreeItem.h b/code/family-shop-mgr/model/ShoppingTreeItem.h
new file mode 100644 (file)
index 0000000..d0a14a4
--- /dev/null
@@ -0,0 +1,45 @@
+/*\r
+ * This file is part of family-shop-mgr.\r
+ *\r
+ * family-shop-mgr is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * family-shop-mgr is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with family-shop-mgr.  If not, see <http://www.gnu.org/licenses/>.\r
+ *\r
+ * Author: Unai IRIGOYEN\r
+ * Date: 12/07/2009\r
+ *\r
+ */\r
+\r
+#ifndef SHOPPINGTREEITEM_H\r
+#define SHOPPINGTREEITEM_H\r
+\r
+#include <QDomNode>\r
+#include <QHash>\r
+\r
+class ShoppingTreeItem\r
+{\r
+public:\r
+    ShoppingTreeItem(QDomNode &node, int row, ShoppingTreeItem *parent = 0);\r
+    ~ShoppingTreeItem();\r
+    ShoppingTreeItem *child(int i);\r
+    ShoppingTreeItem *parent();\r
+    QDomNode node() const;\r
+    int row();\r
+\r
+private:\r
+    QDomNode domNode;\r
+    QHash<int,ShoppingTreeItem*> childItems;\r
+    ShoppingTreeItem *parentItem;\r
+    int rowNumber;\r
+};\r
+\r
+#endif // SHOPPINGTREEITEM_H\r
diff --git a/code/family-shop-mgr/model/ShoppingTreeModel.cpp b/code/family-shop-mgr/model/ShoppingTreeModel.cpp
new file mode 100644 (file)
index 0000000..ee00a89
--- /dev/null
@@ -0,0 +1,300 @@
+/*\r
+ * This file is part of family-shop-mgr.\r
+ *\r
+ * family-shop-mgr is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * family-shop-mgr is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with family-shop-mgr.  If not, see <http://www.gnu.org/licenses/>.\r
+ *\r
+ * Author: Unai IRIGOYEN\r
+ * Date: 12/07/2009\r
+ *\r
+ */\r
+\r
+#include <QtXml>\r
+\r
+#include "ShoppingTreeItem.h"\r
+#include "ShoppingTreeModel.h"\r
+\r
+ShoppingTreeModel::ShoppingTreeModel(QString fileName, QObject *parent)\r
+    : QAbstractItemModel(parent),m_fileName(fileName)\r
+{\r
+    connect(this, SIGNAL(documentChanged()), this, SLOT(save()));\r
+    QFile file(m_fileName);\r
+    if (!file.open(QIODevice::ReadOnly))\r
+        return;\r
+    if (!domDocument.setContent(&file)) {\r
+        file.close();\r
+        return;\r
+    }\r
+    file.close();\r
+\r
+    rootItemNode = domDocument.firstChildElement("shoppingList");\r
+    rootItem = new ShoppingTreeItem(rootItemNode, 0);\r
+}\r
+\r
+/*******************************************************************/\r
+ShoppingTreeModel::~ShoppingTreeModel()\r
+{\r
+    delete rootItem;\r
+}\r
+\r
+/*******************************************************************/\r
+int ShoppingTreeModel::columnCount(const QModelIndex &/*parent*/) const\r
+{\r
+    return 4;\r
+}\r
+\r
+/*******************************************************************/\r
+QVariant ShoppingTreeModel::data(const QModelIndex &index, int role) const\r
+{\r
+    if (!index.isValid())\r
+        return QVariant();\r
+\r
+    if (role != Qt::DisplayRole)\r
+        return QVariant();\r
+\r
+    ShoppingTreeItem *item = static_cast<ShoppingTreeItem*>(index.internalPointer());\r
+\r
+    QDomNode node = item->node();\r
+\r
+    switch (index.column()) {\r
+        case 0:\r
+            return node.firstChildElement("title").text();\r
+        case 1:\r
+            if(node.toElement().nodeName() == "item")\r
+            {\r
+                return node.firstChildElement("quantity").text();\r
+            }\r
+            return QVariant();\r
+        case 2:\r
+            if(node.toElement().nodeName() == "item")\r
+            {\r
+                return node.firstChildElement("store").text();\r
+            }\r
+            return QVariant();\r
+        case 3:\r
+            if(node.toElement().nodeName() == "item")\r
+            {\r
+                return (bool) node.firstChildElement("checked").text().toInt();\r
+            }\r
+            return QVariant();\r
+        default:\r
+            return QVariant();\r
+    }\r
+}\r
+\r
+/*******************************************************************/\r
+Qt::ItemFlags ShoppingTreeModel::flags(const QModelIndex &index) const\r
+{\r
+    if (!index.isValid())\r
+        return 0;\r
+\r
+    return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;\r
+}\r
+\r
+/*******************************************************************/\r
+QVariant ShoppingTreeModel::headerData(int section, Qt::Orientation orientation,\r
+                              int role) const\r
+{\r
+    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {\r
+        switch (section) {\r
+            case 0:\r
+                return tr("Category/Item name");\r
+            case 1:\r
+                return tr("Quantity");\r
+            case 2:\r
+                return tr("Store");\r
+            case 3:\r
+                return tr("Checked");\r
+            default:\r
+                return QVariant();\r
+        }\r
+    }\r
+\r
+    return QVariant();\r
+}\r
+\r
+/*******************************************************************/\r
+QModelIndex ShoppingTreeModel::index(int row, int column, const QModelIndex &parent)\r
+            const\r
+{\r
+    if (!hasIndex(row, column, parent))\r
+        return QModelIndex();\r
+\r
+    ShoppingTreeItem *parentItem;\r
+\r
+    if (!parent.isValid())\r
+        parentItem = rootItem;\r
+    else\r
+        parentItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());\r
+\r
+    ShoppingTreeItem *childItem = parentItem->child(row);\r
+    if (childItem)\r
+        return createIndex(row, column, childItem);\r
+    else\r
+        return QModelIndex();\r
+}\r
+\r
+/*******************************************************************/\r
+QModelIndex ShoppingTreeModel::parent(const QModelIndex &child) const\r
+{\r
+    if (!child.isValid())\r
+        return QModelIndex();\r
+\r
+    ShoppingTreeItem *childItem = static_cast<ShoppingTreeItem*>(child.internalPointer());\r
+    ShoppingTreeItem *parentItem = childItem->parent();\r
+\r
+    if (!parentItem || parentItem == rootItem)\r
+        return QModelIndex();\r
+\r
+    return createIndex(parentItem->row(), 0, parentItem);\r
+}\r
+\r
+/*******************************************************************/\r
+int ShoppingTreeModel::rowCount(const QModelIndex &parent) const\r
+{\r
+    if (parent.column() > 0)\r
+        return 0;\r
+\r
+    ShoppingTreeItem *parentItem;\r
+\r
+    if (!parent.isValid())\r
+        parentItem = rootItem;\r
+    else\r
+        parentItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());\r
+\r
+    int childCount = 0;\r
+    for(QDomElement childElement = parentItem->node().firstChildElement();\r
+        !childElement.isNull(); childElement = childElement.nextSiblingElement())\r
+    {\r
+        if(childElement.nodeName() == "category" || childElement.nodeName() == "item")\r
+            childCount++;\r
+    }\r
+    return childCount;\r
+}\r
+\r
+/*******************************************************************/\r
+bool ShoppingTreeModel::insertRows(int row, int count, const QModelIndex &parent, ItemType type)\r
+{\r
+    ShoppingTreeItem *parentItem;\r
+\r
+    if (!parent.isValid())\r
+        parentItem = rootItem;\r
+    else\r
+        parentItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());\r
+\r
+    for(int i = 0; i < count; i++)\r
+    {\r
+        QDomElement newElement;\r
+        QDomElement titleElement;\r
+        titleElement.setTagName("title");\r
+        if(type == category)\r
+        {\r
+            newElement.setTagName("category");\r
+            titleElement.setNodeValue(tr("new category"));\r
+            newElement.appendChild(titleElement);\r
+            parentItem->node().insertAfter(newElement, parentItem->child(row + i)->node());\r
+        }\r
+        else if(type == item)\r
+        {\r
+            newElement.setTagName("item");\r
+            titleElement.setNodeValue(tr("new item"));\r
+            newElement.appendChild(titleElement);\r
+            QDomElement quantityElement;\r
+            quantityElement.setTagName("quantity");\r
+            newElement.appendChild(quantityElement);\r
+            QDomElement storeElement;\r
+            storeElement.setTagName("store");\r
+            newElement.appendChild(storeElement);\r
+            QDomElement checkedElement;\r
+            checkedElement.setTagName("checked");\r
+            checkedElement.setNodeValue("0");\r
+            newElement.appendChild(checkedElement);\r
+            QDomElement lastModifiedElement;\r
+            lastModifiedElement.setTagName("lastModified");\r
+            lastModifiedElement.setNodeValue(QDateTime::currentDateTime().toString("dd/MM/yyyy-hh:mm:ss"));\r
+            newElement.appendChild(lastModifiedElement);\r
+            parentItem->node().insertAfter(newElement, parentItem->child(row + i)->node());\r
+        }\r
+    }\r
+    emit documentChanged();\r
+}\r
+\r
+/*******************************************************************/\r
+ShoppingTreeModel::removeRows(int row, int count, const QModelIndex &parent)\r
+{\r
+    ShoppingTreeItem *parentItem;\r
+\r
+    if (!parent.isValid())\r
+        parentItem = rootItem;\r
+    else\r
+        parentItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());\r
+\r
+    for(int i = 0; i < count; i++)\r
+    {\r
+        parentItem->node().removeChild(parentItem->child(i)->node());\r
+    }\r
+    emit documentChanged();\r
+}\r
+\r
+/*******************************************************************/\r
+ShoppingTreeModel::addCategory(QModelIndex &index)\r
+{\r
+    ShoppingTreeItem *selectedItem;\r
+\r
+    if (!parent.isValid())\r
+        selectedItem = rootItem;\r
+    else\r
+        selectedItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());\r
+\r
+    insertRows(selectedItem->row(), 1, parent(index), category);\r
+}\r
+\r
+/*******************************************************************/\r
+ShoppingTreeModel::addItem(QModelIndex &index)\r
+{\r
+    ShoppingTreeItem *selectedItem;\r
+\r
+    if (!parent.isValid())\r
+        selectedItem = rootItem;\r
+    else\r
+        selectedItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());\r
+\r
+    insertRows(selectedItem->row(), 1, parent(index), item);\r
+}\r
+\r
+/*******************************************************************/\r
+ShoppingTreeModel::removeElement(QModelIndex &index)\r
+{\r
+    ShoppingTreeItem *selectedItem;\r
+\r
+    if (!parent.isValid())\r
+        selectedItem = rootItem;\r
+    else\r
+        selectedItem = static_cast<ShoppingTreeItem*>(parent.internalPointer());\r
+\r
+    removeRows(selectedItem->row(), 1, parent(index));\r
+}\r
+\r
+/*******************************************************************/\r
+ShoppingTreeModel::save()\r
+{\r
+    QFile file(m_fileName);\r
+    file.remove();\r
+    if (!file.open(QIODevice::WriteOnly))\r
+        return;\r
+    if (!file.write(domDocument.toString(4).toUtf8())) {\r
+        file.close();\r
+        return;\r
+    }\r
+    file.close();\r
+}\r
diff --git a/code/family-shop-mgr/model/ShoppingTreeModel.h b/code/family-shop-mgr/model/ShoppingTreeModel.h
new file mode 100644 (file)
index 0000000..cb283b0
--- /dev/null
@@ -0,0 +1,75 @@
+/*\r
+ * This file is part of family-shop-mgr.\r
+ *\r
+ * family-shop-mgr is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * family-shop-mgr is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with family-shop-mgr.  If not, see <http://www.gnu.org/licenses/>.\r
+ *\r
+ * Author: Unai IRIGOYEN\r
+ * Date: 12/07/2009\r
+ *\r
+ */\r
+\r
+#ifndef SHOPPINGTREEMODEL_H\r
+#define SHOPPINGTREEMODEL_H\r
+\r
+#include <QAbstractItemModel>\r
+#include <QDomDocument>\r
+#include <QModelIndex>\r
+#include <QVariant>\r
+\r
+class ShoppingTreeItem;\r
+\r
+enum ItemType{\r
+    item,\r
+    category\r
+};\r
+\r
+class ShoppingTreeModel : public QAbstractItemModel\r
+{\r
+    Q_OBJECT\r
+\r
+public:\r
+    ShoppingTreeModel(QString fileName, QObject *parent = 0);\r
+    ~ShoppingTreeModel();\r
+\r
+    QVariant data(const QModelIndex &index, int role) const;\r
+    Qt::ItemFlags flags(const QModelIndex &index) const;\r
+    QVariant headerData(int section, Qt::Orientation orientation,\r
+                        int role = Qt::DisplayRole) const;\r
+    QModelIndex index(int row, int column,\r
+                      const QModelIndex &parent = QModelIndex()) const;\r
+    QModelIndex parent(const QModelIndex &child) const;\r
+    int rowCount(const QModelIndex &parent = QModelIndex()) const;\r
+    int columnCount(const QModelIndex &parent = QModelIndex()) const;\r
+    bool insertRows(int row, int count,\r
+                    const QModelIndex & parent = QModelIndex(),\r
+                    ItemType type = item);\r
+    bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex());\r
+\r
+public slots:\r
+    void addCategory(QModelIndex & index);\r
+    void addItem(QModelIndex & index);\r
+    void removeElement(QModelIndex & index);\r
+    void save();\r
+\r
+signals:\r
+    void documentChanged();\r
+\r
+private:\r
+    QString m_fileName;\r
+    QDomDocument domDocument;\r
+    QDomNode rootItemNode;\r
+    ShoppingTreeItem *rootItem;\r
+};\r
+\r
+#endif // SHOPPINGTREEMODEL_H\r