add more spacing
[personal-kdebase.git] / workspace / plasma / scriptengines / ruby / examples / applets / clock / contents / code / clockapplet.rb
blobc65020132454347781b87354cd94c6f935df27f3
1 =begin
2 /***************************************************************************
3  *   Copyright (C) 2007-2008 by Riccardo Iaconelli <riccardo@kde.org>      *
4  *   Copyright (C) 2007-2008 by Sebastian Kuegler <sebas@kde.org>          *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
20  ***************************************************************************/
21 =end
23 class ClockApplet < PlasmaScripting::Applet
24   attr_accessor :currentTimezone
26   slots :configAccepted, 
27         'showCalendar(QGraphicsSceneMouseEvent *)',
28         'currentTimezone=(QString)'
30   def initialize(parent, args)
31     super(parent, args)
32     @calendar = nil
33     @currentTimezone = "Local"
35     @calendarUi = Ui::Calendar.new
36     @timezonesUi = Ui::TimezonesConfig.new
37     @clicked = Qt::Point.new
38     @timeZones = []
39   end
41   def updateToolTipContent()
42   end
44   def createConfigurationInterface(parent)
45     createClockConfigurationInterface(parent)
47     widget = Qt::Widget.new
48     @timezonesUi.setupUi(widget)
50     parent.addPage(widget, KDE.i18n("Time Zones"), icon())
52     @timezonesUi.localTimeZone.checked = localTimezone?
53     @timezonesUi.timeZones.setSelected(@currentTimezone, true)
54     @timezonesUi.timeZones.enabled = !localTimezone?
56     parent.buttons = KDE::Dialog::Ok | KDE::Dialog::Cancel | KDE::Dialog::Apply
57     connect(parent, SIGNAL(:applyClicked), self, SLOT(:configAccepted))
58     connect(parent, SIGNAL(:okClicked), self, SLOT(:configAccepted))
59   end
61   def createClockConfigurationInterface(parent)
62   end
64   def clockConfigAccepted()
65   end
67   def configAccepted()
68     cg = config()
70     @timeZones = @timezonesUi.timeZones.selection()
71     cg.writeEntry("timeZones", @timeZones)
73     newTimezone = localTimezone()
75     if !@timezonesUi.localTimeZone.checked? && !@timeZones.empty?
76         newTimezone = @timeZones[0]
77     end
79     changeEngineTimezone(@currentTimezone, newTimezone)
81     @currentTimezone = newTimezone
82     cg.writeEntry("currentTimezone", newTimezone)
84     clockConfigAccepted()
86     constraintsEvent(Plasma::SizeConstraint)
87     update()
88     emit configNeedsSaving
89   end
91   def changeEngineTimezone(oldTimezone, newTimezone)
92   end
94   def mousePressEvent(event)
95     if event.buttons == Qt::LeftButton
96       @clicked = scenePos.toPoint
97       event.accepted = true
98         return
99     end
101     super(event)
102   end
104   def mouseReleaseEvent(event)
105     if (@clicked - scenePos.toPoint).manhattanLength <
106         KDE::GlobalSettings.dndEventDelay()
107         showCalendar(event)
108     end
109   end
111   def showCalendar(event)
112     if @calendar == 0
113       @calendar = Plasma::Dialog.new
114       @calendarUi.setupUi(@calendar)
115       @calendar.setWindowFlags(Qt::Popup)
116       @calendar.adjustSize
117     end
119     if @calendar.visible?
120       @calendar.hide
121     else
122       data = dataEngine("time").query(@currentTimezone)
123       @calendarUi.kdatepicker.date = data["Date"].toDate
124       @calendar.move(popupPosition(@calendar.sizeHint))
125       @calendar.show
126     end
127   end
129   def localTimezone?
130     return @currentTimezone == localTimezone()
131   end
133   def localTimezone()
134     return "Local"
135   end