git-svn-id: file:///svnroot/family-shop-mgr@26 26eb2498-383b-47a6-be48-5d6f36779e85
[family-shop-mgr] / code / family-shop-mgr / FamilyShoppingManagerMainWindow.cpp
index 0e6308c..6407261 100644 (file)
 #include "FamilyShoppingManagerMainWindow.h"
 #include <QMenuBar>
 #include <QMessageBox>
+#include <QInputDialog>
 
 #include "ListManagerView.h"
 #include "GoShoppingView.h"
+#include "ShoppingTreeModel.h"
 
+/*******************************************************************/
 FamilyShoppingManagerMainWindow::FamilyShoppingManagerMainWindow(QWidget *parent)
-    : QMainWindow(parent), activityView(NULL)
+    : QMainWindow(parent), activityView(NULL), //editMenu(NULL),
+    showCheckedItemsAction(NULL), goShoppingAction(NULL), endShoppingAction(NULL)
 {
-    editMenu = new QMenu(tr("&Edit"), this);
-    editMenu->addAction(tr("Add category"));
-    editMenu->addAction(tr("Remove category"));
-    editMenu->addAction(tr("Add item"));
-    editMenu->addAction(tr("Remove item"));
-    menuBar()->addMenu(editMenu);
-
     aboutAction = new QAction(tr("&About"), this);
     connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
     menuBar()->addAction(aboutAction);
 
+
     showListManager();
-    setCentralWidget(activityView);
-    update();
 }
 
+/*******************************************************************/
 FamilyShoppingManagerMainWindow::~FamilyShoppingManagerMainWindow()
 {
     delete activityView;
 }
 
+/*******************************************************************/
 void FamilyShoppingManagerMainWindow::showListManager()
 {
+    menuBar()->clear();
+
+    delete showCheckedItemsAction;
+    delete endShoppingAction;
+
     delete activityView;
     activityView = new ListManagerView("ShoppingList.xml", this);
+
+//    editMenu = new QMenu(tr("&Edit"), this);
+    addCategoryAction = new QAction(tr("Add category"), this);
+    connect(addCategoryAction, SIGNAL(triggered()),
+            this, SLOT(addCategory()));
+//    editMenu->addAction(addCategoryAction);
+    removeCategoryAction = new QAction(tr("Remove category"), this);
+    connect(removeCategoryAction, SIGNAL(triggered()),
+            this, SLOT(removeCategoryOrItem()));
+//    editMenu->addAction(removeCategoryAction);
+    addItemAction = new QAction(tr("Add item"), this);
+    connect(addItemAction, SIGNAL(triggered()),
+            this, SLOT(addItem()));
+//    editMenu->addAction(addItemAction);
+    removeItemAction = new QAction(tr("Remove item"), this);
+    connect(removeItemAction, SIGNAL(triggered()),
+            this, SLOT(removeCategoryOrItem()));
+//    editMenu->addAction(removeItemAction);
+    menuBar()->addAction(addCategoryAction);
+    activityView->addAction(removeCategoryAction);
+    activityView->addAction(addItemAction);
+    activityView->addAction(removeItemAction);
+
+    goShoppingAction = new QAction(tr("Go shopping!"), this);
+    connect(goShoppingAction, SIGNAL(triggered()),
+            this, SLOT(showGoShopping()));
+    menuBar()->addAction(goShoppingAction);
+
+    menuBar()->addAction(aboutAction);
+
+    setCentralWidget(activityView);
+    update();
 }
 
+/*******************************************************************/
 void FamilyShoppingManagerMainWindow::showGoShopping()
 {
+    menuBar()->clear();
+//    delete editMenu;
+    delete goShoppingAction;
+
     delete activityView;
     activityView = new GoShoppingView("ShoppingList.xml", this);
+    setCentralWidget(activityView);
+
+    showCheckedItemsAction = new QAction(tr("&Show checked"), this);
+    showCheckedItemsAction->setCheckable(true);
+    connect(showCheckedItemsAction, SIGNAL(toggled(bool)),
+            activityView, SLOT(showChecked(bool)));
+    menuBar()->addAction(showCheckedItemsAction);
+
+    endShoppingAction = new QAction(tr("&End shopping"), this);
+    connect(endShoppingAction, SIGNAL(triggered()),
+            this, SLOT(showListManager()));
+    menuBar()->addAction(endShoppingAction);
+
+    menuBar()->addAction(aboutAction);
+    update();
 }
 
+/*******************************************************************/
 void FamilyShoppingManagerMainWindow::showAbout()
 {
     QString text;
@@ -70,3 +126,106 @@ void FamilyShoppingManagerMainWindow::showAbout()
     text += "Licence: GPL";
     QMessageBox::about(this,tr("About"), text);
 }
+
+/*******************************************************************/
+/*
+void FamilyShoppingManagerMainWindow::insertChild()
+{
+     QModelIndex index = ((ListManagerView*) activityView)->
+                         selectionModel()->currentIndex();
+     QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
+
+     if (model->columnCount(index) == 0) {
+         if (!model->insertColumn(0, index))
+             return;
+     }
+
+     if (!model->insertRow(0, index))
+         return;
+
+     for (int column = 0; column < model->columnCount(index); ++column)
+     {
+         QModelIndex child = model->index(0, column, index);
+         model->setData(child, QVariant("[No data]"), Qt::EditRole);
+         if (!model->headerData(column, Qt::Horizontal).isValid())
+             model->setHeaderData(column, Qt::Horizontal,
+                                  QVariant("[No header]"), Qt::EditRole);
+     }
+
+     ((ListManagerView*) activityView)->selectionModel()->
+             setCurrentIndex(model->index(0, 0, index),
+                             QItemSelectionModel::ClearAndSelect);
+     ((ListManagerView*) activityView)->updateActions();
+ }
+ */
+
+/*******************************************************************/
+void FamilyShoppingManagerMainWindow::addCategory()
+{
+    ShoppingTreeModel *model = (ShoppingTreeModel*)
+                               ((ListManagerView*) activityView)->model();
+
+    bool* ok = new bool(false);
+    QString name = QInputDialog::getText(this, tr("Enter category name"),
+                                         tr("Category name:"),
+                                         QLineEdit::Normal, QString(), ok);
+    if(!ok)
+        return;
+
+    if(model->addCategory(name))
+        ((ListManagerView*) activityView)->updateActions();
+    delete ok;
+}
+
+/*******************************************************************/
+void FamilyShoppingManagerMainWindow::addSubCategory()
+{
+    QModelIndex index = ((ListManagerView*) activityView)->
+                        selectionModel()->currentIndex().parent();
+    ShoppingTreeModel *model = (ShoppingTreeModel*)
+                               ((ListManagerView*) activityView)->model();
+
+    bool* ok = new bool(false);
+    QString name = QInputDialog::getText(this, tr("Enter category name"),
+                                         tr("Category name:"),
+                                         QLineEdit::Normal, QString(), ok);
+    if(!ok)
+        return;
+
+    if(model->addSubCategory(name, index.row()+1, index))
+        ((ListManagerView*) activityView)->updateActions();
+
+    delete ok;
+}
+
+/*******************************************************************/
+void FamilyShoppingManagerMainWindow::addItem()
+{
+    QModelIndex index = ((ListManagerView*) activityView)->
+                        selectionModel()->currentIndex().parent();
+    ShoppingTreeModel *model = (ShoppingTreeModel*)
+                               ((ListManagerView*) activityView)->model();
+
+    bool* ok = new bool(false);
+    QString name = QInputDialog::getText(this, tr("Enter item name"),
+                                         tr("Item name:"),
+                                         QLineEdit::Normal, QString(), ok);
+    if(!ok)
+        return;
+
+    if(model->addItem(name, index.row()+1, index))
+        ((ListManagerView*) activityView)->updateActions();
+
+    delete ok;
+}
+
+/*******************************************************************/
+void FamilyShoppingManagerMainWindow::removeCategoryOrItem()
+{
+     QModelIndex index = ((ListManagerView*) activityView)->
+                         selectionModel()->currentIndex();
+     ShoppingTreeModel *model = (ShoppingTreeModel*)
+                                ((ListManagerView*) activityView)->model();
+     if (model->removeCategoryOrItem(index))
+         ((ListManagerView*) activityView)->updateActions();
+}