Debian Codegraph @

kysdk-qtwidgets/src/kminicalendaritem.cpp

raw
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 "kminicalendaritem.h"
26 #include "qdatetime.h"
27 #include "qdebug.h"
28 #include "qevent.h"
29 #include "qpainter.h"
30
31 namespace kdk
32 {
33 class KMiniCalendarItemPrivate : public QObject
34 {
35 Q_OBJECT
36 Q_DECLARE_PUBLIC(KMiniCalendarItem)
37 public:
38 KMiniCalendarItemPrivate(KMiniCalendarItem *parent);
39
40 private:
41 KMiniCalendarItem *q_ptr;
42 KMiniCalendarItem::SelectType selectType; // 选中模式
43 KMiniCalendarItem::DayType dayType; // 当前日类型
44 bool hover; // 鼠标是否悬停
45 bool pressed; // 鼠标是否按下
46
47 bool select; // 是否选中
48 bool showLunar; // 显示农历
49 bool enableBorder; // 显示边框
50
51 QDate date; // 当前日期
52 QString lunar; // 农历信息
53
54 QColor borderColor; // 边框颜色
55 QColor weekColor; // 周末颜色
56 QColor superColor; // 角标颜色
57 QColor lunarColor; // 农历节日颜色
58
59 QColor currentTextColor; // 当前月文字颜色
60 QColor otherTextColor; // 其他月文字颜色
61 QColor selectTextColor; // 选中日期文字颜色
62 QColor hoverTextColor; // 悬停日期文字颜色
63
64 QColor currentLunarColor; // 当前月农历文字颜色
65 QColor otherLunarColor; // 其他月农历文字颜色
66 QColor selectLunarColor; // 选中日期农历文字颜色
67 QColor hoverLunarColor; // 悬停日期农历文字颜色
68
69 QColor currentBgColor; // 当前月背景颜色
70 QColor otherBgColor; // 其他月背景颜色
71 QColor selectBgColor; // 选中日期背景颜色
72 QColor hoverBgColor; // 悬停日期背景颜色
73 };
74
75 KMiniCalendarItemPrivate::KMiniCalendarItemPrivate(KMiniCalendarItem *parent)
76 : q_ptr(parent)
77 {
78 Q_Q(KMiniCalendarItem);
79 hover = false;
80 pressed = false;
81
82 select = false;
83 showLunar = true;
84 enableBorder = false;
85 selectType = KMiniCalendarItem::SelectType_Rect;
86
87 date = QDate::currentDate();
88 lunar = "初一";
89 dayType = KMiniCalendarItem::DayType_MonthCurrent;
90
91 borderColor = QColor(180, 180, 180);
92 weekColor = QColor(255, 0, 0);
93 superColor = QColor(255, 129, 6);
94 lunarColor = QColor(55, 156, 238);
95
96 currentTextColor = QColor(0, 0, 0);
97 otherTextColor = QColor(200, 200, 200);
98 selectTextColor = QColor(255, 255, 255);
99 hoverTextColor = QColor(250, 250, 250);
100
101 currentLunarColor = QColor(150, 150, 150);
102 otherLunarColor = QColor(200, 200, 200);
103 selectLunarColor = QColor(255, 255, 255);
104 hoverLunarColor = QColor(250, 250, 250);
105
106 currentBgColor = QColor(255, 255, 255);
107 otherBgColor = QColor(240, 240, 240);
108 selectBgColor = QColor(208, 47, 18);
109 hoverBgColor = QColor(204, 183, 180);
110 }
111
112 KMiniCalendarItem::KMiniCalendarItem(QWidget *parent)
113 : QWidget(parent)
114 , d_ptr(new KMiniCalendarItemPrivate(this))
115 {
116 Q_D(KMiniCalendarItem);
117 }
118
119 void KMiniCalendarItem::enterEvent(QEvent *)
120 {
121 Q_D(KMiniCalendarItem);
122 d->hover = true;
123 update();
124 }
125
126 void KMiniCalendarItem::leaveEvent(QEvent *)
127 {
128 Q_D(KMiniCalendarItem);
129 d->hover = false;
130 update();
131 }
132
133 void KMiniCalendarItem::mousePressEvent(QMouseEvent *)
134 {
135 Q_D(KMiniCalendarItem);
136 d->pressed = true;
137 update();
138
139 emit clicked(d->date, d->dayType);
140 }
141
142 void KMiniCalendarItem::mouseReleaseEvent(QMouseEvent *)
143 {
144 Q_D(KMiniCalendarItem);
145 d->pressed = false;
146 update();
147 }
148
149 void KMiniCalendarItem::paintEvent(QPaintEvent *)
150 {
151 Q_D(KMiniCalendarItem);
152 // 绘制准备工作,启用反锯齿
153 QPainter painter(this);
154 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
155
156 // 绘制背景和边框
157 drawBg(&painter);
158
159 // 优先绘制选中状态,其次绘制悬停状态
160 if (d->select) {
161 drawBgCurrent(&painter, d->selectBgColor);
162 } else if (d->hover) {
163 drawBgCurrent(&painter, d->hoverBgColor);
164 }
165
166 // 绘制日期
167 drawDay(&painter);
168
169 // 绘制农历信息
170 drawLunar(&painter);
171 }
172
173 void KMiniCalendarItem::drawBg(QPainter *painter)
174 {
175 Q_D(KMiniCalendarItem);
176 painter->save();
177
178 // 根据当前类型选择对应的颜色
179 QColor bgColor = d->currentBgColor;
180
181 if (d->dayType == DayType_MonthPre || d->dayType == DayType_MonthNext) {
182 bgColor = d->otherBgColor;
183 }
184
185 if (getEnableBorder())
186 painter->setPen(d->borderColor);
187 else
188 painter->setPen(bgColor);
189
190 painter->setBrush(bgColor);
191 painter->drawRect(rect());
192
193 painter->restore();
194 }
195
196 void KMiniCalendarItem::drawBgCurrent(QPainter *painter, const QColor &color)
197 {
198 Q_D(KMiniCalendarItem);
199 int width = this->width();
200 int height = this->height();
201 int side = qMin(width, height);
202
203 painter->save();
204
205 painter->setPen(Qt::NoPen);
206 painter->setBrush(color);
207
208 // 根据设定绘制背景样式
209 if (d->selectType == SelectType_Rect) {
210 painter->drawRect(rect());
211 } else if (d->selectType == SelectType_Circle) {
212 int radius = side / 2;
213 painter->drawEllipse(QPointF(width / 2, height / 2), radius, radius);
214 } else if (d->selectType == SelectType_Triangle) {
215 int radius = side / 3;
216 QPolygon pts;
217 pts.setPoints(3, 1, 1, radius, 1, 1, radius);
218 painter->drawRect(rect());
219 painter->setBrush(d->superColor);
220 painter->drawConvexPolygon(pts);
221 }
222
223 painter->restore();
224 }
225
226 void KMiniCalendarItem::drawDay(QPainter *painter)
227 {
228 Q_D(KMiniCalendarItem);
229 int width = this->width();
230 int height = this->height();
231 int side = qMin(width, height);
232
233 painter->save();
234
235 // 根据当前类型选择对应的颜色
236 QColor color = d->currentTextColor;
237
238 if (d->dayType == DayType_MonthPre || d->dayType == DayType_MonthNext) {
239 color = d->otherTextColor;
240 } else if (d->dayType == DayType_WeekEnd) {
241 color = d->weekColor;
242 }
243
244 if (d->select) {
245 color = d->selectTextColor;
246 } else if (d->hover) {
247 color = d->hoverTextColor;
248 }
249
250 painter->setPen(color);
251
252 if (d->showLunar) {
253 QFont font;
254 font.setPixelSize(side / 2.7);
255 painter->setFont(font);
256
257 QRect dayRect = QRect(0, 0, width, height / 1.5);
258 painter->drawText(dayRect, Qt::AlignHCenter | Qt::AlignBottom, QString::number(d->date.day()));
259 } else {
260 QFont font;
261 font.setPixelSize(side / 2);
262 painter->setFont(font);
263
264 QRect dayRect = QRect(0, 0, width, height);
265 painter->drawText(dayRect, Qt::AlignCenter, QString::number(d->date.day()));
266 }
267
268 painter->restore();
269 }
270
271 void KMiniCalendarItem::drawLunar(QPainter *painter)
272 {
273 Q_D(KMiniCalendarItem);
274
275 if (!d->showLunar) {
276 return;
277 }
278
279 int width = this->width();
280 int height = this->height();
281 int side = qMin(width, height);
282
283 painter->save();
284
285 QStringList listDayName;
286 listDayName << "*"
287 << "初一"
288 << "初二"
289 << "初三"
290 << "初四"
291 << "初五"
292 << "初六"
293 << "初七"
294 << "初八"
295 << "初九"
296 << "初十"
297 << "十一"
298 << "十二"
299 << "十三"
300 << "十四"
301 << "十五"
302 << "十六"
303 << "十七"
304 << "十八"
305 << "十九"
306 << "二十"
307 << "廿一"
308 << "廿二"
309 << "廿三"
310 << "廿四"
311 << "廿五"
312 << "廿六"
313 << "廿七"
314 << "廿八"
315 << "廿九"
316 << "三十";
317
318 // 根据当前类型选择对应的颜色
319 QColor color = d->currentLunarColor;
320
321 if (d->dayType == DayType_MonthPre || d->dayType == DayType_MonthNext) {
322 color = d->otherLunarColor;
323 }
324
325 if (d->select) {
326 color = d->selectTextColor;
327 } else if (d->hover) {
328 color = d->hoverTextColor;
329 }
330
331 painter->setPen(color);
332
333 QFont font;
334 font.setPixelSize(side / 4);
335 font.setBold(true);
336 painter->setFont(font);
337
338 QRect lunarRect(0, height / 2, width, height / 2.2);
339 painter->drawText(lunarRect, Qt::AlignCenter, d->lunar);
340
341 painter->restore();
342 }
343
344 bool KMiniCalendarItem::getSelect() const
345 {
346 Q_D(const KMiniCalendarItem);
347 return d->select;
348 }
349
350 bool KMiniCalendarItem::getShowLunar() const
351 {
352 Q_D(const KMiniCalendarItem);
353 return d->showLunar;
354 }
355
356 bool KMiniCalendarItem::getEnableBorder() const
357 {
358 Q_D(const KMiniCalendarItem);
359 return d->enableBorder;
360 }
361
362 KMiniCalendarItem::SelectType KMiniCalendarItem::getSelectType() const
363 {
364 Q_D(const KMiniCalendarItem);
365 return d->selectType;
366 }
367
368 QDate KMiniCalendarItem::getDate() const
369 {
370 Q_D(const KMiniCalendarItem);
371 return d->date;
372 }
373
374 QString KMiniCalendarItem::getLunar() const
375 {
376 Q_D(const KMiniCalendarItem);
377 return d->lunar;
378 }
379
380 KMiniCalendarItem::DayType KMiniCalendarItem::getDayType() const
381 {
382 Q_D(const KMiniCalendarItem);
383 return d->dayType;
384 }
385
386 QColor KMiniCalendarItem::getBorderColor() const
387 {
388 Q_D(const KMiniCalendarItem);
389 return d->borderColor;
390 }
391
392 QColor KMiniCalendarItem::getWeekColor() const
393 {
394 Q_D(const KMiniCalendarItem);
395 return d->weekColor;
396 }
397
398 QColor KMiniCalendarItem::getSuperColor() const
399 {
400 Q_D(const KMiniCalendarItem);
401 return d->superColor;
402 }
403
404 QColor KMiniCalendarItem::getLunarColor() const
405 {
406 Q_D(const KMiniCalendarItem);
407 return d->lunarColor;
408 }
409
410 QColor KMiniCalendarItem::getCurrentTextColor() const
411 {
412 Q_D(const KMiniCalendarItem);
413 return d->currentTextColor;
414 }
415
416 QColor KMiniCalendarItem::getOtherTextColor() const
417 {
418 Q_D(const KMiniCalendarItem);
419 return d->otherTextColor;
420 }
421
422 QColor KMiniCalendarItem::getSelectTextColor() const
423 {
424 Q_D(const KMiniCalendarItem);
425 return d->selectTextColor;
426 }
427
428 QColor KMiniCalendarItem::getHoverTextColor() const
429 {
430 Q_D(const KMiniCalendarItem);
431 return d->hoverTextColor;
432 }
433
434 QColor KMiniCalendarItem::getCurrentLunarColor() const
435 {
436 Q_D(const KMiniCalendarItem);
437 return d->currentLunarColor;
438 }
439
440 QColor KMiniCalendarItem::getOtherLunarColor() const
441 {
442 Q_D(const KMiniCalendarItem);
443 return d->otherLunarColor;
444 }
445
446 QColor KMiniCalendarItem::getSelectLunarColor() const
447 {
448 Q_D(const KMiniCalendarItem);
449 return d->selectLunarColor;
450 }
451
452 QColor KMiniCalendarItem::getHoverLunarColor() const
453 {
454 Q_D(const KMiniCalendarItem);
455 return d->hoverLunarColor;
456 }
457
458 QColor KMiniCalendarItem::getCurrentBgColor() const
459 {
460 Q_D(const KMiniCalendarItem);
461 return d->currentBgColor;
462 }
463
464 QColor KMiniCalendarItem::getOtherBgColor() const
465 {
466 Q_D(const KMiniCalendarItem);
467 return d->otherBgColor;
468 }
469
470 QColor KMiniCalendarItem::getSelectBgColor() const
471 {
472 Q_D(const KMiniCalendarItem);
473 return d->selectBgColor;
474 }
475
476 QColor KMiniCalendarItem::getHoverBgColor() const
477 {
478 Q_D(const KMiniCalendarItem);
479 return d->hoverBgColor;
480 }
481
482 QSize KMiniCalendarItem::sizeHint() const
483 {
484 Q_D(const KMiniCalendarItem);
485 return QSize(100, 100);
486 }
487
488 QSize KMiniCalendarItem::minimumSizeHint() const
489 {
490 Q_D(const KMiniCalendarItem);
491 return QSize(20, 20);
492 }
493
494 void KMiniCalendarItem::setSelect(bool select)
495 {
496 Q_D(KMiniCalendarItem);
497
498 if (d->select != select) {
499 d->select = select;
500 update();
501 }
502 }
503
504 void KMiniCalendarItem::setShowLunar(bool showLunar)
505 {
506 Q_D(KMiniCalendarItem);
507
508 if (d->showLunar != showLunar) {
509 d->showLunar = showLunar;
510 update();
511 }
512 }
513
514 void KMiniCalendarItem::setSelectType(const KMiniCalendarItem::SelectType &selectType)
515 {
516 Q_D(KMiniCalendarItem);
517
518 if (d->selectType != selectType) {
519 d->selectType = selectType;
520 update();
521 }
522 }
523
524 void KMiniCalendarItem::setDate(const QDate &date)
525 {
526 Q_D(KMiniCalendarItem);
527
528 if (d->date != date) {
529 d->date = date;
530 update();
531 }
532 }
533
534 void KMiniCalendarItem::setLunar(const QString &lunar)
535 {
536 Q_D(KMiniCalendarItem);
537
538 if (d->lunar != lunar) {
539 d->lunar = lunar;
540 update();
541 }
542 }
543
544 void KMiniCalendarItem::setDayType(const DayType &dayType)
545 {
546 Q_D(KMiniCalendarItem);
547
548 if (d->dayType != dayType) {
549 d->dayType = dayType;
550 update();
551 }
552 }
553
554 void KMiniCalendarItem::setDate(const QDate &date, const QString &lunar, const DayType &dayType)
555 {
556 Q_D(KMiniCalendarItem);
557 d->date = date;
558 d->lunar = lunar;
559 d->dayType = dayType;
560 update();
561 }
562
563 void KMiniCalendarItem::setEnableBorder(bool bVisible)
564 {
565 Q_D(KMiniCalendarItem);
566
567 if (d->enableBorder != bVisible) {
568 d->enableBorder = bVisible;
569 update();
570 }
571 }
572
573 void KMiniCalendarItem::setBorderColor(const QColor &borderColor)
574 {
575 Q_D(KMiniCalendarItem);
576
577 if (d->borderColor != borderColor) {
578 d->borderColor = borderColor;
579 update();
580 }
581 }
582
583 void KMiniCalendarItem::setWeekColor(const QColor &weekColor)
584 {
585 Q_D(KMiniCalendarItem);
586
587 if (d->weekColor != weekColor) {
588 d->weekColor = weekColor;
589 update();
590 }
591 }
592
593 void KMiniCalendarItem::setSuperColor(const QColor &superColor)
594 {
595 Q_D(KMiniCalendarItem);
596
597 if (d->superColor != superColor) {
598 d->superColor = superColor;
599 update();
600 }
601 }
602
603 void KMiniCalendarItem::setLunarColor(const QColor &lunarColor)
604 {
605 Q_D(KMiniCalendarItem);
606
607 if (d->lunarColor != lunarColor) {
608 d->lunarColor = lunarColor;
609 update();
610 }
611 }
612
613 void KMiniCalendarItem::setCurrentTextColor(const QColor &currentTextColor)
614 {
615 Q_D(KMiniCalendarItem);
616
617 if (d->currentTextColor != currentTextColor) {
618 d->currentTextColor = currentTextColor;
619 update();
620 }
621 }
622
623 void KMiniCalendarItem::setOtherTextColor(const QColor &otherTextColor)
624 {
625 Q_D(KMiniCalendarItem);
626
627 if (d->otherTextColor != otherTextColor) {
628 d->otherTextColor = otherTextColor;
629 update();
630 }
631 }
632
633 void KMiniCalendarItem::setSelectTextColor(const QColor &selectTextColor)
634 {
635 Q_D(KMiniCalendarItem);
636
637 if (d->selectTextColor != selectTextColor) {
638 d->selectTextColor = selectTextColor;
639 update();
640 }
641 }
642
643 void KMiniCalendarItem::setHoverTextColor(const QColor &hoverTextColor)
644 {
645 Q_D(KMiniCalendarItem);
646
647 if (d->hoverTextColor != hoverTextColor) {
648 d->hoverTextColor = hoverTextColor;
649 update();
650 }
651 }
652
653 void KMiniCalendarItem::setCurrentLunarColor(const QColor &currentLunarColor)
654 {
655 Q_D(KMiniCalendarItem);
656
657 if (d->currentLunarColor != currentLunarColor) {
658 d->currentLunarColor = currentLunarColor;
659 update();
660 }
661 }
662
663 void KMiniCalendarItem::setOtherLunarColor(const QColor &otherLunarColor)
664 {
665 Q_D(KMiniCalendarItem);
666
667 if (d->otherLunarColor != otherLunarColor) {
668 d->otherLunarColor = otherLunarColor;
669 update();
670 }
671 }
672
673 void KMiniCalendarItem::setSelectLunarColor(const QColor &selectLunarColor)
674 {
675 Q_D(KMiniCalendarItem);
676
677 if (d->selectLunarColor != selectLunarColor) {
678 d->selectLunarColor = selectLunarColor;
679 update();
680 }
681 }
682
683 void KMiniCalendarItem::setHoverLunarColor(const QColor &hoverLunarColor)
684 {
685 Q_D(KMiniCalendarItem);
686
687 if (d->hoverLunarColor != hoverLunarColor) {
688 d->hoverLunarColor = hoverLunarColor;
689 update();
690 }
691 }
692
693 void KMiniCalendarItem::setCurrentBgColor(const QColor &currentBgColor)
694 {
695 Q_D(KMiniCalendarItem);
696
697 if (d->currentBgColor != currentBgColor) {
698 d->currentBgColor = currentBgColor;
699 update();
700 }
701 }
702
703 void KMiniCalendarItem::setOtherBgColor(const QColor &otherBgColor)
704 {
705 Q_D(KMiniCalendarItem);
706
707 if (d->otherBgColor != otherBgColor) {
708 d->otherBgColor = otherBgColor;
709 update();
710 }
711 }
712
713 void KMiniCalendarItem::setSelectBgColor(const QColor &selectBgColor)
714 {
715 Q_D(KMiniCalendarItem);
716
717 if (d->selectBgColor != selectBgColor) {
718 d->selectBgColor = selectBgColor;
719 update();
720 }
721 }
722
723 void KMiniCalendarItem::setHoverBgColor(const QColor &hoverBgColor)
724 {
725 Q_D(KMiniCalendarItem);
726
727 if (d->hoverBgColor != hoverBgColor) {
728 d->hoverBgColor = hoverBgColor;
729 update();
730 }
731 }
732 }
733
734 #include "kminicalendaritem.moc"
735 #include "moc_kminicalendaritem.cpp"