#include "widget.h"
#include <QPainter>
ShadowCalendar::ShadowCalendar(QWidget *parent)
: QWidget(parent)
{
setMouseTracking(true);
int d=1;
int m = 3;
for (int row = 0; row < 6; row++) {
for (int column = 0; column < 7; column++) {
DateItem item ;
item.year = 2024;
item.month =m;
item .day =d;
d++;
if(d>=31){
m++;
d=1;
}
dateItem[row][column] =item;
}
}
}
ShadowCalendar::~ShadowCalendar()
{
}
void ShadowCalendar::updateCalendar(const QDate &selectDate)
{
this->selectDate = selectDate;
}
void ShadowCalendar::leaveEvent(QEvent *)
{
testAttribute(Qt::WidgetAttribute::WA_UnderMouse);
update();
}
void ShadowCalendar::mouseMoveEvent(QMouseEvent *)
{
testAttribute(Qt::WidgetAttribute::WA_Hover);
update();
}
void ShadowCalendar::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing);
int sw = 336;
int sh = 336;
qreal scaleX = this->width() * 1.0 / sw;
qreal scaleY = this->height() * 1.0 / sh;
painter.scale(scaleX, scaleY);
painter.setPen(Qt::NoPen);
painter.fillRect(0, 0, sw, sh, bgColor);
qreal iw = sw / 7.0;
qreal ih = sh / 7.0;
//mask
QPointF globalpoint = this->mapFromGlobal(QCursor::pos());
const QPointF &point = QPointF(globalpoint.x() / scaleX, globalpoint.y() / scaleY);
//绘制光晕背景
if (this->underMouse()) {
int effectradius = 58;
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
QRadialGradient radialGrad(point, effectradius);
radialGrad.setColorAt(0, QColor(0, 0, 0, 0));
radialGrad.setColorAt(1, QColor(0, 0, 0, 255));
painter.setBrush(radialGrad);
painter.drawEllipse(point, effectradius, effectradius);
painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
painter.setBrush(Qt::NoBrush);
for (int row = 0; row < 6; row++) {
for (int column = 0; column < 7; column++) {
QRectF rect = QRectF(column * iw, (row + 1) * ih, iw, ih).adjusted(3, 3, -3, -3);
if (rect.contains(point)) {
painter.save();
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.setPen(QPen(QColor(220, 220, 220, 160), 2));
painter.drawRoundedRect(rect, 2, 2);
painter.restore();
continue;
} else {
painter.setPen(QPen(shadowColor, 2));
}
painter.drawRoundedRect(rect, 2, 2);
}
}
//绘制圆形的光晕底层背景
painter.fillRect(0, 0, sw, sh, QColor(200, 200, 200, 50));
}
//绘制头部中文数字,先设置图像叠加模式为源在上面
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.setPen(textColor);
QStringList listHead;
listHead << "一" << "二" << "三" << "四" << "五" << "六" << "日";
for (int i = 0; i < 7; i++) {
painter.drawText(i * iw, 0, iw, ih, Qt::AlignCenter, listHead.at(i));
}
//绘制日期
for (int row = 0; row < 6; row++) {
for (int column = 0; column < 7; column++) {
if (dateItem[row][column].day > 0) {
QRectF rect = QRectF(column * iw, (row + 1) * ih, iw, ih).adjusted(3, 3, -3, -3);
//如果是选中的日期则突出绘制背景
if (QDate::currentDate() == QDate(dateItem[row][column].year, dateItem[row][column].month, dateItem[row][column].day)) {
painter.setPen(QPen(selectColor, 2));
painter.setBrush(Qt::NoBrush);
//如果和光晕效果重叠则边框高亮
if (rect.contains(point)) {
painter.setPen(QPen(selectColor.lighter(), 2));
}
//绘制圆角边框
painter.drawRoundedRect(rect, 2, 2);
//绘制里边背景
painter.setPen(Qt::NoPen);
painter.setBrush(selectColor);
painter.drawRoundedRect(rect.adjusted(4, 4, -4, -4), 2, 2);
}
painter.setPen(textColor);
painter.drawText(rect, Qt::AlignCenter, QString::number(dateItem[row][column].day));
}
}
}
}
QColor ShadowCalendar::getBgColor() const
{
return bgColor;
}
QColor ShadowCalendar::getTextColor() const
{
return textColor;
}
QColor ShadowCalendar::getShadowColor() const
{
return shadowColor;
}
QColor ShadowCalendar::getSelectColor() const
{
return selectColor;
}
QSize ShadowCalendar::sizeHint() const
{
return QSize (50,50);
}
QSize ShadowCalendar::minimumSizeHint() const
{
return QSize(40,40);
}
void ShadowCalendar::setBgColor(const QColor &bgColor)
{
this->bgColor = bgColor;
}
void ShadowCalendar::setTextColor(const QColor &textColor)
{
this->textColor =textColor;
}
void ShadowCalendar::setShadowColor(const QColor &shadowColor)
{
this->shadowColor= shadowColor;
}
void ShadowCalendar::setSelectColor(const QColor &selectColor)
{
this ->selectColor= selectColor;
}
Lo_1558_ve
- 粉丝: 0
- 资源: 32
最新资源
- 木工台锯 木板切割机sw18可编辑全套技术资料100%好用.zip
- HTML/CSS/JavaScript实现圣诞树与飘雪花效果
- Q-GDW10929.5-2018信息系统应用安全第5部分代码安全检测
- RA8876 + STM32F103 LVDS VGA 驱动的线路图
- 基于扩散模型逆向生成的图像超分辨率方法研究与应用
- 脉冲布袋除尘器sw18可编辑全套技术资料100%好用.zip
- 字符分割函数,方便分割字符串
- 数据湖构建(Data Lake Formation,DLF)-大数据管理和分析解决方案
- 基于SSM 的家庭财务记账系统的设计与实现
- 旅游网站用户行为数据集.zip
- 内裤松紧带绷缝机 sw18可编辑全套技术资料100%好用.zip
- 视频游戏检测3-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- python入门-表达式语句.pdf
- python基于tensorflow的人脸识别系统设计与实现源码+说明.zip
- 电子钟程序(已补充完成).zip
- (3298038)数学建模 matlab 课件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈