Debian Codegraph @

kysdk-qtwidgets/src/kaddfilebutton.cpp

raw
1 /*
2 * libkysdk-qtwidgets's Library
3 *
4 * Copyright (C) 2023, 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 "kaddfilebutton.h"
24 #include "themeController.h"
25 #include <QDebug>
26 #include <QEvent>
27 #include <QFileDialog>
28 #include <QPainter>
29 #include <QPainterPath>
30 #include <QStandardPaths>
31 #include <QStyleOptionButton>
32 #include "accessinfohelper.h"
33
34 namespace kdk
35 {
36
37 #define BLANK_SPACE 20
38
39 class AddFileDialog : public QFileDialog
40 {
41 Q_OBJECT
42 public:
43 AddFileDialog(QWidget *parent = nullptr);
44 virtual ~AddFileDialog();
45
46 public slots:
47 void goAccept();
48
49 protected:
50 void showEvent(QShowEvent *event) override;
51 };
52
53 class KAddFileButtonPrivate : public QObject, public ThemeController
54 {
55 Q_DECLARE_PUBLIC(KAddFileButton)
56 Q_OBJECT
57 public:
58 KAddFileButtonPrivate(KAddFileButton *parent);
59
60 void updateColor(QStyleOptionButton option, QPalette palette);
61 void openFile();
62
63 protected:
64 void changeTheme();
65
66 private:
67 KAddFileButton *q_ptr;
68 QColor m_bkgColor;
69 QString m_text;
70 QRect m_rect;
71 QRect m_textRect;
72 AddFileDialog *m_FileDialog;
73 };
74
75 KAddFileButton::KAddFileButton(QWidget *parent)
76 : QPushButton(parent)
77 , d_ptr(new KAddFileButtonPrivate(this))
78 {
79 Q_D(KAddFileButton);
80 setFixedSize(104, 110);
81 connect(this, &KAddFileButton::clicked, this, [=] {
82 d->openFile();
83 });
84 }
85
86 void KAddFileButton::paintEvent(QPaintEvent *event)
87 {
88 Q_D(KAddFileButton);
89 QStyleOptionButton option;
90 initStyleOption(&option);
91 d->updateColor(option, palette());
92
93 d->m_rect = QRect(QPoint(20, 8), QSize(64, 64));
94
95 QPainter painter(this);
96 painter.save();
97 painter.setBrush(ThemeController::getCustomColorFromDT("base-active"));
98 painter.setPen(Qt::NoPen);
99 painter.drawRoundedRect(rect(), 0, 0);
100 painter.restore();
101 QPen pen = painter.pen();
102 pen.setStyle(Qt::DashLine);
103 pen.setColor(ThemeController::getCustomColorFromDT("highlight-active"));
104 painter.save();
105 painter.setRenderHint(QPainter::Antialiasing);
106 painter.setPen(pen);
107 painter.setBrush(d->m_bkgColor);
108 int radius = ThemeController::getRadiusFromDT("kradius-normal");
109 if (radius == -1)
110 radius = 6;
111 painter.drawRoundedRect(d->m_rect, radius, radius);
112 painter.setPen(ThemeController::getCustomColorFromDT("highlight-active"));
113 painter.drawLine(QPoint(d->m_rect.center().x() - 10, d->m_rect.center().y() + 1), QPoint(d->m_rect.center().x() + 10, d->m_rect.center().y() + 1));
114 painter.drawLine(QPoint(d->m_rect.center().x() + 1, d->m_rect.center().y() - 10), QPoint(d->m_rect.center().x() + 1, d->m_rect.center().y() + 10));
115 painter.restore();
116
117 painter.save();
118 QFontMetrics fm = painter.fontMetrics();
119 int width = fm.horizontalAdvance(d->m_text);
120 painter.setPen(ThemeController::getCustomColorFromDT("highlight-active"));
121 d->m_textRect = QRect(QPoint(rect().center().x() - width / 2, d->m_rect.bottom() + 8), QSize(width, fm.height()));
122 painter.drawText(d->m_textRect, d->m_text);
123 painter.restore();
124 }
125
126 bool KAddFileButton::eventFilter(QObject *watched, QEvent *event)
127 {
128 return QPushButton::eventFilter(watched, event);
129 }
130
131 void KAddFileButton::mouseMoveEvent(QMouseEvent *event)
132 {
133 return QPushButton::mouseMoveEvent(event);
134 }
135
136 void KAddFileButton::mousePressEvent(QMouseEvent *event)
137 {
138 return QPushButton::mousePressEvent(event);
139 }
140
141 void KAddFileButton::mouseReleaseEvent(QMouseEvent *event)
142 {
143 return QPushButton::mouseReleaseEvent(event);
144 }
145
146 void KAddFileButton::setText(QString str)
147 {
148 Q_D(KAddFileButton);
149 d->m_text = str;
150 }
151
152 QFileDialog *KAddFileButton::fileDialog()
153 {
154 Q_D(KAddFileButton);
155 return dynamic_cast<QFileDialog *>(d->m_FileDialog);
156 }
157
158 void KAddFileButton::setNameFilter(const QString &filter)
159 {
160 Q_D(KAddFileButton);
161 QString f(filter);
162 if (f.isEmpty())
163 return;
164 QString sep(QLatin1String(";;"));
165 int i = f.indexOf(sep, 0);
166 if (i == -1) {
167 if (f.indexOf(QLatin1Char('\n'), 0) != -1) {
168 sep = QLatin1Char('\n');
169 i = f.indexOf(sep, 0);
170 }
171 }
172 setNameFilters(f.split(sep));
173 }
174
175 void KAddFileButton::setNameFilters(const QStringList &filters)
176 {
177 Q_D(KAddFileButton);
178 d->m_FileDialog->setNameFilters(filters);
179 }
180
181 KAddFileButtonPrivate::KAddFileButtonPrivate(KAddFileButton *parent)
182 : q_ptr(parent)
183 , m_text("添加文件")
184 {
185 Q_Q(KAddFileButton);
186 m_FileDialog = new AddFileDialog(q);
187 m_FileDialog->setDirectory(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
188 m_FileDialog->setFileMode(QFileDialog::ExistingFiles);
189
190 KDK_ALL_INFO_FORMAT(m_FileDialog,"");
191 }
192
193 void KAddFileButtonPrivate::updateColor(QStyleOptionButton option, QPalette palette)
194 {
195 Q_Q(KAddFileButton);
196 m_bkgColor = ThemeController::getCustomColorFromDT("base-active");
197 QColor hightColor = ThemeController::getCustomColorFromDT("highlight-active");
198 if (option.state.testFlag(QStyle::State_MouseOver)) {
199 m_bkgColor = hightColor;
200 if (option.state.testFlag(QStyle::State_Sunken))
201 m_bkgColor.setAlphaF(0.4);
202 else
203 m_bkgColor.setAlphaF(0.3);
204 }
205 }
206
207 void KAddFileButtonPrivate::openFile()
208 {
209 Q_Q(KAddFileButton);
210
211 m_FileDialog->setWindowTitle(tr("Please select file"));
212
213 m_FileDialog->exec();
214 }
215
216 void KAddFileButtonPrivate::changeTheme()
217 {
218 initThemeStyle();
219 }
220
221 AddFileDialog::AddFileDialog(QWidget *parent)
222 {
223 this->setFileMode(QFileDialog::ExistingFiles);
224 }
225
226 AddFileDialog::~AddFileDialog()
227 {
228 }
229
230 void AddFileDialog::goAccept()
231 {
232 accept();
233 }
234
235 void AddFileDialog::showEvent(QShowEvent *event)
236 {
237 if (this->isVisible()) {
238 for (QWidget *widget : this->findChildren<QWidget *>()) {
239 if (widget->objectName() == "acceptButton") {
240 QPushButton *btn = qobject_cast<QPushButton *>(widget);
241 disconnect(btn, &QPushButton::clicked, 0, 0);
242 connect(btn, &QPushButton::clicked, [=](bool checked) {
243 Q_UNUSED(checked)
244 this->goAccept();
245 });
246 }
247 }
248 }
249 }
250
251 }
252
253 #include "kaddfilebutton.moc"
254 #include "moc_kaddfilebutton.cpp"