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