git-svn-id: file:///svnroot/family-shop-mgr@24 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     setCentralWidget(activityView);
61
62     editMenu = new QMenu(tr("&Edit"), this);
63     addCategoryAction = new QAction(tr("Add category"), this);
64     connect(addCategoryAction, SIGNAL(triggered()),
65             this, SLOT(addCategory()));
66     editMenu->addAction(addCategoryAction);
67 //    removeCategoryAction = new QAction(tr("Remove category"), this);
68 //    connect(removeCategoryAction, SIGNAL(triggered()),
69 //            this, SLOT(removeRow()));
70 //    editMenu->addAction(removeCategoryAction);
71 //    addItemAction = new QAction(tr("Add item"), this);
72 //    connect(addItemAction, SIGNAL(triggered()),
73 //            this, SLOT(insertRow()));
74 //    editMenu->addAction(addItemAction);
75 //    removeItemAction = new QAction(tr("Remove item"), this);
76 //    connect(removeItemAction, SIGNAL(triggered()),
77 //            this, SLOT(removeRow()));
78 //    editMenu->addAction(removeItemAction);
79     menuBar()->addMenu(editMenu);
80
81     goShoppingAction = new QAction(tr("Go shopping!"), this);
82     connect(goShoppingAction, SIGNAL(triggered()),
83             this, SLOT(showGoShopping()));
84     menuBar()->addAction(goShoppingAction);
85
86     menuBar()->addAction(aboutAction);
87     update();
88 }
89
90 /*******************************************************************/
91 void FamilyShoppingManagerMainWindow::showGoShopping()
92 {
93     menuBar()->clear();
94     delete editMenu;
95     delete goShoppingAction;
96
97     delete activityView;
98     activityView = new GoShoppingView("ShoppingList.xml", this);
99     setCentralWidget(activityView);
100
101     showCheckedItemsAction = new QAction(tr("&Show checked"), this);
102     showCheckedItemsAction->setCheckable(true);
103     connect(showCheckedItemsAction, SIGNAL(toggled(bool)),
104             activityView, SLOT(showChecked(bool)));
105     menuBar()->addAction(showCheckedItemsAction);
106
107     endShoppingAction = new QAction(tr("&End shopping"), this);
108     connect(endShoppingAction, SIGNAL(triggered()),
109             this, SLOT(showListManager()));
110     menuBar()->addAction(endShoppingAction);
111
112     menuBar()->addAction(aboutAction);
113     update();
114 }
115
116 /*******************************************************************/
117 void FamilyShoppingManagerMainWindow::showAbout()
118 {
119     QString text;
120     text = "Application name: Family shopping manager\n";
121     text += "Author: Unai IRIGOYEN\n\n";
122     text += "Licence: GPL";
123     QMessageBox::about(this,tr("About"), text);
124 }
125
126 /*******************************************************************/
127 /*
128 void FamilyShoppingManagerMainWindow::insertChild()
129 {
130      QModelIndex index = ((ListManagerView*) activityView)->
131                          selectionModel()->currentIndex();
132      QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
133
134      if (model->columnCount(index) == 0) {
135          if (!model->insertColumn(0, index))
136              return;
137      }
138
139      if (!model->insertRow(0, index))
140          return;
141
142      for (int column = 0; column < model->columnCount(index); ++column)
143      {
144          QModelIndex child = model->index(0, column, index);
145          model->setData(child, QVariant("[No data]"), Qt::EditRole);
146          if (!model->headerData(column, Qt::Horizontal).isValid())
147              model->setHeaderData(column, Qt::Horizontal,
148                                   QVariant("[No header]"), Qt::EditRole);
149      }
150
151      ((ListManagerView*) activityView)->selectionModel()->
152              setCurrentIndex(model->index(0, 0, index),
153                              QItemSelectionModel::ClearAndSelect);
154      ((ListManagerView*) activityView)->updateActions();
155  }
156  */
157
158 /*******************************************************************/
159 void FamilyShoppingManagerMainWindow::addCategory()
160 {
161     ShoppingTreeModel *model = (ShoppingTreeModel*)
162                                ((ListManagerView*) activityView)->model();
163
164     bool* ok = new bool(false);
165     QString name = QInputDialog::getText(this, tr("Enter category name"),
166                                          tr("Category name:"),
167                                          QLineEdit::Normal, QString(), ok);
168     if(!ok)
169         return;
170
171     if(model->addCategory(name))
172         ((ListManagerView*) activityView)->updateActions();
173     delete ok;
174 }
175
176 /*******************************************************************/
177 void FamilyShoppingManagerMainWindow::addSubCategory()
178 {
179     QModelIndex index = ((ListManagerView*) activityView)->
180                         selectionModel()->currentIndex().parent();
181     ShoppingTreeModel *model = (ShoppingTreeModel*)
182                                ((ListManagerView*) activityView)->model();
183
184     bool* ok = new bool(false);
185     QString name = QInputDialog::getText(this, tr("Enter category name"),
186                                          tr("Category name:"),
187                                          QLineEdit::Normal, QString(), ok);
188     if(!ok)
189         return;
190
191     if(model->addSubCategory(name, index.row()+1, index))
192         ((ListManagerView*) activityView)->updateActions();
193
194     delete ok;
195 }
196
197 /*******************************************************************/
198 void FamilyShoppingManagerMainWindow::addItem()
199 {
200     QModelIndex index = ((ListManagerView*) activityView)->
201                         selectionModel()->currentIndex().parent();
202     ShoppingTreeModel *model = (ShoppingTreeModel*)
203                                ((ListManagerView*) activityView)->model();
204
205     bool* ok = new bool(false);
206     QString name = QInputDialog::getText(this, tr("Enter item name"),
207                                          tr("Item name:"),
208                                          QLineEdit::Normal, QString(), ok);
209     if(!ok)
210         return;
211
212     if(model->addItem(name, index.row()+1, index))
213         ((ListManagerView*) activityView)->updateActions();
214
215     delete ok;
216 }
217
218 /*******************************************************************/
219 void FamilyShoppingManagerMainWindow::removeCategoryOrItem()
220 {
221      QModelIndex index = ((ListManagerView*) activityView)->
222                          selectionModel()->currentIndex();
223      ShoppingTreeModel *model = (ShoppingTreeModel*)
224                                 ((ListManagerView*) activityView)->model();
225      if (model->removeCategoryOrItem(index))
226          ((ListManagerView*) activityView)->updateActions();
227 }