This repository has been archived by the owner on Apr 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luarccalendar.cpp
324 lines (286 loc) · 10.2 KB
/
luarccalendar.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
313
314
315
316
317
318
319
320
321
322
323
324
// ***************************************************************************
// * LICENSE : GPLv3 (GNU GENERAL PUBLIC LICENSE Version 3)
// * Copyright (C) 2018 Sven Augustus
// * email: svenaugustus@outlook.com
// ***************************************************************************
#include "luarccalendar.h"
LuarCCalendar::LuarCCalendar(QWidget *parent) :
QWidget(parent)//,
//ui(new Ui::LuarCCalendar)
{
//ui->setupUi(this);
setupUi(this);
ui_fix_width = 115;
icon = QIcon(":/img/icon.png");
icon64 = QIcon(":/img/chinesecalendar-64.png");
setTime();
QTimer *timer = new QTimer(this);
timer->setInterval(1000);
connect(timer,SIGNAL(timeout()),this,SLOT(setTime()));
timer->start();
mycalendar=new MyCTCalendar;
mycalendar->InitConnection(QDir::currentPath()+"/database"); // sqlite
data.clear();
today();
this->trayIcon = new QSystemTrayIcon(this);
this->trayIcon->setIcon(icon);
connect(this->trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconCLicked(QSystemTrayIcon::ActivationReason)));
this->trayIcon->show();
}
LuarCCalendar::~LuarCCalendar()
{
delete mycalendar;
//delete ui;
}
//***************************************************************************
// *函数:void loadCalendar()
// *功能:在 ccalendar 中插入日期数据
// *参数:
// *返回值:
// ***************************************************************************/
void LuarCCalendar::loadCalendar()
{
data.clear();
struct CTCalendar c;
ccalendar->clear();
ccalendar->setRowCount(2);
ccalendar->setColumnCount(7);
ccalendar->horizontalHeader()->hide();
ccalendar->verticalHeader()->hide();
qDebug()<<"loadCalendar,width:"<<width();
for(int i=0;i<7;i++)
ccalendar->setColumnWidth(i,(width()- ui_fix_width)/7);
QDate date(selectedDate.year(), selectedDate.month(), 1);
qDebug()<<"loadCalendar,year:"<<selectedDate.year()<<",month:"<<selectedDate.month();
//写入星期
for (int weekDay = 1; weekDay <= 7; ++weekDay) {
ccalendar->setItem(0,weekDay-1,new QTableWidgetItem(QDate::shortDayName(weekDay)));
if(weekDay<=5)
ccalendar->item(0,weekDay-1)->setBackgroundColor(Qt::cyan);
else
ccalendar->item(0,weekDay-1)->setBackgroundColor(Qt::red);
}
//加入日期
while (date.month() == selectedDate.month()) {
int weekDay = date.dayOfWeek();
mycalendar->ctcl_solar_to_lunar(selectedDate.year(),selectedDate.month(),date.day(),&c);//获取农历信息
QString mycday;
mycday=c.cday;
if(c.sterm.at(0) >=QChar('0') && c.sterm.at(0) <= QChar('9'))
mycday=c.cday;
else
{
if(c.laststerm)
mycday=c.cday;
else
mycday=c.sterm;
}
QTableWidgetItem* item=new QTableWidgetItem(QString("%1\n%2").arg(date.day()).arg(mycday));
ccalendar->setItem(ccalendar->rowCount()-1, weekDay-1 , item);
QString CuText0=c.ganzhi+tr("year(")+c.shengxiao+")";// 农历年
QString CuText1=c.cmonth+c.cday; // 农历日期
QString CuText2=QString::number(selectedDate.year())+"-"
+QString::number(selectedDate.month())+"-"
+QString::number(date.day());
QString zodiac=c.zodiac; // 星座
QString holiday=c.holiday; // 节假日
QString choliday=c.choliday; // 节假日
QString CuText=tr("NongLi year");
CuText.append(CuText0);
CuText.append("\n"+tr("NongLi Day:")+CuText1);
CuText.append("\n"+tr("today constellation:")+zodiac);
CuText.append("\n"+tr("forthcoming sterm:")+c.sterm);
if(!c.holiday.isNull())
CuText.append("\n"+tr("holiday:")+c.holiday);
if(!c.choliday.isNull())
CuText.append("\n"+tr("choliday:")+c.choliday);
item->setToolTip(CuText);
QString key=QString::number(item->row())+QString::number(item->column());
data[key] = LunarCCDateTime(CuText2, QString::number(date.day()),CuText0,CuText1,zodiac,holiday,choliday);
date = date.addDays(1);
if (weekDay == 7 && date.month() == selectedDate.month())
ccalendar-> insertRow(ccalendar-> rowCount());
}
QDate today = QDate::currentDate();
if(today.year() == selectedDate.year() && today.month() == selectedDate.month())
{
QDate curMonth = QDate(selectedDate.year(),selectedDate.month(),1);
int week = curMonth.dayOfWeek();
// qDebug()<<"week:"<< week;
int curweek = today.dayOfWeek();
int curday = today.day();
int x = ( curday+(1-week)*(-1)) % 7;
int y =( curday+(1-week)*(-1)) /7 ;
if( x != 0)
y=y+1;
QTableWidgetItem* currentItem=ccalendar-> item(y,curweek-1);
currentItem-> setBackgroundColor(Qt::red);
showDayInfo(currentItem);
// QString toolTip = currentItem->toolTip();
// systemtray->setToolTip(toolTip);
}
ccalendar->resizeRowsToContents();
connect(ccalendar,SIGNAL(itemClicked(QTableWidgetItem*)),this,SLOT(showDayInfo(QTableWidgetItem*)));
}
//***************************************************************************
// *函数:void showDayInfo()
// *功能:设置日期信息
// *参数: item
// *返回值:
// ***************************************************************************/
void LuarCCalendar::showDayInfo(QTableWidgetItem* item)
{
QString key=QString::number(item->row())+QString::number(item->column());
if(data.contains(key))
{
LunarCCDateTime dateytime = data.value(key);
label_dayFull->setText(dateytime.getDayFull());
label_day->setText(dateytime.getDay());
label_noliday->setText(tr("NongLi") + dateytime.getNoliDay());
label_noliyear->setText(dateytime.getNoliYear());
label_constellation->setText(dateytime.getConstellation());
QString holidays = dateytime.getHoliday();
if(!dateytime.getCholiday().isNull())holidays.append("\n"+dateytime.getCholiday());
label_holidays->setText(holidays);
}
}
//***************************************************************************
// *函数:void today();
// *功能:设置今天
// *参数:
// *返回值:
// ***************************************************************************/
void LuarCCalendar::today(){
selectedDate = QDate::currentDate();
setYear(selectedDate.year());
setMonth(selectedDate.month() - 1);
loadCalendar();
}
//***************************************************************************
// *函数:void setTime()
// *功能:设置当前时间
// *参数:
// *返回值:
// ***************************************************************************/
void LuarCCalendar::setTime()
{
QDateTime dateTime = QDateTime::currentDateTime();
label_time->setText(dateTime.toString("hh:mm:ss"));
}
//***************************************************************************
// *函数:void setYear(int yearIndex)
// *功能:设置年
// *参数:int yearIndex
// *返回值:
// ***************************************************************************/
void LuarCCalendar::setYear(int yearIndex)
{
selectedDate = QDate(yearIndex, selectedDate.month(), selectedDate.day());
t_yearIndex = yearIndex;
label_year->setText(QString::number(t_yearIndex)); // change the text.
}
//***************************************************************************
// *函数:void setMonth(int monthIndex);
// *功能:设置月
// *参数:int monthIndex
// *返回值:
// ***************************************************************************/
void LuarCCalendar::setMonth(int monthIndex)
{
if(monthIndex < 0)
{
monthIndex = 0;
}
if(monthIndex > 11)
{
monthIndex = 11;
}
int day=0;
int month = monthIndex + 1;
//qDebug()<<"month:"<<month;
if(month ==1 || month ==3 || month == 5 || month == 7 || month == 8 || month == 10 || month ==12)
{
day =31;
}
else if(month ==4 || month==6 || month ==9 || month ==11)
{
day =30;
}
if(selectedDate.year()%4 ==0 && selectedDate.year()%100!=0)
{
day = 29;
}
else if(selectedDate.year()%400 ==0)
{
day = 29;
}
else
{
day = 28;
}
selectedDate = QDate(selectedDate.year(), month, day);
t_monthIndex = monthIndex;
//label_month->setText(QDate::shortMonthName(t_monthIndex + 1));
label_month->setText(QString::number(t_monthIndex + 1));// change the text.
}
//***************************************************************************
// *函数:void trayIconCLicked(QSystemTrayIcon::ActivationReason reason);
// *功能:系统图标点击事件
// *参数:QSystemTrayIcon::ActivationReason reason
// *返回值:
// ***************************************************************************/
void LuarCCalendar::trayIconCLicked(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
case QSystemTrayIcon::Trigger:
if(this->isHidden())
{
this->show();
}else
{
this->hide();
}
break;
default:
;
}
}
void LuarCCalendar::on_pushButton_preYear_clicked()
{
//qDebug()<<"on_pushButton_preYear_clicked";
if(t_monthIndex == 0 )
{
t_monthIndex = 11;
setMonth(t_monthIndex);
}
setYear(t_yearIndex - 1);
loadCalendar();
}
void LuarCCalendar::on_pushButton_nextYear_clicked()
{
//qDebug()<<"on_pushButton_nextYear_clicked";
if(t_monthIndex==11)
{
t_monthIndex = 0;
setMonth(t_monthIndex);
}
setYear(t_yearIndex + 1);
loadCalendar();
}
void LuarCCalendar::on_pushButton_preMonth_clicked()
{
//qDebug()<<"on_pushButton_preMonth_clicked";
setMonth(t_monthIndex - 1);
loadCalendar();
}
void LuarCCalendar::on_pushButton_nextMonth_clicked()
{
//qDebug()<<"on_pushButton_nextMonth_clicked";
setMonth(t_monthIndex + 1);
loadCalendar();
}
void LuarCCalendar:: on_pushButton_today_clicked(){
//qDebug()<<"on_pushButton_today_clicked";
today();
}