git-svn-id: file:///svnroot/family-shop-mgr@23 26eb2498-383b-47a6-be48-5d6f36779e85
[family-shop-mgr] / code / family-shop-mgr / ShoppingTreeModel.cpp
index 5f2985f..d304c41 100644 (file)
@@ -115,6 +115,64 @@ ShoppingTreeModel::~ShoppingTreeModel()
 }\r
 \r
 /*******************************************************************/\r
+bool ShoppingTreeModel::addCategory(QString name)\r
+{\r
+    QModelIndex parent = QModelIndex();\r
+\r
+    if (!this->insertRow(parent.row()+1, parent))\r
+        return false;\r
+\r
+    for(int column = 0; column < this->columnCount(parent); ++column)\r
+    {\r
+        QModelIndex child = this->index(parent.row()+1, column, parent);\r
+        ShoppingTreeItem* item = this->getItem(child);\r
+        item->setItemType(ShoppingTreeItem::Category);\r
+        this->setData(child, QVariant(name), Qt::EditRole);\r
+    }\r
+    return true;\r
+}\r
+\r
+/*******************************************************************/\r
+bool ShoppingTreeModel::addSubCategory(QString name, int position,\r
+                    const QModelIndex &parent)\r
+{\r
+    if (!this->insertRow(position, parent))\r
+        return false;\r
+\r
+    for(int column = 0; column < this->columnCount(parent); ++column)\r
+    {\r
+        QModelIndex child = this->index(parent.row()+1, column, parent);\r
+        ShoppingTreeItem* item = this->getItem(child);\r
+        item->setItemType(ShoppingTreeItem::Category);\r
+        this->setData(child, QVariant(name), Qt::EditRole);\r
+    }\r
+    return true;\r
+}\r
+\r
+/*******************************************************************/\r
+bool ShoppingTreeModel::addItem(QString name, int position,\r
+             const QModelIndex &parent)\r
+{\r
+    if (!this->insertRow(position, parent))\r
+        return false;\r
+\r
+    for(int column = 0; column < this->columnCount(parent); ++column)\r
+    {\r
+        QModelIndex child = this->index(parent.row()+1, column, parent);\r
+        ShoppingTreeItem* item = this->getItem(child);\r
+        item->setItemType(ShoppingTreeItem::Item);\r
+        this->setData(child, QVariant(name), Qt::EditRole);\r
+    }\r
+    return true;\r
+}\r
+\r
+/*******************************************************************/\r
+bool ShoppingTreeModel::removeCategoryOrItem(const QModelIndex &index)\r
+{\r
+    return this->removeRow(index.row(), index.parent());\r
+}\r
+\r
+/*******************************************************************/\r
 QVariant ShoppingTreeModel::data(const QModelIndex &index, int role) const\r
 {\r
     if(!index.isValid())\r
@@ -140,7 +198,8 @@ Qt::ItemFlags ShoppingTreeModel::flags(const QModelIndex &index) const
 ShoppingTreeItem *ShoppingTreeModel::getItem(const QModelIndex &index) const\r
 {\r
     if(index.isValid()){\r
-        ShoppingTreeItem *item = static_cast<ShoppingTreeItem*>(index.internalPointer());\r
+        ShoppingTreeItem *item =\r
+                static_cast<ShoppingTreeItem*>(index.internalPointer());\r
         if(item) return item;\r
     }\r
 \r