1 // Copyright 2010 Jochen Becher
3 // This file is part of MovieSchedule.
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.
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.
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/>.
18 #include "mainwindow.h"
19 #include "ui_mainwindow.h"
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"
37 #include <QAbstractKineticScroller>
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),
56 setAttribute(Qt::WA_Maemo5StackedWindow);
58 QStackedLayout *layout = dynamic_cast<QStackedLayout*>(ui->MainStack->layout());
60 layout->setStackingMode(QStackedLayout::StackAll);
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);
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()));
88 MainWindow::~MainWindow()
93 void MainWindow::SetOrientation(Settings::OrientationMode orientation)
96 switch (orientation) {
97 case Settings::LANDSCAPE:
98 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
100 case Settings::PORTRAIT:
101 setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
103 case Settings::AUTOROTATION:
104 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
108 Q_UNUSED(orientation);
112 void MainWindow::SetLocation(Location location)
114 _location = location;
115 if (_location.IsValid()) {
116 ui->Background->SetLabelText(_location.GetLocationName());
118 ui->Background->SetLabelText("");
122 void MainWindow::SetTheaterModel(QAbstractItemModel *theater_model)
124 _theater_model = theater_model;
125 setWindowTitle(tr("Theaters"));
126 ui->Theaters->setModel(theater_model);
127 ui->MainPages->setCurrentWidget(ui->TheatersPage);
130 void MainWindow::SetMovieModel(QAbstractItemModel *movie_model)
132 _movie_model = movie_model;
133 setWindowTitle(tr("Movies"));
134 ui->Movies->setModel(movie_model);
135 ui->MainPages->setCurrentWidget(ui->MoviesPage);
138 void MainWindow::SetError(QString error_text)
140 setWindowTitle(tr("MovieSchedule"));
141 ui->ErrorMessage->setText(error_text);
142 ui->MainPages->setCurrentWidget(ui->ErrorPage);
145 void MainWindow::TheaterClicked(QModelIndex index)
147 emit TheaterSelected(GetCinema(index));
150 void MainWindow::TheaterContextMenuRequested(const QPoint &pos)
152 AssertedReadLocker locker(_cinema_schedule->GetLock());
153 const Cinema *cinema = _cinema_schedule->FindCinema(GetCinema(ui->Theaters->currentIndex()));
155 ShowContextMenu(cinema, ui->Theaters->viewport()->mapToGlobal(pos));
159 void MainWindow::MovieClicked(QModelIndex index)
161 emit MovieSelected(GetMovie(index));
164 void MainWindow::MovieContextMenuRequested(const QPoint &pos)
166 AssertedReadLocker locker(_cinema_schedule->GetLock());
167 const Movie *movie = _cinema_schedule->FindMovie(GetMovie(ui->Movies->currentIndex()));
169 ShowContextMenu(movie, ui->Movies->viewport()->mapToGlobal(pos));
173 void MainWindow::CallTheaterByPhone()
175 emit CallTheaterByPhone(GetCinema(ui->Theaters->currentIndex()));
178 void MainWindow::FindRouteToTheater()
180 emit FindRouteToTheater(GetCinema(ui->Theaters->currentIndex()));
183 void MainWindow::SearchTheaterInWeb()
185 emit SearchTheaterInWeb(GetCinema(ui->Theaters->currentIndex()));
188 void MainWindow::SearchMovieInWeb()
190 emit SearchMovieInWeb(GetMovie(ui->Movies->currentIndex()));
193 void MainWindow::changeEvent(QEvent *e)
195 QMainWindow::changeEvent(e);
197 case QEvent::LanguageChange:
198 ui->retranslateUi(this);
205 CinemaKey MainWindow::GetCinema(const QModelIndex &index)
207 QVariant var = _theater_model->data(index, TheaterModel::TheaterKeyRole);
208 CinemaKey cinema_key = qVariantValue<CinemaKey>(var);
212 MovieKey MainWindow::GetMovie(const QModelIndex &index)
214 QVariant var = _movie_model->data(index, MovieModel::MovieKeyRole);
215 MovieKey movie_key = qVariantValue<MovieKey>(var);
219 void MainWindow::ShowContextMenu(const Cinema *cinema, const QPoint &pos)
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);
228 void MainWindow::ShowContextMenu(const Movie *movie, const QPoint &pos)
231 _movie_search_web->setVisible(true);
232 _movie_context_menu->popup(pos);