#include "customCalendarWidget.h"

#include <QTableView>
#include <QLocale>
#include <QPainter>
#include <QTextCharFormat>
#include <QHeaderView>

CustomCalendarWidget::CustomCalendarWidget(QWidget *parent)
    : QCalendarWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    //setAttribute(Qt::WA_TranslucentBackground);

    initialize();
}

CustomCalendarWidget::~CustomCalendarWidget()
{
}

void CustomCalendarWidget::initialize()
{
    setLocale(QLocale(QLocale::Chinese));
    setNavigationBarVisible(false);
    setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
    setHorizontalHeaderFormat(QCalendarWidget::SingleLetterDayNames);
    setSelectionMode(SingleSelection);
    //qss中设置无效，需要这里获取具体子部件做全透明，然后在外部创建对象处设置需要的透明值,eg:obj->setStyleSheet("background-color:rgba(32,47,62,150);");
    QTableView *view = this->findChild<QTableView*>("qt_calendar_calendarview");
    if (view)
    {
        view->setStyleSheet("QTableView{color:rgb(250,250,250);}");
        //view->horizontalHeader()->setStyleSheet("QHeaderView::section{background-color:transparent;color:rgb(250,250,250);}");
    }

    QTextCharFormat format;
    QFont font;
    font.setFamily("微软雅黑");
    font.setPointSize(12);
    format.setFont(font);
    //format.setForeground(QBrush(QColor(250, 250, 250))); //QT6下通过设置前景色无效，需要通过上面用setStyleSheet实现
    format.setBackground(Qt::transparent);
    setHeaderTextFormat(format);

    // QTextCharFormat formatWeekday;
    // formatWeekday.setForeground(QBrush(QColor(250, 250, 250)));
    // formatWeekday.setBackground(Qt::transparent);
    // setWeekdayTextFormat(Qt::Saturday, formatWeekday);
    // setWeekdayTextFormat(Qt::Sunday, formatWeekday);
    // setWeekdayTextFormat(Qt::Monday, formatWeekday);
    // setWeekdayTextFormat(Qt::Tuesday, formatWeekday);
    // setWeekdayTextFormat(Qt::Wednesday, formatWeekday);
    // setWeekdayTextFormat(Qt::Thursday, formatWeekday);
    // setWeekdayTextFormat(Qt::Friday, formatWeekday);
}

// void CustomCalendarWidget::paintEvent(QPaintEvent *event)
// {
// }

void CustomCalendarWidget::paintCell(QPainter *painter, const QRect &rect, QDate date) const
{
    if (date == selectedDate())
    {
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing);
        //背景
        painter->setPen(Qt::NoPen);
        painter->setBrush(QColor(76, 86, 102, 220));
        painter->drawRoundedRect(rect.x(), rect.y(), rect.width(), rect.height(), 2, 2);
        //文字
        painter->setPen(QColor(250, 250, 250));
        QFont font;
        font.setFamily("微软雅黑");
        font.setPointSize(11);
        painter->setFont(font);
        painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));
        painter->restore();
    }
    else if (date == QDate::currentDate())
    {
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing);
        //文字
        painter->setPen(QColor(90, 188, 231));
        QFont font;
        font.setFamily("微软雅黑");
        font.setPointSize(12);
        font.setBold(true);
        painter->setFont(font);
        painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));
        painter->restore();
    }
    else if (date > QDate::currentDate())
    {
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing);
        //文字
        painter->setPen(QColor(150, 150, 150));
        QFont font;
        font.setFamily("微软雅黑");
        font.setPointSize(11);
        font.setBold(true);
        painter->setFont(font);
        painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));
        painter->restore();
    }
    else
    {
        //QCalendarWidget::paintCell(painter, rect, date);
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing);
        //文字
        painter->setPen(QColor(250, 250, 250));
        QFont font;
        font.setFamily("微软雅黑");
        font.setPointSize(11);
        painter->setFont(font);
        painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));
        painter->restore();
    }
}
