| 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: Zhen Sun <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "kcustomdateedit.h" |
| 24 |
#include "themeController.h" |
| 25 |
#include <QApplication> |
| 26 |
#include <QBrush> |
| 27 |
#include <QPainter> |
| 28 |
#include <QRandomGenerator> |
| 29 |
#include <QRect> |
| 30 |
#include <QStyle> |
| 31 |
#include <QStyleOptionComboBox> |
| 32 |
#include <QSvgRenderer> |
| 33 |
|
| 34 |
namespace kdk |
| 35 |
{ |
| 36 |
class KCustomDateEditPrivate : public QObject |
| 37 |
{ |
| 38 |
Q_DECLARE_PUBLIC(KCustomDateEdit) |
| 39 |
Q_OBJECT |
| 40 |
public: |
| 41 |
KCustomDateEditPrivate(KCustomDateEdit *parent); |
| 42 |
|
| 43 |
private slots: |
| 44 |
void onDateSelected(const QDate &date); |
| 45 |
QPixmap loadSvg(const QString &path, int size); |
| 46 |
QPixmap drawSymbolicColoredPixmap(const QPixmap &source); |
| 47 |
|
| 48 |
private: |
| 49 |
KCustomDateEdit *q_ptr; |
| 50 |
KMiniCalendarWidget *m_pCalendar; |
| 51 |
bool hoverFlag = false; |
| 52 |
bool focusFlag = false; |
| 53 |
}; |
| 54 |
|
| 55 |
KCustomDateEditPrivate::KCustomDateEditPrivate(KCustomDateEdit *parent) |
| 56 |
: q_ptr(parent) |
| 57 |
{ |
| 58 |
Q_Q(KCustomDateEdit); |
| 59 |
m_pCalendar = new KMiniCalendarWidget(q); |
| 60 |
m_pCalendar->setFixedSize(244, 264); |
| 61 |
m_pCalendar->setWeekNameFormat(KMiniCalendarWidget::WeekNameFormat_Short); |
| 62 |
connect(m_pCalendar, &KMiniCalendarWidget::clicked, this, &KCustomDateEditPrivate::onDateSelected); |
| 63 |
} |
| 64 |
|
| 65 |
void KCustomDateEditPrivate::onDateSelected(const QDate &date) |
| 66 |
{ |
| 67 |
Q_Q(KCustomDateEdit); |
| 68 |
q->setDate(date); |
| 69 |
} |
| 70 |
|
| 71 |
QPixmap KCustomDateEditPrivate::loadSvg(const QString &path, int size) |
| 72 |
{ |
| 73 |
Q_Q(KCustomDateEdit); |
| 74 |
int origSize = size; |
| 75 |
const auto ratio = qApp->devicePixelRatio(); |
| 76 |
|
| 77 |
if (2 == ratio) { |
| 78 |
size += origSize; |
| 79 |
} else if (3 == ratio) { |
| 80 |
size += origSize; |
| 81 |
} |
| 82 |
|
| 83 |
QPixmap pixmap(size, size); |
| 84 |
QSvgRenderer renderer(path); |
| 85 |
pixmap.fill(Qt::transparent); |
| 86 |
|
| 87 |
QPainter painter; |
| 88 |
painter.begin(&pixmap); |
| 89 |
renderer.render(&painter); |
| 90 |
painter.end(); |
| 91 |
|
| 92 |
pixmap.setDevicePixelRatio(ratio); |
| 93 |
return drawSymbolicColoredPixmap(pixmap); |
| 94 |
} |
| 95 |
|
| 96 |
QPixmap KCustomDateEditPrivate::drawSymbolicColoredPixmap(const QPixmap &source) |
| 97 |
{ |
| 98 |
Q_Q(KCustomDateEdit); |
| 99 |
QImage img = source.toImage(); |
| 100 |
|
| 101 |
for (int x = 0; x < img.width(); x++) { |
| 102 |
for (int y = 0; y < img.height(); y++) { |
| 103 |
QColor color = img.pixelColor(x, y); |
| 104 |
|
| 105 |
if (color.alpha() > 0) { |
| 106 |
QColor colorSet = ThemeController::getCustomColorFromDT("buttontext-active"); |
| 107 |
color.setRed(colorSet.red()); |
| 108 |
color.setGreen(colorSet.green()); |
| 109 |
color.setBlue(colorSet.blue()); |
| 110 |
img.setPixelColor(x, y, color); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return QPixmap::fromImage(img); |
| 116 |
} |
| 117 |
|
| 118 |
KCustomDateEdit::KCustomDateEdit(QWidget *parent) |
| 119 |
: QDateTimeEdit(parent) |
| 120 |
, d_ptr(new KCustomDateEditPrivate(this)) |
| 121 |
{ |
| 122 |
Q_D(KCustomDateEdit); |
| 123 |
this->installEventFilter(this); |
| 124 |
this->setButtonSymbols(QAbstractSpinBox::NoButtons); |
| 125 |
} |
| 126 |
|
| 127 |
void KCustomDateEdit::showCalendar() |
| 128 |
{ |
| 129 |
Q_D(KCustomDateEdit); |
| 130 |
QStyleOptionComboBox opt; |
| 131 |
opt.initFrom(this); |
| 132 |
QRect arrowRect = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxArrow, this); |
| 133 |
int randomX = QRandomGenerator::global()->bounded(arrowRect.x(), arrowRect.x() + arrowRect.width()); |
| 134 |
int randomY = QRandomGenerator::global()->bounded(arrowRect.y(), arrowRect.y() + arrowRect.height()); |
| 135 |
|
| 136 |
QMouseEvent *event = new QMouseEvent(QEvent::MouseButtonPress, QPoint(randomX, randomY), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); |
| 137 |
QApplication::postEvent(this, event); |
| 138 |
} |
| 139 |
|
| 140 |
void KCustomDateEdit::setShowLunar(bool showLunar) |
| 141 |
{ |
| 142 |
Q_D(KCustomDateEdit); |
| 143 |
d->m_pCalendar->setShowLunar(showLunar); |
| 144 |
} |
| 145 |
|
| 146 |
void KCustomDateEdit::updateStyle(bool isDark) |
| 147 |
{ |
| 148 |
Q_D(KCustomDateEdit); |
| 149 |
|
| 150 |
if (d->m_pCalendar == nullptr) |
| 151 |
return; |
| 152 |
|
| 153 |
if (isDark) { |
| 154 |
d->m_pCalendar->setWeekBgColor(QColor(29, 29, 29)); |
| 155 |
d->m_pCalendar->setWeekTextColor(QColor(255, 255, 255)); |
| 156 |
d->m_pCalendar->setBorderColor(QColor(180, 180, 180)); |
| 157 |
d->m_pCalendar->setWeekColor(QColor(255, 255, 255)); |
| 158 |
d->m_pCalendar->setLunarColor(QColor(55, 156, 238)); |
| 159 |
d->m_pCalendar->setCurrentTextColor(QColor(255, 255, 255)); |
| 160 |
d->m_pCalendar->setOtherTextColor(QColor(200, 200, 200)); |
| 161 |
d->m_pCalendar->setSelectTextColor(QColor(255, 255, 255)); |
| 162 |
d->m_pCalendar->setHoverTextColor(QColor(250, 250, 250)); |
| 163 |
d->m_pCalendar->setCurrentLunarColor(QColor(179, 179, 179)); |
| 164 |
d->m_pCalendar->setOtherLunarColor(QColor(179, 179, 179)); |
| 165 |
d->m_pCalendar->setSelectLunarColor(QColor(179, 179, 179)); |
| 166 |
d->m_pCalendar->setHoverLunarColor(QColor(179, 179, 179)); |
| 167 |
d->m_pCalendar->setCurrentBgColor(QColor(29, 29, 29)); |
| 168 |
d->m_pCalendar->setOtherBgColor(QColor(29, 29, 29)); |
| 169 |
d->m_pCalendar->setSelectBgColor(ThemeController::getCustomColorFromDT("kbrand-normal")); |
| 170 |
d->m_pCalendar->setHoverBgColor(QColor(180, 180, 180)); |
| 171 |
} else { |
| 172 |
d->m_pCalendar->setWeekBgColor(QColor(255, 255, 255)); |
| 173 |
d->m_pCalendar->setWeekTextColor(QColor(0, 0, 0)); |
| 174 |
d->m_pCalendar->setBorderColor(QColor(180, 180, 180)); |
| 175 |
d->m_pCalendar->setWeekColor(QColor(0, 0, 0)); |
| 176 |
d->m_pCalendar->setLunarColor(QColor(55, 156, 238)); |
| 177 |
d->m_pCalendar->setCurrentTextColor(QColor(0, 0, 0)); |
| 178 |
d->m_pCalendar->setOtherTextColor(QColor(200, 200, 200)); |
| 179 |
d->m_pCalendar->setSelectTextColor(QColor(255, 255, 255)); |
| 180 |
d->m_pCalendar->setHoverTextColor(QColor(250, 250, 250)); |
| 181 |
d->m_pCalendar->setCurrentLunarColor(QColor(179, 179, 179)); |
| 182 |
d->m_pCalendar->setOtherLunarColor(QColor(179, 179, 179)); |
| 183 |
d->m_pCalendar->setSelectLunarColor(QColor(179, 179, 179)); |
| 184 |
d->m_pCalendar->setHoverLunarColor(QColor(179, 179, 179)); |
| 185 |
d->m_pCalendar->setCurrentBgColor(QColor(255, 255, 255)); |
| 186 |
d->m_pCalendar->setOtherBgColor(QColor(255, 255, 255)); |
| 187 |
d->m_pCalendar->setSelectBgColor(ThemeController::getCustomColorFromDT("kbrand-normal")); |
| 188 |
d->m_pCalendar->setHoverBgColor(QColor(180, 180, 180)); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void KCustomDateEdit::mousePressEvent(QMouseEvent *event) |
| 193 |
{ |
| 194 |
Q_D(KCustomDateEdit); |
| 195 |
QStyleOptionComboBox opt; |
| 196 |
opt.initFrom(this); |
| 197 |
QRect arrowRect = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxArrow, this); |
| 198 |
QPoint point = event->pos(); |
| 199 |
|
| 200 |
if (arrowRect.contains(event->pos())) { |
| 201 |
d->m_pCalendar->setDate(this->date()); |
| 202 |
d->m_pCalendar->move(mapToGlobal(QPoint(0, height()))); |
| 203 |
d->m_pCalendar->show(); |
| 204 |
} else { |
| 205 |
QDateTimeEdit::mousePressEvent(event); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void KCustomDateEdit::paintEvent(QPaintEvent *e) |
| 210 |
{ |
| 211 |
Q_D(KCustomDateEdit); |
| 212 |
Q_UNUSED(e); |
| 213 |
QPainter painter(this); |
| 214 |
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); // 反锯齿; |
| 215 |
QBrush brush = QBrush(ThemeController::getCustomColorFromDT("button-active")); |
| 216 |
painter.setPen(Qt::NoPen); |
| 217 |
painter.setBrush(brush); |
| 218 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 219 |
if (radius == -1) |
| 220 |
radius = 6; |
| 221 |
painter.drawRoundedRect(this->rect(), radius, radius); |
| 222 |
QPixmap pix = d->loadSvg("/usr/share/kylin-user-guide/data/ukui-down-symbolic.svg", 64); |
| 223 |
// QRect rect = QRect(113, 10, 15, 15); |
| 224 |
QRect rect = QRect(this->rect().right()-24, 10, 16, 16); |
| 225 |
painter.drawPixmap(rect, pix); |
| 226 |
|
| 227 |
QRect rectBoxt = this->rect(); |
| 228 |
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); // 反锯齿; |
| 229 |
|
| 230 |
if (d->m_pCalendar && d->m_pCalendar->isVisible()) { |
| 231 |
d->focusFlag = true; |
| 232 |
} |
| 233 |
|
| 234 |
if (d->focusFlag == true) { |
| 235 |
// QPen pen(palette().brush(QPalette::Active, QPalette::Highlight), 2); |
| 236 |
QPen pen(ThemeController::getCustomColorFromDT("highlight-active"), 2); |
| 237 |
pen.setJoinStyle(Qt::RoundJoin); |
| 238 |
painter.setPen(pen); |
| 239 |
painter.setBrush(Qt::NoBrush); |
| 240 |
painter.translate(1, 1); |
| 241 |
painter.drawRoundedRect(rectBoxt.adjusted(0, 0, -2, -2), 6, 6); |
| 242 |
} else if (d->hoverFlag == true) { |
| 243 |
painter.setPen(ThemeController::getCustomColorFromDT("highlight-active")); |
| 244 |
painter.setBrush(Qt::NoBrush); |
| 245 |
painter.translate(0.5, 0.5); |
| 246 |
painter.drawRoundedRect(rectBoxt.adjusted(0, 0, -1, -1), 6, 6); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
bool KCustomDateEdit::eventFilter(QObject *obj, QEvent *event) |
| 251 |
{ |
| 252 |
Q_D(KCustomDateEdit); |
| 253 |
|
| 254 |
if (QEvent::HoverEnter == event->type()) { |
| 255 |
d->hoverFlag = true; |
| 256 |
repaint(); |
| 257 |
} else if (QEvent::HoverLeave == event->type()) { |
| 258 |
d->hoverFlag = false; |
| 259 |
repaint(); |
| 260 |
} else if (QEvent::FocusIn == event->type()) { |
| 261 |
d->focusFlag = true; |
| 262 |
repaint(); |
| 263 |
} else if (QEvent::FocusOut == event->type()) { |
| 264 |
d->focusFlag = false; |
| 265 |
d->hoverFlag = false; |
| 266 |
repaint(); |
| 267 |
|
| 268 |
if (this->date() != QDateTime::currentDateTime().date() && !d->m_pCalendar->isVisible()) { |
| 269 |
Q_EMIT changeDate(); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
return QObject::eventFilter(obj, event); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
#include "kcustomdateedit.moc" |
| 278 |
#include "moc_kcustomdateedit.cpp" |