Fixed searchclients to handle new Google URLs correctly; added GUI
[movie-schedule] / src / ui / mainwindow.cpp
1 // Copyright 2010 Jochen Becher
2 //
3 // This file is part of MovieSchedule.
4 //
5 // MovieSchedule is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // MovieSchedule is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with MovieSchedule.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "mainwindow.h"
19 #include "ui_mainwindow.h"
20
21 #include "theatermodel.h"
22 #include "theaterdelegate.h"
23 #include "moviemodel.h"
24 #include "moviedelegate.h"
25 #include "locationdialog.h"
26 #include "data/cinemaschedule.h"
27 #include "data/cinema.h"
28 #include "data/movie.h"
29 #include "data/cinemakey.h"
30 #include "data/moviekey.h"
31 #include "data/cinemakey.h"
32 #include "utils/assertedlocker.h"
33
34 #include <QCursor>
35 #include <QtGui>
36 #ifdef Q_WS_MAEMO_5
37 #include <QAbstractKineticScroller>
38 #endif
39 #include <iostream>
40
41 MainWindow::MainWindow(const CinemaSchedule *cinema_schedule, QWidget *parent) :
42         AbstractMainWindow(parent),
43         ui(new Ui::MainWindow),
44         _cinema_schedule(cinema_schedule),
45         _theater_context_menu(0),
46         _movie_context_menu(0),
47         _theater_phone_call(0),
48         _theater_find_route(0),
49         _theater_search_web(0),
50         _movie_search_web(0),
51         _theater_model(0),
52         _movie_model(0)
53 {
54     ui->setupUi(this);
55 #ifdef Q_WS_MAEMO_5
56     setAttribute(Qt::WA_Maemo5StackedWindow);
57 #endif
58     QStackedLayout *layout = dynamic_cast<QStackedLayout*>(ui->MainStack->layout());
59     if (layout != 0) {
60         layout->setStackingMode(QStackedLayout::StackAll);
61     }
62     ui->Theaters->setItemDelegate(new TheaterDelegate(this));
63     ui->Theaters->setProperty("FingerScrollable", true);
64     ui->Theaters->setContextMenuPolicy(Qt::CustomContextMenu);
65     ui->Movies->setItemDelegate(new MovieDelegate(this));
66     ui->Movies->setProperty("FingerScrollable", true);
67     ui->Movies->setContextMenuPolicy(Qt::CustomContextMenu);
68     QFont f(font());
69     f.setPointSizeF(f.pointSizeF() * 1.5);
70     ui->ErrorMessage->setFont(f);
71     connect(ui->Theaters, SIGNAL(clicked(QModelIndex)), this, SLOT(TheaterClicked(QModelIndex)));
72     connect(ui->Theaters, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(TheaterContextMenuRequested(QPoint)));
73     connect(ui->Movies, SIGNAL(clicked(QModelIndex)), this, SLOT(MovieClicked(QModelIndex)));
74     connect(ui->Movies, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(MovieContextMenuRequested(QPoint)));
75     ui->menubar->addAction(tr("Movies"), this, SIGNAL(SearchMovies()));
76     ui->menubar->addAction(tr("Theaters"), this, SIGNAL(SearchTheaters()));
77     ui->menubar->addAction(tr("Location"), this, SIGNAL(OpenLocationDialog()));
78     ui->menubar->addAction(tr("Options"), this, SIGNAL(OpenOptionsDialog()));
79     ui->menubar->addAction(tr("About"), this, SIGNAL(OpenAboutDialog()));
80     _theater_context_menu = new QMenu(this);
81     _theater_phone_call = _theater_context_menu->addAction(tr("Call By Phone"), this, SLOT(CallTheaterByPhone()));
82     _theater_find_route = _theater_context_menu->addAction(tr("Find Route"), this, SLOT(FindRouteToTheater()));
83     _theater_search_web = _theater_context_menu->addAction(tr("Search In Web"), this, SLOT(SearchTheaterInWeb()));
84     _movie_context_menu = new QMenu(this);
85     _movie_search_web = _movie_context_menu->addAction(tr("Search In Web"), this, SLOT(SearchMovieInWeb()));
86 }
87
88 MainWindow::~MainWindow()
89 {
90     delete ui;
91 }
92
93 void MainWindow::SetOrientation(Settings::OrientationMode orientation)
94 {
95 #ifdef Q_WS_MAEMO_5
96     switch (orientation) {
97     case Settings::LANDSCAPE:
98         setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
99         break;
100     case Settings::PORTRAIT:
101         setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
102         break;
103     case Settings::AUTOROTATION:
104         setAttribute(Qt::WA_Maemo5AutoOrientation, true);
105         break;
106     }
107 #else
108     Q_UNUSED(orientation);
109 #endif
110 }
111
112 void MainWindow::SetLocation(Location location)
113 {
114     _location = location;
115     if (_location.IsValid()) {
116         ui->Background->SetLabelText(_location.GetLocationName());
117     } else {
118         ui->Background->SetLabelText("");
119     }
120 }
121
122 void MainWindow::SetTheaterModel(QAbstractItemModel *theater_model)
123 {
124     _theater_model = theater_model;
125     setWindowTitle(tr("Theaters"));
126     ui->Theaters->setModel(theater_model);
127     ui->MainPages->setCurrentWidget(ui->TheatersPage);
128 }
129
130 void MainWindow::SetMovieModel(QAbstractItemModel *movie_model)
131 {
132     _movie_model = movie_model;
133     setWindowTitle(tr("Movies"));
134     ui->Movies->setModel(movie_model);
135     ui->MainPages->setCurrentWidget(ui->MoviesPage);
136 }
137
138 void MainWindow::SetError(QString error_text)
139 {
140     setWindowTitle(tr("MovieSchedule"));
141     ui->ErrorMessage->setText(error_text);
142     ui->MainPages->setCurrentWidget(ui->ErrorPage);
143 }
144
145 void MainWindow::TheaterClicked(QModelIndex index)
146 {
147     emit TheaterSelected(GetCinema(index));
148 }
149
150 void MainWindow::TheaterContextMenuRequested(const QPoint &pos)
151 {
152     AssertedReadLocker locker(_cinema_schedule->GetLock());
153     const Cinema *cinema = _cinema_schedule->FindCinema(GetCinema(ui->Theaters->currentIndex()));
154     if (cinema != 0) {
155         ShowContextMenu(cinema, ui->Theaters->viewport()->mapToGlobal(pos));
156     }
157 }
158
159 void MainWindow::MovieClicked(QModelIndex index)
160 {
161     emit MovieSelected(GetMovie(index));
162 }
163
164 void MainWindow::MovieContextMenuRequested(const QPoint &pos)
165 {
166     AssertedReadLocker locker(_cinema_schedule->GetLock());
167     const Movie *movie = _cinema_schedule->FindMovie(GetMovie(ui->Movies->currentIndex()));
168     if (movie != 0) {
169         ShowContextMenu(movie, ui->Movies->viewport()->mapToGlobal(pos));
170     }
171 }
172
173 void MainWindow::CallTheaterByPhone()
174 {
175     emit CallTheaterByPhone(GetCinema(ui->Theaters->currentIndex()));
176 }
177
178 void MainWindow::FindRouteToTheater()
179 {
180     emit FindRouteToTheater(GetCinema(ui->Theaters->currentIndex()));
181 }
182
183 void MainWindow::SearchTheaterInWeb()
184 {
185     emit SearchTheaterInWeb(GetCinema(ui->Theaters->currentIndex()));
186 }
187
188 void MainWindow::SearchMovieInWeb()
189 {
190     emit SearchMovieInWeb(GetMovie(ui->Movies->currentIndex()));
191 }
192
193 void MainWindow::changeEvent(QEvent *e)
194 {
195     QMainWindow::changeEvent(e);
196     switch (e->type()) {
197     case QEvent::LanguageChange:
198         ui->retranslateUi(this);
199         break;
200     default:
201         break;
202     }
203 }
204
205 CinemaKey MainWindow::GetCinema(const QModelIndex &index)
206 {
207     QVariant var = _theater_model->data(index, TheaterModel::TheaterKeyRole);
208     CinemaKey cinema_key = qVariantValue<CinemaKey>(var);
209     return cinema_key;
210 }
211
212 MovieKey MainWindow::GetMovie(const QModelIndex &index)
213 {
214     QVariant var = _movie_model->data(index, MovieModel::MovieKeyRole);
215     MovieKey movie_key = qVariantValue<MovieKey>(var);
216     return movie_key;
217 }
218
219 void MainWindow::ShowContextMenu(const Cinema *cinema, const QPoint &pos)
220 {
221     _theater_phone_call->setVisible(!cinema->GetTelephone().isEmpty());
222     // TODO enable if find-route works
223     _theater_find_route->setVisible(false && !cinema->GetAddress().isEmpty());
224     _theater_search_web->setVisible(true);
225     _theater_context_menu->popup(pos);
226 }
227
228 void MainWindow::ShowContextMenu(const Movie *movie, const QPoint &pos)
229 {
230     Q_UNUSED(movie);
231     _movie_search_web->setVisible(true);
232     _movie_context_menu->popup(pos);
233 }