Merge branch 'stacked_friends'
[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       Jussi Laitinen jussi.laitinen@ixonos.com
8
9    Situare is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License
11    version 2 as published by the Free Software Foundation.
12
13    Situare is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Situare; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
21    USA.
22 */
23
24 #include <QtGui>
25 #include "mainwindow.h"
26 #include "listviewscreen.h"
27 #include "mapviewscreen.h"
28 #include "settingsdialog.h"
29 #include "facebookservice/facebookauthentication.h"
30 #include "situareservice/situareservice.h"
31
32 MainWindow::MainWindow(QWidget *parent)
33     : QMainWindow(parent)
34 {
35     qDebug() << __PRETTY_FUNCTION__;
36
37     createViews();
38     setCentralWidget(m_situareViews);
39     createMenus();
40     setWindowTitle(tr("List view"));
41     this->hide();
42
43     m_locationDialog = new UpdateLocationDialog(this);
44     connect(m_listViewScreen,SIGNAL(launchUpdateDialog()),
45             this,SLOT(openLocationUpdateDialog()));
46
47     connect(this, SIGNAL(reverseGeoReady(QString)),
48             m_locationDialog, SLOT(setAddress(QString)));
49     connect(m_locationDialog, SIGNAL(statusUpdate(QString,bool)), this,
50             SIGNAL(statusUpdate(QString,bool)));
51
52     connect(this, SIGNAL(userLocationReady(User*)),
53             m_mapViewScreen, SIGNAL(userLocationReady(User*)));
54     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
55             m_mapViewScreen, SIGNAL(friendsLocationsReady(QList<User*>&)));
56
57     connect(this, SIGNAL(userLocationReady(User*)),
58             m_listViewScreen, SLOT(userDataReceived(User*)));
59     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
60             m_listViewScreen, SLOT(friendInfoReceived(QList<User*>&)));
61     connect(m_listViewScreen, SIGNAL(updateFriendsData()),
62             this, SIGNAL(refreshUserData()));
63
64     this->toggleProgressIndicator(true);
65 }
66
67 MainWindow::~MainWindow()
68 {
69     qDebug() << __PRETTY_FUNCTION__;
70     delete m_toListViewAct;
71     delete m_toMapViewAct;
72     delete m_toSettingsAct;
73     delete m_situareViews;
74 }
75
76 void MainWindow::toggleProgressIndicator(bool value)
77 {
78     qDebug() << __PRETTY_FUNCTION__;
79 #ifdef Q_WS_MAEMO_5
80     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
81 #else
82     Q_UNUSED(value);
83 #endif // Q_WS_MAEMO_5
84 }
85
86 void MainWindow::createMenus()
87 {
88     qDebug() << __PRETTY_FUNCTION__;
89     m_toListViewAct = new QAction(tr("List"), this);
90     m_toListViewAct->setObjectName(tr("List"));
91     connect(m_toListViewAct, SIGNAL(triggered()), this, SLOT(toListView()));
92     m_toMapViewAct = new QAction(tr("Map"), this);
93     m_toMapViewAct->setObjectName(tr("Map"));
94     connect(m_toMapViewAct, SIGNAL(triggered()), this, SLOT(toMapView()));
95     m_toSettingsAct = new QAction(tr("Settings"), this);
96     m_toSettingsAct->setObjectName(tr("Settings"));
97     connect(m_toSettingsAct, SIGNAL(triggered()), this, SLOT(openSettingsDialog()));
98     m_viewMenu = menuBar()->addMenu(tr("View"));
99     m_viewMenu->addAction(m_toListViewAct);
100     m_viewMenu->addAction(m_toMapViewAct);
101     m_viewMenu->addAction(m_toSettingsAct);
102     m_viewMenu->setObjectName(tr("View Menu"));
103 }
104
105 void MainWindow::createViews()
106 {
107     qDebug() << __PRETTY_FUNCTION__;
108     m_listViewScreen = new ListViewScreen(this);
109     m_mapViewScreen = new MapViewScreen(this);
110
111     m_situareViews = new QStackedWidget;
112     m_situareViews->addWidget(m_listViewScreen);
113     m_situareViews->addWidget(m_mapViewScreen);
114 }
115
116 void MainWindow::toListView()
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119     switchView(0);
120 }
121
122 void MainWindow::toMapView()
123 {
124     qDebug() << __PRETTY_FUNCTION__;
125     switchView(1);
126 }
127
128 void MainWindow::switchView(int nextIndex)
129 {
130     qDebug() << __PRETTY_FUNCTION__ << ":" << nextIndex;
131     if (nextIndex < 0 || nextIndex > 1) {
132         qDebug() << "Illegal parameter value in MainWindow::switchView";
133         return;
134     }
135     m_situareViews->setCurrentIndex(nextIndex);
136     switch (m_situareViews->currentIndex()) {
137         case 0:
138             setWindowTitle(tr("List view"));
139             break;
140         case 1:
141             setWindowTitle(tr("Map view"));
142             break;
143         default:
144             qDebug() << "Illegal switch value in MainWindow::switchView";
145             break;
146     }
147 }
148
149 void MainWindow::openLocationUpdateDialog()
150 {
151     qDebug() << __PRETTY_FUNCTION__;
152
153     emit requestReverseGeo();
154     m_locationDialog->exec();
155 }
156
157 void MainWindow::openSettingsDialog()
158 {
159     qDebug() << __PRETTY_FUNCTION__;
160     SettingsDialog *dialog = new SettingsDialog(this);
161     dialog->show();
162 }