not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / plasmaclock / clockapplet.cpp
blobc93c5c92e434d2c91fbe8c1240ca0d75724d4378
1 /***************************************************************************
2 * Copyright (C) 2007-2008 by Riccardo Iaconelli <riccardo@kde.org> *
3 * Copyright (C) 2007-2008 by Sebastian Kuegler <sebas@kde.org> *
4 * *
5 * This program 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 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program 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 this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
19 ***************************************************************************/
21 #include "clockapplet.h"
23 #include <math.h>
25 #include <QtGui/QPainter>
26 #include <QtGui/QStyleOptionGraphicsItem>
27 #include <QtGui/QSpinBox>
28 #include <QtCore/QTimeLine>
29 #include <QtGui/QGraphicsProxyWidget>
30 #include <QtGui/QGraphicsSceneMouseEvent>
31 #include <QtGui/QGraphicsView>
32 #include <QtCore/QDate>
33 #include <QtCore/QTimer>
35 #include <KColorScheme>
36 #include <KConfigDialog>
37 #include <KConfigGroup>
38 #include <KDatePicker>
39 #include <KDebug>
40 #include <KDialog>
41 #include <KGlobalSettings>
42 #include <KTimeZone>
44 #include <Plasma/Containment>
45 #include <Plasma/Corona>
46 #include <Plasma/DataEngine>
47 #include <Plasma/Dialog>
48 #include <Plasma/Extender>
49 #include <Plasma/ExtenderItem>
50 #include <Plasma/Theme>
52 #include "calendar.h"
54 #include "ui_timezonesConfig.h"
56 class ClockApplet::Private
58 public:
59 Private(ClockApplet *clockapplet)
60 : q(clockapplet),
61 timezone(ClockApplet::localTimezoneUntranslated()),
62 forceTzDisplay(false)
65 ClockApplet *q;
66 Ui::timezonesConfig ui;
67 QString timezone;
68 QString defaultTimezone;
69 QPoint clicked;
70 QStringList selectedTimezones;
71 QString prettyTimezone;
72 bool forceTzDisplay;
74 void addTzToTipText(QString &subText, QString tz)
76 Plasma::Applet applet;
77 Plasma::DataEngine::Data data = applet.dataEngine("time")->query(tz);
78 if (tz == "UTC") {
79 subText += "<br><b>UTC</b> ";
81 else {
82 subText += "<br><b>" + data["Timezone City"].toString().replace("_", " ")+"</b> ";
84 subText += KGlobal::locale()->formatTime(data["Time"].toTime(), false) + ", ";
85 subText += KGlobal::locale()->formatDate(data["Date"].toDate());
88 void createCalendar()
90 if (!q->extender()->item("calendar")) {
91 Plasma::ExtenderItem *eItem = new Plasma::ExtenderItem(q->extender());
92 eItem->setName("calendar");
93 q->initExtenderItem(eItem);
97 void setPrettyTimezone()
99 if (timezone == "UTC") {
100 prettyTimezone = timezone;
101 } else if (!q->isLocalTimezone()) {
102 QStringList tzParts = timezone.split("/");
103 prettyTimezone = tzParts.value(1);
104 } else {
105 prettyTimezone = localTimezone();
108 prettyTimezone = prettyTimezone.replace('_', ' ');
112 ClockApplet::ClockApplet(QObject *parent, const QVariantList &args)
113 : Plasma::PopupApplet(parent, args),
114 d(new Private(this))
116 setPopupIcon(QIcon());
117 setPassivePopup(true);
120 ClockApplet::~ClockApplet()
122 delete d;
125 void ClockApplet::toolTipAboutToShow()
127 updateTipContent();
130 void ClockApplet::toolTipHidden()
132 Plasma::ToolTipManager::self()->clearContent(this);
135 void ClockApplet::updateTipContent()
137 Plasma::ToolTipContent tipData;
140 // the main text contains the current timezone's time and date
141 Plasma::DataEngine::Data data = dataEngine("time")->query(currentTimezone());
142 QString mainText = d->prettyTimezone + " ";
143 mainText += KGlobal::locale()->formatTime(data["Time"].toTime(), false) + "<br>";
144 mainText += KGlobal::locale()->formatDate(data["Date"].toDate());
145 tipData.setMainText(mainText);
148 QString subText;
149 if (!isLocalTimezone()) {
150 d->addTzToTipText(subText, localTimezone());
153 foreach (const QString &tz, getSelectedTimezones()) {
154 if (tz == currentTimezone()) {
155 continue;
157 d->addTzToTipText(subText, tz);
160 tipData.setSubText(subText);
162 // query for custom content
163 Plasma::ToolTipContent customContent = toolTipContent();
164 if (customContent.image().isNull()) {
165 tipData.setImage(KIcon("chronometer").pixmap(IconSize(KIconLoader::Desktop)));
166 } else {
167 tipData.setImage(customContent.image());
170 if (!customContent.mainText().isEmpty()) {
171 // add their main text
172 tipData.setMainText(customContent.mainText() + "<br>" + tipData.mainText());
175 if (!customContent.subText().isEmpty()) {
176 // add their sub text
177 tipData.setSubText(customContent.subText() + "<br>" + tipData.subText());
180 tipData.setAutohide(false);
181 Plasma::ToolTipManager::self()->setContent(this, tipData);
184 Plasma::ToolTipContent ClockApplet::toolTipContent()
186 return Plasma::ToolTipContent();
189 void ClockApplet::createConfigurationInterface(KConfigDialog *parent)
191 createClockConfigurationInterface(parent);
193 QWidget *widget = new QWidget();
194 d->ui.setupUi(widget);
196 parent->addPage(widget, i18n("Time Zones"), Applet::icon());
198 foreach (const QString &tz, d->selectedTimezones) {
199 d->ui.timeZones->setSelected(tz, true);
202 updateClockDefaultsTo();
203 int defaultSelection = d->ui.clockDefaultsTo->findData(d->defaultTimezone);
204 if (defaultSelection < 0) {
205 defaultSelection = 0; //if it's something unexpected default to local
206 kDebug() << d->defaultTimezone << "not in list!?";
208 d->ui.clockDefaultsTo->setCurrentIndex(defaultSelection);
210 parent->setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
211 connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
212 connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
213 connect(d->ui.timeZones, SIGNAL(itemSelectionChanged()), this, SLOT(updateClockDefaultsTo()));
215 #if 0
216 #ifdef CLOCK_APPLET_CONF
217 ui.localTimeZone->setChecked(isLocalTimezone());
218 ui.timeZones->setEnabled(!isLocalTimezone());
219 foreach (const QString &str, selectedTimezones) {
220 ui.timeZones->setSelected(str, true);
222 #endif
223 #endif
226 void ClockApplet::createClockConfigurationInterface(KConfigDialog *parent)
228 Q_UNUSED(parent)
231 void ClockApplet::clockConfigAccepted()
236 void ClockApplet::configAccepted()
238 KConfigGroup cg = config();
240 d->selectedTimezones = d->ui.timeZones->selection();
241 cg.writeEntry("timeZones", d->selectedTimezones);
243 if (d->ui.clockDefaultsTo->currentIndex() == 0) {
244 //The first position in ui.clockDefaultsTo is "Local"
245 d->defaultTimezone = localTimezoneUntranslated();
246 } else {
247 d->defaultTimezone = d->ui.clockDefaultsTo->itemData(d->ui.clockDefaultsTo->currentIndex()).toString();
250 cg.writeEntry("defaultTimezone", d->defaultTimezone);
251 changeEngineTimezone(currentTimezone(), d->defaultTimezone);
252 setCurrentTimezone(d->defaultTimezone);
254 clockConfigAccepted();
255 constraintsEvent(Plasma::SizeConstraint);
256 update();
258 emit configNeedsSaving();
261 void ClockApplet::updateClockDefaultsTo()
263 QString oldSelection = d->ui.clockDefaultsTo->currentText();
264 d->ui.clockDefaultsTo->clear();
265 d->ui.clockDefaultsTo->addItem(localTimezone(), localTimezone());
266 foreach(const QString &tz, d->ui.timeZones->selection())
267 d->ui.clockDefaultsTo->addItem(KTimeZoneWidget::displayName(KTimeZone(tz)), tz);
268 int newPosition = d->ui.clockDefaultsTo->findText(oldSelection);
269 if (newPosition >= 0) {
270 d->ui.clockDefaultsTo->setCurrentIndex(newPosition);
272 if (d->ui.clockDefaultsTo->count() > 1) {
273 d->ui.clockDefaultsTo->setEnabled(true);
275 else {
276 // Only "Local" in ui.clockDefaultsTo
277 d->ui.clockDefaultsTo->setEnabled(false);
281 void ClockApplet::changeEngineTimezone(const QString &oldTimezone, const QString &newTimezone)
283 // reimplemented by subclasses to get the new data
284 Q_UNUSED(oldTimezone);
285 Q_UNUSED(newTimezone);
288 bool ClockApplet::shouldDisplayTimezone() const
290 return d->forceTzDisplay;
293 void ClockApplet::wheelEvent(QGraphicsSceneWheelEvent *event)
295 if (d->selectedTimezones.count() < 1) {
296 return;
299 QString newTimezone;
301 if (isLocalTimezone()) {
302 if (event->delta() > 0) {
303 newTimezone = d->selectedTimezones.last();
304 } else {
305 newTimezone = d->selectedTimezones.first();
307 } else {
308 int current = d->selectedTimezones.indexOf(currentTimezone());
310 if (event->delta() > 0) {
311 int previous = current - 1;
312 if (previous < 0) {
313 newTimezone = localTimezoneUntranslated();
314 } else {
315 newTimezone = d->selectedTimezones.at(previous);
317 } else {
318 int next = current + 1;
319 if (next > d->selectedTimezones.count() - 1) {
320 newTimezone = localTimezoneUntranslated();
321 } else {
322 newTimezone = d->selectedTimezones.at(next);
327 changeEngineTimezone(currentTimezone(), newTimezone);
328 setCurrentTimezone(newTimezone);
330 update();
333 void ClockApplet::initExtenderItem(Plasma::ExtenderItem *item)
335 Plasma::Calendar *calendar = new Plasma::Calendar;
336 calendar->setMinimumSize(QSize(230, 220));
338 Plasma::DataEngine::Data data = dataEngine("time")->query(currentTimezone());
339 QDate date = data["Date"].toDate();
340 if (date.isValid()) {
341 calendar->setDate(date);
344 item->setWidget(calendar);
345 item->setTitle(i18n("Calendar"));
346 item->setIcon("view-pim-calendar");
349 void ClockApplet::init()
351 KConfigGroup cg = config();
352 d->selectedTimezones = cg.readEntry("timeZones", QStringList());
353 d->timezone = cg.readEntry("timezone", d->timezone);
354 d->defaultTimezone = cg.readEntry("defaultTimezone", d->timezone);
355 d->forceTzDisplay = d->timezone != d->defaultTimezone;
357 d->setPrettyTimezone();
358 Plasma::ToolTipManager::self()->registerWidget(this);
360 extender();
361 QTimer::singleShot(0, this, SLOT(createCalendar()));
364 void ClockApplet::popupEvent(bool show)
366 if (!show) {
367 return;
370 Plasma::ExtenderItem *item = extender()->item("calendar");
371 if (!item) {
372 return;
375 Plasma::Calendar *calendar = dynamic_cast<Plasma::Calendar *>(item->widget());
376 if (calendar){
377 Plasma::DataEngine::Data data = dataEngine("time")->query(currentTimezone());
378 QDate date = data["Date"].toDate();
379 if (date.isValid()) {
380 calendar->setDate(date);
385 void ClockApplet::setCurrentTimezone(const QString &tz)
387 if (d->timezone == tz) {
388 return;
391 if (tz == localTimezone()) {
392 // catch peple accidentally passing in the translation of "Local"
393 d->timezone = localTimezoneUntranslated();
394 } else {
395 d->timezone = tz;
398 d->forceTzDisplay = tz != d->defaultTimezone;
399 d->setPrettyTimezone();
401 KConfigGroup cg = config();
402 cg.writeEntry("timezone", tz);
403 emit configNeedsSaving();
406 QString ClockApplet::currentTimezone() const
408 return d->timezone;
411 QString ClockApplet::prettyTimezone() const
413 return d->prettyTimezone;
416 QStringList ClockApplet::getSelectedTimezones() const
418 return d->selectedTimezones;
421 bool ClockApplet::isLocalTimezone() const
423 return d->timezone == localTimezoneUntranslated();
426 QString ClockApplet::localTimezone()
428 return i18nc("Local time zone", "Local");
431 QString ClockApplet::localTimezoneUntranslated()
433 return "Local";
436 #include "clockapplet.moc"