2 * This file is part of family-shop-mgr.
4 * family-shop-mgr is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * family-shop-mgr is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with family-shop-mgr. If not, see <http://www.gnu.org/licenses/>.
17 * Author: Unai IRIGOYEN
22 #include "FamilyShoppingManagerMainWindow.h"
24 #include <QMessageBox>
26 #include "ListManagerView.h"
27 #include "GoShoppingView.h"
28 #include "ShoppingTreeModel.h"
30 /*******************************************************************/
31 FamilyShoppingManagerMainWindow::FamilyShoppingManagerMainWindow(QWidget *parent)
32 : QMainWindow(parent), activityView(NULL), editMenu(NULL),
33 showCheckedItemsAction(NULL), goShoppingAction(NULL), endShoppingAction(NULL)
35 aboutAction = new QAction(tr("&About"), this);
36 connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
37 menuBar()->addAction(aboutAction);
43 /*******************************************************************/
44 FamilyShoppingManagerMainWindow::~FamilyShoppingManagerMainWindow()
49 /*******************************************************************/
50 void FamilyShoppingManagerMainWindow::showListManager()
54 delete showCheckedItemsAction;
55 delete endShoppingAction;
58 activityView = new ListManagerView("ShoppingList.xml", this);
59 setCentralWidget(activityView);
61 editMenu = new QMenu(tr("&Edit"), this);
62 addCategoryAction = new QAction(tr("Add category"), this);
63 connect(addCategoryAction, SIGNAL(triggered()),
64 this, SLOT(addCategory()));
65 editMenu->addAction(addCategoryAction);
66 // removeCategoryAction = new QAction(tr("Remove category"), this);
67 // connect(removeCategoryAction, SIGNAL(triggered()),
68 // this, SLOT(removeRow()));
69 // editMenu->addAction(removeCategoryAction);
70 // addItemAction = new QAction(tr("Add item"), this);
71 // connect(addItemAction, SIGNAL(triggered()),
72 // this, SLOT(insertRow()));
73 // editMenu->addAction(addItemAction);
74 // removeItemAction = new QAction(tr("Remove item"), this);
75 // connect(removeItemAction, SIGNAL(triggered()),
76 // this, SLOT(removeRow()));
77 // editMenu->addAction(removeItemAction);
78 menuBar()->addMenu(editMenu);
80 goShoppingAction = new QAction(tr("Go shopping!"), this);
81 connect(goShoppingAction, SIGNAL(triggered()),
82 this, SLOT(showGoShopping()));
83 menuBar()->addAction(goShoppingAction);
85 menuBar()->addAction(aboutAction);
89 /*******************************************************************/
90 void FamilyShoppingManagerMainWindow::showGoShopping()
94 delete goShoppingAction;
97 activityView = new GoShoppingView("ShoppingList.xml", this);
98 setCentralWidget(activityView);
100 showCheckedItemsAction = new QAction(tr("&Show checked"), this);
101 showCheckedItemsAction->setCheckable(true);
102 connect(showCheckedItemsAction, SIGNAL(toggled(bool)),
103 activityView, SLOT(showChecked(bool)));
104 menuBar()->addAction(showCheckedItemsAction);
106 endShoppingAction = new QAction(tr("&End shopping"), this);
107 connect(endShoppingAction, SIGNAL(triggered()),
108 this, SLOT(showListManager()));
109 menuBar()->addAction(endShoppingAction);
111 menuBar()->addAction(aboutAction);
115 /*******************************************************************/
116 void FamilyShoppingManagerMainWindow::showAbout()
119 text = "Application name: Family shopping manager\n";
120 text += "Author: Unai IRIGOYEN\n\n";
121 text += "Licence: GPL";
122 QMessageBox::about(this,tr("About"), text);
125 /*******************************************************************/
127 void FamilyShoppingManagerMainWindow::insertChild()
129 QModelIndex index = ((ListManagerView*) activityView)->
130 selectionModel()->currentIndex();
131 QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
133 if (model->columnCount(index) == 0) {
134 if (!model->insertColumn(0, index))
138 if (!model->insertRow(0, index))
141 for (int column = 0; column < model->columnCount(index); ++column)
143 QModelIndex child = model->index(0, column, index);
144 model->setData(child, QVariant("[No data]"), Qt::EditRole);
145 if (!model->headerData(column, Qt::Horizontal).isValid())
146 model->setHeaderData(column, Qt::Horizontal,
147 QVariant("[No header]"), Qt::EditRole);
150 ((ListManagerView*) activityView)->selectionModel()->
151 setCurrentIndex(model->index(0, 0, index),
152 QItemSelectionModel::ClearAndSelect);
153 ((ListManagerView*) activityView)->updateActions();
157 /*******************************************************************/
158 void FamilyShoppingManagerMainWindow::addCategory()
160 ShoppingTreeModel *model = (ShoppingTreeModel*)
161 ((ListManagerView*) activityView)->model();
163 if(model->addCategory("New category"))
164 ((ListManagerView*) activityView)->updateActions();
167 /*******************************************************************/
168 void FamilyShoppingManagerMainWindow::addSubCategory()
170 QModelIndex index = ((ListManagerView*) activityView)->
171 selectionModel()->currentIndex().parent();
172 ShoppingTreeModel *model = (ShoppingTreeModel*)
173 ((ListManagerView*) activityView)->model();
175 if(model->addSubCategory("New sub-category", index.row()+1, index))
176 ((ListManagerView*) activityView)->updateActions();
179 /*******************************************************************/
180 void FamilyShoppingManagerMainWindow::addItem()
182 QModelIndex index = ((ListManagerView*) activityView)->
183 selectionModel()->currentIndex().parent();
184 ShoppingTreeModel *model = (ShoppingTreeModel*)
185 ((ListManagerView*) activityView)->model();
187 if(model->addItem("New item", index.row()+1, index))
188 ((ListManagerView*) activityView)->updateActions();
191 /*******************************************************************/
192 void FamilyShoppingManagerMainWindow::removeCategoryOrItem()
194 QModelIndex index = ((ListManagerView*) activityView)->
195 selectionModel()->currentIndex();
196 ShoppingTreeModel *model = (ShoppingTreeModel*)
197 ((ListManagerView*) activityView)->model();
198 if (model->removeCategoryOrItem(index))
199 ((ListManagerView*) activityView)->updateActions();