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>
25 #include <QInputDialog>
27 #include "ListManagerView.h"
28 #include "GoShoppingView.h"
29 #include "ShoppingTreeModel.h"
31 /*******************************************************************/
32 FamilyShoppingManagerMainWindow::FamilyShoppingManagerMainWindow(QWidget *parent)
33 : QMainWindow(parent), activityView(NULL), editMenu(NULL),
34 showCheckedItemsAction(NULL), goShoppingAction(NULL), endShoppingAction(NULL)
36 aboutAction = new QAction(tr("&About"), this);
37 connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
38 menuBar()->addAction(aboutAction);
44 /*******************************************************************/
45 FamilyShoppingManagerMainWindow::~FamilyShoppingManagerMainWindow()
50 /*******************************************************************/
51 void FamilyShoppingManagerMainWindow::showListManager()
55 delete showCheckedItemsAction;
56 delete endShoppingAction;
59 activityView = new ListManagerView("ShoppingList.xml", this);
60 setCentralWidget(activityView);
62 editMenu = new QMenu(tr("&Edit"), this);
63 addCategoryAction = new QAction(tr("Add category"), this);
64 connect(addCategoryAction, SIGNAL(triggered()),
65 this, SLOT(addCategory()));
66 editMenu->addAction(addCategoryAction);
67 // removeCategoryAction = new QAction(tr("Remove category"), this);
68 // connect(removeCategoryAction, SIGNAL(triggered()),
69 // this, SLOT(removeRow()));
70 // editMenu->addAction(removeCategoryAction);
71 // addItemAction = new QAction(tr("Add item"), this);
72 // connect(addItemAction, SIGNAL(triggered()),
73 // this, SLOT(insertRow()));
74 // editMenu->addAction(addItemAction);
75 // removeItemAction = new QAction(tr("Remove item"), this);
76 // connect(removeItemAction, SIGNAL(triggered()),
77 // this, SLOT(removeRow()));
78 // editMenu->addAction(removeItemAction);
79 menuBar()->addMenu(editMenu);
81 goShoppingAction = new QAction(tr("Go shopping!"), this);
82 connect(goShoppingAction, SIGNAL(triggered()),
83 this, SLOT(showGoShopping()));
84 menuBar()->addAction(goShoppingAction);
86 menuBar()->addAction(aboutAction);
90 /*******************************************************************/
91 void FamilyShoppingManagerMainWindow::showGoShopping()
95 delete goShoppingAction;
98 activityView = new GoShoppingView("ShoppingList.xml", this);
99 setCentralWidget(activityView);
101 showCheckedItemsAction = new QAction(tr("&Show checked"), this);
102 showCheckedItemsAction->setCheckable(true);
103 connect(showCheckedItemsAction, SIGNAL(toggled(bool)),
104 activityView, SLOT(showChecked(bool)));
105 menuBar()->addAction(showCheckedItemsAction);
107 endShoppingAction = new QAction(tr("&End shopping"), this);
108 connect(endShoppingAction, SIGNAL(triggered()),
109 this, SLOT(showListManager()));
110 menuBar()->addAction(endShoppingAction);
112 menuBar()->addAction(aboutAction);
116 /*******************************************************************/
117 void FamilyShoppingManagerMainWindow::showAbout()
120 text = "Application name: Family shopping manager\n";
121 text += "Author: Unai IRIGOYEN\n\n";
122 text += "Licence: GPL";
123 QMessageBox::about(this,tr("About"), text);
126 /*******************************************************************/
128 void FamilyShoppingManagerMainWindow::insertChild()
130 QModelIndex index = ((ListManagerView*) activityView)->
131 selectionModel()->currentIndex();
132 QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
134 if (model->columnCount(index) == 0) {
135 if (!model->insertColumn(0, index))
139 if (!model->insertRow(0, index))
142 for (int column = 0; column < model->columnCount(index); ++column)
144 QModelIndex child = model->index(0, column, index);
145 model->setData(child, QVariant("[No data]"), Qt::EditRole);
146 if (!model->headerData(column, Qt::Horizontal).isValid())
147 model->setHeaderData(column, Qt::Horizontal,
148 QVariant("[No header]"), Qt::EditRole);
151 ((ListManagerView*) activityView)->selectionModel()->
152 setCurrentIndex(model->index(0, 0, index),
153 QItemSelectionModel::ClearAndSelect);
154 ((ListManagerView*) activityView)->updateActions();
158 /*******************************************************************/
159 void FamilyShoppingManagerMainWindow::addCategory()
161 ShoppingTreeModel *model = (ShoppingTreeModel*)
162 ((ListManagerView*) activityView)->model();
164 bool* ok = new bool(false);
165 QString name = QInputDialog::getText(this, tr("Enter category name"),
166 tr("Category name:"),
167 QLineEdit::Normal, QString(), ok);
171 if(model->addCategory(name))
172 ((ListManagerView*) activityView)->updateActions();
176 /*******************************************************************/
177 void FamilyShoppingManagerMainWindow::addSubCategory()
179 QModelIndex index = ((ListManagerView*) activityView)->
180 selectionModel()->currentIndex().parent();
181 ShoppingTreeModel *model = (ShoppingTreeModel*)
182 ((ListManagerView*) activityView)->model();
184 bool* ok = new bool(false);
185 QString name = QInputDialog::getText(this, tr("Enter category name"),
186 tr("Category name:"),
187 QLineEdit::Normal, QString(), ok);
191 if(model->addSubCategory(name, index.row()+1, index))
192 ((ListManagerView*) activityView)->updateActions();
197 /*******************************************************************/
198 void FamilyShoppingManagerMainWindow::addItem()
200 QModelIndex index = ((ListManagerView*) activityView)->
201 selectionModel()->currentIndex().parent();
202 ShoppingTreeModel *model = (ShoppingTreeModel*)
203 ((ListManagerView*) activityView)->model();
205 bool* ok = new bool(false);
206 QString name = QInputDialog::getText(this, tr("Enter item name"),
208 QLineEdit::Normal, QString(), ok);
212 if(model->addItem(name, index.row()+1, index))
213 ((ListManagerView*) activityView)->updateActions();
218 /*******************************************************************/
219 void FamilyShoppingManagerMainWindow::removeCategoryOrItem()
221 QModelIndex index = ((ListManagerView*) activityView)->
222 selectionModel()->currentIndex();
223 ShoppingTreeModel *model = (ShoppingTreeModel*)
224 ((ListManagerView*) activityView)->model();
225 if (model->removeCategoryOrItem(index))
226 ((ListManagerView*) activityView)->updateActions();