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("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()));
87 MainWindow::~MainWindow()
92 void MainWindow::SetLocation(Location location)
95 if (_location.IsValid()) {
96 ui->Background->SetLabelText(_location.GetLocationName());
98 ui->Background->SetLabelText("");
102 void MainWindow::SetTheaterModel(QAbstractItemModel *theater_model)
104 _theater_model = theater_model;
105 setWindowTitle(tr("Theaters"));
106 ui->Theaters->setModel(theater_model);
107 ui->MainPages->setCurrentWidget(ui->TheatersPage);
110 void MainWindow::SetMovieModel(QAbstractItemModel *movie_model)
112 _movie_model = movie_model;
113 setWindowTitle(tr("Movies"));
114 ui->Movies->setModel(movie_model);
115 ui->MainPages->setCurrentWidget(ui->MoviesPage);
118 void MainWindow::SetError(QString error_text)
120 setWindowTitle(tr("MovieSchedule"));
121 ui->ErrorMessage->setText(error_text);
122 ui->MainPages->setCurrentWidget(ui->ErrorPage);
125 void MainWindow::TheaterClicked(QModelIndex index)
127 emit TheaterSelected(GetCinema(index));
130 void MainWindow::TheaterContextMenuRequested(const QPoint &pos)
132 AssertedReadLocker locker(_cinema_schedule->GetLock());
133 const Cinema *cinema = _cinema_schedule->FindCinema(GetCinema(ui->Theaters->currentIndex()));
135 ShowContextMenu(cinema, ui->Theaters->viewport()->mapToGlobal(pos));
139 void MainWindow::MovieClicked(QModelIndex index)
141 emit MovieSelected(GetMovie(index));
144 void MainWindow::MovieContextMenuRequested(const QPoint &pos)
146 AssertedReadLocker locker(_cinema_schedule->GetLock());
147 const Movie *movie = _cinema_schedule->FindMovie(GetMovie(ui->Movies->currentIndex()));
149 ShowContextMenu(movie, ui->Movies->viewport()->mapToGlobal(pos));
153 void MainWindow::CallTheaterByPhone()
155 emit CallTheaterByPhone(GetCinema(ui->Theaters->currentIndex()));
158 void MainWindow::FindRouteToTheater()
160 emit FindRouteToTheater(GetCinema(ui->Theaters->currentIndex()));
163 void MainWindow::SearchTheaterInWeb()
165 emit SearchTheaterInWeb(GetCinema(ui->Theaters->currentIndex()));
168 void MainWindow::SearchMovieInWeb()
170 emit SearchMovieInWeb(GetMovie(ui->Movies->currentIndex()));
173 void MainWindow::changeEvent(QEvent *e)
175 QMainWindow::changeEvent(e);
177 case QEvent::LanguageChange:
178 ui->retranslateUi(this);
185 CinemaKey MainWindow::GetCinema(const QModelIndex &index)
187 QVariant var = _theater_model->data(index, TheaterModel::TheaterKeyRole);
188 CinemaKey cinema_key = qVariantValue<CinemaKey>(var);
192 MovieKey MainWindow::GetMovie(const QModelIndex &index)
194 QVariant var = _movie_model->data(index, MovieModel::MovieKeyRole);
195 MovieKey movie_key = qVariantValue<MovieKey>(var);
199 void MainWindow::ShowContextMenu(const Cinema *cinema, const QPoint &pos)
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);
208 void MainWindow::ShowContextMenu(const Movie *movie, const QPoint &pos)
211 _movie_search_web->setVisible(true);
212 _movie_context_menu->popup(pos);