Merge https://vcs.maemo.org/git/movie-schedule
[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("About"), this, SIGNAL(OpenAboutDialog()));
79     _theater_context_menu = new QMenu(this);
80     _theater_phone_call = _theater_context_menu->addAction(tr("Call By Phone"), this, SLOT(CallTheaterByPhone()));
81     _theater_find_route = _theater_context_menu->addAction(tr("Find Route"), this, SLOT(FindRouteToTheater()));
82     _theater_search_web = _theater_context_menu->addAction(tr("Search In Web"), this, SLOT(SearchTheaterInWeb()));
83     _movie_context_menu = new QMenu(this);
84     _movie_search_web = _movie_context_menu->addAction(tr("Search In Web"), this, SLOT(SearchMovieInWeb()));
85 }
86
87 MainWindow::~MainWindow()
88 {
89     delete ui;
90 }
91
92 void MainWindow::SetLocation(Location location)
93 {
94     _location = location;
95     if (_location.IsValid()) {
96         ui->Background->SetLabelText(_location.GetLocationName());
97     } else {
98         ui->Background->SetLabelText("");
99     }
100 }
101
102 void MainWindow::SetTheaterModel(QAbstractItemModel *theater_model)
103 {
104     _theater_model = theater_model;
105     setWindowTitle(tr("Theaters"));
106     ui->Theaters->setModel(theater_model);
107     ui->MainPages->setCurrentWidget(ui->TheatersPage);
108 }
109
110 void MainWindow::SetMovieModel(QAbstractItemModel *movie_model)
111 {
112     _movie_model = movie_model;
113     setWindowTitle(tr("Movies"));
114     ui->Movies->setModel(movie_model);
115     ui->MainPages->setCurrentWidget(ui->MoviesPage);
116 }
117
118 void MainWindow::SetError(QString error_text)
119 {
120     setWindowTitle(tr("MovieSchedule"));
121     ui->ErrorMessage->setText(error_text);
122     ui->MainPages->setCurrentWidget(ui->ErrorPage);
123 }
124
125 void MainWindow::TheaterClicked(QModelIndex index)
126 {
127     emit TheaterSelected(GetCinema(index));
128 }
129
130 void MainWindow::TheaterContextMenuRequested(const QPoint &pos)
131 {
132     AssertedReadLocker locker(_cinema_schedule->GetLock());
133     const Cinema *cinema = _cinema_schedule->FindCinema(GetCinema(ui->Theaters->currentIndex()));
134     if (cinema != 0) {
135         ShowContextMenu(cinema, ui->Theaters->viewport()->mapToGlobal(pos));
136     }
137 }
138
139 void MainWindow::MovieClicked(QModelIndex index)
140 {
141     emit MovieSelected(GetMovie(index));
142 }
143
144 void MainWindow::MovieContextMenuRequested(const QPoint &pos)
145 {
146     AssertedReadLocker locker(_cinema_schedule->GetLock());
147     const Movie *movie = _cinema_schedule->FindMovie(GetMovie(ui->Movies->currentIndex()));
148     if (movie != 0) {
149         ShowContextMenu(movie, ui->Movies->viewport()->mapToGlobal(pos));
150     }
151 }
152
153 void MainWindow::CallTheaterByPhone()
154 {
155     emit CallTheaterByPhone(GetCinema(ui->Theaters->currentIndex()));
156 }
157
158 void MainWindow::FindRouteToTheater()
159 {
160     emit FindRouteToTheater(GetCinema(ui->Theaters->currentIndex()));
161 }
162
163 void MainWindow::SearchTheaterInWeb()
164 {
165     emit SearchTheaterInWeb(GetCinema(ui->Theaters->currentIndex()));
166 }
167
168 void MainWindow::SearchMovieInWeb()
169 {
170     emit SearchMovieInWeb(GetMovie(ui->Movies->currentIndex()));
171 }
172
173 void MainWindow::changeEvent(QEvent *e)
174 {
175     QMainWindow::changeEvent(e);
176     switch (e->type()) {
177     case QEvent::LanguageChange:
178         ui->retranslateUi(this);
179         break;
180     default:
181         break;
182     }
183 }
184
185 CinemaKey MainWindow::GetCinema(const QModelIndex &index)
186 {
187     QVariant var = _theater_model->data(index, TheaterModel::TheaterKeyRole);
188     CinemaKey cinema_key = qVariantValue<CinemaKey>(var);
189     return cinema_key;
190 }
191
192 MovieKey MainWindow::GetMovie(const QModelIndex &index)
193 {
194     QVariant var = _movie_model->data(index, MovieModel::MovieKeyRole);
195     MovieKey movie_key = qVariantValue<MovieKey>(var);
196     return movie_key;
197 }
198
199 void MainWindow::ShowContextMenu(const Cinema *cinema, const QPoint &pos)
200 {
201     _theater_phone_call->setVisible(!cinema->GetTelephone().isEmpty());
202     // TODO enable if find-route works
203     _theater_find_route->setVisible(false && !cinema->GetAddress().isEmpty());
204     _theater_search_web->setVisible(true);
205     _theater_context_menu->popup(pos);
206 }
207
208 void MainWindow::ShowContextMenu(const Movie *movie, const QPoint &pos)
209 {
210     Q_UNUSED(movie);
211     _movie_search_web->setVisible(true);
212     _movie_context_menu->popup(pos);
213 }