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