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"
29 /*******************************************************************/
30 FamilyShoppingManagerMainWindow::FamilyShoppingManagerMainWindow(QWidget *parent)
31 : QMainWindow(parent), activityView(NULL), editMenu(NULL),
32 showCheckedItemsAction(NULL), goShoppingAction(NULL), endShoppingAction(NULL)
34 aboutAction = new QAction(tr("&About"), this);
35 connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
36 menuBar()->addAction(aboutAction);
42 /*******************************************************************/
43 FamilyShoppingManagerMainWindow::~FamilyShoppingManagerMainWindow()
48 /*******************************************************************/
49 void FamilyShoppingManagerMainWindow::showListManager()
53 delete showCheckedItemsAction;
54 delete endShoppingAction;
57 activityView = new ListManagerView("ShoppingList.xml", this);
58 setCentralWidget(activityView);
60 editMenu = new QMenu(tr("&Edit"), this);
61 addCategoryAction = new QAction(tr("Add category"), this);
62 connect(addCategoryAction, SIGNAL(triggered()),
63 this, SLOT(insertRow()));
64 editMenu->addAction(addCategoryAction);
65 removeCategoryAction = new QAction(tr("Remove category"), this);
66 connect(removeCategoryAction, SIGNAL(triggered()),
67 this, SLOT(removeRow()));
68 editMenu->addAction(removeCategoryAction);
69 addItemAction = new QAction(tr("Add item"), this);
70 connect(addItemAction, SIGNAL(triggered()),
71 this, SLOT(insertRow()));
72 editMenu->addAction(addItemAction);
73 removeItemAction = new QAction(tr("Remove item"), this);
74 connect(removeItemAction, SIGNAL(triggered()),
75 this, SLOT(removeRow()));
76 editMenu->addAction(removeItemAction);
77 menuBar()->addMenu(editMenu);
79 goShoppingAction = new QAction(tr("Go shopping!"), this);
80 connect(goShoppingAction, SIGNAL(triggered()),
81 this, SLOT(showGoShopping()));
82 menuBar()->addAction(goShoppingAction);
84 menuBar()->addAction(aboutAction);
88 /*******************************************************************/
89 void FamilyShoppingManagerMainWindow::showGoShopping()
93 delete goShoppingAction;
96 activityView = new GoShoppingView("ShoppingList.xml", this);
97 setCentralWidget(activityView);
99 showCheckedItemsAction = new QAction(tr("&Show checked"), this);
100 showCheckedItemsAction->setCheckable(true);
101 connect(showCheckedItemsAction, SIGNAL(toggled(bool)),
102 activityView, SLOT(showChecked(bool)));
103 menuBar()->addAction(showCheckedItemsAction);
105 endShoppingAction = new QAction(tr("&End shopping"), this);
106 connect(endShoppingAction, SIGNAL(triggered()),
107 this, SLOT(showListManager()));
108 menuBar()->addAction(endShoppingAction);
110 menuBar()->addAction(aboutAction);
114 /*******************************************************************/
115 void FamilyShoppingManagerMainWindow::showAbout()
118 text = "Application name: Family shopping manager\n";
119 text += "Author: Unai IRIGOYEN\n\n";
120 text += "Licence: GPL";
121 QMessageBox::about(this,tr("About"), text);
124 /*******************************************************************/
125 void FamilyShoppingManagerMainWindow::insertChild()
127 QModelIndex index = ((ListManagerView*) activityView)->
128 selectionModel()->currentIndex();
129 QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
131 if (model->columnCount(index) == 0) {
132 if (!model->insertColumn(0, index))
136 if (!model->insertRow(0, index))
139 for (int column = 0; column < model->columnCount(index); ++column)
141 QModelIndex child = model->index(0, column, index);
142 model->setData(child, QVariant("[No data]"), Qt::EditRole);
143 if (!model->headerData(column, Qt::Horizontal).isValid())
144 model->setHeaderData(column, Qt::Horizontal,
145 QVariant("[No header]"), Qt::EditRole);
148 ((ListManagerView*) activityView)->selectionModel()->
149 setCurrentIndex(model->index(0, 0, index),
150 QItemSelectionModel::ClearAndSelect);
151 ((ListManagerView*) activityView)->updateActions();
154 /*******************************************************************/
156 bool FamilyShoppingManagerMainWindow::insertColumn(const QModelIndex &parent)
158 QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
159 int column = ((ListManagerView*) activityView)->selectionModel()->
160 currentIndex().column();
162 // Insert a column in the parent item.
163 bool changed = model->insertColumn(column + 1, parent);
165 model->setHeaderData(column + 1, Qt::Horizontal,
166 QVariant("[No header]"), Qt::EditRole);
168 ((ListManagerView*) activityView)->updateActions();
174 /*******************************************************************/
175 void FamilyShoppingManagerMainWindow::insertRow()
177 QModelIndex index = ((ListManagerView*) activityView)->
178 selectionModel()->currentIndex();
179 QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
181 if (!model->insertRow(index.row()+1, index.parent()))
184 ((ListManagerView*) activityView)->updateActions();
186 for(int column = 0; column < model->columnCount(index.parent()); ++column)
188 QModelIndex child = model->index(index.row()+1, column, index.parent());
189 model->setData(child, QVariant("New Item"), Qt::EditRole);
193 /*******************************************************************/
195 bool FamilyShoppingManagerMainWindow::removeColumn(const QModelIndex &parent)
197 QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
198 int column = ((ListManagerView*) activityView)->selectionModel()->
199 currentIndex().column();
201 // Insert columns in each child of the parent item.
202 bool changed = model->removeColumn(column, parent);
204 if (!parent.isValid() && changed)
205 ((ListManagerView*) activityView)->updateActions();
211 /*******************************************************************/
212 void FamilyShoppingManagerMainWindow::removeRow()
214 QModelIndex index = ((ListManagerView*) activityView)->
215 selectionModel()->currentIndex();
216 QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
217 if (model->removeRow(index.row(), index.parent()))
218 ((ListManagerView*) activityView)->updateActions();