git-svn-id: file:///svnroot/family-shop-mgr@23 26eb2498-383b-47a6-be48-5d6f36779e85
[family-shop-mgr] / code / family-shop-mgr / FamilyShoppingManagerMainWindow.cpp
index bb8329e..4b821f7 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "ListManagerView.h"
 #include "GoShoppingView.h"
+#include "ShoppingTreeModel.h"
 
 /*******************************************************************/
 FamilyShoppingManagerMainWindow::FamilyShoppingManagerMainWindow(QWidget *parent)
@@ -60,20 +61,20 @@ void FamilyShoppingManagerMainWindow::showListManager()
     editMenu = new QMenu(tr("&Edit"), this);
     addCategoryAction = new QAction(tr("Add category"), this);
     connect(addCategoryAction, SIGNAL(triggered()),
-            this, SLOT(insertRow()));
+            this, SLOT(addCategory()));
     editMenu->addAction(addCategoryAction);
-    removeCategoryAction = new QAction(tr("Remove category"), this);
-    connect(removeCategoryAction, SIGNAL(triggered()),
-            this, SLOT(removeRow()));
-    editMenu->addAction(removeCategoryAction);
-    addItemAction = new QAction(tr("Add item"), this);
-    connect(addItemAction, SIGNAL(triggered()),
-            this, SLOT(insertRow()));
-    editMenu->addAction(addItemAction);
-    removeItemAction = new QAction(tr("Remove item"), this);
-    connect(removeItemAction, SIGNAL(triggered()),
-            this, SLOT(removeRow()));
-    editMenu->addAction(removeItemAction);
+//    removeCategoryAction = new QAction(tr("Remove category"), this);
+//    connect(removeCategoryAction, SIGNAL(triggered()),
+//            this, SLOT(removeRow()));
+//    editMenu->addAction(removeCategoryAction);
+//    addItemAction = new QAction(tr("Add item"), this);
+//    connect(addItemAction, SIGNAL(triggered()),
+//            this, SLOT(insertRow()));
+//    editMenu->addAction(addItemAction);
+//    removeItemAction = new QAction(tr("Remove item"), this);
+//    connect(removeItemAction, SIGNAL(triggered()),
+//            this, SLOT(removeRow()));
+//    editMenu->addAction(removeItemAction);
     menuBar()->addMenu(editMenu);
 
     goShoppingAction = new QAction(tr("Go shopping!"), this);
@@ -122,6 +123,7 @@ void FamilyShoppingManagerMainWindow::showAbout()
 }
 
 /*******************************************************************/
+/*
 void FamilyShoppingManagerMainWindow::insertChild()
 {
      QModelIndex index = ((ListManagerView*) activityView)->
@@ -150,70 +152,49 @@ void FamilyShoppingManagerMainWindow::insertChild()
                              QItemSelectionModel::ClearAndSelect);
      ((ListManagerView*) activityView)->updateActions();
  }
+ */
 
 /*******************************************************************/
-/*
-bool FamilyShoppingManagerMainWindow::insertColumn(const QModelIndex &parent)
+void FamilyShoppingManagerMainWindow::addCategory()
 {
-    QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
-    int column = ((ListManagerView*) activityView)->selectionModel()->
-            currentIndex().column();
+    ShoppingTreeModel *model = (ShoppingTreeModel*)
+                               ((ListManagerView*) activityView)->model();
 
-    // Insert a column in the parent item.
-    bool changed = model->insertColumn(column + 1, parent);
-    if (changed)
-        model->setHeaderData(column + 1, Qt::Horizontal,
-                             QVariant("[No header]"), Qt::EditRole);
-
-    ((ListManagerView*) activityView)->updateActions();
-
-    return changed;
+    if(model->addCategory("New category"))
+        ((ListManagerView*) activityView)->updateActions();
 }
-*/
 
 /*******************************************************************/
-void FamilyShoppingManagerMainWindow::insertRow()
+void FamilyShoppingManagerMainWindow::addSubCategory()
 {
-     QModelIndex index = ((ListManagerView*) activityView)->
-                         selectionModel()->currentIndex();
-     QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
+    QModelIndex index = ((ListManagerView*) activityView)->
+                        selectionModel()->currentIndex().parent();
+    ShoppingTreeModel *model = (ShoppingTreeModel*)
+                               ((ListManagerView*) activityView)->model();
 
-     if (!model->insertRow(index.row()+1, index.parent()))
-         return;
-
-     ((ListManagerView*) activityView)->updateActions();
-
-     for(int column = 0; column < model->columnCount(index.parent()); ++column)
-     {
-         QModelIndex child = model->index(index.row()+1, column, index.parent());
-         model->setData(child, QVariant("New Item"), Qt::EditRole);
-     }
+    if(model->addSubCategory("New sub-category", index.row()+1, index))
+        ((ListManagerView*) activityView)->updateActions();
 }
 
 /*******************************************************************/
-/*
-bool FamilyShoppingManagerMainWindow::removeColumn(const QModelIndex &parent)
+void FamilyShoppingManagerMainWindow::addItem()
 {
-     QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
-     int column = ((ListManagerView*) activityView)->selectionModel()->
-                  currentIndex().column();
+    QModelIndex index = ((ListManagerView*) activityView)->
+                        selectionModel()->currentIndex().parent();
+    ShoppingTreeModel *model = (ShoppingTreeModel*)
+                               ((ListManagerView*) activityView)->model();
 
-     // Insert columns in each child of the parent item.
-     bool changed = model->removeColumn(column, parent);
-
-     if (!parent.isValid() && changed)
-         ((ListManagerView*) activityView)->updateActions();
-
-     return changed;
+    if(model->addItem("New item", index.row()+1, index))
+        ((ListManagerView*) activityView)->updateActions();
 }
- */
 
 /*******************************************************************/
-void FamilyShoppingManagerMainWindow::removeRow()
+void FamilyShoppingManagerMainWindow::removeCategoryOrItem()
 {
      QModelIndex index = ((ListManagerView*) activityView)->
                          selectionModel()->currentIndex();
-     QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
-     if (model->removeRow(index.row(), index.parent()))
+     ShoppingTreeModel *model = (ShoppingTreeModel*)
+                                ((ListManagerView*) activityView)->model();
+     if (model->removeCategoryOrItem(index))
          ((ListManagerView*) activityView)->updateActions();
 }