Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
Copyright (C) 2011, 2012, 2013 Glad Deschrijver <glad.deschrijver@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 1.1
import org.kde.plasma.components 0.1 as PlasmaComponents
import org.kde.qtextracomponents 0.1
import "plasmapackage:/code/phases.js" as Phases
import "plasmapackage:/code/lunacalc.js" as LunaCalc
Item {
id: lunaWidget
// width: labelButtonColumn.width + 20 // dirty hack to have 10px margins around the contents
width: maximumWidth
// height: labelButtonColumn.height + 20 // idem
height: maximumHeight // dirty hack: since the highlight of a PlasmaComponents.ToolButton is wider than the button itself, we must calculate the maximum height in order to avoid resizing the dialog each time a button is pressed
focus: true // needs to be focused, otherwise the key events aren't caught
property variant dateFormat: Qt.DefaultLocaleLongDate;
property int maximumWidth: 0
property int maximumHeight: 0
Component.onCompleted: {
plasmoid.addEventListener("ConfigChanged", configChanged);
configChanged();
showTodayPhases();
}
function configChanged() {
setDateFormat(plasmoid.readConfig("dateFormat"), plasmoid.readConfig("dateFormatString"));
}
function update() {
showTodayPhases();
}
Keys.onUpPressed: showPreviousPhases()
Keys.onLeftPressed: showPreviousPhases()
Keys.onRightPressed: showNextPhases()
Keys.onDownPressed: showNextPhases()
Keys.onPressed: {
if (event.key == Qt.Key_Home)
showTodayPhases();
else if (event.key == Qt.Key_PageUp)
showPreviousPhases();
else if (event.key == Qt.Key_PageDown)
showNextPhases();
}
function setDateFormat(format, formatString) {
if (format == 0)
lunaWidget.dateFormat = Qt.TextDate;
else if (format == 1)
lunaWidget.dateFormat = Qt.DefaultLocaleShortDate;
else if (format == 2)
lunaWidget.dateFormat = Qt.DefaultLocaleLongDate;
else if (format == 3)
lunaWidget.dateFormat = Qt.ISODate;
else
lunaWidget.dateFormat = formatString;
showPhases(LunaCalc.reloadPhases());
}
function showPhases(phases) {
lastNewText.text = Qt.formatDateTime(phases[0], dateFormat);
firstQuarterText.text = Qt.formatDateTime(phases[1], dateFormat);
fullMoonText.text = Qt.formatDateTime(phases[2], dateFormat);
thirdQuarterText.text = Qt.formatDateTime(phases[3], dateFormat);
nextNewText.text = Qt.formatDateTime(phases[4], dateFormat);
lunaWidget.focus = true;
maximumWidth = Math.max(maximumWidth, labelButtonColumn.width + 20);
}
function showTodayPhases() {
showPhases(LunaCalc.getTodayPhases());
}
function showPreviousPhases() {
showPhases(LunaCalc.getPreviousPhases());
}
function showNextPhases() {
showPhases(LunaCalc.getNextPhases());
}
MouseEventListener {
anchors.fill: parent
onWheelMoved: {
if (wheel.delta > 0)
showNextPhases();
else
showPreviousPhases();
}
}
Column {
id: labelButtonColumn
// anchors.centerIn: lunaWidget
anchors.top: lunaWidget.top
anchors.topMargin: 10
anchors.horizontalCenter: lunaWidget.horizontalCenter
spacing: 10
onHeightChanged: maximumHeight = Math.max(maximumHeight, height + 20);
Grid {
id: labelArea
columns: 2
flow: Grid.LeftToRight
PlasmaComponents.Label {
id: lastNewLabel
text: i18n("Last new:") + " "
}
PlasmaComponents.Label {
id: lastNewText
}
PlasmaComponents.Label {
id: firstQuarterLabel
text: i18n("First quarter:") + " "
}
PlasmaComponents.Label {
id: firstQuarterText
}
PlasmaComponents.Label {
id: fullMoonLabel
text: i18n("Full moon:") + " "
}
PlasmaComponents.Label {
id: fullMoonText
}
PlasmaComponents.Label {
id: thirdQuarterLabel
text: i18n("Third quarter:") + " "
}
PlasmaComponents.Label {
id: thirdQuarterText
}
PlasmaComponents.Label {
id: nextNewLabel
text: i18n("Next new:") + " "
}
PlasmaComponents.Label {
id: nextNewText
}
}
Row {
id: buttonRow
anchors.horizontalCenter: parent.horizontalCenter
PlasmaComponents.ToolButton {
id: previousButton
iconSource: "go-previous"
onClicked: showPreviousPhases();
}
PlasmaComponents.ToolButton {
id: todayButton
iconSource: "go-jump-today"
onClicked: showTodayPhases();
}
PlasmaComponents.ToolButton {
id: nextButton
iconSource: "go-next"
onClicked: showNextPhases();
}
}
}
}