农历日历 v1.0.4
载入中...
搜索中...
未找到
MainWindow.cpp
1// 作者:康林 <kl222@126.com>
2
3#include "MainWindow.h"
4#include "ui_MainWindow.h"
5#include <QLoggingCategory>
6#include <QDate>
7#include <QIcon>
8#include <QDir>
9#include <QApplication>
10#include <QActionGroup>
11#ifdef RABBITCOMMON
12#include "DlgAbout.h"
13#include "FrmUpdater.h"
14#include "RabbitCommonTools.h"
15#endif
16
17static Q_LOGGING_CATEGORY(Logger, "App.MainWindow")
18
19
20uint CHandler::onHandle(/*in*/const QDate& d,
21 /*out*/QStringList& tasks) {
22
23 // 纪念日(以年为周期)
24 if(10 == d.month() && 25 == d.day())
25 {
26 tasks << "抗美援朝纪念日";
27 return 0;
28 }
29
30 int lunarYear = 0;
31 int lunarMonth = 0;
32 int lunarDay = 0;
33 CLunarCalendar::GetLunar(d, lunarYear, lunarMonth, lunarDay);
34 // 纪念日(以农历年为周期)
35 if(8 == lunarMonth && 22 == lunarDay)
36 {
37 tasks << "结婚纪念日";
38 return 0;
39 }
40
41 // 以月为周期
42 if(21 == d.day())
43 {
44 // 表示只显示圆点,不显示内容
45 return 1;
46 }
47
48 // 以周为周期
49 if(Qt::Wednesday == d.dayOfWeek())
50 {
51 // 表示只显示圆点,不显示内容。建议用上面的方式
52 tasks << QString();
53 return 0;
54 }
55
56 // 单个任务
57 if(2015 == d.year() && 10 == d.month() && 22 == d.day())
58 {
59 // 表示只显示圆点,不显示内容
60 return 1;
61 }
62
63 // 没有任务
64 return 0;
65}
67
68MainWindow::MainWindow(QWidget *parent) :
69 QMainWindow(parent),
70 ui(new Ui::MainWindow)
71{
72 ui->setupUi(this);
73
75 m_pLunarCalendar = new CLunarCalendar(this);
76 if(!m_pLunarCalendar)
77 throw std::bad_alloc();
79
80 QMenu* pViewMenu = menuBar()->addMenu(tr("View"));
81 QAction* pAction = pViewMenu->addAction(tr("Lunar"),
82 this, SLOT(slotActionLunar(bool)));
83 pAction->setCheckable(true);
84 pAction->setChecked(true);
85
86 pAction = pViewMenu->addAction(tr("Solar"),
87 this, SLOT(slotActionSolar(bool)));
88 pAction->setCheckable(true);
89 pAction->setChecked(true);
90 pViewMenu->addSeparator();
91
92 QActionGroup* pViewTypeGroup = new QActionGroup(this);
93 QAction* pViewMonth = pViewMenu->addAction(tr("Month"),
94 this, SLOT(slotViewMonth()));
95 pViewMonth->setCheckable(true);
96 pViewMonth->setChecked(true);
97 pViewTypeGroup->addAction(pViewMonth);
98 QAction* pViewWeek = pViewMenu->addAction(tr("Week"),
99 this, SLOT(slotViewWeek()));
100 pViewWeek->setCheckable(true);
101 pViewTypeGroup->addAction(pViewWeek);
102 pViewMenu->addSeparator();
103
105 pAction = pViewMenu->addAction(tr("Show Head"),
106 m_pLunarCalendar, SLOT(ShowHead(bool)));
107 pAction->setCheckable(true);
108 pAction->setChecked(true);
109 pAction = pViewMenu->addAction(tr("Show Tools"),
110 m_pLunarCalendar, SLOT(ShowTools(bool)));
111 pAction->setCheckable(true);
112 pAction->setChecked(true);
113 pAction = pViewMenu->addAction(tr("Show today"),
114 m_pLunarCalendar, SLOT(ShowToday(bool)));
115 pAction->setCheckable(true);
116 pAction->setChecked(true);
117 pAction = pViewMenu->addAction(tr("Show date"),
118 m_pLunarCalendar, SLOT(ShowDate(bool)));
119 pAction->setCheckable(true);
120 pAction->setChecked(true);
121 pAction = pViewMenu->addAction(tr("Show time"),
122 m_pLunarCalendar, SLOT(ShowTime(bool)));
123 pAction->setCheckable(true);
124 pAction->setChecked(true);
125 pAction = pViewMenu->addAction(tr("Show week head"),
126 m_pLunarCalendar, SLOT(ShowWeekHead(bool)));
127 pAction->setCheckable(true);
128 pAction->setChecked(true);
129 pAction = pViewMenu->addAction(tr("Show weeks"),
130 m_pLunarCalendar, SLOT(ShowWeeks(bool)));
131 pAction->setCheckable(true);
132 pAction->setChecked(true);
133 pAction = pViewMenu->addAction(tr("Show grid"),
134 m_pLunarCalendar, SLOT(ShowGrid(bool)));
135 pAction->setCheckable(true);
136 pAction->setChecked(false);
137 pAction = pViewMenu->addAction(tr("Show tooltip"),
138 m_pLunarCalendar, SLOT(EnableToolTip(bool)));
139 pAction->setCheckable(true);
140 pAction->setChecked(true);
141 pAction = pViewMenu->addAction(tr("Use backgroup image"),
142 m_pLunarCalendar, SLOT(ShowBackgroupImage(bool)));
143 pAction->setCheckable(true);
144 pAction->setChecked(false);
145 m_pLunarCalendar->ShowBackgroupImage(false);
146
147 QActionGroup* pViewHeadposition = new QActionGroup(this);
148 QMenu* pViewHeadpositionMenu = pViewMenu->addMenu(tr("Head position"));
149 QAction* pHeadNot = pViewHeadpositionMenu->addAction(tr("Not"),
150 this, SLOT(slotHeadpositionNot()));
151 pHeadNot->setCheckable(true);
152 QAction* pHeadTop = pViewHeadpositionMenu->addAction(tr("Top"),
153 this, SLOT(slotHeadpositionTop()));
154 pHeadTop->setCheckable(true);
155 pHeadTop->setChecked(true);
156 QAction* pHeadDown = pViewHeadpositionMenu->addAction(tr("Down"),
157 this, SLOT(slotHeadpositionDown()));
158 pHeadDown->setCheckable(true);
159 QAction* pHeadLeft = pViewHeadpositionMenu->addAction(tr("Left"),
160 this, SLOT(slotHeadpositionLeft()));
161 pHeadLeft->setCheckable(true);
162 QAction* pHeadRight = pViewHeadpositionMenu->addAction(tr("Right"),
163 this, SLOT(slotHeadpositionRight()));
164 pHeadRight->setCheckable(true);
165 pViewHeadposition->addAction(pHeadNot);
166 pViewHeadposition->addAction(pHeadTop);
167 pViewHeadposition->addAction(pHeadDown);
168 pViewHeadposition->addAction(pHeadLeft);
169 pViewHeadposition->addAction(pHeadRight);
171
173 pViewMenu->addAction(tr("Clear holidays"), m_pLunarCalendar, SLOT(ClearHolidays()));
174 pAction = pViewMenu->addAction(tr("Enable holidays"), m_pLunarCalendar, SLOT(EnableHolidays(bool)));
175 pAction->setChecked(true);
176 pAction->setCheckable(true);
177 pAction = pViewMenu->addAction(tr("Enable solar term"), m_pLunarCalendar, SLOT(EnableSolarTerm(bool)));
178 pAction->setChecked(true);
179 pAction->setCheckable(true);
181
182 pViewMenu->addSeparator();
183 pViewMenu->addAction(RabbitCommon::CTools::Instance()->AddStyleMenu(pViewMenu));
184 pViewMenu->addAction(tr("Genetate cache table"), this, SLOT(slotGenerateCalendarTable()));
185
186#ifdef RABBITCOMMON
187 QMenu* pHelp = menuBar()->addMenu(tr("Help"));
188 CFrmUpdater updater;
189 pHelp->addAction(updater.windowIcon(), tr("Update"),
190 this, SLOT(slotUpdate()));
191 pHelp->addAction(windowIcon(), tr("About"), this, SLOT(slotAbout()));
192 pHelp->addMenu(RabbitCommon::CTools::Instance()->GetLogMenu(this));
193#endif
194
195 //m_pLunarCalendar->setLocale(QLocale("zh_CN"));
196 m_pLunarCalendar->SetSelectedDate(QDate(2015, 10, 8));
197
199 m_Hnadler = QSharedPointer<CHandler>(new CHandler());
202 m_pLunarCalendar->SetTaskHandle(m_Hnadler);
204
205 //*! [User defined tasks]
206 m_pLunarCalendar->SetTaskHandle([](const QDate& d,
207 QStringList& tasks)->uint {
208 int lunarYear = 0;
209 int lunarMonth = 0;
210 int lunarDay = 0;
211 CLunarCalendar::GetLunar(d, lunarYear, lunarMonth, lunarDay);
212
213 // 节日(以年为周期)
214 if(d.month() == 10 && d.day() == 26)
215 {
216 tasks << "环卫工人节";
217 return 0;
218 }
219
220 // 农历节日(以农历年为周期)
221 if(9 == lunarMonth && 9 == lunarDay)
222 {
223 tasks << "老人节" << "老年节" << "登高节" << "踏秋";
224 return 0;
225 }
226
227 // 纪念日(以年为周期)
228 if(d.day() == 10 && d.month() == 10)
229 {
230 // 表示显示圆点,也显示内容
231 tasks << "辛亥革命纪念日";
232 return 0;
233 }
234
235 // 生日(纪念日:以农历年为周期)
236 if(8 == lunarMonth && 23 == lunarDay)
237 {
238 tasks << "爸生日";
239 return 0;
240 }
241
242 // 以月为周期
243 if(d.day() == 11)
244 {
245 // 表示只显示圆点,不显示内容
246 return 1;
247 }
248
249 // 以周为周期
250 if(Qt::Monday == d.dayOfWeek())
251 {
252 // 表示只显示圆点,不显示内容。建议用上面的方式
253 tasks << QString();
254 return 0;
255 }
256
257 // 单个任务
258 if(d.day() == 12 && d.month() == 10 && d.year() == 2015)
259 {
260 // 表示只显示圆点,不显示内容
261 return 1;
262 }
263
264 // 没有任务
265 return 0;});
266 m_pLunarCalendar->Update();
268
269 //m_pLunarCalendar->SetCalendarType(CLunarCalendar::CalendarTypeLunar);
270 //m_pLunarCalendar->SetViewType(CLunarCalendar::ViewTypeWeek);
271 m_pLunarCalendar->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
272 this->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
273 setCentralWidget(m_pLunarCalendar);
274
275 QFont font = m_pLunarCalendar->font();
276 font.setItalic(true);
277 font.setStrikeOut(true);
278 int nFontSize = 12;
279 font.setPointSize(nFontSize);
280 //m_pLunarCalendar->setFont(font);
281
283 bool check = connect(m_pLunarCalendar, SIGNAL(sigSelectionChanged()),
284 this, SLOT(slotUpdateCalendar()));
285 Q_ASSERT(check);
287}
288
289MainWindow::~MainWindow()
290{
291 delete ui;
292}
293
296void MainWindow::slotAbout()
297{
298#ifdef RABBITCOMMON
299 CDlgAbout about(this);
300 QIcon icon = QIcon::fromTheme("calendar");
301 if(icon.isNull()) return;
302 auto sizeList = icon.availableSizes();
303 if(sizeList.isEmpty()) return;
304 QPixmap p = icon.pixmap(*sizeList.begin());
305 about.m_AppIcon = p.toImage();
306 about.m_szHomePage = "https://github.com/KangLin/LunarCalendar";
307 about.m_szBuildTime = QString("%1/%2").arg(__DATE__, __TIME__);
308 about.m_szCopyrightStartTime = "2019";
309 about.m_szVersionRevision = LunarCalendar_REVISION;
310#if defined (Q_OS_ANDROID)
311 about.showMaximized();
312#endif
313 about.exec();
314#endif
315}
316
317void MainWindow::slotUpdate()
318{
319#ifdef RABBITCOMMON
320 CFrmUpdater* pUpdate = new CFrmUpdater();
321 QIcon icon = QIcon::fromTheme("calendar");
322 if(icon.isNull()) return;
323 auto sizeList = icon.availableSizes();
324 if(sizeList.isEmpty()) return;
325 QPixmap p = icon.pixmap(*sizeList.begin());
326 pUpdate->SetTitle(p.toImage());
327#if defined (Q_OS_ANDROID)
328 pUpdate->showMaximized();
329#else
330 pUpdate->show();
331#endif
332#endif
333}
335
337void MainWindow::slotUpdateCalendar()
338{
339 int y, m, d;
340 m_pLunarCalendar->SelectedLunar(y, m, d);
341 qDebug(Logger) << "\n"
342 << tr("Solar:") << m_pLunarCalendar->SelectedDate() << "\n"
343 << tr("Lunar:") << m_pLunarCalendar->SelectedLunar()
344 << "(" << y << "年" << m << "月" << d << "日" << ")\n"
345 << tr("SolarTerm:") << m_pLunarCalendar->SelectedSolarTerm() << "\n"
346 << tr("Year:") << m_pLunarCalendar->GetShowYear() << "\n"
347 << tr("Month:") << m_pLunarCalendar->GetShowMonth() << "\n"
348 << tr("MinimumDate:") << m_pLunarCalendar->MinimumDate() << "\n"
349 << tr("MaximumDate:") << m_pLunarCalendar->MaximumDate()
350 ;
351}
353
354void MainWindow::slotActionLunar(bool checked)
355{
356 if(checked)
357 m_pLunarCalendar->SetCalendarType(
360 | static_cast<int>(m_pLunarCalendar->GetCalendarType())));
361 else
362 m_pLunarCalendar->SetCalendarType(
365 & static_cast<int>(m_pLunarCalendar->GetCalendarType())));
366}
367
368void MainWindow::slotActionSolar(bool checked)
369{
370 if(checked)
371 m_pLunarCalendar->SetCalendarType(
374 | static_cast<int>(m_pLunarCalendar->GetCalendarType())));
375 else
376 m_pLunarCalendar->SetCalendarType(
379 & static_cast<int>(m_pLunarCalendar->GetCalendarType())));
380}
381
382void MainWindow::slotViewMonth()
383{
385}
386
387void MainWindow::slotViewWeek()
388{
390}
391
392void MainWindow::slotHeadpositionNot()
393{
395}
396
397void MainWindow::slotHeadpositionTop()
398{
400}
401
402void MainWindow::slotHeadpositionDown()
403{
405}
406
407void MainWindow::slotHeadpositionLeft()
408{
410}
411
412void MainWindow::slotHeadpositionRight()
413{
415}
416
417void MainWindow::slotGenerateCalendarTable()
418{
419 //TODO:自动生成指定日期内的农历缓存表,生成完后,把cache.dat放到Src\Resource\Data目录下
420 //*
421 m_pLunarCalendar->SetDateRange(QDate(2000, 1, 1), QDate(2119, 1, 1));
422 QString szFileCache = qApp->applicationDirPath() + QDir::separator() + "cache.dat";
423 m_pLunarCalendar->GenerateCalendarTable(szFileCache, 1, true, false);
424 qInfo(Logger) << "Generate calendar cache table:" << szFileCache;
425 m_pLunarCalendar->LoadCalendarTable(szFileCache);
426 //*/
427}
[Define CTaskHandler derived class]
Definition MainWindow.h:16
农历日历类
const QDate SelectedDate() const
得到当前选择的日期。 当前选择的日期在指定的最小日期 MinimumDate() 与最大日期 MaximumDate() 的范围内。 默认为系统当前日期。
const QDate MaximumDate() const
得到日历的支持最大日期
void SetDateRange(const QDate &min, const QDate &max)
设置日历的支持日期的范围
const int GetShowMonth() const
得到当前界面显示的月份
const int GetShowYear() const
得到当前界面显示的年份
int SetCalendarType(_CalendarType type)
设置日历类型(组合)
void SetSelectedDate(const QDate &date, bool bForce=false)
设置当前选择的日期
int Update()
更新数据和视图
int SetTaskHandle(QSharedPointer< CTaskHandler > handler)
设置 使用者自定义任务接口
void ShowBackgroupImage(bool show)
显示或隐藏背景图片
const QString SelectedLunar() const
得到当前选择的日期的农历的字符串
int LoadCalendarTable(const QString &szFile)
从缓存文件中得到农历
const _CalendarType GetCalendarType() const
得到日历类型
const QString SelectedSolarTerm() const
得到当前选的节气
int SetHeadposition(_HEAD_position pos=_HEAD_position::Top)
设置日历头的位置
_CalendarType
日历类型
int SetViewType(_VIEW_TYPE type)
 设置视图类型
int GenerateCalendarTable(const QString &szFile, int nThreadNumber=2, bool bClearCache=false, bool bSaveAllDate=true)
产生日历缓存表
const QDate MinimumDate() const
得到日历的支持最小日期
static int GetLunar(const QDate date, int &year, int &month, int &day)
得到给定日期的农历(公历转农历)
[Define CTaskHandler derived class]
Definition MainWindow.h:26
MainWindow(QWidget *parent=nullptr)
[Implement the onHandle function]