农历日历 v1.0.4
载入中...
搜索中...
未找到
LunarCalendar.cpp
1
6#include "LunarCalendar.h"
7#include "LunarCalendarModel.h"
8#include <QDate>
9#include <QLocale>
10#include <QLoggingCategory>
11#include <QHeaderView>
12#include <QScrollBar>
13#include <QKeyEvent>
14#include <QWheelEvent>
15#include <QModelIndex>
16#include <QTranslator>
17#include <QApplication>
18#include <QDir>
19#include <QStyleOption>
20#include <QPainter>
21#include <QLineEdit>
22#include <algorithm>
23#include <QGestureEvent>
24#include <QSwipeGesture>
25#include <QMouseEvent>
26#include <QEvent>
27
28#include "CalendarLunar.h"
29#include "LunarCalendarDelegate.h"
30#include "CalendarLunar.h"
31#include "LunarTable.h"
32#include "RabbitCommonDir.h"
33#include "RabbitCommonTools.h"
34
35static Q_LOGGING_CATEGORY(Logger, "Rabbit.LunarCalendar")
36
37static bool g_bLundarCalendarInit = false;
38static QSharedPointer<QTranslator> g_Translator;
39
40CLunarCalendar::CLunarCalendar(QWidget *parent) :
41 QWidget(parent),
42 m_cmbYear(this),
43 m_tbPreYear(this),
44 m_tbNextYear(this),
45 m_cmbMonth(this),
46 m_tbPreMonth(this),
47 m_tbNextMonth(this),
48 m_pbToday(this),
49 m_lbDate(this),
50 m_lbTime(this),
51 m_View(this),
52 m_pToolLayout(nullptr),
53 m_pHeadLayout(nullptr),
54 m_pMainLayout(nullptr),
55 m_Headposition(_HEAD_position::Top),
56 m_oldRow(0),
57 m_oldCol(0),
58 m_bShowToday(true),
59 m_bShowBackgroupImage(false),
60 m_TouchFunction(_TOUCH_UP_DOWN_FUNCTION::TouchChangeMounth)
61{
62 InitResource();
63 setWindowTitle(tr("Lunar calendar"));
64
65 //setLocale(QLocale("zh_CN"));
66
67 m_tbPreYear.setArrowType(Qt::UpArrow);
68 m_tbPreYear.setToolTip(tr("Previous year"));
69 m_tbPreYear.setStatusTip(tr("Previous year"));
70 m_tbNextYear.setArrowType(Qt::DownArrow);
71 m_tbNextYear.setToolTip(tr("Next year"));
72 m_tbNextYear.setStatusTip(tr("Next year"));
73 m_tbPreMonth.setArrowType(Qt::UpArrow);
74 m_tbPreMonth.setToolTip(tr("Previous month"));
75 m_tbPreMonth.setStatusTip(tr("Previous month"));
76 m_tbNextMonth.setArrowType(Qt::DownArrow);
77 m_tbNextMonth.setToolTip(tr("Next month"));
78 m_tbNextMonth.setStatusTip(tr("Next month"));
79
80 // QLineEdit *yearEdit = new QLineEdit;
81 // yearEdit->setAlignment(Qt::AlignCenter);
82 // m_cmbYear.setLineEdit(yearEdit);
83 // QLineEdit *monthEdit = new QLineEdit;
84 // monthEdit->setReadOnly(true);
85 // monthEdit->setAlignment(Qt::AlignCenter);
86 // m_cmbMonth.setLineEdit(monthEdit);
87
88 //m_pbToday.setText(tr("Today"));
89 m_pbToday.setIcon(QIcon::fromTheme("today"));
90 m_pbToday.setToolTip(tr("Today"));
91 m_pbToday.setStatusTip(tr("Today"));
92 m_pbToday.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
93
94 m_lbDate.setAlignment(Qt::AlignCenter);
95 m_lbTime.setAlignment(Qt::AlignCenter);
96// m_lbDate.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
97// m_lbTime.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
98
99 CLunarCalendarModel* pModel = new CLunarCalendarModel(&m_View);
100 m_View.setModel(pModel);
101 m_View.setItemDelegate(new CLunarCalendarDelegate(&m_View));
102 /* TODO: 未能生效
103 if(m_View.horizontalHeader())
104 m_View.horizontalHeader()->setItemDelegate(new CLunarCalendarHeaderDelegate(&m_View));
105 if(m_View.verticalHeader())
106 m_View.verticalHeader()->setItemDelegate(new CLunarCalendarHeaderDelegate(&m_View));
107 //*/
108 m_View.setAttribute(Qt::WA_AcceptTouchEvents);
109 ShowGrid(false);
110
111 m_View.setFocusPolicy(Qt::WheelFocus);
112 m_View.setSelectionBehavior(QAbstractItemView::SelectItems);
113 m_View.setSelectionMode(QAbstractItemView::SingleSelection);
114 m_View.setEditTriggers(QAbstractItemView::NoEditTriggers);
115 m_View.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
116
117 /*
118 m_View.horizontalHeader()->setMinimumSize(cell.size());
119 m_View.horizontalHeader()->setMinimumSectionSize(cell.width());
120 m_View.verticalHeader()->setMinimumSize(cell.size());
121 m_View.verticalHeader()->setMinimumSectionSize(cell.height());
122 */
123 m_View.horizontalHeader()->setSectionsClickable(false);
124 m_View.verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
125 m_View.verticalHeader()->setSectionsClickable(false);
126 m_View.setFrameStyle(QFrame::NoFrame);
127 //m_View.grabGesture(Qt::PanGesture);
128 m_View.grabGesture(Qt::SwipeGesture);
129 m_View.installEventFilter(this);
130 m_View.setAttribute(Qt::WA_AcceptTouchEvents, true);
131 m_View.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
132 m_View.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
133 //m_View.setAutoScroll(false);
134 //m_View.setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
135 //m_View.setAlternatingRowColors(true); //设置奇偶行颜色
136// qApp->setAttribute(Qt::AA_SynthesizeMouseForUnhandledTouchEvents, false);
137// qApp->setAttribute(Qt::AA_SynthesizeTouchForUnhandledMouseEvents, true);
138
139 QFont font = m_View.font();
140 float nFontSize = 0, nPixSize = 0;
141 nFontSize = font.pointSize();
142 nPixSize = font.pixelSize();
143 qDebug(Logger) << "Font point size:" << nFontSize
144 << "pixel size:" << nPixSize;
145 if(nFontSize > 0)
146 font.setPointSize(nFontSize * 2 / 3);
147 if(nPixSize > 0)
148 font.setPixelSize(nPixSize * 2 / 3);
149#if defined(Q_OS_ANDROID)
150 m_View.setFont(font);
151#endif
152 /*
153 font = m_lbDate.font();
154 font.setPointSize(nFontSize);
155 m_lbDate.setFont(font);
156
157 font = m_lbTime.font();
158 font.setPointSize(nFontSize);
159 m_lbTime.setFont(font);
160
161 font = m_pbToday.font();
162 font.setPointSize(nFontSize);
163 m_pbToday.setFont(font);
164
165 font = m_cmbYear.font();
166 font.setPointSize(nFontSize);
167 m_cmbYear.setFont(font);
168
169 font = m_cmbMonth.font();
170 font.setPointSize(nFontSize);
171 m_cmbMonth.setFont(font);
172 //*/
173
174 //m_View.setStyleSheet("background-color:rgba(0,0,0,0)"); //设置背景透明
175 //setStyleSheet("border-image:url(d:/temp/4.jpg)"); //设置背景图片
176
177 for(int i = 0; i < 12; i++)
178 {
179 m_cmbMonth.addItem(locale().monthName(i + 1), i + 1);
180 }
181 m_cmbMonth.setToolTip(tr("Month"));
182 m_cmbMonth.setStatusTip(tr("Month"));
183
184 m_cmbMonth.setCurrentIndex(m_cmbMonth.findData(pModel->GetDate().month()));
185 SetSelectedDate(pModel->GetDate());
186
187 SetYearRange(pModel->GetMinimumDate().year(),
188 pModel->GetMaximumDate().year());
189 m_cmbYear.setCurrentIndex(m_cmbYear.findData(pModel->GetDate().year()));
190 m_cmbYear.setEditable(true);
191 m_cmbYear.setToolTip(tr("Year"));
192 m_cmbYear.setStatusTip(tr("Year"));
193
194// m_View.sizePolicy().setVerticalPolicy(QSizePolicy::Preferred);
195// m_View.sizePolicy().setHorizontalPolicy(QSizePolicy::Preferred);
196
197 m_pToolLayout = new QHBoxLayout();
198 m_pToolLayout->addWidget(&m_tbPreYear);
199 m_pToolLayout->addWidget(&m_cmbYear);
200 m_pToolLayout->addWidget(&m_tbNextYear);
201 m_pToolLayout->addWidget(&m_tbPreMonth);
202 m_pToolLayout->addWidget(&m_cmbMonth);
203 m_pToolLayout->addWidget(&m_tbNextMonth);
204 m_pToolLayout->addWidget(&m_pbToday);
205#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
206 m_pToolLayout->setMargin(0);
207#else
208 m_pToolLayout->setContentsMargins(0, 0, 0, 0);
209#endif
210 m_pToolLayout->setSpacing(0);
211
212 SetHeadposition();
213
214 bool check = connect(&m_Timer, SIGNAL(timeout()),
215 this, SLOT(slotTimeout()));
216 Q_ASSERT(check);
217 check = connect(&m_tbPreYear, SIGNAL(clicked()),
218 this, SLOT(on_tbPreviousYear_clicked()));
219 Q_ASSERT(check);
220 check = connect(&m_tbNextYear, SIGNAL(clicked()),
221 this, SLOT(on_tbNextYear_clicked()));
222 Q_ASSERT(check);
223 check = connect(&m_tbPreMonth, SIGNAL(clicked()),
224 this, SLOT(on_tbPreviousMonth_clicked()));
225 Q_ASSERT(check);
226 check = connect(&m_tbNextMonth, SIGNAL(clicked()),
227 this, SLOT(on_tbNextMonth_clicked()));
228 Q_ASSERT(check);
229 check = connect(&m_pbToday, SIGNAL(clicked()),
230 this, SLOT(soltShowToday()));
231 Q_ASSERT(check);
232// check = connect(&m_cmbYear, SIGNAL(currentIndexChanged(int)),
233// this, SLOT(on_cbYear_currentIndexChanged(int)));
234// Q_ASSERT(check);
235 check = connect(&m_cmbMonth, SIGNAL(currentIndexChanged(int)),
236 this, SLOT(on_cbMonth_currentIndexChanged(int)));
237 Q_ASSERT(check);
238 check = connect(&m_View, SIGNAL(pressed(const QModelIndex&)),
239 this, SLOT(on_tvMonth_pressed(const QModelIndex&)));
240 Q_ASSERT(check);
241
242 updateGeometry();
243}
244
245CLunarCalendar::~CLunarCalendar()
246{}
247
248void CLunarCalendar::InitResource()
249{
250 if(g_bLundarCalendarInit) return;
251
252 g_bLundarCalendarInit = true;
253
254 g_Translator = RabbitCommon::CTools::Instance()->InstallTranslator(
255 "LunarCalendar", RabbitCommon::CTools::TranslationType::Library);
256
257 Q_INIT_RESOURCE(ResourceLunarCalendar);
258#if _DEBUG
259 Q_INIT_RESOURCE(ResourceSql);
260 Q_INIT_RESOURCE(translations_LunarCalendar);
261#endif
262}
263
264void CLunarCalendar::CLeanResource()
265{
266 RabbitCommon::CTools::Instance()->RemoveTranslator(g_Translator);
267 Q_CLEANUP_RESOURCE(ResourceLunarCalendar);
268#if _DEBUG
269 Q_CLEANUP_RESOURCE(ResourceSql);
270 Q_CLEANUP_RESOURCE(translations_LunarCalendar);
271#endif
272}
273
275{
276 m_Headposition = pos;
277 if(m_pMainLayout)
278 {
279 m_pMainLayout->removeWidget(&m_View);
280 m_pMainLayout->removeItem(m_pHeadLayout);
281 delete m_pMainLayout;
282 m_pMainLayout = nullptr;
283 }
284 if(m_pHeadLayout)
285 {
286 m_pHeadLayout->removeItem(m_pToolLayout);
287 m_pHeadLayout->removeWidget(&m_lbDate);
288 m_pHeadLayout->removeWidget(&m_lbTime);
289 delete m_pHeadLayout;
290 m_pHeadLayout = nullptr;
291 }
292
293 m_pMainLayout = new QGridLayout(this);
294#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
295 m_pMainLayout->setMargin(0);
296#else
297 m_pMainLayout->setContentsMargins(0, 0, 0, 0);
298#endif
299 m_pMainLayout->setSpacing(0);
300 setLayout(m_pMainLayout);
301
302 switch (pos) {
304 ShowHead(false);
305 m_pMainLayout->addWidget(&m_View);
306 break;
308 ShowHead(true);
309 m_pHeadLayout = new QVBoxLayout();
310 m_pHeadLayout->addLayout(m_pToolLayout);
311 m_pHeadLayout->addWidget(&m_lbDate);
312 m_pHeadLayout->addWidget(&m_lbTime);
313#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
314 m_pHeadLayout->setMargin(0);
315#else
316 m_pHeadLayout->setContentsMargins(0, 0, 0, 0);
317#endif
318 m_pHeadLayout->setSpacing(0);
319
320 m_pMainLayout->addLayout(m_pHeadLayout, 0, 0);
321 m_pMainLayout->addWidget(&m_View);
322 break;
324 ShowHead(true);
325 m_pMainLayout->addWidget(&m_View);
326
327 m_pHeadLayout = new QVBoxLayout();
328 m_pHeadLayout->addWidget(&m_lbDate);
329 m_pHeadLayout->addWidget(&m_lbTime);
330 m_pHeadLayout->addLayout(m_pToolLayout);
331#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
332 m_pHeadLayout->setMargin(0);
333#else
334 m_pHeadLayout->setContentsMargins(0, 0, 0, 0);
335#endif
336 m_pHeadLayout->setSpacing(0);
337
338 m_pMainLayout->addLayout(m_pHeadLayout, 1, 0);
339 break;
341 ShowHead(true);
342 m_pHeadLayout = new QVBoxLayout();
343 m_pHeadLayout->addStretch();
344 m_pHeadLayout->addLayout(m_pToolLayout);
345 m_pHeadLayout->addWidget(&m_lbDate);
346 m_pHeadLayout->addWidget(&m_lbTime);
347 m_pHeadLayout->addStretch();
348#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
349 m_pHeadLayout->setMargin(0);
350#else
351 m_pHeadLayout->setContentsMargins(0, 0, 0, 0);
352#endif
353 m_pHeadLayout->setSpacing(0);
354
355 m_pMainLayout->addLayout(m_pHeadLayout, 0, 0);
356 m_pMainLayout->addWidget(&m_View, 0, 1);
357 break;
359 ShowHead(true);
360 m_pMainLayout->addWidget(&m_View);
361 m_pHeadLayout = new QVBoxLayout();
362 m_pHeadLayout->addStretch();
363 m_pHeadLayout->addLayout(m_pToolLayout);
364 m_pHeadLayout->addWidget(&m_lbDate);
365 m_pHeadLayout->addWidget(&m_lbTime);
366 m_pHeadLayout->addStretch();
367#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
368 m_pHeadLayout->setMargin(0);
369#else
370 m_pHeadLayout->setContentsMargins(0, 0, 0, 0);
371#endif
372 m_pHeadLayout->setSpacing(0);
373
374 m_pMainLayout->addLayout(m_pHeadLayout, 0, 1);
375 break;
376 }
377 return 0;
378}
379
380int CLunarCalendar::ShowSelectTitle()
381{
382 QDate d;
383 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
384 if(!pModel)
385 return -1;
386 d = pModel->GetDate();
387 if(d.isNull()) return -2;
388 QString szDate;
389 if(static_cast<int>(_CalendarType::CalendarTypeSolar)
390 & static_cast<int>(GetCalendarType()))
391 szDate = d.toString(locale().dateFormat(QLocale::LongFormat));
392 if(static_cast<int>(_CalendarType::CalendarTypeLunar)
393 & static_cast<int>(GetCalendarType()))
394 szDate += " " + SelectedLunar();
395 m_lbDate.setText(szDate);
396 return 0;
397}
398
399void CLunarCalendar::on_cbYear_currentIndexChanged(int index)
400{
401 qDebug(Logger) << "CLunarCalendar::on_cbYear_currentIndexChanged";
402 Q_UNUSED(index);
403 UpdateViewModel();
404 UpdateMonthMenu();
405}
406
407void CLunarCalendar::on_tbNextYear_clicked()
408{
409 int nIndex = m_cmbYear.currentIndex() + 1;
410 if(m_cmbYear.count() <= nIndex)
411 return;
412 m_cmbYear.setCurrentIndex(nIndex);
413}
414
415void CLunarCalendar::on_tbPreviousYear_clicked()
416{
417 int nIndex = m_cmbYear.currentIndex() - 1;
418 if(0 > nIndex)
419 return;
420 m_cmbYear.setCurrentIndex(nIndex);
421}
422
423void CLunarCalendar::on_tbNextMonth_clicked()
424{
425 if(!m_tbNextMonth.isEnabled())
426 return;
427 int i = m_cmbMonth.currentIndex() + 1;
428 if(i > m_cmbMonth.count() - 1)
429 {
430 i = 0;
431 m_cmbYear.setCurrentIndex(
432 m_cmbYear.findData(m_cmbYear.currentData().toInt() + 1));
433 }
434 m_cmbMonth.setCurrentIndex(i);
435}
436
437void CLunarCalendar::on_tbPreviousMonth_clicked()
438{
439 if(!m_tbPreMonth.isEnabled())
440 return;
441 int i = m_cmbMonth.currentIndex() - 1;
442 if(i < 0)
443 {
444 m_cmbYear.setCurrentIndex(
445 m_cmbYear.findData(m_cmbYear.currentData().toInt() - 1));
446 i = m_cmbMonth.count() - 1;
447 }
448 m_cmbMonth.setCurrentIndex(i);
449}
450
451void CLunarCalendar::on_cbMonth_currentIndexChanged(int index)
452{
453 qDebug(Logger) << "CLunarCalendar::on_cbMonth_currentIndexChanged:" << index;
454 Q_UNUSED(index);
455 UpdateViewModel();
456 EnableMonthMenu();
457}
458
459void CLunarCalendar::soltShowToday()
460{
461// m_cmbYear.setCurrentIndex(m_cmbYear.findData(QDate::currentDate().year()));
462// int nIndex = m_cmbMonth.findData(QDate::currentDate().month());
463// if(nIndex > -1)
464// m_cmbMonth.setCurrentIndex(nIndex);
465 SetSelectedDate(QDate::currentDate());
466}
467
474int CLunarCalendar::UpdateViewModel(bool bForce)
475{
476 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
477 if(!pModel)
478 return -2;
479
480 if (pModel->GetShowYear() != m_cmbYear.currentData().toInt()
481 || pModel->GetShowMonth() != m_cmbMonth.currentData().toInt()) {
482 //m_View.selectionModel()->setCurrentIndex(QModelIndex(), QItemSelectionModel::Clear);
483 m_View.selectionModel()->clear();
484 }
485 switch (GetViewType())
486 {
488 {
489 pModel->showMonth(m_cmbYear.currentData().toInt(),
490 m_cmbMonth.currentData().toInt(),
491 bForce);
492 if(pModel && m_bShowBackgroupImage)
493 {
494 QString szBackgrpup = ":/image/" + QString::number(pModel->GetShowMonth());
495 if(static_cast<int>(_CalendarType::CalendarTypeLunar)
496 & static_cast<int>(GetCalendarType()))
497 szBackgrpup += "_zh_CN";
498 SetBackgroup(szBackgrpup);
499 }
500
501 break;
502 }
504 pModel->showWeek(m_cmbYear.currentData().toInt(),
505 m_cmbMonth.currentData().toInt(),
506 bForce);
507 break;
508 }
509
510 ShowSelectTitle();
511
512 updateGeometry();
513 return 0;
514}
515
517{
518 m_View.setShowGrid(show);
519}
520/*
521bool CLunarCalendar::ShowGrid() const
522{
523 return m_View.showGrid();
524}//*/
525
527{
528 m_bShowBackgroupImage = show;
529 if(m_bShowBackgroupImage)
530 UpdateViewModel();
531 else
532 m_View.setStyleSheet("border-image:none");
533
534 return;
535}
536/*
537bool CLunarCalendar::ShowBackgroupImage()
538{
539 return m_bShowBackgroupImage;
540}//*/
541
543{
544 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
545 if(!pModel) return;
546 return pModel->EnableToolTip(enable);
547}
548
550{
551 if(m_bShowToday == bShow)
552 return;
553 m_bShowToday = bShow;
554 m_pbToday.setVisible(m_bShowToday);
555}
556
558{
559 m_View.verticalHeader()->setVisible(bShow);
560 updateGeometry();
561}
562
564{
565 m_View.horizontalHeader()->setVisible(bShow);
566 updateGeometry();
567}
568
570{
571 ShowTools(bShow);
572 ShowDate(bShow);
573 ShowTime(bShow);
574}
575
577{
578 m_tbPreYear.setVisible(bShow);
579 m_tbNextYear.setVisible(bShow);
580 m_cmbYear.setVisible(bShow);
581 m_tbNextMonth.setVisible(bShow);
582 m_tbPreMonth.setVisible(bShow);
583 m_cmbMonth.setVisible(bShow);
584 ShowToday(bShow);
585 updateGeometry();
586}
587
589{
590 m_lbDate.setVisible(bShow);
591 updateGeometry();
592}
593
595{
596 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
597 if(!pModel) return QDate();
598 return pModel->GetDate();
599}
600
601void CLunarCalendar::SetSelectedDate(const QDate &date, bool bForce)
602{
603 if (!date.isValid())
604 return;
605
606 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
607 if(!pModel) return;
608 if(bForce || pModel->GetDate() != date)
609 {
610 pModel->setDate(date);
611 }
612
613 QDate newDate = pModel->GetDate();
614 m_cmbYear.setCurrentIndex(m_cmbYear.findData(newDate.year()));
615 switch (GetViewType()) {
617 m_cmbMonth.setCurrentIndex(m_cmbMonth.findData(newDate.month()));
618 break;
620 {
621 int week = 1;
622 int year = 0;
623 week = newDate.weekNumber(&year);
624 if(year != newDate.year())
625 {
626 m_cmbYear.setCurrentIndex(m_cmbYear.findData(newDate.year()));
627 }
628 m_cmbMonth.setCurrentIndex(m_cmbMonth.findData(week));
629 }
630 break;
631 }
632
633 UpdateViewModel();
634
635 int row, col;
636 pModel->cellForDate(pModel->GetDate(), &row, &col);
637 if(row >= 0 && col >= 0)
638 {
639 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
640 if(pModel && m_bShowBackgroupImage && GetViewType() == _VIEW_TYPE::ViewTypeMonth)
641 {
642 QString szJiQi = pModel->data(pModel->index(row, col),
643 CLunarCalendarModel::ROLE::BackgroupImage).toString();
644 if(!szJiQi.isEmpty())
645 SetBackgroup(szJiQi);
646 }
647
648 m_View.selectionModel()->clear();
649 m_View.selectionModel()->setCurrentIndex(pModel->index(row, col),
650 QItemSelectionModel::SelectCurrent);
651 //m_View.setCurrentIndex(pModel->index(row, col));
652 //m_View.selectionModel()->select(pModel->index(row, col),
653 // QItemSelectionModel::SelectCurrent);
654 m_View.setFocus();
655 m_oldCol = col;
656 m_oldRow = row;
657 }
658 emit sigSelectionChanged();
659}
660
661const QString CLunarCalendar::SelectedLunar() const
662{
663 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
664 if(!pModel) return QString();
665 QDate date = pModel->GetDate();
666 CCalendarLunar l(date);
667 return l.GetLunar();
668}
669
671{
672 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
673 if(!pModel) return QString();
674 QDate date = pModel->GetDate();
675 CCalendarLunar l(date);
676 return l.GetJieQi();
677}
678
679int CLunarCalendar::SelectedLunar(int &year, int &month, int &day)
680{
681 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
682 if(!pModel) return -1;
683 QDate date = pModel->GetDate();
684 CCalendarLunar l(date);
685 year = l.GetYear();
686 month = l.GetMonth();
687 day = l.GetDay();
688 return 0;
689}
690
691int CLunarCalendar::GetLunar(const QDate date, int &year, int &month, int &day)
692{
693 CCalendarLunar l(date);
694 year = l.GetYear();
695 month = l.GetMonth();
696 day = l.GetDay();
697 return 0;
698}
699
701{
702 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
703 if(!pModel) return -1;
704 return pModel->GetShowYear();
705}
706
708{
709 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
710 if(!pModel) return -1;
711 return pModel->GetShowMonth();
712}
713
715{
716 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
717 if(!pModel) return QDate();
718 return pModel->GetMaximumDate();
719}
720
721void CLunarCalendar::SetMaximumDate(const QDate &date)
722{
723 if(!date.isValid())
724 return;
725
726 if(QDate::currentDate() > date)
727 m_pbToday.setVisible(false);
728 else
729 ShowToday(m_bShowToday);
730
731 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
732 if(!pModel) return;
733 QDate oldDate = pModel->GetDate();
734 pModel->SetMaximumDate(date);
735 int year = date.year();
737 {
738 date.weekNumber(&year);
739 }
740
741 SetYearRange(date.year(), pModel->GetMaximumDate().year());
742 m_cmbYear.setCurrentIndex(m_cmbYear.findData(pModel->GetShowYear()));
743
744 QDate newDate = pModel->GetDate();
745 if (oldDate != newDate) {
746 UpdateViewModel();
747 emit sigSelectionChanged();
748 }
749}
750
752{
753 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
754 if(!pModel) return QDate();
755 return pModel->GetMinimumDate();
756}
757
758void CLunarCalendar::SetMinimumDate(const QDate &date)
759{
760 if(!date.isValid())
761 return;
762
763 if(QDate::currentDate() < date)
764 m_pbToday.setVisible(false);
765 else
766 ShowToday(m_bShowToday);
767 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
768 if(!pModel) return;
769 QDate oldDate = pModel->GetDate();
770 pModel->SetMinimumDate(date);
771 int year = date.year();
773 {
774 date.weekNumber(&year);
775 }
776
777 SetYearRange(date.year(), pModel->GetMaximumDate().year());
778 m_cmbYear.setCurrentIndex(m_cmbYear.findData(pModel->GetShowYear()));
779
780 QDate newDate = pModel->GetDate();
781 if (oldDate != newDate) {
782 UpdateViewModel();
783 emit sigSelectionChanged();
784 }
785}
786
787void CLunarCalendar::SetDateRange(const QDate &min, const QDate &max)
788{
789 if (!min.isValid() || !max.isValid())
790 return;
791
792 if(min > max)
793 {
794 qCritical() << "SetDateRange parameter error: min =" << min << " max =" << max;
795 return;
796 }
797
798 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
799 if(!pModel) return;
800 if(pModel->GetMaximumDate() == max && pModel->GetMinimumDate() == min)
801 return;
802
803 if(QDate::currentDate() < min || QDate::currentDate() > max)
804 m_pbToday.setVisible(false);
805 else
806 ShowToday(m_bShowToday);
807
808 QDate oldDate = pModel->GetDate();
809 pModel->setRange(min, max);
810
811 int yearMin = min.year();
813 {
814 min.weekNumber(&yearMin);
815 }
816 int yearMax = max.year();
818 max.weekNumber(&yearMax);
819
820 SetYearRange(min.year(), max.year());
821 m_cmbYear.setCurrentIndex(m_cmbYear.findData(pModel->GetShowYear()));
822
823 QDate newDate = pModel->GetDate();
824 if (oldDate != newDate) {
825 UpdateViewModel();
826 emit sigSelectionChanged();
827 }
828}
829
830int CLunarCalendar::SetYearRange(int min, int max)
831{
832 if(min > max)
833 return -1;
834
835 bool check = m_cmbYear.disconnect();
836
837 m_cmbYear.clear();
838 for(int i = min; i < max; i++)
839 {
840 if(0 == i)
841 continue;
842 m_cmbYear.addItem(QString::number(i), i);
843 }
844 check = connect(&m_cmbYear, SIGNAL(currentIndexChanged(int)),
845 this, SLOT(on_cbYear_currentIndexChanged(int)));
846 Q_ASSERT(check);
847 return 0;
848}
849
850int CLunarCalendar::EnableMonthMenu()
851{
852 bool prevEnabled = true;
853 bool nextEnabled = true;
854
855 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
856 if(!pModel) return -1;
857
858 int minYear = pModel->GetMinimumDate().year();
859 int maxYear = pModel->GetMaximumDate().year();
861 pModel->GetMinimumDate().weekNumber(&minYear);
862 pModel->GetMaximumDate().weekNumber(&maxYear);
863 }
864
865 if (pModel->GetShowYear() == minYear) {
866 switch (GetViewType())
867 {
869 if (pModel->GetShowMonth() == pModel->GetMinimumDate().month())
870 prevEnabled = false;
871 break;
873 if (pModel->GetShowWeek() == pModel->GetMinimumDate().weekNumber())
874 prevEnabled = false;
875 break;
876 }
877 }
878
879 if (pModel->GetShowYear() == maxYear) {
880 switch (GetViewType()) {
882 if (pModel->GetShowMonth() == pModel->GetMaximumDate().month())
883 nextEnabled = false;
884 break;
886 if(pModel->GetShowWeek() == pModel->GetMaximumDate().weekNumber())
887 nextEnabled = false;
888 break;
889 }
890 }
891
892 m_tbPreMonth.setEnabled(prevEnabled);
893 m_tbNextMonth.setEnabled(nextEnabled);
894 return 0;
895}
896
897int CLunarCalendar::UpdateMonthMenu()
898{
899 int beg = 1, end = 12;
900
901 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
902 if(!pModel) return -1;
903
904 EnableMonthMenu();
905
906 int minYear = pModel->GetMinimumDate().year();
907 int maxYear = pModel->GetMaximumDate().year();
908
910 end = pModel->GetWeeksOfYear(m_cmbYear.currentData().toInt());
911 pModel->GetMinimumDate().weekNumber(&minYear);
912 pModel->GetMaximumDate().weekNumber(&maxYear);
913 }
914
915 if (pModel->GetShowYear() == minYear) {
916 switch (GetViewType())
917 {
919 beg = pModel->GetMinimumDate().month();
920 break;
922 beg = pModel->GetMinimumDate().weekNumber();
923 break;
924 }
925 }
926
927 if (pModel->GetShowYear() == maxYear) {
928 switch (GetViewType()) {
930 end = pModel->GetMaximumDate().month();
931 break;
933 end = pModel->GetMaximumDate().weekNumber();
934 break;
935 }
936 }
937
938 int index = m_cmbMonth.currentIndex();
939 bool check = m_cmbMonth.disconnect();
940 Q_ASSERT(check);
941 m_cmbMonth.clear();
942 for (int i = beg; i <= end; i++) {
943 switch (GetViewType()) {
945 m_cmbMonth.addItem(locale().monthName(i), i);
946 break;
948 m_cmbMonth.addItem(QString::number(i), i);
949 break;
950 }
951 }
952 check = connect(&m_cmbMonth, SIGNAL(currentIndexChanged(int)),
953 this, SLOT(on_cbMonth_currentIndexChanged(int)));
954 Q_ASSERT(check);
955 if(index >= m_cmbMonth.count())
956 index = m_cmbMonth.count() - 1;
957 if(index < 0)
958 index = 0;
959 m_cmbMonth.setCurrentIndex(index);
960
961 return 0;
962}
963
964const Qt::DayOfWeek CLunarCalendar::FirstDayOfWeek() const
965{
966 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
967 if(!pModel) return locale().firstDayOfWeek();
968 return pModel->firstDayOfWeek();
969}
970
971//void CLunarCalendar::SetFirstDayOfWeek(Qt::DayOfWeek dayOfWeek)
972//{
973// CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
974// if(!pModel) return;
975// pModel->setFirstDayOfWeek(dayOfWeek);
976//}
977
978void CLunarCalendar::on_tvMonth_pressed(const QModelIndex &index)
979{
980 qDebug(Logger) << "CLunarCalendar::on_tvMonth_pressed" << index;
981 if(!index.isValid())
982 return;
983
984 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
985 if(!pModel) return;
986 QDate d = pModel->dateForCell(index.row(), index.column());
987 if(d.isValid())
989
990 m_oldCol = index.column();
991 m_oldRow = index.row();
992}
993
994bool CLunarCalendar::eventFilter(QObject *watched, QEvent *event)
995{
996 //qDebug(Logger) << event->type();
997 switch(event->type()){
998// case QEvent::Gesture:
999// {
1000// qDebug(Logger) << "QEvent::Gesture";
1001// QGestureEvent *gestureEvent = static_cast<QGestureEvent*>(event);
1002// if (QGesture *swipe = gestureEvent->gesture(Qt::SwipeGesture))
1003// if (swipe->state() == Qt::GestureFinished) {
1004// QSwipeGesture *gesture = static_cast<QSwipeGesture *>(swipe);
1005// if (gesture->horizontalDirection() == QSwipeGesture::Left
1006// || gesture->verticalDirection() == QSwipeGesture::Up) {
1007// qDebug(Logger) << "swipeTriggered(): swipe to previous";
1008// } else {
1009// qDebug(Logger) << "swipeTriggered(): swipe to next";
1010// }
1011// }
1012// }
1013// break;
1014 case QEvent::TouchBegin:
1015 {
1016// QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
1017// QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
1018// qDebug(Logger) << "touch begin points:" << touchPoints.length() << touchPoints;
1019 event->accept();
1020 return true;
1021 }
1022 case QEvent::TouchUpdate:
1023 {
1024// QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
1025// QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
1026// qDebug(Logger) << "touch update points:" << touchPoints.length() << touchPoints;
1027 //event->accept();
1028 break;
1029 }
1030 case QEvent::TouchEnd:
1031 {
1032 event->accept();
1033 QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
1034#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1035 QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->points();
1036#else
1037 QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
1038#endif
1039 //qDebug(Logger) << "touch end points:" << touchPoints.length() << touchPoints;
1040 if(touchPoints.length() == 1)
1041 {
1042 QTouchEvent::TouchPoint t = touchPoints.first();
1043#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1044 QLineF line(QLineF(t.pressPosition(), t.lastPosition()));
1045#else
1046 QLineF line(QLineF(t.startPos(), t.lastPos()));
1047#endif
1048 if(qAbs(line.dx()) > qAbs(line.dy()))
1049 {
1050 //qDebug(Logger) << "dx:" << line.dx() << "hor:" << m_View.horizontalHeader()->minimumSectionSize();
1051 if(qAbs(line.dx()) > m_View.horizontalHeader()->minimumSectionSize())
1052 {
1053 if(line.dx() > 0)
1054 on_tbPreviousMonth_clicked();
1055 else
1056 on_tbNextMonth_clicked();
1057 }
1058 }else {
1059 //qDebug(Logger) << "dy:" << line.dy();
1060 if(qAbs(line.dy()) > m_View.verticalHeader()->minimumSectionSize())
1061 {
1062 if(line.dy() > 0)
1063 {
1065 && m_TouchFunction == _TOUCH_UP_DOWN_FUNCTION::TouchChangeView)
1067 else
1068 on_tbPreviousMonth_clicked();
1069 }
1070 else
1071 {
1073 && m_TouchFunction == _TOUCH_UP_DOWN_FUNCTION::TouchChangeView)
1075 else
1076 on_tbNextMonth_clicked();
1077 }
1078 }
1079 }
1080#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1081 on_tvMonth_pressed(m_View.indexAt(t.lastPosition().toPoint()));
1082#else
1083 on_tvMonth_pressed(m_View.indexAt(t.lastPos().toPoint()));
1084#endif
1085 }
1086
1087 break;
1088 }
1089#ifndef QT_NO_WHEELEVENT
1090 case QEvent::Wheel:
1091 {
1092 QWheelEvent *we = dynamic_cast<QWheelEvent*>(event);
1093 const int numDegrees = we->angleDelta().y() / 8;
1094 const int numSteps = numDegrees / 15;
1095 //qDebug(Logger) << "step:" << numSteps;
1096 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
1097 if(pModel)
1098 {
1099 const QModelIndex index = m_View.currentIndex();
1100 if(index.isValid())
1101 {
1102 QDate currentDate = pModel->dateForCell(index.row(), index.column());
1103 currentDate = currentDate.addDays(-numSteps * 7);
1104 this->SetSelectedDate(currentDate);
1105 }
1106 }
1107 break;
1108 }
1109#endif
1110 case QEvent::KeyRelease:
1111 {
1112 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
1113 if(!pModel)
1114 break;
1115 QKeyEvent* key = dynamic_cast<QKeyEvent*>(event);
1116 qDebug(Logger) << "CLunarCalendar::eventFilter key:" << key
1117 << "old:" << m_oldRow << m_oldCol
1118 << "current:" << m_View.currentIndex().row()
1119 << m_View.currentIndex().column();
1120 switch (key->key()) {
1121 case Qt::Key_Up:
1122 {
1123 QModelIndex index = m_View.currentIndex();
1124 if(index.isValid())
1125 {
1126 QDate d = pModel->dateForCell(index.row(), index.column());
1127 if(index.row() == 0 && m_oldRow == 0)
1128 {
1130 && d.month() == m_cmbMonth.currentData())
1131 d = d.addDays(-7);
1133 d = d.addDays(-7);
1134 }
1135 SetSelectedDate(d);
1136 }
1137 }
1138 break;
1139 case Qt::Key_Down:
1140 {
1141 QModelIndex index = m_View.currentIndex();
1142 if(index.isValid())
1143 {
1144 QDate d = pModel->dateForCell(index.row(), index.column());
1145 if(index.row() >= pModel->rowCount() -1
1146 && m_oldRow == index.row())
1147 {
1149 && d.month() == m_cmbMonth.currentData())
1150 d = d.addDays(7);
1151
1153 d = d.addDays(7);
1154 }
1155 SetSelectedDate(d);
1156 }
1157 }
1158 break;
1159 case Qt::Key_Left:
1160 {
1161 QModelIndex index = m_View.currentIndex();
1162 if(index.isValid())
1163 {
1164 QDate d = pModel->dateForCell(index.row(), index.column());
1165 if(index.column() == m_oldCol)
1166 d = d.addDays(-1);
1167 SetSelectedDate(d);
1168 }
1169 }
1170 break;
1171 case Qt::Key_Right:
1172 {
1173 QModelIndex index = m_View.currentIndex();
1174 if(index.isValid())
1175 {
1176 QDate d = pModel->dateForCell(index.row(), index.column());
1177 if(index.column() == m_oldCol)
1178 d = d.addDays(1);
1179 SetSelectedDate(d);
1180 }
1181 }
1182 break;
1183 default:
1184 break;
1185 };
1186 break;
1187 }
1188 default:
1189 break;
1190 }
1191
1192 return QWidget::eventFilter(watched, event);
1193}
1194
1196{
1197 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
1198 if(!pModel) return true;
1199 return pModel->EnableHolidays(bEnable);
1200}
1201
1203{
1204 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
1205 if(!pModel) return true;
1206 return pModel->EnableSolarTerm(bEnable);
1207}
1208
1209int CLunarCalendar::SetTaskHandle(QSharedPointer<CTaskHandler> handler)
1210{
1211 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
1212 if(!pModel) return -1;
1213 return pModel->SetTaskHandle(handler);
1214}
1215
1216#if HAS_CPP_11
1218 std::function<uint (/*in*/const QDate&,
1219 /*out*/QStringList &)> cbHandler)
1220{
1221 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
1222 if(!pModel) return -1;
1223 return pModel->SetTaskHandle(cbHandler);
1224}
1225#endif
1226
1228{
1229 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
1230 if(!pModel)
1231 return -1;
1232 int nRet = pModel->SetViewType(type);
1233
1234 int yearMin = pModel->GetMinimumDate().year();
1236 {
1237 pModel->GetMinimumDate().weekNumber(&yearMin);
1238 if(m_bShowBackgroupImage)
1239 m_View.setStyleSheet("border-image:none");
1240 }
1241 int yearMax = pModel->GetMaximumDate().year();
1243 pModel->GetMaximumDate().weekNumber(&yearMax);
1244
1245 SetYearRange(yearMin, yearMax);
1246 m_cmbYear.setCurrentIndex(m_cmbYear.findData(pModel->GetShowYear()));
1247
1248 SetSelectedDate(QDate::currentDate(), true);
1249
1250 switch (GetViewType()) {
1252 m_cmbMonth.setToolTip(tr("Week"));
1253 m_cmbMonth.setStatusTip(tr("Week"));
1254 break;
1256 m_cmbMonth.setToolTip(tr("Month"));
1257 m_cmbMonth.setStatusTip(tr("Month"));
1258 break;
1259 }
1260
1261 updateGeometry();
1262 return nRet;
1263}
1264
1266{
1267 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
1268 if(!pModel)
1270 return pModel->GetViewType();
1271}
1272
1273int CLunarCalendar::SetTouchUpDownFunction(_TOUCH_UP_DOWN_FUNCTION f)
1274{
1275 m_TouchFunction = f;
1276 return 0;
1277}
1278
1280{
1281 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
1282 if(!pModel)
1283 return static_cast<_CalendarType>(
1284 static_cast<int>(_CalendarType::CalendarTypeLunar)
1285 | static_cast<int>(_CalendarType::CalendarTypeSolar));
1286 return pModel->GetCalendarType();
1287}
1288
1290{
1291 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
1292 if(!pModel)
1293 return -1;
1294 int nRet = pModel->SetCalendarType(type);
1295 UpdateViewModel();
1296
1297 return nRet;
1298}
1299
1300int CLunarCalendar::LoadCalendarTable(const QString &szFile)
1301{
1302 return CLunarTable::Instance()->Load(szFile);
1303}
1304
1306 int nThreadNumber, bool bClearCache,
1307 bool bSaveAllDate)
1308{
1309 return CLunarTable::Instance()->Generate(
1310 this->MinimumDate(),
1311 MaximumDate(),
1312 szFile,
1313 nThreadNumber,
1314 bClearCache,
1315 bSaveAllDate);
1316}
1317
1318void CLunarCalendar::slotTimeout()
1319{
1320 m_lbTime.setText(QTime::currentTime().toString(locale().timeFormat())); // + " " + QString::number(QTime::currentTime().msec()));
1321}
1322
1324{
1325 m_lbTime.setVisible(bShow);
1326 if(bShow)
1327 m_Timer.start(1000);
1328 else
1329 m_Timer.stop();
1330 updateGeometry();
1331}
1332
1333int CLunarCalendar::SetBackgroup(const QString &szFile)
1334{
1335 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
1336 if(!pModel)
1337 return -1;
1338
1339 if(m_bShowBackgroupImage)
1340 m_View.setStyleSheet("border-image:url(" + szFile + ")");
1341 else
1342 m_View.setStyleSheet("border-image:none");
1343 return 0;
1344}
1345
1347{
1348 int nRet = UpdateViewModel(true);
1349 return nRet;
1350}
1351
1352//*
1353void CLunarCalendar::setFont(const QFont& font)
1354{
1355 QWidget::setFont(font);
1356 m_View.setFont(font);
1357 m_View.horizontalHeader()->setFont(font);
1358 m_View.verticalHeader()->setFont(font);
1359}//*/
1360
1361QSize CLunarCalendar::sizeHint() const
1362{
1363 return minimumSizeHint();
1364}
1365
1366QSize CLunarCalendar::minimumSizeHint() const
1367{
1368 CLunarCalendarModel* pModel = dynamic_cast<CLunarCalendarModel*>(m_View.model());
1369 if(!pModel)
1370 return QSize();
1371
1372 ensurePolished();
1373 int w = 0;
1374 int h = 0;
1375
1376 int rows = pModel->rowCount();
1377 int cols = pModel->columnCount();
1378
1379 int marginW = m_View.horizontalHeader()->style()->pixelMetric(
1380 QStyle::PM_FocusFrameHMargin) << 1;
1381 int marginH = m_View.verticalHeader()->style()->pixelMetric(
1382 QStyle::PM_FocusFrameVMargin) << 1;
1383 QMargins cm = m_View.horizontalHeader()->contentsMargins();
1384 w = (m_View.horizontalHeader()->minimumSectionSize() + marginW) * cols
1385 + marginW + cm.left() + cm.right();
1386 if(m_View.verticalHeader()->isVisible())
1387 {
1388// qDebug(Logger) << "m_View.verticalHeader()->isVisible()";
1389 w += m_View.verticalHeader()->sizeHint().width();
1390 }
1391 cm = m_View.verticalHeader()->contentsMargins();
1393 h = (m_View.verticalHeader()->minimumSectionSize() + marginH) * rows;
1394 else
1395 h = (m_View.verticalHeader()->minimumSectionSize() + marginH);
1396 h = h + marginH + cm.top() + cm.bottom();
1397 if(m_View.horizontalHeader()->isVisible())
1398 {
1399// qDebug(Logger) << "m_View.horizontalHeader()->isVisible()";
1400 h += m_View.horizontalHeader()->sizeHint().height();
1401 }
1402
1403// qDebug(Logger) << "w:" << w << "h:" << h << "marginW:" << marginW
1404// << "m_View.horizontalHeader()->minimumSectionSize():"
1405// << m_View.horizontalHeader()->minimumSectionSize()
1406// << "m_View.verticalHeader()->minimumSectionSize():"
1407// << m_View.verticalHeader()->minimumSectionSize()
1408// << "marginH:" << marginH << cm
1409// << "ver height:" << m_View.verticalHeader()->sizeHint()
1410// << "hor width:" << m_View.horizontalHeader()->sizeHint();
1411
1412 //add the size of the header.
1413 int headerH = 0;
1414 int headerW = 0;
1415 if(m_tbPreYear.isVisible())
1416 {
1417 QSize s = m_tbPreYear.sizeHint();
1418 headerH = s.height();
1419 headerW = s.width();
1420 }
1421 if(m_cmbYear.isVisible())
1422 {
1423 QSize s = m_cmbYear.sizeHint();
1424 headerH = qMax(headerH, s.height());
1425 headerW += s.width();
1426 }
1427 if(m_tbNextYear.isVisible())
1428 {
1429 QSize s = m_tbNextYear.sizeHint();
1430 headerH = qMax(headerH, s.height());
1431 headerW += s.width();
1432 }
1433 if(m_tbPreMonth.isVisible())
1434 {
1435 QSize s = m_tbPreMonth.sizeHint();
1436 headerH = qMax(headerH, s.height());
1437 headerW += s.width();
1438 }
1439 if(m_cmbMonth.isVisible())
1440 {
1441 QSize s = m_cmbMonth.sizeHint();
1442 headerH = qMax(headerH, s.height());
1443 headerW += s.width();
1444 }
1445 if(m_tbNextMonth.isVisible())
1446 {
1447 QSize s = m_tbNextMonth.sizeHint();
1448 headerH = qMax(headerH, s.height());
1449 headerW += s.width();
1450 }
1451 if(m_pbToday.isVisible())
1452 {
1453 QSize s = m_pbToday.sizeHint();
1454 headerH = qMax(headerH, s.height());
1455 headerW += s.width();
1456 }
1457 if(m_lbDate.isVisible())
1458 {
1459 QSize s = m_lbDate.sizeHint();
1460 headerH += s.height();
1461 headerW = qMax(headerW, s.width());
1462 }
1463 if(m_lbTime.isVisible())
1464 {
1465 QSize s = m_lbTime.sizeHint();
1466 headerH += s.height();
1467 headerW = qMax(headerW, s.width());
1468 }
1469
1470 switch (m_Headposition) {
1473 w = qMax(w, headerW);
1474 h += headerH;
1475 break;
1478 w += headerW;
1479 h = qMax(h, headerH);
1480 break;
1481 default:
1482 break;
1483 }
1484
1485 cm = contentsMargins();
1486 w += cm.left() + cm.right();
1487 h += cm.top() + cm.bottom();
1488
1489// qDebug(Logger) << "w:" << w << "h:" << h;
1490
1491 return QSize(w, h);
1492}
1493
1494const QString CLunarCalendar::Version()
1495{
1496 QString szVersion(tr("Version:"));
1497
1498#ifdef LunarCalendar_VERSION
1499 szVersion += LunarCalendar_VERSION;
1500#endif
1501
1502#ifdef LunarCalendar_REVISION
1503 szVersion += " (" + tr("Revision: [") + LunarCalendar_REVISION
1504 + "](https://github.com/KangLin/LunarCalendar/tree/"
1505 + LunarCalendar_REVISION + "))";
1506#endif
1507
1508 return szVersion;
1509}
QString GetLunar()
得到指定日期的农历(汉字)
int GetDay()
得到指定日期的农历天数
QString GetJieQi()
得到节气
int GetMonth()
得到指定日期的农历月份
int GetYear()
得到指定日期的农历年份
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
得到数据
农历日历类
const QDate SelectedDate() const
得到当前选择的日期。 当前选择的日期在指定的最小日期 MinimumDate() 与最大日期 MaximumDate() 的范围内。 默认为系统当前日期。
const QDate MaximumDate() const
得到日历的支持最大日期
void SetDateRange(const QDate &min, const QDate &max)
设置日历的支持日期的范围
bool EnableSolarTerm(bool bEnable=true)
允许或禁用节气
const int GetShowMonth() const
得到当前界面显示的月份
const int GetShowYear() const
得到当前界面显示的年份
const Qt::DayOfWeek FirstDayOfWeek() const
得到年周从哪天开始
int SetCalendarType(_CalendarType type)
设置日历类型(组合)
void SetSelectedDate(const QDate &date, bool bForce=false)
设置当前选择的日期
void SetMinimumDate(const QDate &date)
设置日历的支持最小日期
int Update()
更新数据和视图
int SetTaskHandle(QSharedPointer< CTaskHandler > handler)
设置 使用者自定义任务接口
void ShowHead(bool bShow)
显示或隐藏日历头
void ShowBackgroupImage(bool show)
显示或隐藏背景图片
const QString SelectedLunar() const
得到当前选择的日期的农历的字符串
void ShowDate(bool bShow)
显示或隐藏选择日期
int LoadCalendarTable(const QString &szFile)
从缓存文件中得到农历
const _CalendarType GetCalendarType() const
得到日历类型
void sigSelectionChanged()
当前选择日期改变时触发 可以在相应的槽函数中调用下列函数
_VIEW_TYPE
视图类型
void ShowToday(bool bShow)
显示或隐藏今日工具按钮
const QString SelectedSolarTerm() const
得到当前选的节气
void ShowWeekHead(bool bShow)
显示或隐藏周
void ShowGrid(bool show)
显示或隐藏网格
const _VIEW_TYPE GetViewType() const
得到视图类型
void ShowTime(bool bShow)
显示或隐藏当前时间
int SetHeadposition(_HEAD_position pos=_HEAD_position::Top)
设置日历头的位置
bool EnableHolidays(bool bEnable=true)
允许或禁用节日
_CalendarType
日历类型
void ShowTools(bool bShow)
显示或隐藏日期工具按钮
int SetViewType(_VIEW_TYPE type)
 设置视图类型
int GenerateCalendarTable(const QString &szFile, int nThreadNumber=2, bool bClearCache=false, bool bSaveAllDate=true)
产生日历缓存表
void SetMaximumDate(const QDate &date)
设置日历的支持最大日期
void ShowWeeks(bool bShow)
显示或隐藏第几周
const QDate MinimumDate() const
得到日历的支持最小日期
static int GetLunar(const QDate date, int &year, int &month, int &day)
得到给定日期的农历(公历转农历)
void EnableToolTip(bool show)
显示或隐藏工具提示