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);
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(removeCategoryOrItem()));
69 // editMenu->addAction(removeCategoryAction);
70 addItemAction = new QAction(tr("Add item"), this);
71 connect(addItemAction, SIGNAL(triggered()),
72 this, SLOT(addItem()));
73 // editMenu->addAction(addItemAction);
74 removeItemAction = new QAction(tr("Remove item"), this);
75 connect(removeItemAction, SIGNAL(triggered()),
76 this, SLOT(removeCategoryOrItem()));
77 // editMenu->addAction(removeItemAction);
78 menuBar()->addAction(addCategoryAction);
79 activityView->addAction(removeCategoryAction);
80 activityView->addAction(addItemAction);
81 activityView->addAction(removeItemAction);
83 goShoppingAction = new QAction(tr("Go shopping!"), this);
84 connect(goShoppingAction, SIGNAL(triggered()),
85 this, SLOT(showGoShopping()));
86 menuBar()->addAction(goShoppingAction);
88 menuBar()->addAction(aboutAction);
90 setCentralWidget(activityView);
94 /*******************************************************************/
95 void FamilyShoppingManagerMainWindow::showGoShopping()
99 delete goShoppingAction;
102 activityView = new GoShoppingView("ShoppingList.xml", this);
103 setCentralWidget(activityView);
105 showCheckedItemsAction = new QAction(tr("&Show checked"), this);
106 showCheckedItemsAction->setCheckable(true);
107 connect(showCheckedItemsAction, SIGNAL(toggled(bool)),
108 activityView, SLOT(showChecked(bool)));
109 menuBar()->addAction(showCheckedItemsAction);
111 endShoppingAction = new QAction(tr("&End shopping"), this);
112 connect(endShoppingAction, SIGNAL(triggered()),
113 this, SLOT(showListManager()));
114 menuBar()->addAction(endShoppingAction);
116 menuBar()->addAction(aboutAction);
120 /*******************************************************************/
121 void FamilyShoppingManagerMainWindow::showAbout()
124 text = "Application name: Family shopping manager\n";
125 text += "Author: Unai IRIGOYEN\n\n";
126 text += "Licence: GPL";
127 QMessageBox::about(this,tr("About"), text);
130 /*******************************************************************/
132 void FamilyShoppingManagerMainWindow::insertChild()
134 QModelIndex index = ((ListManagerView*) activityView)->
135 selectionModel()->currentIndex();
136 QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
138 if (model->columnCount(index) == 0) {
139 if (!model->insertColumn(0, index))
143 if (!model->insertRow(0, index))
146 for (int column = 0; column < model->columnCount(index); ++column)
148 QModelIndex child = model->index(0, column, index);
149 model->setData(child, QVariant("[No data]"), Qt::EditRole);
150 if (!model->headerData(column, Qt::Horizontal).isValid())
151 model->setHeaderData(column, Qt::Horizontal,
152 QVariant("[No header]"), Qt::EditRole);
155 ((ListManagerView*) activityView)->selectionModel()->
156 setCurrentIndex(model->index(0, 0, index),
157 QItemSelectionModel::ClearAndSelect);
158 ((ListManagerView*) activityView)->updateActions();
162 /*******************************************************************/
163 void FamilyShoppingManagerMainWindow::addCategory()
165 ShoppingTreeModel *model = (ShoppingTreeModel*)
166 ((ListManagerView*) activityView)->model();
168 bool* ok = new bool(false);
169 QString name = QInputDialog::getText(this, tr("Enter category name"),
170 tr("Category name:"),
171 QLineEdit::Normal, QString(), ok);
175 if(model->addCategory(name))
176 ((ListManagerView*) activityView)->updateActions();
180 /*******************************************************************/
181 void FamilyShoppingManagerMainWindow::addSubCategory()
183 QModelIndex index = ((ListManagerView*) activityView)->
184 selectionModel()->currentIndex().parent();
185 ShoppingTreeModel *model = (ShoppingTreeModel*)
186 ((ListManagerView*) activityView)->model();
188 bool* ok = new bool(false);
189 QString name = QInputDialog::getText(this, tr("Enter category name"),
190 tr("Category name:"),
191 QLineEdit::Normal, QString(), ok);
195 if(model->addSubCategory(name, index.row()+1, index))
196 ((ListManagerView*) activityView)->updateActions();
201 /*******************************************************************/
202 void FamilyShoppingManagerMainWindow::addItem()
204 QModelIndex index = ((ListManagerView*) activityView)->
205 selectionModel()->currentIndex().parent();
206 ShoppingTreeModel *model = (ShoppingTreeModel*)
207 ((ListManagerView*) activityView)->model();
209 bool* ok = new bool(false);
210 QString name = QInputDialog::getText(this, tr("Enter item name"),
212 QLineEdit::Normal, QString(), ok);
216 if(model->addItem(name, index.row()+1, index))
217 ((ListManagerView*) activityView)->updateActions();
222 /*******************************************************************/
223 void FamilyShoppingManagerMainWindow::removeCategoryOrItem()
225 QModelIndex index = ((ListManagerView*) activityView)->
226 selectionModel()->currentIndex();
227 ShoppingTreeModel *model = (ShoppingTreeModel*)
228 ((ListManagerView*) activityView)->model();
229 if (model->removeCategoryOrItem(index))
230 ((ListManagerView*) activityView)->updateActions();