cdate.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "cdate.h"
  2. #include <QHBoxLayout>
  3. #include <QPainter>
  4. #include <QEvent>
  5. #include <QDebug>
  6. #include <QMouseEvent>
  7. #include "calendarwidgetex.h"
  8. #include "oneshadow.h"
  9. CDate::CDate(const QDate& defaultDate,QWidget *parent,PopupType type) :
  10. QWidget{parent},
  11. m_parent(parent),
  12. m_type(type)
  13. {
  14. init(defaultDate);
  15. }
  16. CDate::CDate(QWidget *parent,PopupType type) :
  17. QWidget{parent},
  18. m_parent(parent),
  19. m_type(type)
  20. {
  21. // if(type == Popup)
  22. // {
  23. // setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
  24. // }
  25. init(QDate::currentDate());
  26. }
  27. void CDate::paintEvent(QPaintEvent *event)
  28. {
  29. QPainter painter(this);
  30. painter.setRenderHint(QPainter::Antialiasing);
  31. painter.drawImage(QPoint(0,0),m_shadow->image());
  32. }
  33. /**
  34. * @brief 在这里实现点击空白处隐藏或者关闭自身
  35. * @param watched
  36. * @param event
  37. * @return
  38. */
  39. bool CDate::eventFilter(QObject *watched, QEvent *event)
  40. {
  41. auto me = dynamic_cast<QMouseEvent*>(event);
  42. if(nullptr != me)
  43. {
  44. if(me->type() == QEvent::MouseButtonPress)
  45. {
  46. if(m_type == Popup)
  47. {
  48. /* 先将自身区域和鼠标坐标都转换成全局坐标 */
  49. QPoint gTopLeft = this->mapToGlobal(this->rect().topLeft());
  50. QRect gRect(gTopLeft.x(),gTopLeft.y(),this->width(),this->height());
  51. /* 判断此时的鼠标坐标是否在这个控件中 */
  52. if(!gRect.contains(me->globalPos()))
  53. {
  54. // qDebug() << QTime::currentTime() << "CDate关闭了";
  55. /* 发送信号,关闭自身 */
  56. emit signal_close();
  57. emit signal_DateChanged(m_date);
  58. this->close();
  59. }
  60. }
  61. }
  62. }
  63. return QWidget::eventFilter(watched,event);
  64. }
  65. void CDate::init(const QDate &defaultDate)
  66. {
  67. /* 设置无边框 */
  68. setWindowFlags(Qt::FramelessWindowHint);
  69. /* 设置底层样式表,让最底层的透明 */
  70. this->setAttribute(Qt::WA_TranslucentBackground);
  71. /* 将父类的事件注册给自己 */
  72. if(m_type == Popup && m_parent != nullptr)
  73. {
  74. m_parent->installEventFilter(this);
  75. }
  76. /* 设置布局,通过设置layout的Margin距离显示阴影 */
  77. QHBoxLayout* layout = new QHBoxLayout(this);
  78. this->setLayout(layout);
  79. layout->setMargin(RADIUS);
  80. this->resize(CALENDAR_WIDTH + RADIUS*2,CALENDAR_HEIGHT + RADIUS*2);
  81. /* 设置日历组件 */
  82. m_calendarEx = new CalendarWidgetEx(this);
  83. m_calendarEx->resize(CALENDAR_WIDTH,CALENDAR_HEIGHT);
  84. m_calendarEx->setCurrentPage(defaultDate.year(),defaultDate.month());
  85. m_calendarEx->setSelectedDate(defaultDate);
  86. layout->addWidget(m_calendarEx);
  87. /* 设置自身时间 */
  88. m_date = defaultDate;
  89. /* 设置阴影 */
  90. m_shadow = new OneShadow(QSize(m_calendarEx->width(),m_calendarEx->height()),RADIUS);
  91. /* 信号和槽 */
  92. connect(m_calendarEx,&CalendarWidgetEx::clicked,this,[this](const QDate& date){
  93. m_date = date;
  94. emit signal_DateChanged(date);
  95. this->close();
  96. });
  97. }