git-svn-id: file:///svnroot/family-shop-mgr@23 26eb2498-383b-47a6-be48-5d6f36779e85
[family-shop-mgr] / code / family-shop-mgr / FamilyShoppingManagerMainWindow.cpp
1 /*
2  * This file is part of family-shop-mgr.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Author: Unai IRIGOYEN
18  * Date: 12/07/2009
19  *
20  */
21
22 #include "FamilyShoppingManagerMainWindow.h"
23 #include <QMenuBar>
24 #include <QMessageBox>
25
26 #include "ListManagerView.h"
27 #include "GoShoppingView.h"
28 #include "ShoppingTreeModel.h"
29
30 /*******************************************************************/
31 FamilyShoppingManagerMainWindow::FamilyShoppingManagerMainWindow(QWidget *parent)
32     : QMainWindow(parent), activityView(NULL), editMenu(NULL),
33     showCheckedItemsAction(NULL), goShoppingAction(NULL), endShoppingAction(NULL)
34 {
35     aboutAction = new QAction(tr("&About"), this);
36     connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
37     menuBar()->addAction(aboutAction);
38
39
40     showListManager();
41 }
42
43 /*******************************************************************/
44 FamilyShoppingManagerMainWindow::~FamilyShoppingManagerMainWindow()
45 {
46     delete activityView;
47 }
48
49 /*******************************************************************/
50 void FamilyShoppingManagerMainWindow::showListManager()
51 {
52     menuBar()->clear();
53
54     delete showCheckedItemsAction;
55     delete endShoppingAction;
56
57     delete activityView;
58     activityView = new ListManagerView("ShoppingList.xml", this);
59     setCentralWidget(activityView);
60
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);
79
80     goShoppingAction = new QAction(tr("Go shopping!"), this);
81     connect(goShoppingAction, SIGNAL(triggered()),
82             this, SLOT(showGoShopping()));
83     menuBar()->addAction(goShoppingAction);
84
85     menuBar()->addAction(aboutAction);
86     update();
87 }
88
89 /*******************************************************************/
90 void FamilyShoppingManagerMainWindow::showGoShopping()
91 {
92     menuBar()->clear();
93     delete editMenu;
94     delete goShoppingAction;
95
96     delete activityView;
97     activityView = new GoShoppingView("ShoppingList.xml", this);
98     setCentralWidget(activityView);
99
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);
105
106     endShoppingAction = new QAction(tr("&End shopping"), this);
107     connect(endShoppingAction, SIGNAL(triggered()),
108             this, SLOT(showListManager()));
109     menuBar()->addAction(endShoppingAction);
110
111     menuBar()->addAction(aboutAction);
112     update();
113 }
114
115 /*******************************************************************/
116 void FamilyShoppingManagerMainWindow::showAbout()
117 {
118     QString text;
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);
123 }
124
125 /*******************************************************************/
126 /*
127 void FamilyShoppingManagerMainWindow::insertChild()
128 {
129      QModelIndex index = ((ListManagerView*) activityView)->
130                          selectionModel()->currentIndex();
131      QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
132
133      if (model->columnCount(index) == 0) {
134          if (!model->insertColumn(0, index))
135              return;
136      }
137
138      if (!model->insertRow(0, index))
139          return;
140
141      for (int column = 0; column < model->columnCount(index); ++column)
142      {
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);
148      }
149
150      ((ListManagerView*) activityView)->selectionModel()->
151              setCurrentIndex(model->index(0, 0, index),
152                              QItemSelectionModel::ClearAndSelect);
153      ((ListManagerView*) activityView)->updateActions();
154  }
155  */
156
157 /*******************************************************************/
158 void FamilyShoppingManagerMainWindow::addCategory()
159 {
160     ShoppingTreeModel *model = (ShoppingTreeModel*)
161                                ((ListManagerView*) activityView)->model();
162
163     if(model->addCategory("New category"))
164         ((ListManagerView*) activityView)->updateActions();
165 }
166
167 /*******************************************************************/
168 void FamilyShoppingManagerMainWindow::addSubCategory()
169 {
170     QModelIndex index = ((ListManagerView*) activityView)->
171                         selectionModel()->currentIndex().parent();
172     ShoppingTreeModel *model = (ShoppingTreeModel*)
173                                ((ListManagerView*) activityView)->model();
174
175     if(model->addSubCategory("New sub-category", index.row()+1, index))
176         ((ListManagerView*) activityView)->updateActions();
177 }
178
179 /*******************************************************************/
180 void FamilyShoppingManagerMainWindow::addItem()
181 {
182     QModelIndex index = ((ListManagerView*) activityView)->
183                         selectionModel()->currentIndex().parent();
184     ShoppingTreeModel *model = (ShoppingTreeModel*)
185                                ((ListManagerView*) activityView)->model();
186
187     if(model->addItem("New item", index.row()+1, index))
188         ((ListManagerView*) activityView)->updateActions();
189 }
190
191 /*******************************************************************/
192 void FamilyShoppingManagerMainWindow::removeCategoryOrItem()
193 {
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();
200 }