Created new private utility funtion to handle view changes. Added test
[situare] / src / ui / mainwindow.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Henri Lampela - henri.lampela@ixonos.com
6       Kaj Wallin - kaj.wallin@ixonos.com
7
8    Situare is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License
10    version 2 as published by the Free Software Foundation.
11
12    Situare is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Situare; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20    USA.
21 */
22
23 #include <QtGui>
24 #include "mainwindow.h"
25 #include "listviewscreen.h"
26 #include "mapviewscreen.h"
27
28 MainWindow::MainWindow(QWidget *parent)
29     : QMainWindow(parent)
30 {
31     qDebug() << __PRETTY_FUNCTION__;
32     QWidget *widget = new QWidget;
33     setCentralWidget(widget);
34     createViews();
35     QVBoxLayout *mainLayout = new QVBoxLayout;
36     infoLabel = new QLabel(tr("Click \"view\", to change between views"),this);
37     infoLabel->setAttribute(Qt::WA_TranslucentBackground, true);
38     mainLayout->addWidget(infoLabel);
39     mainLayout->addWidget(situareViews);
40     widget->setLayout(mainLayout);
41     createMenus();
42
43     setWindowTitle(tr("View"));
44 }
45
46 MainWindow::~MainWindow()
47 {
48     qDebug() << __PRETTY_FUNCTION__;
49     delete toListViewAct;
50     delete toMapViewAct;
51     delete situareViews;
52     delete infoLabel;
53 }
54
55 void MainWindow::createMenus()
56 {
57     qDebug() << __PRETTY_FUNCTION__;
58     toListViewAct = new QAction(tr("List"), this);
59     connect(toListViewAct, SIGNAL(triggered()), this, SLOT(toListView()));
60     toMapViewAct = new QAction(tr("Map"), this);
61     connect(toMapViewAct, SIGNAL(triggered()), this, SLOT(toMapView()));
62     viewMenu = menuBar()->addMenu(tr("View"));
63     viewMenu->addAction(toListViewAct);
64     viewMenu->addAction(toMapViewAct);
65 }
66
67 void MainWindow::createViews()
68 {
69     qDebug() << __PRETTY_FUNCTION__;
70     situareViews = new QStackedWidget;
71     situareViews->addWidget(new ListViewScreen);
72     situareViews->addWidget(new MapViewScreen);
73 }
74
75 void MainWindow::toListView()
76 {
77     qDebug() << __PRETTY_FUNCTION__;
78     switchView(0);
79 }
80
81 void MainWindow::toMapView()
82 {
83     qDebug() << __PRETTY_FUNCTION__;
84     switchView(1);
85 }
86
87 void MainWindow::switchView(int nextIndex)
88 {
89     qDebug() << __PRETTY_FUNCTION__ << ":" << nextIndex;
90     if (nextIndex < 0 || nextIndex > 1) {
91         qDebug() << tr("Illegal parameter value in MainWindow::switchView");
92         return;
93     }
94     situareViews->setCurrentIndex(nextIndex);
95     infoLabel->setText(tr("Current: %1").arg(situareViews->currentIndex()));
96     switch (situareViews->currentIndex()) {
97         case 0:
98             setWindowTitle(tr("List"));
99             break;
100         case 1:
101             setWindowTitle(tr("Map"));
102             break;
103         default:
104             qDebug() << tr("Illegal switch value in MainWindow::switchView");
105             break;
106     }
107 }
108
109 int MainWindow::getViewIndex()
110 {
111     return situareViews->currentIndex();
112 }