calendarex.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "calendarex.h"
  2. #include <QEvent>
  3. #include <QScreen>
  4. #include <QKeyEvent>
  5. #include <QApplication>
  6. #include <QDesktopWidget>
  7. #include <QHBoxLayout>
  8. #include <QDebug>
  9. #include "scopeselectionmodel.h"
  10. #include "PaintHelper/painthelper.h"
  11. CalendarInterface::CalendarInterface(QWidget *parent)
  12. : QWidget(parent)
  13. , IDropShadowable(this)
  14. , m_pLayout(new QHBoxLayout)
  15. {
  16. setWindowFlag(Qt::Popup);
  17. setWindowFlag(Qt::NoDropShadowWindowHint);
  18. //设置无边框属性
  19. setWindowFlag(Qt::FramelessWindowHint);
  20. //设置背景透明属性
  21. setAttribute(Qt::WA_TranslucentBackground, true);
  22. //关闭对话框时,删除自身对象
  23. setAttribute(Qt::WA_DeleteOnClose, true);
  24. }
  25. CalendarInterface::~CalendarInterface()
  26. {
  27. //qDebug()<<"~ScopeCalendar";
  28. }
  29. /**
  30. * @brief 这个只是坐标位移?
  31. * @param q
  32. */
  33. void CalendarInterface::positionCalendarPopup(QWidget *q)
  34. {
  35. if (nullptr == m_pLayout) return;
  36. QPoint pos = (q->layoutDirection() == Qt::RightToLeft) ? q->rect().bottomRight() : q->rect().bottomLeft();
  37. QPoint pos2 = (q->layoutDirection() == Qt::RightToLeft) ? q->rect().topRight() : q->rect().topLeft();
  38. pos = q->mapToGlobal(pos);
  39. pos2 = q->mapToGlobal(pos2);
  40. QSize size = QSize(CALENDAR_WIDTH * m_pLayout->count() + 2 * SHADOW_RADIUS, CALENDAR_HEIGHT + 2 * SHADOW_RADIUS);
  41. //QRect screen = QApplication::desktop()->availableGeometry(pos);
  42. QRect screen = QGuiApplication::screenAt(pos)->availableGeometry();
  43. //handle popup falling "off screen"
  44. if (q->layoutDirection() == Qt::RightToLeft) {
  45. pos.setX(pos.x()-size.width());
  46. pos2.setX(pos2.x()-size.width());
  47. if (pos.x() < screen.left())
  48. pos.setX(qMax(pos.x(), screen.left()));
  49. else if (pos.x()+size.width() > screen.right())
  50. pos.setX(qMax(pos.x()-size.width(), screen.right()-size.width()));
  51. } else {
  52. if (pos.x()+size.width() > screen.right())
  53. pos.setX(screen.right()-size.width());
  54. pos.setX(qMax(pos.x(), screen.left()));
  55. }
  56. if (pos.y() + size.height() > screen.bottom())
  57. pos.setY(pos2.y() - size.height());
  58. else if (pos.y() < screen.top())
  59. pos.setY(screen.top());
  60. if (pos.y() < screen.top())
  61. pos.setY(screen.top());
  62. if (pos.y()+size.height() > screen.bottom())
  63. pos.setY(screen.bottom()-size.height());
  64. pos.setX(pos.rx() - SHADOW_RADIUS);
  65. pos.setY(pos.ry() - SHADOW_RADIUS + SPACING);
  66. move(pos);
  67. }
  68. CalendarEx::CalendarEx(const QDate& defaultDate, QWidget* parent) :
  69. CalendarInterface(parent),
  70. m_pCalendar(nullptr)
  71. {
  72. QHBoxLayout* pLay = new QHBoxLayout(this);
  73. QWidget* pMain = new QWidget(this);
  74. pMain->setLayout(m_pLayout);
  75. pLay->addWidget(pMain);
  76. pLay->setMargin(SHADOW_RADIUS);
  77. m_pCalendar = new CalendarWidgetEx(this);
  78. m_pCalendar->setStyleSheet("QCalendarView{background-color: transparent;}");
  79. m_pLayout->addWidget(m_pCalendar);
  80. connect(m_pCalendar, &CalendarWidgetEx::clicked, this, [this](const QDate& date){
  81. emit sig_DateChanged(date);
  82. close();
  83. });
  84. m_pLayout->setMargin(0);
  85. pMain->resize(CALENDAR_WIDTH, CALENDAR_HEIGHT);
  86. SetCalendarSync(defaultDate);
  87. /* 设置大小,包含阴影的大小 */
  88. resize(CALENDAR_WIDTH + 2 * SHADOW_RADIUS, CALENDAR_HEIGHT + 2 * SHADOW_RADIUS);
  89. /* 设置阴影 */
  90. SetDropShadow(BoxShadow{0, 0, SHADOW_RADIUS, 0, QColor(0, 0, 0, 60), QSize(0, 0), QImage()}, pMain->size());
  91. }
  92. void CalendarEx::SetCalendarSync(const QDate &defaultDate)
  93. {
  94. if (nullptr == m_pCalendar || !defaultDate.isValid()) return;
  95. m_pCalendar->setCurrentPage(defaultDate.year(), defaultDate.month());
  96. m_pCalendar->setSelectedDate(defaultDate);
  97. }
  98. ScopedCalendar::ScopedCalendar(const QDate &from, const QDate &to, QWidget *parent) :
  99. CalendarInterface(parent),
  100. m_pCalendar_L(nullptr),
  101. m_pCalendar_R(nullptr)
  102. {
  103. QHBoxLayout* pLay = new QHBoxLayout(this);
  104. QWidget* pMain = new QWidget(this);
  105. pMain->setLayout(m_pLayout);
  106. pLay->addWidget(pMain);
  107. pLay->setMargin(SHADOW_RADIUS);
  108. ScopeSelectionModel *pDateScopeModel = new ScopeSelectionModel();
  109. if(from.isValid() && to.isValid())
  110. {
  111. pDateScopeModel->dtFirst = from;
  112. pDateScopeModel->dtSecond = to;
  113. pDateScopeModel->bLocked = true;
  114. }
  115. m_pCalendar_L = new CalendarWidgetEx(this);
  116. m_pCalendar_L->setStyleSheet("QCalendarView{background-color: transparent;}");
  117. m_pCalendar_L->SetSelectMode(CalendarWidgetEx::Scope, pDateScopeModel);
  118. m_pCalendar_L->hideNavigatioinButton(false, false, true, true);
  119. m_pLayout->addWidget(m_pCalendar_L);
  120. m_pCalendar_R = new CalendarWidgetEx(this);
  121. m_pCalendar_R->setStyleSheet("QCalendarView{background-color: transparent;}");
  122. m_pCalendar_R->SetSelectMode(CalendarWidgetEx::Scope, pDateScopeModel);
  123. m_pCalendar_R->hideNavigatioinButton(true, true, false, false);
  124. m_pLayout->addWidget(m_pCalendar_R);
  125. m_pLayout->setSpacing(0);
  126. m_pLayout->setMargin(0);
  127. pMain->resize(2 * CALENDAR_WIDTH, CALENDAR_HEIGHT);
  128. SetCalendarSync(from);
  129. connect(pDateScopeModel, &ScopeSelectionModel::sig_ScopeSelected, this, &ScopedCalendar::OnScopeSelected);
  130. resize(2 * CALENDAR_WIDTH + 2 * SHADOW_RADIUS, CALENDAR_HEIGHT + 2 * SHADOW_RADIUS);
  131. SetDropShadow(BoxShadow{0, 0, SHADOW_RADIUS, 0, QColor(0, 0, 0, 60), QSize(0, 0), QImage()}, pMain->size());
  132. }
  133. void ScopedCalendar::SetMinimumDate(const QDate &date)
  134. {
  135. if (nullptr == m_pCalendar_L || nullptr == m_pCalendar_R) return;
  136. m_pCalendar_L->setMinimumDate(date);
  137. m_pCalendar_R->setMinimumDate(date);
  138. }
  139. void ScopedCalendar::SetMaximumDate(const QDate &date)
  140. {
  141. if (nullptr == m_pCalendar_L || nullptr == m_pCalendar_R) return;
  142. m_pCalendar_L->setMaximumDate(date);
  143. m_pCalendar_R->setMaximumDate(date);
  144. }
  145. void ScopedCalendar::OnScopeSelected(const QDate &from, const QDate &to)
  146. {
  147. emit sig_ScopeSelected(from, to);
  148. close();
  149. }
  150. void ScopedCalendar::SetCalendarSync(const QDate &defaultDate)
  151. {
  152. if (nullptr == m_pCalendar_L || nullptr == m_pCalendar_R || !defaultDate.isValid()) return;
  153. m_pCalendar_L->setCurrentPage(defaultDate.year(), defaultDate.month());
  154. connect(m_pCalendar_L, &QCalendarWidget::currentPageChanged, this, [&](int year, int month) {
  155. QDate nextMonth = QDate(year, month, 1).addMonths(1);
  156. m_pCalendar_R->setCurrentPage(nextMonth.year(), nextMonth.month());
  157. });
  158. m_pCalendar_R->setCurrentPage(defaultDate.addMonths(1).year(), defaultDate.addMonths(1).month());
  159. connect(m_pCalendar_R, &QCalendarWidget::currentPageChanged, this, [&](int year, int month) {
  160. QDate prevMonth = QDate(year, month, 1).addMonths(-1);
  161. m_pCalendar_L->setCurrentPage(prevMonth.year(), prevMonth.month());
  162. });
  163. }