git-svn-id: file:///svnroot/family-shop-mgr@22 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
29 /*******************************************************************/
30 FamilyShoppingManagerMainWindow::FamilyShoppingManagerMainWindow(QWidget *parent)
31     : QMainWindow(parent), activityView(NULL), editMenu(NULL),
32     showCheckedItemsAction(NULL), goShoppingAction(NULL), endShoppingAction(NULL)
33 {
34     aboutAction = new QAction(tr("&About"), this);
35     connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
36     menuBar()->addAction(aboutAction);
37
38
39     showListManager();
40 }
41
42 /*******************************************************************/
43 FamilyShoppingManagerMainWindow::~FamilyShoppingManagerMainWindow()
44 {
45     delete activityView;
46 }
47
48 /*******************************************************************/
49 void FamilyShoppingManagerMainWindow::showListManager()
50 {
51     menuBar()->clear();
52
53     delete showCheckedItemsAction;
54     delete endShoppingAction;
55
56     delete activityView;
57     activityView = new ListManagerView("ShoppingList.xml", this);
58     setCentralWidget(activityView);
59
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);
78
79     goShoppingAction = new QAction(tr("Go shopping!"), this);
80     connect(goShoppingAction, SIGNAL(triggered()),
81             this, SLOT(showGoShopping()));
82     menuBar()->addAction(goShoppingAction);
83
84     menuBar()->addAction(aboutAction);
85     update();
86 }
87
88 /*******************************************************************/
89 void FamilyShoppingManagerMainWindow::showGoShopping()
90 {
91     menuBar()->clear();
92     delete editMenu;
93     delete goShoppingAction;
94
95     delete activityView;
96     activityView = new GoShoppingView("ShoppingList.xml", this);
97     setCentralWidget(activityView);
98
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);
104
105     endShoppingAction = new QAction(tr("&End shopping"), this);
106     connect(endShoppingAction, SIGNAL(triggered()),
107             this, SLOT(showListManager()));
108     menuBar()->addAction(endShoppingAction);
109
110     menuBar()->addAction(aboutAction);
111     update();
112 }
113
114 /*******************************************************************/
115 void FamilyShoppingManagerMainWindow::showAbout()
116 {
117     QString text;
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);
122 }
123
124 /*******************************************************************/
125 void FamilyShoppingManagerMainWindow::insertChild()
126 {
127      QModelIndex index = ((ListManagerView*) activityView)->
128                          selectionModel()->currentIndex();
129      QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
130
131      if (model->columnCount(index) == 0) {
132          if (!model->insertColumn(0, index))
133              return;
134      }
135
136      if (!model->insertRow(0, index))
137          return;
138
139      for (int column = 0; column < model->columnCount(index); ++column)
140      {
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);
146      }
147
148      ((ListManagerView*) activityView)->selectionModel()->
149              setCurrentIndex(model->index(0, 0, index),
150                              QItemSelectionModel::ClearAndSelect);
151      ((ListManagerView*) activityView)->updateActions();
152  }
153
154 /*******************************************************************/
155 /*
156 bool FamilyShoppingManagerMainWindow::insertColumn(const QModelIndex &parent)
157 {
158     QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
159     int column = ((ListManagerView*) activityView)->selectionModel()->
160             currentIndex().column();
161
162     // Insert a column in the parent item.
163     bool changed = model->insertColumn(column + 1, parent);
164     if (changed)
165         model->setHeaderData(column + 1, Qt::Horizontal,
166                              QVariant("[No header]"), Qt::EditRole);
167
168     ((ListManagerView*) activityView)->updateActions();
169
170     return changed;
171 }
172 */
173
174 /*******************************************************************/
175 void FamilyShoppingManagerMainWindow::insertRow()
176 {
177      QModelIndex index = ((ListManagerView*) activityView)->
178                          selectionModel()->currentIndex();
179      QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
180
181      if (!model->insertRow(index.row()+1, index.parent()))
182          return;
183
184      ((ListManagerView*) activityView)->updateActions();
185
186      for(int column = 0; column < model->columnCount(index.parent()); ++column)
187      {
188          QModelIndex child = model->index(index.row()+1, column, index.parent());
189          model->setData(child, QVariant("New Item"), Qt::EditRole);
190      }
191 }
192
193 /*******************************************************************/
194 /*
195 bool FamilyShoppingManagerMainWindow::removeColumn(const QModelIndex &parent)
196 {
197      QAbstractItemModel *model = ((ListManagerView*) activityView)->model();
198      int column = ((ListManagerView*) activityView)->selectionModel()->
199                   currentIndex().column();
200
201      // Insert columns in each child of the parent item.
202      bool changed = model->removeColumn(column, parent);
203
204      if (!parent.isValid() && changed)
205          ((ListManagerView*) activityView)->updateActions();
206
207      return changed;
208 }
209  */
210
211 /*******************************************************************/
212 void FamilyShoppingManagerMainWindow::removeRow()
213 {
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();
219 }