5456037bd588fa5264e84fca36e5d08dc866e736
[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, SLOT(userLocationReady(User*)));
54     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
55             m_mapViewScreen, SLOT(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     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
65             m_mapViewScreen, SIGNAL(SIG_friendsLocationsReady(QList<User*>&)));
66
67     this->toggleProgressIndicator(true);
68 }
69
70 MainWindow::~MainWindow()
71 {
72     qDebug() << __PRETTY_FUNCTION__;
73     delete m_toListViewAct;
74     delete m_toMapViewAct;
75     delete m_toSettingsAct;
76     delete m_situareViews;
77 }
78
79 void MainWindow::toggleProgressIndicator(bool value)
80 {
81     qDebug() << __PRETTY_FUNCTION__;
82 #ifdef Q_WS_MAEMO_5
83     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
84 #else
85     Q_UNUSED(value);
86 #endif // Q_WS_MAEMO_5
87 }
88
89 void MainWindow::createMenus()
90 {
91     qDebug() << __PRETTY_FUNCTION__;
92     m_toListViewAct = new QAction(tr("List"), this);
93     m_toListViewAct->setObjectName(tr("List"));
94     connect(m_toListViewAct, SIGNAL(triggered()), this, SLOT(toListView()));
95     m_toMapViewAct = new QAction(tr("Map"), this);
96     m_toMapViewAct->setObjectName(tr("Map"));
97     connect(m_toMapViewAct, SIGNAL(triggered()), this, SLOT(toMapView()));
98     m_toSettingsAct = new QAction(tr("Settings"), this);
99     m_toSettingsAct->setObjectName(tr("Settings"));
100     connect(m_toSettingsAct, SIGNAL(triggered()), this, SLOT(openSettingsDialog()));
101     m_viewMenu = menuBar()->addMenu(tr("View"));
102     m_viewMenu->addAction(m_toListViewAct);
103     m_viewMenu->addAction(m_toMapViewAct);
104     m_viewMenu->addAction(m_toSettingsAct);
105     m_viewMenu->setObjectName(tr("View Menu"));
106 }
107
108 void MainWindow::createViews()
109 {
110     qDebug() << __PRETTY_FUNCTION__;
111     m_listViewScreen = new ListViewScreen(this);
112     m_mapViewScreen = new MapViewScreen(this);
113
114     m_situareViews = new QStackedWidget;
115     m_situareViews->addWidget(m_listViewScreen);
116     m_situareViews->addWidget(m_mapViewScreen);
117 }
118
119 void MainWindow::toListView()
120 {
121     qDebug() << __PRETTY_FUNCTION__;
122     switchView(0);
123 }
124
125 void MainWindow::toMapView()
126 {
127     qDebug() << __PRETTY_FUNCTION__;
128     switchView(1);
129 }
130
131 void MainWindow::switchView(int nextIndex)
132 {
133     qDebug() << __PRETTY_FUNCTION__ << ":" << nextIndex;
134     if (nextIndex < 0 || nextIndex > 1) {
135         qDebug() << "Illegal parameter value in MainWindow::switchView";
136         return;
137     }
138     m_situareViews->setCurrentIndex(nextIndex);
139     switch (m_situareViews->currentIndex()) {
140         case 0:
141             setWindowTitle(tr("List view"));
142             break;
143         case 1:
144             setWindowTitle(tr("Map view"));
145             break;
146         default:
147             qDebug() << "Illegal switch value in MainWindow::switchView";
148             break;
149     }
150 }
151
152 void MainWindow::openLocationUpdateDialog()
153 {
154     qDebug() << __PRETTY_FUNCTION__;
155
156     emit requestReverseGeo();
157     m_locationDialog->exec();
158 }
159
160 void MainWindow::openSettingsDialog()
161 {
162     qDebug() << __PRETTY_FUNCTION__;
163     SettingsDialog *dialog = new SettingsDialog(this);
164     dialog->show();
165 }