2 * Copyright (C) 2009 Petri Damstén <damu@iki.fi>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "weatherconfig.h"
24 #include <KInputDialog>
25 #include <KPixmapSequence>
26 #include <KPixmapSequenceWidget>
27 #include <KUnitConversion>
28 #include <KMessageBox>
30 #include "weatherlocation.h"
31 #include "weathervalidator.h"
32 #include "weatheri18ncatalog.h"
33 #include "ui_weatherconfig.h"
35 class WeatherConfig::Private
38 Private(WeatherConfig *weatherconfig)
48 void setSource(int index)
50 QString text = ui.locationCombo->itemData(index).toString();
51 if (!text.isEmpty()) {
53 emit q->settingsChanged();
59 QString text = ui.locationCombo->currentText();
70 searchLocation = text;
71 ui.locationCombo->clear();
74 busyWidget = new KPixmapSequenceWidget(q);
75 KPixmapSequence seq(QLatin1String("process-working"), 22);
76 busyWidget->setSequence(seq);
77 ui.locationSearchLayout->insertWidget(1, busyWidget);
80 validator->validate(text, true);
86 dlg->enableButton(KDialog::Ok, !source.isEmpty());
90 void addSource(const QString &sources);
91 void addSources(const QMap<QString, QString> &sources);
92 void validatorError(const QString &error);
95 QString searchLocation;
96 WeatherLocation* location;
97 WeatherValidator* validator;
100 Ui::WeatherConfig ui;
102 KPixmapSequenceWidget *busyWidget;
105 WeatherConfig::WeatherConfig(QWidget *parent)
107 , d(new Private(this))
109 Weatheri18nCatalog::loadCatalog();
111 d->dlg = qobject_cast<KDialog*>(parent);
113 for (int i = 0; i < KTemperature::UnitCount; i++) {
114 KTemperature::KTempUnit unit = static_cast<KTemperature::KTempUnit>(i);
115 d->ui.temperatureComboBox->addItem(KTemperature::unitDescription(unit), unit);
117 for (int i = 0; i < KPressure::UnitCount; i++) {
118 KPressure::KPresUnit unit = static_cast<KPressure::KPresUnit>(i);
119 d->ui.pressureComboBox->addItem(KPressure::unitDescription(unit), unit);
121 for (int i = 0; i < KVelocity::UnitCount; i++) {
122 KVelocity::KVeloUnit unit = static_cast<KVelocity::KVeloUnit>(i);
123 d->ui.speedComboBox->addItem(KVelocity::unitDescription(unit), unit);
125 for (int i = 0; i < KLength::UnitCount; i++) {
126 KLength::KLengUnit unit = static_cast<KLength::KLengUnit>(i);
127 d->ui.visibilityComboBox->addItem(KLength::unitDescription(unit), unit);
130 connect(d->ui.locationCombo, SIGNAL(returnPressed()), this, SLOT(changePressed()));
131 connect(d->ui.changeButton, SIGNAL(clicked()), this, SLOT(changePressed()));
133 connect(d->ui.updateIntervalSpinBox, SIGNAL(valueChanged(int)),
134 this, SLOT(setUpdateInterval(int)));
136 connect(d->ui.updateIntervalSpinBox, SIGNAL(valueChanged(int)),
137 this, SIGNAL(settingsChanged()));
138 connect(d->ui.temperatureComboBox, SIGNAL(currentIndexChanged(int)),
139 this, SIGNAL(settingsChanged()));
140 connect(d->ui.pressureComboBox, SIGNAL(currentIndexChanged(int)),
141 this, SIGNAL(settingsChanged()));
142 connect(d->ui.speedComboBox, SIGNAL(currentIndexChanged(int)),
143 this, SIGNAL(settingsChanged()));
144 connect(d->ui.visibilityComboBox, SIGNAL(currentIndexChanged(int)),
145 this, SIGNAL(settingsChanged()));
146 connect(d->ui.locationCombo, SIGNAL(currentIndexChanged(int)),
147 this, SLOT(setSource(int)));
149 connect(d->ui.locationCombo, SIGNAL(currentIndexChanged(int)) , this , SIGNAL(configValueChanged()));
150 connect(d->ui.pressureComboBox, SIGNAL(currentIndexChanged(int)) , this , SIGNAL(configValueChanged()));
151 connect(d->ui.updateIntervalSpinBox, SIGNAL(valueChanged(int)) , this , SIGNAL(configValueChanged()));
152 connect(d->ui.temperatureComboBox, SIGNAL(currentIndexChanged(int)) , this , SIGNAL(configValueChanged()));
153 connect(d->ui.pressureComboBox, SIGNAL(currentIndexChanged(int)) , this , SIGNAL(configValueChanged()));
154 connect(d->ui.speedComboBox, SIGNAL(currentIndexChanged(int)) , this , SIGNAL(configValueChanged()));
155 connect(d->ui.visibilityComboBox, SIGNAL(currentIndexChanged(int)) , this , SIGNAL(configValueChanged()));
158 WeatherConfig::~WeatherConfig()
163 void WeatherConfig::setDataEngines(Plasma::DataEngine *location, Plasma::DataEngine *weather)
166 d->validator = nullptr;
168 d->validator = new WeatherValidator(this);
169 connect(d->validator, SIGNAL(error(QString)), this, SLOT(validatorError(QString)));
170 connect(d->validator, SIGNAL(finished(QMap<QString,QString>)), this, SLOT(addSources(QMap<QString,QString>)));
171 d->validator->setDataEngine(weather);
172 d->validator->setIon(d->ion);
174 if (location && weather) {
175 d->location = new WeatherLocation(this);
176 connect(d->location, SIGNAL(valid(QString)), this, SLOT(addSource(QString)));
177 d->location->setDataEngines(location, weather);
181 void WeatherConfig::Private::validatorError(const QString &error)
186 void WeatherConfig::Private::addSource(const QString &source)
188 QStringList list = source.split(QLatin1Char('|'), QString::SkipEmptyParts);
189 if (list.count() > 2) {
191 QString result = i18nc("A weather station location and the weather service it comes from",
192 "%1 (%2)", list[2], list[0]); // the names are too looong ions.value(list[0]));
193 const int resultIndex = ui.locationCombo->findText(result);
194 if (resultIndex < 0) {
195 ui.locationCombo->addItem(result, source);
200 void WeatherConfig::Private::addSources(const QMap<QString, QString> &sources)
202 QMapIterator<QString, QString> it(sources);
204 while (it.hasNext()) {
206 addSource(it.value());
210 busyWidget = nullptr;
211 kDebug() << ui.locationCombo->count();
212 if (ui.locationCombo->count() == 0) {
213 KMessageBox::information(
215 i18n("No weather stations found for '%1'", searchLocation),
216 i18n("No weather stations found"),
217 QString::fromLatin1("WeatherConfig")
220 ui.locationCombo->showPopup();
224 void WeatherConfig::setUpdateInterval(int interval)
226 d->ui.updateIntervalSpinBox->setValue(interval);
227 d->ui.updateIntervalSpinBox->setSuffix(ki18np(" minute", " minutes"));
230 void WeatherConfig::setTemperatureUnit(const QString &unit)
232 KTemperature temp(0.0, unit);
233 if (temp.unitEnum() != KTemperature::Invalid) {
234 d->ui.temperatureComboBox->setCurrentIndex(static_cast<int>(temp.unitEnum()));
238 void WeatherConfig::setPressureUnit(const QString &unit)
240 KPressure pres(0.0, unit);
241 if (pres.unitEnum() != KPressure::Invalid) {
242 d->ui.pressureComboBox->setCurrentIndex(static_cast<int>(pres.unitEnum()));
246 void WeatherConfig::setSpeedUnit(const QString &unit)
248 KVelocity velo(0.0, unit);
249 if (velo.unitEnum() != KVelocity::Invalid) {
250 d->ui.pressureComboBox->setCurrentIndex(static_cast<int>(velo.unitEnum()));
254 void WeatherConfig::setVisibilityUnit(const QString &unit)
256 KLength leng(0.0, unit);
257 if (leng.unitEnum() != KLength::Invalid) {
258 d->ui.pressureComboBox->setCurrentIndex(static_cast<int>(leng.unitEnum()));
262 void WeatherConfig::setSource(const QString &source)
264 // kDebug() << "source set to" << source;
265 d->addSource(source);
269 QString WeatherConfig::source() const
274 int WeatherConfig::updateInterval() const
276 return d->ui.updateIntervalSpinBox->value();
279 QString WeatherConfig::temperatureUnit() const
281 KTemperature temp(0.0, static_cast<KTemperature::KTempUnit>(d->ui.temperatureComboBox->currentIndex()));
285 QString WeatherConfig::pressureUnit() const
287 KPressure pres(0.0, static_cast<KPressure::KPresUnit>(d->ui.pressureComboBox->currentIndex()));
291 QString WeatherConfig::speedUnit() const
293 KVelocity velo(0.0, static_cast<KVelocity::KVeloUnit>(d->ui.speedComboBox->currentIndex()));
297 QString WeatherConfig::visibilityUnit() const
299 KLength leng(0.0, static_cast<KLength::KLengUnit>(d->ui.visibilityComboBox->currentIndex()));
303 void WeatherConfig::setConfigurableUnits(const ConfigurableUnits units)
305 d->ui.unitsLabel->setVisible(units != None);
306 d->ui.temperatureLabel->setVisible(units & Temperature);
307 d->ui.temperatureComboBox->setVisible(units & Temperature);
308 d->ui.pressureLabel->setVisible(units & Pressure);
309 d->ui.pressureComboBox->setVisible(units & Pressure);
310 d->ui.speedLabel->setVisible(units & Speed);
311 d->ui.speedComboBox->setVisible(units & Speed);
312 d->ui.visibilityLabel->setVisible(units & Visibility);
313 d->ui.visibilityComboBox->setVisible(units & Visibility);
316 void WeatherConfig::setHeadersVisible(bool visible)
318 d->ui.locationLabel->setVisible(visible);
319 d->ui.unitsLabel->setVisible(visible);
322 void WeatherConfig::setIon(const QString &ion)
326 d->validator->setIon(ion);
329 d->location->getDefault(ion);
333 #include "moc_weatherconfig.cpp"