| 1 |
/* |
| 2 |
* libkysdk-qtwidgets's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2024, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhenyu Wang <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#pragma execution_character_set("utf-8") |
| 24 |
|
| 25 |
#include "kminicalendarwidget.h" |
| 26 |
#include "kborderlessbutton.h" |
| 27 |
#include "kpushbutton.h" |
| 28 |
#include "qdatetime.h" |
| 29 |
#include "qfontdatabase.h" |
| 30 |
#include "qlayout.h" |
| 31 |
#include "themeController.h" |
| 32 |
#include <QPainter> |
| 33 |
#include <QPainterPath> |
| 34 |
|
| 35 |
#include <QGraphicsDropShadowEffect> |
| 36 |
//#include "kshadowhelper.h" //2503 |
| 37 |
|
| 38 |
namespace kdk |
| 39 |
{ |
| 40 |
class KMiniCalendarWidgetPrivate : public QObject, public ThemeController |
| 41 |
{ |
| 42 |
Q_DECLARE_PUBLIC(KMiniCalendarWidget) |
| 43 |
Q_OBJECT |
| 44 |
public: |
| 45 |
KMiniCalendarWidgetPrivate(KMiniCalendarWidget *parent); |
| 46 |
|
| 47 |
protected: |
| 48 |
void changeTheme(); |
| 49 |
|
| 50 |
private: |
| 51 |
KMiniCalendarWidget *q_ptr; |
| 52 |
KMiniCalendarWidget::CalendarStyle calendarStyle; // 整体样式 |
| 53 |
KMiniCalendarWidget::WeekNameFormat weekNameFormat; // 星期名称格式 |
| 54 |
KMiniCalendarWidget::SelectType selectType; // 选中模式 |
| 55 |
bool btnClick; // 按钮单击,避开下拉选择重复触发 |
| 56 |
QLabel *labelYearMonth; // 年月 |
| 57 |
QList<QLabel *> labWeeks; // 顶部星期名称 |
| 58 |
QList<KMiniCalendarItem *> dayItems; // 日期元素 |
| 59 |
|
| 60 |
QDate date; // 当前日期 |
| 61 |
|
| 62 |
QColor weekTextColor; // 星期名称文字颜色 |
| 63 |
QColor weekBgColor; // 星期名称背景色 |
| 64 |
|
| 65 |
bool showLunar; // 显示农历 |
| 66 |
bool enableBorder; // 显示边框 |
| 67 |
|
| 68 |
QColor borderColor; // 边框颜色 |
| 69 |
QColor weekColor; // 周末颜色 |
| 70 |
QColor lunarColor; // 农历节日颜色 |
| 71 |
|
| 72 |
QColor currentTextColor; // 当前月文字颜色 |
| 73 |
QColor otherTextColor; // 其他月文字颜色 |
| 74 |
QColor selectTextColor; // 选中日期文字颜色 |
| 75 |
QColor hoverTextColor; // 悬停日期文字颜色 |
| 76 |
|
| 77 |
QColor currentLunarColor; // 当前月农历文字颜色 |
| 78 |
QColor otherLunarColor; // 其他月农历文字颜色 |
| 79 |
QColor selectLunarColor; // 选中日期农历文字颜色 |
| 80 |
QColor hoverLunarColor; // 悬停日期农历文字颜色 |
| 81 |
|
| 82 |
QColor currentBgColor; // 当前月背景颜色 |
| 83 |
QColor otherBgColor; // 其他月背景颜色 |
| 84 |
QColor selectBgColor; // 选中日期背景颜色 |
| 85 |
QColor hoverBgColor; // 悬停日期背景颜色 |
| 86 |
}; |
| 87 |
|
| 88 |
KMiniCalendarWidgetPrivate::KMiniCalendarWidgetPrivate(KMiniCalendarWidget *parent) |
| 89 |
: q_ptr(parent) |
| 90 |
{ |
| 91 |
Q_Q(KMiniCalendarWidget); |
| 92 |
btnClick = false; |
| 93 |
|
| 94 |
calendarStyle = KMiniCalendarWidget::CalendarStyle_Red; |
| 95 |
weekNameFormat = KMiniCalendarWidget::WeekNameFormat_Short; |
| 96 |
date = QDate::currentDate(); |
| 97 |
|
| 98 |
weekBgColor = QColor(255, 255, 255); |
| 99 |
weekBgColor = QColor(255, 255, 255); |
| 100 |
|
| 101 |
showLunar = false; |
| 102 |
enableBorder = false; |
| 103 |
selectType = KMiniCalendarWidget::SelectType_Circle; |
| 104 |
|
| 105 |
borderColor = QColor(180, 180, 180); |
| 106 |
weekColor = QColor(0, 0, 0); |
| 107 |
lunarColor = QColor(55, 156, 238); |
| 108 |
|
| 109 |
currentTextColor = QColor(0, 0, 0); |
| 110 |
otherTextColor = QColor(200, 200, 200); |
| 111 |
selectTextColor = QColor(255, 255, 255); |
| 112 |
hoverTextColor = QColor(250, 250, 250); |
| 113 |
|
| 114 |
currentLunarColor = QColor(179, 179, 179); |
| 115 |
otherLunarColor = QColor(179, 179, 179); |
| 116 |
selectLunarColor = QColor(179, 179, 179); |
| 117 |
hoverLunarColor = QColor(179, 179, 179); |
| 118 |
|
| 119 |
currentBgColor = QColor(255, 255, 255); |
| 120 |
otherBgColor = QColor(255, 255, 255); |
| 121 |
selectBgColor = ThemeController::getCustomColorFromDT("kbrand-normal"); |
| 122 |
hoverBgColor = QColor(180, 180, 180); |
| 123 |
} |
| 124 |
|
| 125 |
void KMiniCalendarWidgetPrivate::changeTheme() |
| 126 |
{ |
| 127 |
Q_Q(KMiniCalendarWidget); |
| 128 |
selectBgColor = ThemeController::getCustomColorFromDT("kbrand-normal"); |
| 129 |
|
| 130 |
q->initStyle(); |
| 131 |
q->update(); |
| 132 |
} |
| 133 |
|
| 134 |
KMiniCalendarWidget::KMiniCalendarWidget(QWidget *parent) |
| 135 |
: QWidget(parent) |
| 136 |
, d_ptr(new KMiniCalendarWidgetPrivate(this)) |
| 137 |
{ |
| 138 |
Q_D(KMiniCalendarWidget); |
| 139 |
setWindowFlags(Qt::Popup | Qt::FramelessWindowHint); |
| 140 |
setAttribute(Qt::WA_TranslucentBackground); |
| 141 |
// effects::KShadowHelper::self()->setWidget(this,15); |
| 142 |
|
| 143 |
QGraphicsDropShadowEffect *m_pShadow_effect = new QGraphicsDropShadowEffect(this); |
| 144 |
m_pShadow_effect->setOffset(0, 0); |
| 145 |
m_pShadow_effect->setColor(ThemeController::getCustomColorFromDT("text-disable")); |
| 146 |
m_pShadow_effect->setBlurRadius(15); |
| 147 |
setContentsMargins(10,10,10,10); |
| 148 |
this->setGraphicsEffect(m_pShadow_effect); |
| 149 |
|
| 150 |
connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 151 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 152 |
{ |
| 153 |
d->changeTheme(); |
| 154 |
} |
| 155 |
}); |
| 156 |
d->changeTheme(); |
| 157 |
|
| 158 |
initWidget(); |
| 159 |
initStyle(); |
| 160 |
initDate(); |
| 161 |
} |
| 162 |
|
| 163 |
KMiniCalendarWidget::~KMiniCalendarWidget() |
| 164 |
{ |
| 165 |
} |
| 166 |
|
| 167 |
void KMiniCalendarWidget::initWidget() |
| 168 |
{ |
| 169 |
Q_D(KMiniCalendarWidget); |
| 170 |
this->setObjectName("miniCalendarWidget"); |
| 171 |
|
| 172 |
// 顶部widget |
| 173 |
QWidget *widgetTop = new QWidget(); |
| 174 |
widgetTop->setObjectName("widgetTop"); |
| 175 |
widgetTop->setMinimumHeight(36); |
| 176 |
|
| 177 |
// 上个月按钮 |
| 178 |
kdk::KBorderlessButton *btnPrevMonth = new kdk::KBorderlessButton(); |
| 179 |
btnPrevMonth->setObjectName("btnPrevMonth"); |
| 180 |
btnPrevMonth->setFixedSize(45, 36); |
| 181 |
btnPrevMonth->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
| 182 |
btnPrevMonth->setIcon(QIcon::fromTheme("ukui-start-symbolic")); |
| 183 |
|
| 184 |
// 下个月按钮 |
| 185 |
kdk::KBorderlessButton *btnNextMonth = new kdk::KBorderlessButton(); |
| 186 |
btnNextMonth->setObjectName("btnNextMonth"); |
| 187 |
btnNextMonth->setFixedSize(45, 36); |
| 188 |
btnNextMonth->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); |
| 189 |
btnNextMonth->setIcon(QIcon::fromTheme("ukui-end-symbolic")); |
| 190 |
|
| 191 |
d->labelYearMonth = new QLabel(); |
| 192 |
d->labelYearMonth->setFixedSize(96, 36); |
| 193 |
d->labelYearMonth->setAlignment(Qt::AlignCenter); |
| 194 |
QFont labelFont; |
| 195 |
labelFont.setFamily("Microsoft Yahei"); |
| 196 |
labelFont.setPixelSize(14); |
| 197 |
d->labelYearMonth->setFont(labelFont); |
| 198 |
|
| 199 |
// 顶部横向布局 |
| 200 |
QHBoxLayout *layoutTop = new QHBoxLayout(widgetTop); |
| 201 |
layoutTop->setContentsMargins(0, 0, 0, 0); |
| 202 |
layoutTop->addWidget(btnPrevMonth); |
| 203 |
layoutTop->addWidget(d->labelYearMonth); |
| 204 |
layoutTop->addWidget(btnNextMonth); |
| 205 |
|
| 206 |
// 星期widget |
| 207 |
QWidget *widgetWeek = new QWidget(); |
| 208 |
widgetWeek->setObjectName("widgetWeek"); |
| 209 |
widgetWeek->setMinimumHeight(30); |
| 210 |
|
| 211 |
// 星期布局 |
| 212 |
QHBoxLayout *layoutWeek = new QHBoxLayout(widgetWeek); |
| 213 |
layoutWeek->setContentsMargins(0, 0, 0, 0); |
| 214 |
layoutWeek->setSpacing(0); |
| 215 |
|
| 216 |
for (int i = 0; i < 7; i++) { |
| 217 |
QLabel *lab = new QLabel; |
| 218 |
lab->setAlignment(Qt::AlignCenter); |
| 219 |
layoutWeek->addWidget(lab); |
| 220 |
d->labWeeks.append(lab); |
| 221 |
} |
| 222 |
|
| 223 |
setWeekNameFormat(WeekNameFormat_Long); |
| 224 |
|
| 225 |
// 日期标签widget |
| 226 |
QWidget *widgetBody = new QWidget(); |
| 227 |
widgetBody->setObjectName("widgetBody"); |
| 228 |
|
| 229 |
// 日期标签布局 |
| 230 |
QGridLayout *layoutBody = new QGridLayout(widgetBody); |
| 231 |
layoutBody->setContentsMargins(0, 0, 0, 0); |
| 232 |
layoutBody->setHorizontalSpacing(0); |
| 233 |
layoutBody->setVerticalSpacing(0); |
| 234 |
|
| 235 |
// 逐个添加日标签 |
| 236 |
for (int i = 0; i < 42; i++) { |
| 237 |
KMiniCalendarItem *lab = new KMiniCalendarItem(); |
| 238 |
lab->setObjectName("KMiniCalendarItem"); |
| 239 |
connect(lab, SIGNAL(clicked(QDate, KMiniCalendarItem::DayType)), this, SLOT(slotClicked(QDate, KMiniCalendarItem::DayType))); |
| 240 |
layoutBody->addWidget(lab, i / 7, i % 7); |
| 241 |
d->dayItems.append(lab); |
| 242 |
} |
| 243 |
|
| 244 |
// 主布局 |
| 245 |
QVBoxLayout *verLayoutCalendar = new QVBoxLayout(this); |
| 246 |
verLayoutCalendar->setContentsMargins(15, 10, 15, 15); |
| 247 |
verLayoutCalendar->setSpacing(0); |
| 248 |
verLayoutCalendar->addWidget(widgetTop); |
| 249 |
verLayoutCalendar->addWidget(widgetWeek); |
| 250 |
verLayoutCalendar->addWidget(widgetBody, 1); |
| 251 |
|
| 252 |
// 绑定按钮和下拉框信号 |
| 253 |
connect(btnPrevMonth, SIGNAL(clicked(bool)), this, SLOT(showPreviousMonth())); |
| 254 |
connect(btnNextMonth, SIGNAL(clicked(bool)), this, SLOT(showNextMonth())); |
| 255 |
} |
| 256 |
|
| 257 |
void KMiniCalendarWidget::initStyle() |
| 258 |
{ |
| 259 |
Q_D(KMiniCalendarWidget); |
| 260 |
// 设置样式 |
| 261 |
QStringList qss; |
| 262 |
// 星期名称样式 |
| 263 |
qss.append(QString("QLabel{background:%1;color:%2;}").arg(d->weekBgColor.name()).arg(d->weekTextColor.name())); |
| 264 |
// 边框 |
| 265 |
qss.append("QWidget#widgetBody{border:0px;}"); |
| 266 |
|
| 267 |
// 顶部下拉框 |
| 268 |
qss.append(QString("QToolButton{padding:0px;background:none;border:none;border-radius:5px;}")); |
| 269 |
// qss.append(QString("QToolButton:hover{background:rgb(180,180,180);color:#FFFFFF;}")); |
| 270 |
|
| 271 |
// 自定义日控件颜色 |
| 272 |
QString strSelectType; |
| 273 |
|
| 274 |
if (d->selectType == SelectType_Rect) { |
| 275 |
strSelectType = "SelectType_Rect"; |
| 276 |
} else if (d->selectType == SelectType_Circle) { |
| 277 |
strSelectType = "SelectType_Circle"; |
| 278 |
} else if (d->selectType == SelectType_Triangle) { |
| 279 |
strSelectType = "SelectType_Triangle"; |
| 280 |
} |
| 281 |
|
| 282 |
qss.append(QString("KMiniCalendarItem{qproperty-showLunar:%1;}").arg(d->showLunar)); |
| 283 |
qss.append(QString("KMiniCalendarItem{qproperty-selectType:%1;}").arg(strSelectType)); |
| 284 |
qss.append(QString("KMiniCalendarItem{qproperty-borderColor:%1;}").arg(d->borderColor.name())); |
| 285 |
qss.append(QString("KMiniCalendarItem{qproperty-weekColor:%1;}").arg(d->weekColor.name())); |
| 286 |
qss.append(QString("KMiniCalendarItem{qproperty-lunarColor:%1;}").arg(d->lunarColor.name())); |
| 287 |
qss.append(QString("KMiniCalendarItem{qproperty-currentTextColor:%1;}").arg(d->currentTextColor.name())); |
| 288 |
qss.append(QString("KMiniCalendarItem{qproperty-otherTextColor:%1;}").arg(d->otherTextColor.name())); |
| 289 |
qss.append(QString("KMiniCalendarItem{qproperty-selectTextColor:%1;}").arg(d->selectTextColor.name())); |
| 290 |
qss.append(QString("KMiniCalendarItem{qproperty-hoverTextColor:%1;}").arg(d->hoverTextColor.name())); |
| 291 |
qss.append(QString("KMiniCalendarItem{qproperty-currentLunarColor:%1;}").arg(d->currentLunarColor.name())); |
| 292 |
qss.append(QString("KMiniCalendarItem{qproperty-otherLunarColor:%1;}").arg(d->otherLunarColor.name())); |
| 293 |
qss.append(QString("KMiniCalendarItem{qproperty-selectLunarColor:%1;}").arg(d->selectLunarColor.name())); |
| 294 |
qss.append(QString("KMiniCalendarItem{qproperty-hoverLunarColor:%1;}").arg(d->hoverLunarColor.name())); |
| 295 |
qss.append(QString("KMiniCalendarItem{qproperty-currentBgColor:%1;}").arg(d->currentBgColor.name())); |
| 296 |
qss.append(QString("KMiniCalendarItem{qproperty-otherBgColor:%1;}").arg(d->otherBgColor.name())); |
| 297 |
qss.append(QString("KMiniCalendarItem{qproperty-selectBgColor:%1;}").arg(d->selectBgColor.name())); |
| 298 |
qss.append(QString("KMiniCalendarItem{qproperty-hoverBgColor:%1;}").arg(d->hoverBgColor.name())); |
| 299 |
|
| 300 |
for (KMiniCalendarItem *dayItem : d->dayItems) { |
| 301 |
dayItem->setShowLunar(d->showLunar); |
| 302 |
dayItem->setSelectType(static_cast<kdk::KMiniCalendarItem::SelectType>(d->selectType)); |
| 303 |
dayItem->setBorderColor(d->borderColor); |
| 304 |
dayItem->setWeekColor(d->weekColor); |
| 305 |
dayItem->setLunarColor(d->lunarColor); |
| 306 |
dayItem->setCurrentTextColor(d->currentTextColor); |
| 307 |
dayItem->setOtherTextColor(d->otherTextColor); |
| 308 |
dayItem->setSelectTextColor(d->selectTextColor); |
| 309 |
dayItem->setHoverTextColor(d->hoverTextColor); |
| 310 |
dayItem->setCurrentLunarColor(d->currentLunarColor); |
| 311 |
dayItem->setOtherLunarColor(d->otherLunarColor); |
| 312 |
dayItem->setSelectLunarColor(d->selectLunarColor); |
| 313 |
dayItem->setHoverLunarColor(d->hoverLunarColor); |
| 314 |
dayItem->setCurrentBgColor(d->currentBgColor); |
| 315 |
dayItem->setOtherBgColor(d->otherBgColor); |
| 316 |
dayItem->setSelectBgColor(d->selectBgColor); |
| 317 |
dayItem->setHoverBgColor(d->hoverBgColor); |
| 318 |
} |
| 319 |
|
| 320 |
this->setStyleSheet(qss.join("")); |
| 321 |
} |
| 322 |
|
| 323 |
// 初始化日期面板 |
| 324 |
void KMiniCalendarWidget::initDate() |
| 325 |
{ |
| 326 |
Q_D(KMiniCalendarWidget); |
| 327 |
int year = d->date.year(); |
| 328 |
int month = d->date.month(); |
| 329 |
|
| 330 |
// 设置为今天,设置变量防止重复触发 |
| 331 |
d->btnClick = true; |
| 332 |
d->labelYearMonth->setText(QString("%1年%2月").arg(year).arg(month)); |
| 333 |
d->btnClick = false; |
| 334 |
|
| 335 |
// 首先判断当前月的第一天是星期几 |
| 336 |
int week = KLunarCalendarInfo::Instance()->getFirstDayOfWeek(year, month, false); |
| 337 |
// 当前月天数 |
| 338 |
int countDay = KLunarCalendarInfo::Instance()->getMonthDays(year, month); |
| 339 |
// 上月天数 |
| 340 |
int countDayPre = KLunarCalendarInfo::Instance()->getMonthDays(1 == month ? year - 1 : year, 1 == month ? 12 : month - 1); |
| 341 |
|
| 342 |
// 如果上月天数上月刚好一周则另外处理 |
| 343 |
int startPre, endPre, startNext, endNext, index, tempYear, tempMonth, tempDay; |
| 344 |
|
| 345 |
if (0 == week) { |
| 346 |
startPre = 0; |
| 347 |
endPre = 7; |
| 348 |
startNext = 0; |
| 349 |
endNext = 42 - (countDay + 7); |
| 350 |
} else { |
| 351 |
startPre = 0; |
| 352 |
endPre = week; |
| 353 |
startNext = week + countDay; |
| 354 |
endNext = 42; |
| 355 |
} |
| 356 |
|
| 357 |
// 纠正1月份前面部分偏差,1月份前面部分是上一年12月份 |
| 358 |
tempYear = year; |
| 359 |
tempMonth = month - 1; |
| 360 |
|
| 361 |
if (tempMonth < 1) { |
| 362 |
tempYear--; |
| 363 |
tempMonth = 12; |
| 364 |
} |
| 365 |
|
| 366 |
// 显示上月天数 |
| 367 |
for (int i = startPre; i < endPre; i++) { |
| 368 |
index = i; |
| 369 |
tempDay = countDayPre - endPre + i + 1; |
| 370 |
|
| 371 |
QDate date(tempYear, tempMonth, tempDay); |
| 372 |
QString lunar = KLunarCalendarInfo::Instance()->getLunarDay(tempYear, tempMonth, tempDay); |
| 373 |
d->dayItems.at(index)->setDate(date, lunar, KMiniCalendarItem::DayType_MonthPre); |
| 374 |
} |
| 375 |
|
| 376 |
// 纠正12月份后面部分偏差,12月份后面部分是下一年1月份 |
| 377 |
tempYear = year; |
| 378 |
tempMonth = month + 1; |
| 379 |
|
| 380 |
if (tempMonth > 12) { |
| 381 |
tempYear++; |
| 382 |
tempMonth = 1; |
| 383 |
} |
| 384 |
|
| 385 |
// 显示下月天数 |
| 386 |
for (int i = startNext; i < endNext; i++) { |
| 387 |
index = 42 - endNext + i; |
| 388 |
tempDay = i - startNext + 1; |
| 389 |
|
| 390 |
QDate date(tempYear, tempMonth, tempDay); |
| 391 |
QString lunar = KLunarCalendarInfo::Instance()->getLunarDay(tempYear, tempMonth, tempDay); |
| 392 |
d->dayItems.at(index)->setDate(date, lunar, KMiniCalendarItem::DayType_MonthNext); |
| 393 |
} |
| 394 |
|
| 395 |
// 重新置为当前年月 |
| 396 |
tempYear = year; |
| 397 |
tempMonth = month; |
| 398 |
|
| 399 |
// 显示当前月 |
| 400 |
for (int i = week; i < (countDay + week); i++) { |
| 401 |
index = (0 == week ? (i + 7) : i); |
| 402 |
tempDay = i - week + 1; |
| 403 |
|
| 404 |
QDate date(tempYear, tempMonth, tempDay); |
| 405 |
QString lunar = KLunarCalendarInfo::Instance()->getLunarDay(tempYear, tempMonth, tempDay); |
| 406 |
|
| 407 |
if (0 == (i % 7) || 6 == (i % 7)) { |
| 408 |
d->dayItems.at(index)->setDate(date, lunar, KMiniCalendarItem::DayType_WeekEnd); |
| 409 |
} else { |
| 410 |
d->dayItems.at(index)->setDate(date, lunar, KMiniCalendarItem::DayType_MonthCurrent); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
dayChanged(d->date); |
| 415 |
} |
| 416 |
|
| 417 |
void KMiniCalendarWidget::slotClicked(const QDate &date, const KMiniCalendarItem::DayType &dayType) |
| 418 |
{ |
| 419 |
Q_D(KMiniCalendarWidget); |
| 420 |
d->date = date; |
| 421 |
|
| 422 |
if (KMiniCalendarItem::DayType_MonthPre == dayType) { |
| 423 |
d->date = date.addMonths(1); |
| 424 |
showPreviousMonth(); |
| 425 |
} else if (KMiniCalendarItem::DayType_MonthNext == dayType) { |
| 426 |
d->date = date.addMonths(-1); |
| 427 |
showNextMonth(); |
| 428 |
} else { |
| 429 |
dayChanged(d->date); |
| 430 |
} |
| 431 |
|
| 432 |
this->close(); |
| 433 |
} |
| 434 |
|
| 435 |
void KMiniCalendarWidget::dayChanged(const QDate &date) |
| 436 |
{ |
| 437 |
Q_D(KMiniCalendarWidget); |
| 438 |
// 计算星期几,当前天对应标签索引=日期+星期几-1 |
| 439 |
int year = date.year(); |
| 440 |
int month = date.month(); |
| 441 |
int day = date.day(); |
| 442 |
int week = KLunarCalendarInfo::Instance()->getFirstDayOfWeek(year, month, false); |
| 443 |
// qDebug() << QString("%1-%2-%3").arg(year).arg(month).arg(day); |
| 444 |
|
| 445 |
// 选中当前日期,其他日期恢复,这里还有优化空间,比方说类似单选框机制 |
| 446 |
for (int i = 0; i < 42; i++) { |
| 447 |
// 当月第一天是星期天要另外计算 |
| 448 |
int index = day + week - 1; |
| 449 |
|
| 450 |
if (week == 0) { |
| 451 |
index = day + 6; |
| 452 |
} |
| 453 |
|
| 454 |
d->dayItems.at(i)->setSelect(i == index); |
| 455 |
} |
| 456 |
|
| 457 |
// 发送日期单击信号 |
| 458 |
emit clicked(date); |
| 459 |
// 发送日期更新信号 |
| 460 |
emit selectionChanged(); |
| 461 |
} |
| 462 |
|
| 463 |
void KMiniCalendarWidget::dateChanged(int year, int month, int day) |
| 464 |
{ |
| 465 |
Q_D(KMiniCalendarWidget); |
| 466 |
// 如果原有天大于28则设置为1,防止出错 |
| 467 |
d->date.setDate(year, month, day > 28 ? 1 : day); |
| 468 |
initDate(); |
| 469 |
} |
| 470 |
|
| 471 |
KMiniCalendarWidget::CalendarStyle KMiniCalendarWidget::getCalendarStyle() const |
| 472 |
{ |
| 473 |
Q_D(const KMiniCalendarWidget); |
| 474 |
return d->calendarStyle; |
| 475 |
} |
| 476 |
|
| 477 |
KMiniCalendarWidget::WeekNameFormat KMiniCalendarWidget::getWeekNameFormat() const |
| 478 |
{ |
| 479 |
Q_D(const KMiniCalendarWidget); |
| 480 |
return d->weekNameFormat; |
| 481 |
} |
| 482 |
|
| 483 |
QDate KMiniCalendarWidget::getDate() const |
| 484 |
{ |
| 485 |
Q_D(const KMiniCalendarWidget); |
| 486 |
return d->date; |
| 487 |
} |
| 488 |
|
| 489 |
QColor KMiniCalendarWidget::getWeekTextColor() const |
| 490 |
{ |
| 491 |
Q_D(const KMiniCalendarWidget); |
| 492 |
return d->weekTextColor; |
| 493 |
} |
| 494 |
|
| 495 |
QColor KMiniCalendarWidget::getWeekBgColor() const |
| 496 |
{ |
| 497 |
Q_D(const KMiniCalendarWidget); |
| 498 |
return d->weekBgColor; |
| 499 |
} |
| 500 |
|
| 501 |
bool KMiniCalendarWidget::getShowLunar() const |
| 502 |
{ |
| 503 |
Q_D(const KMiniCalendarWidget); |
| 504 |
return d->showLunar; |
| 505 |
} |
| 506 |
|
| 507 |
bool KMiniCalendarWidget::getEnableBorder() const |
| 508 |
{ |
| 509 |
Q_D(const KMiniCalendarWidget); |
| 510 |
return d->enableBorder; |
| 511 |
} |
| 512 |
|
| 513 |
KMiniCalendarWidget::SelectType KMiniCalendarWidget::getSelectType() const |
| 514 |
{ |
| 515 |
Q_D(const KMiniCalendarWidget); |
| 516 |
return d->selectType; |
| 517 |
} |
| 518 |
|
| 519 |
QColor KMiniCalendarWidget::getBorderColor() const |
| 520 |
{ |
| 521 |
Q_D(const KMiniCalendarWidget); |
| 522 |
return d->borderColor; |
| 523 |
} |
| 524 |
|
| 525 |
QColor KMiniCalendarWidget::getWeekColor() const |
| 526 |
{ |
| 527 |
Q_D(const KMiniCalendarWidget); |
| 528 |
return d->weekColor; |
| 529 |
} |
| 530 |
|
| 531 |
QColor KMiniCalendarWidget::getLunarColor() const |
| 532 |
{ |
| 533 |
Q_D(const KMiniCalendarWidget); |
| 534 |
return d->lunarColor; |
| 535 |
} |
| 536 |
|
| 537 |
QColor KMiniCalendarWidget::getCurrentTextColor() const |
| 538 |
{ |
| 539 |
Q_D(const KMiniCalendarWidget); |
| 540 |
return d->currentTextColor; |
| 541 |
} |
| 542 |
|
| 543 |
QColor KMiniCalendarWidget::getOtherTextColor() const |
| 544 |
{ |
| 545 |
Q_D(const KMiniCalendarWidget); |
| 546 |
return d->otherTextColor; |
| 547 |
} |
| 548 |
|
| 549 |
QColor KMiniCalendarWidget::getSelectTextColor() const |
| 550 |
{ |
| 551 |
Q_D(const KMiniCalendarWidget); |
| 552 |
return d->selectTextColor; |
| 553 |
} |
| 554 |
|
| 555 |
QColor KMiniCalendarWidget::getHoverTextColor() const |
| 556 |
{ |
| 557 |
Q_D(const KMiniCalendarWidget); |
| 558 |
return d->hoverTextColor; |
| 559 |
} |
| 560 |
|
| 561 |
QColor KMiniCalendarWidget::getCurrentLunarColor() const |
| 562 |
{ |
| 563 |
Q_D(const KMiniCalendarWidget); |
| 564 |
return d->currentLunarColor; |
| 565 |
} |
| 566 |
|
| 567 |
QColor KMiniCalendarWidget::getOtherLunarColor() const |
| 568 |
{ |
| 569 |
Q_D(const KMiniCalendarWidget); |
| 570 |
return d->otherLunarColor; |
| 571 |
} |
| 572 |
|
| 573 |
QColor KMiniCalendarWidget::getSelectLunarColor() const |
| 574 |
{ |
| 575 |
Q_D(const KMiniCalendarWidget); |
| 576 |
return d->selectLunarColor; |
| 577 |
} |
| 578 |
|
| 579 |
QColor KMiniCalendarWidget::getHoverLunarColor() const |
| 580 |
{ |
| 581 |
Q_D(const KMiniCalendarWidget); |
| 582 |
return d->hoverLunarColor; |
| 583 |
} |
| 584 |
|
| 585 |
QColor KMiniCalendarWidget::getCurrentBgColor() const |
| 586 |
{ |
| 587 |
Q_D(const KMiniCalendarWidget); |
| 588 |
return d->currentBgColor; |
| 589 |
} |
| 590 |
|
| 591 |
QColor KMiniCalendarWidget::getOtherBgColor() const |
| 592 |
{ |
| 593 |
Q_D(const KMiniCalendarWidget); |
| 594 |
return d->otherBgColor; |
| 595 |
} |
| 596 |
|
| 597 |
QColor KMiniCalendarWidget::getSelectBgColor() const |
| 598 |
{ |
| 599 |
Q_D(const KMiniCalendarWidget); |
| 600 |
return d->selectBgColor; |
| 601 |
} |
| 602 |
|
| 603 |
QColor KMiniCalendarWidget::getHoverBgColor() const |
| 604 |
{ |
| 605 |
Q_D(const KMiniCalendarWidget); |
| 606 |
return d->hoverBgColor; |
| 607 |
} |
| 608 |
|
| 609 |
QSize KMiniCalendarWidget::sizeHint() const |
| 610 |
{ |
| 611 |
Q_D(const KMiniCalendarWidget); |
| 612 |
return QSize(600, 500); |
| 613 |
} |
| 614 |
|
| 615 |
QSize KMiniCalendarWidget::minimumSizeHint() const |
| 616 |
{ |
| 617 |
Q_D(const KMiniCalendarWidget); |
| 618 |
return QSize(200, 150); |
| 619 |
} |
| 620 |
|
| 621 |
// 显示上月日期 |
| 622 |
void KMiniCalendarWidget::showPreviousMonth() |
| 623 |
{ |
| 624 |
Q_D(KMiniCalendarWidget); |
| 625 |
int year = d->date.year(); |
| 626 |
int month = d->date.month(); |
| 627 |
int day = d->date.day(); |
| 628 |
|
| 629 |
if (year <= 1901 && month == 1) { |
| 630 |
return; |
| 631 |
} |
| 632 |
|
| 633 |
month--; |
| 634 |
|
| 635 |
if (month < 1) { |
| 636 |
month = 12; |
| 637 |
year--; |
| 638 |
} |
| 639 |
|
| 640 |
dateChanged(year, month, day); |
| 641 |
} |
| 642 |
|
| 643 |
// 显示下月日期 |
| 644 |
void KMiniCalendarWidget::showNextMonth() |
| 645 |
{ |
| 646 |
Q_D(KMiniCalendarWidget); |
| 647 |
int year = d->date.year(); |
| 648 |
int month = d->date.month(); |
| 649 |
int day = d->date.day(); |
| 650 |
|
| 651 |
if (year >= 2099 && month == 12) { |
| 652 |
return; |
| 653 |
} |
| 654 |
|
| 655 |
month++; |
| 656 |
|
| 657 |
if (month > 12) { |
| 658 |
month = 1; |
| 659 |
year++; |
| 660 |
} |
| 661 |
|
| 662 |
dateChanged(year, month, day); |
| 663 |
} |
| 664 |
|
| 665 |
// 转到今天 |
| 666 |
void KMiniCalendarWidget::showToday() |
| 667 |
{ |
| 668 |
Q_D(KMiniCalendarWidget); |
| 669 |
d->date = QDate::currentDate(); |
| 670 |
initDate(); |
| 671 |
dayChanged(d->date); |
| 672 |
} |
| 673 |
|
| 674 |
void KMiniCalendarWidget::setCalendarStyle(const KMiniCalendarWidget::CalendarStyle &calendarStyle) |
| 675 |
{ |
| 676 |
Q_D(KMiniCalendarWidget); |
| 677 |
|
| 678 |
if (d->calendarStyle != calendarStyle) { |
| 679 |
d->calendarStyle = calendarStyle; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
void KMiniCalendarWidget::setWeekNameFormat(const KMiniCalendarWidget::WeekNameFormat &weekNameFormat) |
| 684 |
{ |
| 685 |
Q_D(KMiniCalendarWidget); |
| 686 |
|
| 687 |
if (d->weekNameFormat != weekNameFormat) { |
| 688 |
d->weekNameFormat = weekNameFormat; |
| 689 |
|
| 690 |
QStringList listWeek; |
| 691 |
|
| 692 |
if (weekNameFormat == WeekNameFormat_Short) { |
| 693 |
listWeek << "一" |
| 694 |
<< "二" |
| 695 |
<< "三" |
| 696 |
<< "四" |
| 697 |
<< "五" |
| 698 |
<< "六" |
| 699 |
<< "日"; |
| 700 |
} else if (weekNameFormat == WeekNameFormat_Normal) { |
| 701 |
listWeek << "周一" |
| 702 |
<< "周二" |
| 703 |
<< "周三" |
| 704 |
<< "周四" |
| 705 |
<< "周五" |
| 706 |
<< "周六" |
| 707 |
<< "周日"; |
| 708 |
} else if (weekNameFormat == WeekNameFormat_Long) { |
| 709 |
listWeek << "星期一" |
| 710 |
<< "星期二" |
| 711 |
<< "星期三" |
| 712 |
<< "星期四" |
| 713 |
<< "星期五" |
| 714 |
<< "星期六" |
| 715 |
<< "星期天"; |
| 716 |
} else if (weekNameFormat == WeekNameFormat_En) { |
| 717 |
listWeek << "Mon" |
| 718 |
<< "Tue" |
| 719 |
<< "Wed" |
| 720 |
<< "Thu" |
| 721 |
<< "Fri" |
| 722 |
<< "Sat" |
| 723 |
<< "Sun"; |
| 724 |
} |
| 725 |
|
| 726 |
// 逐个添加日期文字 |
| 727 |
for (int i = 0; i < 7; i++) { |
| 728 |
d->labWeeks.at(i)->setText(listWeek.at(i)); |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
void KMiniCalendarWidget::setDate(const QDate &date) |
| 734 |
{ |
| 735 |
Q_D(KMiniCalendarWidget); |
| 736 |
|
| 737 |
if (d->date != date) { |
| 738 |
d->date = date; |
| 739 |
initDate(); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
void KMiniCalendarWidget::setWeekTextColor(const QColor &weekTextColor) |
| 744 |
{ |
| 745 |
Q_D(KMiniCalendarWidget); |
| 746 |
|
| 747 |
if (d->weekTextColor != weekTextColor) { |
| 748 |
d->weekTextColor = weekTextColor; |
| 749 |
initStyle(); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
void KMiniCalendarWidget::setWeekBgColor(const QColor &weekBgColor) |
| 754 |
{ |
| 755 |
Q_D(KMiniCalendarWidget); |
| 756 |
|
| 757 |
if (d->weekBgColor != weekBgColor) { |
| 758 |
d->weekBgColor = weekBgColor; |
| 759 |
initStyle(); |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
void KMiniCalendarWidget::setShowLunar(bool showLunar) |
| 764 |
{ |
| 765 |
Q_D(KMiniCalendarWidget); |
| 766 |
|
| 767 |
if (d->showLunar != showLunar) { |
| 768 |
d->showLunar = showLunar; |
| 769 |
initStyle(); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
void KMiniCalendarWidget::setEnableBorder(bool bVisible) |
| 774 |
{ |
| 775 |
Q_D(KMiniCalendarWidget); |
| 776 |
|
| 777 |
if (d->enableBorder != bVisible) { |
| 778 |
d->enableBorder = bVisible; |
| 779 |
update(); |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
void KMiniCalendarWidget::setSelectType(const KMiniCalendarWidget::SelectType &selectType) |
| 784 |
{ |
| 785 |
Q_D(KMiniCalendarWidget); |
| 786 |
|
| 787 |
if (d->selectType != selectType) { |
| 788 |
d->selectType = selectType; |
| 789 |
initStyle(); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
void KMiniCalendarWidget::setBorderColor(const QColor &borderColor) |
| 794 |
{ |
| 795 |
Q_D(KMiniCalendarWidget); |
| 796 |
|
| 797 |
if (d->borderColor != borderColor) { |
| 798 |
d->borderColor = borderColor; |
| 799 |
initStyle(); |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
void KMiniCalendarWidget::setWeekColor(const QColor &weekColor) |
| 804 |
{ |
| 805 |
Q_D(KMiniCalendarWidget); |
| 806 |
|
| 807 |
if (d->weekColor != weekColor) { |
| 808 |
d->weekColor = weekColor; |
| 809 |
initStyle(); |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
void KMiniCalendarWidget::setLunarColor(const QColor &lunarColor) |
| 814 |
{ |
| 815 |
Q_D(KMiniCalendarWidget); |
| 816 |
|
| 817 |
if (d->lunarColor != lunarColor) { |
| 818 |
d->lunarColor = lunarColor; |
| 819 |
initStyle(); |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
void KMiniCalendarWidget::setCurrentTextColor(const QColor ¤tTextColor) |
| 824 |
{ |
| 825 |
Q_D(KMiniCalendarWidget); |
| 826 |
|
| 827 |
if (d->currentTextColor != currentTextColor) { |
| 828 |
d->currentTextColor = currentTextColor; |
| 829 |
initStyle(); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
void KMiniCalendarWidget::setOtherTextColor(const QColor &otherTextColor) |
| 834 |
{ |
| 835 |
Q_D(KMiniCalendarWidget); |
| 836 |
|
| 837 |
if (d->otherTextColor != otherTextColor) { |
| 838 |
d->otherTextColor = otherTextColor; |
| 839 |
initStyle(); |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
void KMiniCalendarWidget::setSelectTextColor(const QColor &selectTextColor) |
| 844 |
{ |
| 845 |
Q_D(KMiniCalendarWidget); |
| 846 |
|
| 847 |
if (d->selectTextColor != selectTextColor) { |
| 848 |
d->selectTextColor = selectTextColor; |
| 849 |
initStyle(); |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
void KMiniCalendarWidget::setHoverTextColor(const QColor &hoverTextColor) |
| 854 |
{ |
| 855 |
Q_D(KMiniCalendarWidget); |
| 856 |
|
| 857 |
if (d->hoverTextColor != hoverTextColor) { |
| 858 |
d->hoverTextColor = hoverTextColor; |
| 859 |
initStyle(); |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
void KMiniCalendarWidget::setCurrentLunarColor(const QColor ¤tLunarColor) |
| 864 |
{ |
| 865 |
Q_D(KMiniCalendarWidget); |
| 866 |
|
| 867 |
if (d->currentLunarColor != currentLunarColor) { |
| 868 |
d->currentLunarColor = currentLunarColor; |
| 869 |
initStyle(); |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
void KMiniCalendarWidget::setOtherLunarColor(const QColor &otherLunarColor) |
| 874 |
{ |
| 875 |
Q_D(KMiniCalendarWidget); |
| 876 |
|
| 877 |
if (d->otherLunarColor != otherLunarColor) { |
| 878 |
d->otherLunarColor = otherLunarColor; |
| 879 |
initStyle(); |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
void KMiniCalendarWidget::setSelectLunarColor(const QColor &selectLunarColor) |
| 884 |
{ |
| 885 |
Q_D(KMiniCalendarWidget); |
| 886 |
|
| 887 |
if (d->selectLunarColor != selectLunarColor) { |
| 888 |
d->selectLunarColor = selectLunarColor; |
| 889 |
initStyle(); |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
void KMiniCalendarWidget::setHoverLunarColor(const QColor &hoverLunarColor) |
| 894 |
{ |
| 895 |
Q_D(KMiniCalendarWidget); |
| 896 |
|
| 897 |
if (d->hoverLunarColor != hoverLunarColor) { |
| 898 |
d->hoverLunarColor = hoverLunarColor; |
| 899 |
initStyle(); |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
void KMiniCalendarWidget::setCurrentBgColor(const QColor ¤tBgColor) |
| 904 |
{ |
| 905 |
Q_D(KMiniCalendarWidget); |
| 906 |
|
| 907 |
if (d->currentBgColor != currentBgColor) { |
| 908 |
d->currentBgColor = currentBgColor; |
| 909 |
initStyle(); |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
void KMiniCalendarWidget::setOtherBgColor(const QColor &otherBgColor) |
| 914 |
{ |
| 915 |
Q_D(KMiniCalendarWidget); |
| 916 |
|
| 917 |
if (d->otherBgColor != otherBgColor) { |
| 918 |
d->otherBgColor = otherBgColor; |
| 919 |
initStyle(); |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
void KMiniCalendarWidget::setSelectBgColor(const QColor &selectBgColor) |
| 924 |
{ |
| 925 |
Q_D(KMiniCalendarWidget); |
| 926 |
|
| 927 |
if (d->selectBgColor != selectBgColor) { |
| 928 |
d->selectBgColor = selectBgColor; |
| 929 |
initStyle(); |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
void KMiniCalendarWidget::setHoverBgColor(const QColor &hoverBgColor) |
| 934 |
{ |
| 935 |
Q_D(KMiniCalendarWidget); |
| 936 |
|
| 937 |
if (d->hoverBgColor != hoverBgColor) { |
| 938 |
d->hoverBgColor = hoverBgColor; |
| 939 |
initStyle(); |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
void KMiniCalendarWidget::wheelEvent(QWheelEvent *event) |
| 944 |
{ |
| 945 |
Q_D(KMiniCalendarWidget); |
| 946 |
|
| 947 |
if (event->angleDelta().ry() > 100) { |
| 948 |
showPreviousMonth(); |
| 949 |
} else if (event->angleDelta().ry() < -100) { |
| 950 |
showNextMonth(); |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
void KMiniCalendarWidget::paintEvent(QPaintEvent *event) |
| 955 |
{ |
| 956 |
Q_D(KMiniCalendarWidget); |
| 957 |
QPainter painter(this); |
| 958 |
painter.setRenderHint(QPainter::Antialiasing, true); // 开启抗锯齿 |
| 959 |
|
| 960 |
// 圆角矩形的尺寸和圆角大小 |
| 961 |
QRect rect(0, 0, width(), height()); |
| 962 |
rect.adjust(8,8,-8,-8); |
| 963 |
int radius = ThemeController::getRadiusFromDT("kradius-window"); |
| 964 |
if (radius == -1) |
| 965 |
radius = 12; |
| 966 |
|
| 967 |
// 使用QPainterPath来绘制圆角矩形 |
| 968 |
QPainterPath path; |
| 969 |
path.addRoundedRect(rect, radius, radius); |
| 970 |
|
| 971 |
// 设置画刷 |
| 972 |
painter.setBrush(QBrush(d->weekBgColor)); // 设置背景色 |
| 973 |
// 设置画笔颜色为灰色 |
| 974 |
QPen pen(QColor(233, 233, 233), 1); // 创建一个灰色、宽度为1的画笔 |
| 975 |
painter.setPen(pen); // 将画笔应用于QPainter |
| 976 |
|
| 977 |
// 绘制圆角矩形(这将包括边框和背景) |
| 978 |
painter.drawPath(path); |
| 979 |
|
| 980 |
// 调用基类的paintEvent来绘制子控件(如果有的话) |
| 981 |
QWidget::paintEvent(event); |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
#include "kminicalendarwidget.moc" |
| 986 |
#include "moc_kminicalendarwidget.cpp" |