Debian Codegraph @

kysdk-qtwidgets/src/klunardateedit.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 #include "klunardateedit.h"
24 #include "kcustomdateedit.h"
25 #include <QApplication>
26 #include <QDateTimeEdit>
27 #include <QGSettings>
28 #include <QHBoxLayout>
29 #include <QLabel>
30 #include <QPainter>
31 #include <QPushButton>
32 #include <QSvgRenderer>
33 #include <QVBoxLayout>
34 #define THEME_SCHAME "org.ukui.style"
35 #define COLOR_THEME "styleName"
36
37 namespace kdk
38 {
39 class KLunarDateEditPrivate : public QObject
40 {
41 Q_OBJECT
42 Q_DECLARE_PUBLIC(KLunarDateEdit)
43 public:
44 KLunarDateEditPrivate(KLunarDateEdit *parent);
45
46 private:
47 QPixmap loadSvg(const QString &path, int size);
48 QPixmap drawSymbolicColoredPixmap(const QPixmap &source);
49
50 private:
51 KLunarDateEdit *q_ptr;
52
53 QGSettings *colorGsettings;
54 KCustomDateEdit *dateEdit;
55 QWidget *m_pLunarWidget;
56 QLabel *lunarLabel;
57 QPushButton *popButton;
58 };
59
60 KLunarDateEditPrivate::KLunarDateEditPrivate(KLunarDateEdit *parent)
61 : q_ptr(parent)
62 {
63 Q_Q(KLunarDateEdit);
64 dateEdit = new KCustomDateEdit(q);
65 dateEdit->setContextMenuPolicy(Qt::NoContextMenu);
66 dateEdit->setCalendarPopup(true);
67 dateEdit->setDisplayFormat("yyyy-MM-dd");
68 QSizePolicy sizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
69 sizePolicy.setHorizontalStretch(0);
70 sizePolicy.setVerticalStretch(0);
71 sizePolicy.setHeightForWidth(dateEdit->sizePolicy().hasHeightForWidth());
72 dateEdit->setSizePolicy(sizePolicy);
73
74 // lunarDateEdit
75 m_pLunarWidget = new QWidget(q);
76 m_pLunarWidget->setObjectName(QString::fromUtf8("LunarWidget"));
77 QHBoxLayout *horizontalLayout = new QHBoxLayout(m_pLunarWidget);
78 horizontalLayout->setSpacing(0);
79 horizontalLayout->setContentsMargins(0, 0, 0, 0);
80
81 lunarLabel = new QLabel(m_pLunarWidget);
82 sizePolicy.setHeightForWidth(lunarLabel->sizePolicy().hasHeightForWidth());
83 // lunarLabel->setMargin(10);
84 lunarLabel->setSizePolicy(sizePolicy);
85
86 popButton = new QPushButton(m_pLunarWidget);
87 sizePolicy.setHeightForWidth(popButton->sizePolicy().hasHeightForWidth());
88 popButton->setSizePolicy(sizePolicy);
89 popButton->setFixedWidth(34);
90 popButton->setStyleSheet(QString::fromUtf8("QPushButton{background-color: rgba(255, 255, 255, 0);border: none;}"
91 "QPushButton:hover { background-color: rgba(255, 255, 255, 0); }"
92 "QPushButton:pressed { background-color: rgba(255, 255, 255, 0);}"));
93 popButton->setFlat(true);
94 connect(popButton, SIGNAL(clicked()), q, SLOT(slotClicked()));
95 horizontalLayout->addSpacing(10);
96 horizontalLayout->addWidget(lunarLabel);
97 horizontalLayout->addWidget(popButton);
98
99 dateEdit->setVisible(true);
100 m_pLunarWidget->setVisible(false);
101
102 connect(dateEdit, SIGNAL(dateChanged(QDate)), q, SIGNAL(dateChanged(QDate)));
103 connect(dateEdit, SIGNAL(dateChanged(QDate)), q, SLOT(slotDateChanged(QDate)));
104
105 const QByteArray id(THEME_SCHAME);
106
107 if (QGSettings::isSchemaInstalled(id)) {
108 colorGsettings = new QGSettings(id, QByteArray(), q);
109 connect(colorGsettings, &QGSettings::changed, this, [=](QString key) {
110 if ("themeColor" == key || "styleName" == key) {
111 q->slotPaletteChanged();
112 }
113 });
114 }
115 }
116
117 QPixmap KLunarDateEditPrivate::loadSvg(const QString &path, int size)
118 {
119 Q_Q(KLunarDateEdit);
120 int origSize = size;
121 const auto ratio = qApp->devicePixelRatio();
122
123 if (2 == ratio) {
124 size += origSize;
125 } else if (3 == ratio) {
126 size += origSize;
127 }
128
129 QPixmap pixmap(size, size);
130 QSvgRenderer renderer(path);
131 pixmap.fill(Qt::transparent);
132
133 QPainter painter;
134 painter.begin(&pixmap);
135 renderer.render(&painter);
136 painter.end();
137
138 pixmap.setDevicePixelRatio(ratio);
139 return drawSymbolicColoredPixmap(pixmap);
140 }
141
142 QPixmap KLunarDateEditPrivate::drawSymbolicColoredPixmap(const QPixmap &source)
143 {
144 Q_Q(KLunarDateEdit);
145 QImage img = source.toImage();
146
147 for (int x = 0; x < img.width(); x++) {
148 for (int y = 0; y < img.height(); y++) {
149 QColor color = img.pixelColor(x, y);
150
151 if (color.alpha() > 0) {
152 QColor colorSet = q->palette().color(popButton->palette().ButtonText);
153 color.setRed(colorSet.red());
154 color.setGreen(colorSet.green());
155 color.setBlue(colorSet.blue());
156 img.setPixelColor(x, y, color);
157 }
158 }
159 }
160
161 return QPixmap::fromImage(img);
162 }
163
164 KLunarDateEdit::KLunarDateEdit(QWidget *parent)
165 : QWidget{parent}
166 , d_ptr(new KLunarDateEditPrivate(this))
167 {
168 Q_D(KLunarDateEdit);
169 slotPaletteChanged();
170 }
171
172 void KLunarDateEdit::setShowLunar(bool showLunar)
173 {
174 Q_D(KLunarDateEdit);
175 slotDateChanged(d->dateEdit->date());
176 d->m_pLunarWidget->setVisible(showLunar);
177 d->dateEdit->setShowLunar(showLunar);
178 }
179
180 void KLunarDateEdit::slotPaletteChanged()
181 {
182 Q_D(KLunarDateEdit);
183
184 if (d->popButton == nullptr || d->lunarLabel == nullptr || d->m_pLunarWidget == nullptr)
185 return;
186
187 QPalette pal = qApp->palette();
188 this->setPalette(pal);
189 d->dateEdit->setPalette(pal);
190 d->m_pLunarWidget->setPalette(pal);
191 d->lunarLabel->setPalette(pal);
192 d->popButton->setPalette(pal);
193
194 QGSettings *styleGsettings = nullptr;
195 const QByteArray style_id(THEME_SCHAME);
196
197 if (QGSettings::isSchemaInstalled(style_id)) {
198 styleGsettings = new QGSettings(style_id);
199 QString currentTheme = styleGsettings->get(COLOR_THEME).toString();
200
201 if (currentTheme == "ukui-default" || currentTheme == "ukui-light") {
202 d->dateEdit->updateStyle(false);
203 d->m_pLunarWidget->setStyleSheet(QString("QWidget#LunarWidget{background-color: rgb(233,233,233); border:none;border-radius:5px;}"));
204 d->lunarLabel->setStyleSheet(QString("color:rgb(0,0,0);"));
205 } else if (currentTheme == "ukui-dark") {
206 d->dateEdit->updateStyle(true);
207 d->m_pLunarWidget->setStyleSheet(QString("QWidget#LunarWidget{background-color: rgb(55,55,59); border:none;border-radius:5px;}"));
208 d->lunarLabel->setStyleSheet(QString("color:rgb(233,233,233);"));
209 }
210 }
211
212 QPixmap pixmap = d->loadSvg("/usr/share/kylin-user-guide/data/ukui-down-symbolic.svg", 64);
213 pixmap = pixmap.scaled(QSize(16, 16), Qt::KeepAspectRatio, Qt::SmoothTransformation);
214 QIcon icon(pixmap);
215 d->popButton->setIcon(icon);
216 d->popButton->setIconSize(QSize(16, 16));
217
218 if (styleGsettings != nullptr) {
219 delete styleGsettings;
220 styleGsettings = nullptr;
221 }
222 }
223
224 QDateTimeEdit *KLunarDateEdit::getDateEdit()
225 {
226 Q_D(KLunarDateEdit);
227 return static_cast<QDateTimeEdit *>(d->dateEdit);
228 }
229
230 void KLunarDateEdit::resizeEvent(QResizeEvent *event)
231 {
232 Q_D(KLunarDateEdit);
233 d->dateEdit->setFixedSize(event->size());
234 d->m_pLunarWidget->setFixedSize(event->size());
235 }
236
237 void KLunarDateEdit::slotDateChanged(QDate date)
238 {
239 Q_D(KLunarDateEdit);
240 QString strHoliday;
241 QString strSolarTerms;
242 QString strLunarFestival;
243 QString strLunarYear;
244 QString strLunarMonth;
245 QString strLunarDay;
246
247 KLunarCalendarInfo::Instance()->getLunarCalendarInfo(date.year(),
248 date.month(),
249 date.day(),
250 strHoliday,
251 strSolarTerms,
252 strLunarFestival,
253 strLunarYear,
254 strLunarMonth,
255 strLunarDay);
256
257 QString labBottomarg = strLunarMonth + strLunarDay;
258 d->lunarLabel->setText(labBottomarg);
259 }
260
261 void KLunarDateEdit::slotClicked()
262 {
263 Q_D(KLunarDateEdit);
264 d->dateEdit->showCalendar();
265 }
266 }
267
268 #include "klunardateedit.moc"
269 #include "moc_klunardateedit.cpp"