git-svn-id: file:///svnroot/family-shop-mgr@30 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     connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
38     menuBar()->addAction(aboutAction);
39
40
41     showListManager();
42 }
43
44 /*******************************************************************/
45 FamilyShoppingManagerMainWindow::~FamilyShoppingManagerMainWindow()
46 {
47     delete activityView;
48 }
49
50 /*******************************************************************/
51 void FamilyShoppingManagerMainWindow::showListManager()
52 {
53     menuBar()->clear();
54
55     delete showCheckedItemsAction;
56     delete endShoppingAction;
57
58     delete activityView;
59     activityView = new ListManagerView("ShoppingList.xml", this);
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(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);
82
83     goShoppingAction = new QAction(tr("Go shopping!"), this);
84     connect(goShoppingAction, SIGNAL(triggered()),
85             this, SLOT(showGoShopping()));
86     menuBar()->addAction(goShoppingAction);
87
88     menuBar()->addAction(aboutAction);
89
90     setCentralWidget(activityView);
91     update();
92 }
93
94 /*******************************************************************/
95 void FamilyShoppingManagerMainWindow::showGoShopping()
96 {
97     menuBar()->clear();
98 //    delete editMenu;
99     delete goShoppingAction;
100
101     delete activityView;
102     activityView = new GoShoppingView("ShoppingList.xml", this);
103     setCentralWidget(activityView);
104
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);
110
111     endShoppingAction = new QAction(tr("&End shopping"), this);
112     connect(endShoppingAction, SIGNAL(triggered()),
113             this, SLOT(showListManager()));
114     menuBar()->addAction(endShoppingAction);
115
116     menuBar()->addAction(aboutAction);
117     update();
118 }
119
120 /*******************************************************************/
121 void FamilyShoppingManagerMainWindow::showAbout()
122 {
123     QString text;
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);
128 }
129
130 /*******************************************************************/
131 /*
132 void FamilyShoppingManagerMainWindow::insertChild()
133 {
134      QModelIndex index = ((ListManagerView*) activityView)->
135                          selectionModel()->currentIndex();
136      QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
137
138      if (model->columnCount(index) == 0) {
139          if (!model->insertColumn(0, index))
140              return;
141      }
142
143      if (!model->insertRow(0, index))
144          return;
145
146      for (int column = 0; column < model->columnCount(index); ++column)
147      {
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);
153      }
154
155      ((ListManagerView*) activityView)->selectionModel()->
156              setCurrentIndex(model->index(0, 0, index),
157                              QItemSelectionModel::ClearAndSelect);
158      ((ListManagerView*) activityView)->updateActions();
159  }
160  */
161
162 /*******************************************************************/
163 void FamilyShoppingManagerMainWindow::addCategory()
164 {
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->addCategory(name))
176         ((ListManagerView*) activityView)->updateActions();
177     delete ok;
178 }
179
180 /*******************************************************************/
181 void FamilyShoppingManagerMainWindow::addSubCategory()
182 {
183     QModelIndex index = ((ListManagerView*) activityView)->
184                         selectionModel()->currentIndex().parent();
185     ShoppingTreeModel *model = (ShoppingTreeModel*)
186                                ((ListManagerView*) activityView)->model();
187
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);
192     if(!ok)
193         return;
194
195     if(model->addSubCategory(name, index.row()+1, index))
196         ((ListManagerView*) activityView)->updateActions();
197
198     delete ok;
199 }
200
201 /*******************************************************************/
202 void FamilyShoppingManagerMainWindow::addItem()
203 {
204     QModelIndex index = ((ListManagerView*) activityView)->
205                         selectionModel()->currentIndex().parent();
206     ShoppingTreeModel *model = (ShoppingTreeModel*)
207                                ((ListManagerView*) activityView)->model();
208
209     bool* ok = new bool(false);
210     QString name = QInputDialog::getText(this, tr("Enter item name"),
211                                          tr("Item name:"),
212                                          QLineEdit::Normal, QString(), ok);
213     if(!ok)
214         return;
215
216     if(model->addItem(name, index.row()+1, index))
217         ((ListManagerView*) activityView)->updateActions();
218
219     delete ok;
220 }
221
222 /*******************************************************************/
223 void FamilyShoppingManagerMainWindow::removeCategoryOrItem()
224 {
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();
231 }