forked from psemiletov/tea-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.cpp
312 lines (237 loc) · 8.09 KB
/
calendar.cpp
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
/*
this code is Public Domain
*/
#include <math.h>
#include <QDateTime>
#include <QDate>
#include <QPainter>
#include <QTextCharFormat>
#include <QDebug>
#include "utils.h"
#include "calendar.h"
inline int tropical_year (int year)
{
return 1 + (year % 19);
}
int moon_phase_leueshkanov (int year, int month, int day)
{
int L = tropical_year (year);
int phase = L * 11 - 14 + day + month + 3;
return phase % 30;
}
/*
moon phase - based on Ben Daglish JavaScript code from
based on http://www.ben-daglish.net/moon.shtml
*/
int moon_phase_trig2 (int year, int month, int day)
{
double n = floor (12.37 * (year - 1900 + ((1.0 * month - 0.5) / 12.0)));
double RAD = 3.14159265 / 180.0;
double t = n / 1236.85;
double t2 = t * t;
double as = 359.2242 + 29.105356 * n;
double am = 306.0253 + 385.816918 * n + 0.010730 * t2;
double xtra = 0.75933 + 1.53058868 * n + ((1.178e-4) - (1.55e-7) * t) * t2;
xtra += (0.1734 - 3.93e-4 * t) * sin (RAD * as) - 0.4068 * sin (RAD * am);
double i = (xtra > 0.0 ? floor (xtra) : ceil (xtra - 1.0));
QDate d (year, month, day);
int j1 = d.toJulianDay();
int jd = (2415020 + 28 * n) + i;
int r = (j1 - jd + 30) % 30;
if (r == 0)
r = 30;
return r;
}
/*This simply mods the difference between the date and a known
new moon date (1970-01-07) by the length of the lunar period.
For this reason, it is only valid from 1970 onwards.*/
int moon_phase_simple (int year, int month, int day)
{
int lp = 2551443;
QDateTime now (QDate (year, month - 1, day), QTime (20, 35, 0));
QDateTime new_moon (QDate (1970, 0, 7), QTime (20, 35, 0));
double phase = ((now.toMSecsSinceEpoch() - new_moon.toMSecsSinceEpoch()) / 1000) % lp;
return floor (phase / (24 * 3600)) + 1;
}
/*
Conway
This is based on a 'do it in your head' algorithm by John Conway.
In its current form, it's only valid for the 20th and 21st centuries,
but I'm sure John's got refinements. :)
*/
int moon_phase_conway (int year, int month, int day)
{
int r = year % 100;
r %= 19;
if (r > 9)
r -= 19;
r = ((r * 11) % 30) + month + day;
if (month < 3)
r += 2;
r -= ((year < 2000) ? 4 : 8.3);
r = (int) floor (r + 0.5) % 30;
return (r < 0) ? r + 30 : r;
}
/*Trig1
This is based on some Basic code by Roger W. Sinnot from Sky & Telescope magazine, March 1985.
I don't pretend to understand it - something to do with a first-order approximation
to the 'real' calculation of the position of the bodies involved - which I'm still working on... :)
*/
inline double GetFrac (double fr)
{
return (fr - floor (fr));
}
int moon_phase_trig1 (int year, int month, int day)
{
QDate d (year, month, day);
int thisJD = d.toJulianDay();
double degToRad = 3.14159265 / 180;
double K0, T, T2, T3, J0, F0, M0, M1, B1;
int oldJ = 0;
K0 = floor ((year - 1900) * 12.3685);
T = (year - 1899.5) / 100;
T2 = T * T;
T3 = T * T * T;
J0 = 2415020 + 29*K0;
F0 = 0.0001178 * T2 - 0.000000155 * T3 + (0.75933 + 0.53058868 * K0) - (0.000837 * T + 0.000335 * T2);
M0 = 360 * (GetFrac (K0 * 0.08084821133)) + 359.2242 - 0.0000333 *T2 - 0.00000347 * T3;
M1 = 360 * (GetFrac (K0 * 0.07171366128)) + 306.0253 + 0.0107306 * T2 + 0.00001236 * T3;
B1 = 360 * (GetFrac (K0 * 0.08519585128)) + 21.2964 - (0.0016528 * T2) - (0.00000239 * T3);
int phase = 0;
int jday = 0;
while (jday < thisJD)
{
double F = F0 + 1.530588 * phase;
double M5 = (M0 + phase * 29.10535608) * degToRad;
double M6 = (M1 + phase * 385.81691806) * degToRad;
double B6 = (B1 + phase * 390.67050646) * degToRad;
F -= 0.4068 * sin (M6) + (0.1734 - 0.000393 * T) * sin (M5);
F += 0.0161 * sin (2 * M6) + 0.0104 * sin (2 * B6);
F -= 0.0074 * sin (M5 - M6) - 0.0051 * sin (M5 + M6);
F += 0.0021 * sin (2 * M5) + 0.0010 * sin (2 * B6 - M6);
F += 0.5 / 1440;
oldJ = jday;
jday = J0 + 28 * phase + floor(F);
phase++;
}
return (thisJD - oldJ) % 30;
}
int moon_phase_by_algo (int v, int year, int month, int day)
{
int r = 0;
switch (v)
{
case MOON_PHASE_TRIG2:
r = moon_phase_trig2 (year, month, day);
break;
case MOON_PHASE_TRIG1:
r = moon_phase_trig1 (year, month, day);
break;
case MOON_PHASE_CONWAY:
r = moon_phase_conway (year, month, day);
break;
case MOON_PHASE_LEUESHKANOV:
r = moon_phase_leueshkanov (year, month, day);
break;
case MOON_PHASE_SIMPLE:
r = moon_phase_simple (year, month, day);
break;
};
return r;
}
CCalendarWidget::CCalendarWidget (QWidget *parent, const QString &a_dir_days): QCalendarWidget (parent)
{
dir_days = a_dir_days;
moon_phase_algo = MOON_PHASE_TRIG2;
moon_mode = false;
if (! moon_tiles.load (":/images/moon-phases.png"))
qDebug() << ":/images/moon-phases.png" << " is not loaded";
northern_hemisphere = true;
//setHeaderTextFormat (const QTextCharFormat & format);
QTextCharFormat tformat;
tformat.setForeground (QBrush (Qt::white));
tformat.setBackground (QBrush (Qt::black));
tformat.setFontWeight (QFont::Bold);
setWeekdayTextFormat (Qt::Monday, tformat);
setWeekdayTextFormat (Qt::Tuesday, tformat);
setWeekdayTextFormat (Qt::Wednesday, tformat);
setWeekdayTextFormat (Qt::Thursday, tformat);
setWeekdayTextFormat (Qt::Friday, tformat);
setWeekdayTextFormat (Qt::Saturday, tformat);
setWeekdayTextFormat (Qt::Sunday, tformat);
}
#if QT_VERSION < 0x060000
void CCalendarWidget::paintCell (QPainter *painter, const QRect &rect, const QDate &date) const
#else
void CCalendarWidget::paintCell (QPainter *painter, const QRect &rect, QDate date) const
#endif
{
QSize fsize = fontMetrics().size (Qt::TextSingleLine, "A");
if (moon_mode)
{
int moon_day = moon_phase_by_algo (moon_phase_algo, date.year(), date.month(), date.day());
bool has_image = true;
if (moon_day == 0 || moon_day == 30 || moon_day == 1)
has_image = false;
//вычисляем ряд и колонку
int cursorOffset = moon_day;
/*
int off = 0;
int row = 0;
while (cursorOffset >= (off + 8))
{
off += 7;
row++;
}
int col = cursorOffset - off;
*/
int row = moon_day / 7;
if ((moon_day % 7 == 0) && (row != 0))
row--;
int col = cursorOffset - (row * 7);
/*
qDebug() << "moon day = " << moon_day;
qDebug() << "moon_day / 7 = " << (double) moon_day / 7;
qDebug() << "trow = " << trow;
qDebug() << "row = " << row;
qDebug() << "col = " << col;
qDebug() << "tcol = " << tcol;
*/
//вычисляем, откуда копировать
int pad = 3;
int x = (col - 1) * 73 + (pad * col) - pad;
int y = row * 73 + (pad * row);
QRect r (x, y, 66, 73);
QImage tile = moon_tiles.copy (r);
QColor bg_color (Qt::black);
painter->fillRect (rect, bg_color);
if (has_image)
{
if (northern_hemisphere)
painter->drawImage (rect.x(), rect.y(), tile);
else
painter->drawImage (rect.x(), rect.y(), tile.mirrored (true, false));
}
painter->setPen (QPen (Qt::yellow));
QTextCharFormat tcf = dateTextFormat (date);
if (tcf.fontStrikeOut())
painter->setPen (QPen (Qt::magenta));
else
if (tcf.fontUnderline())
painter->setPen (QPen (Qt::red));
painter->drawText (QPoint (rect.x() + 5, rect.y() + fsize.height()), date.toString("dd") + " / " + QString::number (moon_day));
if (selectedDate() == date)
{
QPen dpen (Qt::yellow);
dpen.setWidth (5);
painter->setPen (dpen);
painter->drawRect (rect);
}
}
else
QCalendarWidget::paintCell (painter, rect, date);
}
void CCalendarWidget::do_update()
{
updateCells();
}