Fixed searchclients to handle new Google URLs correctly; added GUI
[movie-schedule] / src / ui / contextdialog.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 "contextdialog.h"
19 #include "ui_contextdialog.h"
20
21 #include "data/cinemaschedule.h"
22 #include "data/scheduleentrykey.h"
23 #include "data/cinema.h"
24 #include "data/movie.h"
25 #include "utils/assertedlocker.h"
26 #include "uiutils.h"
27
28 #include <QLayoutItem>
29 #include <QDesktopWidget>
30
31 ContextDialog::ContextDialog(CinemaSchedule *cinema_schedule, QWidget *parent) :
32         QDialog(parent),
33         ui(new Ui::ContextDialog),
34         _cinema_schedule(cinema_schedule),
35         _next_row(0),
36         _next_column(0)
37 {
38     ui->setupUi(this);
39     removeAllWidgets();
40     connect(ui->add_to_calendar, SIGNAL(clicked()), this, SLOT(AddToCalendar()));
41     connect(ui->call_theater_by_phone, SIGNAL(clicked()), this, SLOT(CallTheaterByPhone()));
42     connect(ui->find_route_to_theater, SIGNAL(clicked()), this, SLOT(FindRouteToTheater()));
43     connect(ui->search_movie_in_web, SIGNAL(clicked()), this, SLOT(SearchMovieInWeb()));
44     connect(ui->search_theater_in_web, SIGNAL(clicked()), this, SLOT(SearchTheaterInWeb()));
45     connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(Rotate()));
46     Rotate();
47 }
48
49 ContextDialog::~ContextDialog()
50 {
51     delete ui;
52 }
53
54 void ContextDialog::Show(ScheduleEntryKey schedule_entry_key)
55 {
56     removeAllWidgets();
57     _schedule_entry_key = schedule_entry_key;
58     AssertedReadLocker locker(_cinema_schedule->GetLock());
59     ScheduleEntry schedule_entry = _cinema_schedule->FindScheduleEntry(_schedule_entry_key);
60     if (schedule_entry.IsValid()) {
61         addWidget(ui->add_to_calendar);
62         if (!schedule_entry.GetCinema()->GetTelephone().isEmpty()) {
63             addWidget(ui->call_theater_by_phone);
64         }
65         if (!schedule_entry.GetCinema()->GetAddress().isEmpty()) {
66             // TODO enable if find route is implemented
67             // addWidget(ui->find_route_to_theater);
68         }
69         addWidget(ui->search_movie_in_web);
70         if (!schedule_entry.GetCinema()->GetAddress().isEmpty()) {
71             addWidget(ui->search_theater_in_web);
72         }
73         adjustSize();
74         show();
75     }
76 }
77
78 void ContextDialog::changeEvent(QEvent *e)
79 {
80     QDialog::changeEvent(e);
81     switch (e->type()) {
82     case QEvent::LanguageChange:
83         ui->retranslateUi(this);
84         break;
85     default:
86         break;
87     }
88 }
89
90 void ContextDialog::removeAllWidgets()
91 {
92     QLayoutItem *item;
93     while ((item = ui->gridLayout->itemAt(0)) != 0) {
94         if (item->widget() != 0) {
95             item->widget()->setVisible(false);
96         }
97         ui->gridLayout->removeItem(item);
98     }
99     _next_row = 0;
100     _next_column = 0;
101 }
102
103 void ContextDialog::addWidget(QWidget *widget)
104 {
105     bool landscape = UiUtils::IsLandscape();
106     widget->setVisible(true);
107     ui->gridLayout->addWidget(widget, _next_row, _next_column, 1, 1);
108     ++_next_column;
109     if (_next_column > (landscape ? 1 : 0)) {
110         ++_next_row;
111         _next_column = 0;
112     }
113 }
114
115 void ContextDialog::AddToCalendar()
116 {
117     hide();
118     emit AddToCalendar(_schedule_entry_key);
119 }
120
121 void ContextDialog::CallTheaterByPhone()
122 {
123     hide();
124     emit CallTheaterByPhone(_schedule_entry_key.GetCinemaKey());
125 }
126
127 void ContextDialog::FindRouteToTheater()
128 {
129     hide();
130     emit FindRouteToTheater(_schedule_entry_key.GetCinemaKey());
131 }
132
133 void ContextDialog::SearchMovieInWeb()
134 {
135     hide();
136     emit SearchMovieInWeb(_schedule_entry_key.GetMovieKey());
137 }
138
139 void ContextDialog::SearchTheaterInWeb()
140 {
141     hide();
142     emit SearchTheaterInWeb(_schedule_entry_key.GetCinemaKey());
143 }
144
145 void ContextDialog::Rotate()
146 {
147     if (isVisible()) {
148         Show(_schedule_entry_key);
149     }
150 }