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 "locationdialog.h"
19 #include "ui_locationdialog.h"
22 #include <QDesktopWidget>
24 LocationDialog::LocationDialog(QWidget *parent) :
26 ui(new Ui::LocationDialog)
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()));
49 LocationDialog::~LocationDialog()
54 void LocationDialog::SetLocation(const Location &location)
56 ui->Location->setText(location.GetLocationName());
59 void LocationDialog::SetPreviousLocations(const Locations &previous_locations)
61 _previous_locations = previous_locations;
62 QListIterator<Location> it(_previous_locations);
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);
73 while (i < _location_buttons.size()) {
74 _location_buttons[i]->setVisible(false);
75 _location_buttons[i]->setEnabled(false);
80 void LocationDialog::SetGPSEnabled(bool gps_enabled)
82 ui->GPS->setVisible(gps_enabled);
83 ui->GPS->setEnabled(gps_enabled);
86 void LocationDialog::show()
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]);
95 if (ui->GPS->isEnabled()) {
96 ui->gridLayout->addWidget(ui->GPS, row, column);
98 if (column > (landscape ? 1 : 0)) {
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);
107 if (column > (landscape ? 1 : 0)) {
113 for (int i = 0; i < _location_buttons.size(); ++i) {
114 if (_location_buttons[i]->text() == ui->Location->text()) {
115 ui->Location->setText("");
120 ui->Search->setDefault(true);
121 ui->Location->setFocus();
125 void LocationDialog::SearchClicked()
128 location.SetLocationName(ui->Location->text());
129 if (location.IsValid()) {
130 emit Search(location);
136 void LocationDialog::GPSClicked()
143 void LocationDialog::PreviousLocation1Clicked()
145 PreviousLocationClicked(0);
148 void LocationDialog::PreviousLocation2Clicked()
150 PreviousLocationClicked(1);
153 void LocationDialog::PreviousLocation3Clicked()
155 PreviousLocationClicked(2);
158 void LocationDialog::PreviousLocation4Clicked()
160 PreviousLocationClicked(3);
163 void LocationDialog::PreviousLocation5Clicked()
165 PreviousLocationClicked(4);
168 void LocationDialog::PreviousLocationClicked(int i)
171 location.SetLocationName(_location_buttons[i]->text());
172 emit SearchPreviousLocation(location);
177 void LocationDialog::changeEvent(QEvent *e)
179 QDialog::changeEvent(e);
181 case QEvent::LanguageChange:
182 ui->retranslateUi(this);
189 void LocationDialog::Rotate()
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);