git-svn-id: file:///svnroot/family-shop-mgr@35 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 #include <QInputDialog>
26
27 #include "ListManagerView.h"
28 #include "GoShoppingView.h"
29 #include "ShoppingTreeModel.h"
30
31 /*******************************************************************/
32 FamilyShoppingManagerMainWindow::FamilyShoppingManagerMainWindow(QWidget *parent)
33     : QMainWindow(parent), activityView(NULL), //editMenu(NULL),
34     showCheckedItemsAction(NULL), goShoppingAction(NULL), endShoppingAction(NULL)
35 {
36     aboutAction = new QAction(tr("&About"), this);
37     aboutAction->setSoftKeyRole(QAction::PositiveSoftKey);
38     connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
39     menuBar()->addAction(aboutAction);
40
41
42     showListManager();
43 }
44
45 /*******************************************************************/
46 FamilyShoppingManagerMainWindow::~FamilyShoppingManagerMainWindow()
47 {
48     delete activityView;
49 }
50
51 /*******************************************************************/
52 void FamilyShoppingManagerMainWindow::showListManager()
53 {
54     menuBar()->clear();
55
56     delete showCheckedItemsAction;
57     delete endShoppingAction;
58
59     delete activityView;
60     activityView = new ListManagerView("ShoppingList.xml", this);
61
62     addCategoryAction = new QAction(tr("Add category"), this);
63     addCategoryAction->setSoftKeyRole(QAction::PositiveSoftKey);
64     connect(addCategoryAction, SIGNAL(triggered()),
65             this, SLOT(addCategory()));
66     menuBar()->addAction(addCategoryAction);
67     removeCategoryAction = new QAction(tr("Remove category"), this);
68     removeCategoryAction->setSoftKeyRole(QAction::NegativeSoftKey);
69     connect(removeCategoryAction, SIGNAL(triggered()),
70             this, SLOT(removeCategoryOrItem()));
71     menuBar()->addAction(removeCategoryAction);
72     addItemAction = new QAction(tr("Add item"), this);
73     addItemAction->setSoftKeyRole(QAction::PositiveSoftKey);
74     connect(addItemAction, SIGNAL(triggered()),
75             this, SLOT(addItem()));
76     menuBar()->addAction(addItemAction);
77     removeItemAction = new QAction(tr("Remove item"), this);
78     removeItemAction->setSoftKeyRole(QAction::NegativeSoftKey);
79     connect(removeItemAction, SIGNAL(triggered()),
80             this, SLOT(removeCategoryOrItem()));
81     menuBar()->addAction(removeItemAction);
82
83     goShoppingAction = new QAction(tr("Go shopping!"), this);
84     goShoppingAction->setSoftKeyRole(QAction::PositiveSoftKey);
85     connect(goShoppingAction, SIGNAL(triggered()),
86             this, SLOT(showGoShopping()));
87     menuBar()->addAction(goShoppingAction);
88
89     menuBar()->addAction(aboutAction);
90
91     setCentralWidget(activityView);
92     update();
93 }
94
95 /*******************************************************************/
96 void FamilyShoppingManagerMainWindow::showGoShopping()
97 {
98     menuBar()->clear();
99
100     delete addCategoryAction;
101     delete addItemAction;
102     delete removeCategoryAction;
103     delete removeItemAction;
104     delete goShoppingAction;
105
106     delete activityView;
107     activityView = new GoShoppingView("ShoppingList.xml", this);
108     setCentralWidget(activityView);
109
110     showCheckedItemsAction = new QAction(tr("&Show checked"), this);
111     showCheckedItemsAction->setCheckable(true);
112     showCheckedItemsAction->setSoftKeyRole(QAction::PositiveSoftKey);
113     connect(showCheckedItemsAction, SIGNAL(toggled(bool)),
114             activityView, SLOT(showChecked(bool)));
115     menuBar()->addAction(showCheckedItemsAction);
116
117     endShoppingAction = new QAction(tr("&End shopping"), this);
118     endShoppingAction->setSoftKeyRole(QAction::NegativeSoftKey);
119     connect(endShoppingAction, SIGNAL(triggered()),
120             this, SLOT(showListManager()));
121     menuBar()->addAction(endShoppingAction);
122
123     menuBar()->addAction(aboutAction);
124     update();
125 }
126
127 /*******************************************************************/
128 void FamilyShoppingManagerMainWindow::showAbout()
129 {
130     QString text;
131     text = tr("Application name:") + " Family shopping manager\n\n";
132     text += tr("Project maintainer:") + "\n";
133     text += "\tUnai IRIGOYEN";
134     text += "\n\n";
135     text += tr("Developers:") + "\n";
136     text += "\tUnai IRIGOYEN";
137     text += "\n\n";
138     text += tr("Licence:") + " GPLv3";
139     QMessageBox::about(this,tr("About"), text);
140 }
141
142 /*******************************************************************/
143 void FamilyShoppingManagerMainWindow::addCategory()
144 {
145     ShoppingTreeModel *model = (ShoppingTreeModel*)
146                                ((ListManagerView*) activityView)->model();
147
148     bool* ok = new bool(false);
149     QString name = QInputDialog::getText(this, tr("Enter category name"),
150                                          tr("Category name:"),
151                                          QLineEdit::Normal, QString(), ok);
152     if(!ok)
153         return;
154
155     if(model->addCategory(name))
156         ((ListManagerView*) activityView)->updateActions();
157     delete ok;
158 }
159
160 /*******************************************************************/
161 void FamilyShoppingManagerMainWindow::addSubCategory()
162 {
163     QModelIndex index = ((ListManagerView*) activityView)->
164                         selectionModel()->currentIndex().parent();
165     ShoppingTreeModel *model = (ShoppingTreeModel*)
166                                ((ListManagerView*) activityView)->model();
167
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);
172     if(!ok)
173         return;
174
175     if(model->addSubCategory(name, index.row()+1, index))
176         ((ListManagerView*) activityView)->updateActions();
177
178     delete ok;
179 }
180
181 /*******************************************************************/
182 void FamilyShoppingManagerMainWindow::addItem()
183 {
184     QModelIndex index = ((ListManagerView*) activityView)->
185                         selectionModel()->currentIndex().parent();
186     ShoppingTreeModel *model = (ShoppingTreeModel*)
187                                ((ListManagerView*) activityView)->model();
188
189     bool* ok = new bool(false);
190     QString name = QInputDialog::getText(this, tr("Enter item name"),
191                                          tr("Item name:"),
192                                          QLineEdit::Normal, QString(), ok);
193     if(!ok)
194         return;
195
196     if(model->addItem(name, index.row()+1, index))
197         ((ListManagerView*) activityView)->updateActions();
198
199     delete ok;
200 }
201
202 /*******************************************************************/
203 void FamilyShoppingManagerMainWindow::removeCategoryOrItem()
204 {
205      QModelIndex index = ((ListManagerView*) activityView)->
206                          selectionModel()->currentIndex();
207      ShoppingTreeModel *model = (ShoppingTreeModel*)
208                                 ((ListManagerView*) activityView)->model();
209      if (model->removeCategoryOrItem(index))
210          ((ListManagerView*) activityView)->updateActions();
211 }