Fixed searchclients to handle new Google URLs correctly; added GUI
[movie-schedule] / src / ui / locationdialog.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 "locationdialog.h"
19 #include "ui_locationdialog.h"
20 #include "uiutils.h"
21
22 #include <QDesktopWidget>
23
24 LocationDialog::LocationDialog(QWidget *parent) :
25     QDialog(parent),
26     ui(new Ui::LocationDialog)
27 {
28     ui->setupUi(this);
29     ui->_button_box->addButton(ui->Search, QDialogButtonBox::AcceptRole);
30     connect(ui->_button_box, SIGNAL(accepted()), this, SLOT(SearchClicked()));
31     connect(ui->_button_box, SIGNAL(rejected()), this, SLOT(deleteLater()));
32     connect(ui->GPS, SIGNAL(clicked()), this, SLOT(GPSClicked()));
33     connect(ui->Location1, SIGNAL(clicked()), this, SLOT(PreviousLocation1Clicked()));
34     connect(ui->Location2, SIGNAL(clicked()), this, SLOT(PreviousLocation2Clicked()));
35     connect(ui->Location3, SIGNAL(clicked()), this, SLOT(PreviousLocation3Clicked()));
36     connect(ui->Location4, SIGNAL(clicked()), this, SLOT(PreviousLocation4Clicked()));
37     connect(ui->Location5, SIGNAL(clicked()), this, SLOT(PreviousLocation5Clicked()));
38     connect(this, SIGNAL(accepted()), this, SLOT(SearchClicked()));
39     connect(this, SIGNAL(rejected()), this, SIGNAL(Cancel()));
40     _location_buttons.append(ui->Location1);
41     _location_buttons.append(ui->Location2);
42     _location_buttons.append(ui->Location3);
43     _location_buttons.append(ui->Location4);
44     _location_buttons.append(ui->Location5);
45     connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(Rotate()));
46     Rotate();
47 }
48
49 LocationDialog::~LocationDialog()
50 {
51     delete ui;
52 }
53
54 void LocationDialog::SetLocation(const Location &location)
55 {
56     ui->Location->setText(location.GetLocationName());
57 }
58
59 void LocationDialog::SetPreviousLocations(const Locations &previous_locations)
60 {
61     _previous_locations = previous_locations;
62     QListIterator<Location> it(_previous_locations);
63     int i = 0;
64     while (it.hasNext() && i < _location_buttons.size()) {
65         const Location &location = it.next();
66         if (location.IsValid()) {
67             _location_buttons[i]->setText(location.GetLocationName());
68             _location_buttons[i]->setVisible(true);
69             _location_buttons[i]->setEnabled(true);
70             ++i;
71         }
72     }
73     while (i < _location_buttons.size()) {
74         _location_buttons[i]->setVisible(false);
75         _location_buttons[i]->setEnabled(false);
76         ++i;
77     }
78 }
79
80 void LocationDialog::SetGPSEnabled(bool gps_enabled)
81 {
82     ui->GPS->setVisible(gps_enabled);
83     ui->GPS->setEnabled(gps_enabled);
84 }
85
86 void LocationDialog::show()
87 {
88     bool landscape = UiUtils::IsLandscape();
89     ui->gridLayout->removeWidget(ui->GPS);
90     for (int i = 0; i < _location_buttons.size(); ++i) {
91         ui->gridLayout->removeWidget(_location_buttons[i]);
92     }
93     int row = 0;
94     int column = 0;
95     if (ui->GPS->isEnabled()) {
96         ui->gridLayout->addWidget(ui->GPS, row, column);
97         ++column;
98         if (column > (landscape ? 1 : 0)) {
99             ++row;
100             column = 0;
101         }
102     }
103     for (int i = 0; i < _location_buttons.size(); ++i) {
104         if (_location_buttons[i]->isEnabled()) {
105             ui->gridLayout->addWidget(_location_buttons[i], row, column);
106             ++column;
107             if (column > (landscape ? 1 : 0)) {
108                 ++row;
109                 column = 0;
110             }
111         }
112     }
113     for (int i = 0; i < _location_buttons.size(); ++i) {
114         if (_location_buttons[i]->text() == ui->Location->text()) {
115             ui->Location->setText("");
116             break;
117         }
118     }
119     adjustSize();
120     ui->Search->setDefault(true);
121     ui->Location->setFocus();
122     QDialog::show();
123 }
124
125 void LocationDialog::SearchClicked()
126 {
127     Location location;
128     location.SetLocationName(ui->Location->text());
129     if (location.IsValid()) {
130         emit Search(location);
131         hide();
132         deleteLater();
133     }
134 }
135
136 void LocationDialog::GPSClicked()
137 {
138     emit SearchGPS();
139     hide();
140     deleteLater();
141 }
142
143 void LocationDialog::PreviousLocation1Clicked()
144 {
145     PreviousLocationClicked(0);
146 }
147
148 void LocationDialog::PreviousLocation2Clicked()
149 {
150     PreviousLocationClicked(1);
151 }
152
153 void LocationDialog::PreviousLocation3Clicked()
154 {
155     PreviousLocationClicked(2);
156 }
157
158 void LocationDialog::PreviousLocation4Clicked()
159 {
160     PreviousLocationClicked(3);
161 }
162
163 void LocationDialog::PreviousLocation5Clicked()
164 {
165     PreviousLocationClicked(4);
166 }
167
168 void LocationDialog::PreviousLocationClicked(int i)
169 {
170     Location location;
171     location.SetLocationName(_location_buttons[i]->text());
172     emit SearchPreviousLocation(location);
173     hide();
174     deleteLater();
175 }
176
177 void LocationDialog::changeEvent(QEvent *e)
178 {
179     QDialog::changeEvent(e);
180     switch (e->type()) {
181     case QEvent::LanguageChange:
182         ui->retranslateUi(this);
183         break;
184     default:
185         break;
186     }
187 }
188
189 void LocationDialog::Rotate()
190 {
191     bool landscape = UiUtils::IsLandscape();
192     ui->_search_layout->setDirection(landscape ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom);
193     ui->_button_box->setOrientation(landscape ? Qt::Vertical : Qt::Horizontal);
194     if (isVisible()) {
195         show();
196     }
197 }