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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/*
* kmoon - a moon phase indicator
* $Id$
* Copyright (C) 1998,2000 Stephan Kulow
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <qbitmap.h>
#include <qtooltip.h>
#include <qpainter.h>
#include <qpopupmenu.h>
#include <qcolor.h>
#include <dcopclient.h>
#include <kdebug.h>
#include <kapplication.h>
#include <kwin.h>
#include <kstartupinfo.h>
#include <kmessagebox.h>
#include <kaboutdata.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kcmdlineargs.h>
#include <kiconloader.h>
#include <kiconeffect.h>
#include <kconfig.h>
#include "kmoondlg.h"
#include "kmoonwidget.h"
#include <math.h>
extern double moonphasebylunation(int lun, int phi);
extern time_t JDtoDate(double jd, struct tm *event_date);
MoonWidget::MoonWidget(QWidget *parent, const char *name)
: QWidget(parent, name)
{
struct tm * t;
time_t clock;
counter = -1;
KConfig *config = KGlobal::config();
config->setGroup("General");
_angle = config->readNumEntry("Rotation", 0);
_north = config->readBoolEntry("Northern", true);
_mask = config->readBoolEntry("Mask", true);
old_angle = old_w = old_h = old_counter = -1;
old_north = false;
old_mask = false;
startTimer(1000 * 60 * 20);
time(&clock);
t = gmtime(&clock);
// kdDebug() << "time " << t->tm_isdst << " " << timezone << " " << daylight << endl ;
calcStatus(mktime(t));
}
MoonWidget::~MoonWidget()
{
}
void MoonWidget::calcStatus( time_t time )
{
uint lun = 0;
time_t last_new = 0;
time_t next_new = 0;
do {
double JDE = moonphasebylunation(lun, 0);
last_new = next_new;
next_new = JDtoDate(JDE, 0);
lun++;
} while (next_new < time);
lun -= 2;
QDateTime ln;
ln.setTime_t( last_new );
kdDebug() << KGlobal::locale()->formatDateTime( ln ) << endl;
time_t first_quarter = JDtoDate( moonphasebylunation( lun, 1 ), 0 );
QDateTime fq;
fq.setTime_t( first_quarter );
kdDebug() << KGlobal::locale()->formatDateTime( fq ) << endl;
time_t full_moon = JDtoDate( moonphasebylunation( lun, 2 ), 0 );
QDateTime fm;
fm.setTime_t( full_moon );
kdDebug() << KGlobal::locale()->formatDateTime( fm ) << endl;
time_t third_quarter = JDtoDate( moonphasebylunation( lun, 3 ), 0 );
QDateTime tq;
tq.setTime_t( third_quarter );
kdDebug() << KGlobal::locale()->formatDateTime( tq ) << endl;
QDateTime nn;
nn.setTime_t( next_new );
kdDebug() << KGlobal::locale()->formatDateTime( nn ) << endl;
QDateTime now;
now.setTime_t( time );
kdDebug() << KGlobal::locale()->formatDateTime( now ) << endl;
counter = ln.daysTo( now );
kdDebug() << "counter " << counter << " " << fm.daysTo( now ) << endl;
if ( fm.daysTo( now ) == 0 ) {
counter = 14;
tooltip = i18n( "Full Moon" );
return;
} else if ( counter <= 15 && counter >= 13 ) {
counter = 14 + fm.daysTo( now );
kdDebug() << "around full moon " << counter << endl;
}
int diff = fq.daysTo( now );
if ( diff == 0 )
counter = 7;
else if ( counter <= 8 && counter >= 6 ) {
counter = 7 + diff;
kdDebug() << "around first quarter " << counter << endl;
}
diff = ln.daysTo( now );
if ( diff == 0 )
counter = 0;
else if ( counter <= 1 || counter >= 28 )
{
counter = ( 29 + diff ) % 29;
diff = -nn.daysTo( now );
if ( diff == 0 )
counter = 0;
else if ( diff < 3 )
counter = 29 - diff;
kdDebug() << "around new " << counter << " " << diff << endl;
}
if ( tq.daysTo( now ) == 0 )
counter = 21;
else if ( counter <= 22 && counter >= 20 )
{
counter = 21 + tq.daysTo( now );
kdDebug() << "around third quarter " << counter << endl;
}
kdDebug() << "counter " << counter << endl;
assert (counter >= 0 && counter < 29);
switch (counter) {
case 0:
tooltip = i18n("New Moon");
return;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
tooltip = i18n("Waxing Crescent (New Moon was yesterday)", "Waxing Crescent (%n days since New Moon)", counter );
break;
case 7:
tooltip = i18n("First Quarter");
break;
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
tooltip = i18n( "Waxing Gibbous (Tomorrow is Full Moon)", "Waxing Gibbous (%n days to Full Moon)", -fm.daysTo( now ) );
break;
case 14:
assert( false );
break;
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
tooltip = i18n("Waning Gibbous (Yesterday was Full Moon)", "Waning Gibbous (%n days since Full Moon)", fm.daysTo( now ) );
break;
case 21:
tooltip = i18n("Last Quarter");
break;
case 22:
case 23:
case 24:
case 25:
case 26:
case 27:
case 28:
kdDebug() << "nn.days " << ln.daysTo( now ) << " " << nn.daysTo( now ) << endl;
tooltip = i18n("Waning Crescent (Tomorrow is New Moon)", "Waning Crescent (%n days to New Moon)", -nn.daysTo( now ) );
break;
default:
kdFatal() << "coolo can't count\n";
}
renderGraphic();
repaint();
}
QImage MoonWidget::loadMoon(int index)
{
if (index == 0) // the new moon has the wrong filename
index = 29;
QString filename = QString("kmoon/pics/moon%1.png").arg(index);
QString path = locate("data", filename);
if (path.isNull())
kdFatal() << "cound't find " << filename << ". Exiting.\n";
QImage image(path);
KIconEffect iconeffect;
image=iconeffect.apply(image, KIcon::Panel, KIcon::DefaultState);
return image;
}
void MoonWidget::setAngle(int value)
{
_angle = value;
renderGraphic();
repaint();
}
void MoonWidget::setNorthHemi(bool n)
{
_north = n;
renderGraphic();
repaint();
}
void MoonWidget::setMask(bool value)
{
_mask = value;
renderGraphic();
repaint();
}
void MoonWidget::paintEvent(QPaintEvent *)
{
bitBlt(this, 0, 0, &pixmap, 0, 0);
}
void MoonWidget::resizeEvent(QResizeEvent *)
{
renderGraphic();
repaint();
}
void MoonWidget::renderGraphic()
{
if (old_counter == counter && old_w == width() && old_h == height() &&
old_angle == _angle && old_north == _north)
return;
old_counter = counter;
old_w = width();
old_h = height();
old_north = _north;
QImage im = loadMoon(counter);
assert(!im.isNull());
im = im.convertDepth(32, 0);
assert(!im.isNull());
int mw = QMIN(width(), height());
QImage dest;
if (QPixmap::defaultDepth() > 8) {
if (fabs(_angle)!=0) { // nothing to rotate
//We expand the image 2x before rotating, rotate it, and then average out
//the pixel for better quality
int dmw = 2 * mw;
if (!pixmap.convertFromImage(im.smoothScale(dmw, dmw), 0)) {
return;
}
QWMatrix m;
m.rotate(_angle);
QPixmap rotated = pixmap.xForm(m);
//Copy the relevant part back to the pixmap
QRegion r(QRect(0, 0, dmw, dmw), QRegion::Ellipse);
QPainter p;
p.begin(&pixmap);
p.fillRect(0, 0, dmw, dmw, Qt::black);
p.setClipRegion(r);
p.drawPixmap(0, 0, rotated, (rotated.width() - dmw) / 2,
(rotated.height() - dmw) / 2,
dmw, dmw);
p.end();
//Shrink down to the proper size, averaging from the source
im = pixmap.convertToImage();
dest = im.copy(0, 0, mw, mw);
for (int y = 0; y < mw; y++) {
QRgb *destline = (QRgb*)dest.scanLine(y);
QRgb *sourceline1 = (QRgb*)im.scanLine(2*y);
QRgb *sourceline2 = (QRgb*)im.scanLine(2*y + 1);
for (int x = 0; x < mw; x++) {
int r = qRed(sourceline1[2*x]) + qRed(sourceline1[2*x+1]);
r = r + qRed(sourceline2[2*x]) + qRed(sourceline2[2*x+1]);
int g = qGreen(sourceline1[2*x]) + qGreen(sourceline1[2*x+1]);
g = g + qGreen(sourceline2[2*x]) + qGreen(sourceline2[2*x+1]);
int b = qBlue(sourceline1[2*x]) + qBlue(sourceline1[2*x+1]);
b = b + qBlue(sourceline2[2*x]) + qBlue(sourceline2[2*x+1]);
destline[x] = qRgb(qRound(r / 4), qRound(g / 4),
qRound(b / 4));
}
}
} else {
dest = im.smoothScale(mw, mw).convertDepth(32);
}
if (_mask) {
// generate alpha-channel
int dmw = mw*2;
QBitmap dMask(dmw, dmw);
QRegion r(QRect(0, 0, dmw, dmw), QRegion::Ellipse);
QPainter p;
p.begin(&dMask);
p.fillRect(0, 0, dmw, dmw, Qt::white);
p.setClipRegion(r);
p.fillRect(0, 0, dmw, dmw, Qt::black);
p.end();
QImage Mask2 = dMask.convertToImage().convertDepth(32).smoothScale(mw, mw);
dest.setAlphaBuffer(true);
for (int y = 0; y < mw; y++) {
QRgb *destline = (QRgb*)dest.scanLine(y);
QRgb *sourceline = (QRgb*)Mask2.scanLine(y);
for (int x = 0; x < mw; x++) {
destline[x] = (destline[x] & RGB_MASK)|(qRed(sourceline[x]) << 24);
}
}
}
} else {
dest = im.smoothScale(mw, mw);
}
if (!_north)
dest = dest.mirror(true, true);
if (!pixmap.convertFromImage(dest, 0)) {
return;
}
QToolTip::remove(this);
QToolTip::add(this, tooltip);
}
#include "kmoonwidget.moc"
|