Initial commit (software version 0.2.0)
[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
27 #include <QLayoutItem>
28
29 ContextDialog::ContextDialog(CinemaSchedule *cinema_schedule, QWidget *parent) :
30         QDialog(parent),
31         ui(new Ui::ContextDialog),
32         _cinema_schedule(cinema_schedule),
33         _next_row(0),
34         _next_column(0)
35 {
36     ui->setupUi(this);
37     removeAllWidgets();
38     connect(ui->add_to_calendar, SIGNAL(clicked()), this, SLOT(AddToCalendar()));
39     connect(ui->call_theater_by_phone, SIGNAL(clicked()), this, SLOT(CallTheaterByPhone()));
40     connect(ui->find_route_to_theater, SIGNAL(clicked()), this, SLOT(FindRouteToTheater()));
41     connect(ui->search_movie_in_web, SIGNAL(clicked()), this, SLOT(SearchMovieInWeb()));
42     connect(ui->search_theater_in_web, SIGNAL(clicked()), this, SLOT(SearchTheaterInWeb()));
43 }
44
45 ContextDialog::~ContextDialog()
46 {
47     delete ui;
48 }
49
50 void ContextDialog::Show(ScheduleEntryKey schedule_entry_key)
51 {
52     removeAllWidgets();
53     _schedule_entry_key = schedule_entry_key;
54     AssertedReadLocker locker(_cinema_schedule->GetLock());
55     ScheduleEntry schedule_entry = _cinema_schedule->FindScheduleEntry(_schedule_entry_key);
56     if (schedule_entry.IsValid()) {
57         addWidget(ui->add_to_calendar);
58         if (!schedule_entry.GetCinema()->GetTelephone().isEmpty()) {
59             addWidget(ui->call_theater_by_phone);
60         }
61         if (!schedule_entry.GetCinema()->GetAddress().isEmpty()) {
62             // TODO enable if find route is implemented
63             // addWidget(ui->find_route_to_theater);
64         }
65         addWidget(ui->search_movie_in_web);
66         if (!schedule_entry.GetCinema()->GetAddress().isEmpty()) {
67             addWidget(ui->search_theater_in_web);
68         }
69         adjustSize();
70         show();
71     }
72 }
73
74 void ContextDialog::changeEvent(QEvent *e)
75 {
76     QDialog::changeEvent(e);
77     switch (e->type()) {
78     case QEvent::LanguageChange:
79         ui->retranslateUi(this);
80         break;
81     default:
82         break;
83     }
84 }
85
86 void ContextDialog::removeAllWidgets()
87 {
88     QLayoutItem *item;
89     while ((item = ui->gridLayout->itemAt(0)) != 0) {
90         if (item->widget() != 0) {
91             item->widget()->setVisible(false);
92         }
93         ui->gridLayout->removeItem(item);
94     }
95     _next_row = 0;
96     _next_column = 0;
97 }
98
99 void ContextDialog::addWidget(QWidget *widget)
100 {
101     widget->setVisible(true);
102     ui->gridLayout->addWidget(widget, _next_row, _next_column, 1, 1);
103     ++_next_column;
104     if (_next_column > 1) {
105         ++_next_row;
106         _next_column = 0;
107     }
108 }
109
110 void ContextDialog::AddToCalendar()
111 {
112     hide();
113     emit AddToCalendar(_schedule_entry_key);
114 }
115
116 void ContextDialog::CallTheaterByPhone()
117 {
118     hide();
119     emit CallTheaterByPhone(_schedule_entry_key.GetCinemaKey());
120 }
121
122 void ContextDialog::FindRouteToTheater()
123 {
124     hide();
125     emit FindRouteToTheater(_schedule_entry_key.GetCinemaKey());
126 }
127
128 void ContextDialog::SearchMovieInWeb()
129 {
130     hide();
131     emit SearchMovieInWeb(_schedule_entry_key.GetMovieKey());
132 }
133
134 void ContextDialog::SearchTheaterInWeb()
135 {
136     hide();
137     emit SearchTheaterInWeb(_schedule_entry_key.GetCinemaKey());
138 }