Debian Codegraph @

kysdk-qtwidgets/src/kdragwidget.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 "kdragwidget.h"
24 #include "accessinfohelper.h"
25 #include "themeController.h"
26 #include <QBoxLayout>
27 #include <QCoreApplication>
28 #include <QDebug>
29 #include <QMimeData>
30 #include <QPainter>
31 #include <QPainterPath>
32 #include <QScreen>
33 #include <QStandardPaths>
34 #include <QStyleOptionFrame>
35
36 namespace kdk
37 {
38
39 class FileDialog : public QFileDialog
40 {
41 Q_OBJECT
42 public:
43 FileDialog(QWidget *parent = nullptr);
44 virtual ~FileDialog();
45
46 public slots:
47 void goAccept();
48
49 protected:
50 void showEvent(QShowEvent *event) override;
51 };
52
53 class KDragWidgetPrivate : public QObject, public ThemeController
54 {
55 Q_OBJECT
56 Q_DECLARE_PUBLIC(KDragWidget)
57 public:
58 KDragWidgetPrivate(KDragWidget *parent);
59 void openFile();
60
61 protected:
62 void changeTheme();
63
64 private:
65 KDragWidget *q_ptr;
66 QColor lineColor;
67 Qt::PenStyle mainState;
68 KPushButton *m_pIconButton;
69 QLabel *m_pTextLabel;
70 FileDialog *m_FileDialog;
71 QStringList m_NameFilterList;
72 QStringList m_pStringList;
73 bool m_cursorFlag;
74 };
75
76 KDragWidgetPrivate::KDragWidgetPrivate(KDragWidget *parent)
77 : q_ptr(parent)
78 , mainState(Qt::DashLine)
79 {
80 Q_Q(KDragWidget);
81 m_pIconButton = new KPushButton();
82 m_pTextLabel = new QLabel();
83 m_FileDialog = new FileDialog(q);
84 m_FileDialog->setDirectory(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
85 m_cursorFlag = false;
86 lineColor = ThemeController::getCustomColorFromDT("kline-normal");
87
88 KDK_ALL_INFO_FORMAT(m_pIconButton,"");
89 KDK_ALL_INFO_FORMAT(m_pTextLabel,"");
90 KDK_ALL_INFO_FORMAT(m_FileDialog,"");
91 }
92
93 void KDragWidgetPrivate::openFile()
94 {
95 Q_Q(KDragWidget);
96
97 m_FileDialog->setWindowTitle(tr("Please select file"));
98 m_pStringList.clear();
99
100 if (m_FileDialog->exec() == QDialog::Accepted) {
101 QStringList selectFiles = m_FileDialog->selectedFiles();
102 if (selectFiles.count() > 1) {
103 for (const QString &fileName : selectFiles) {
104 QFileInfo fileinfo(fileName);
105
106 if (m_FileDialog->nameFilters().count() == 1 && m_FileDialog->nameFilters().first().contains("(*)")) {
107 m_pStringList.append(fileName);
108 } else {
109 if (fileinfo.isFile() && !fileName.isEmpty())
110 m_pStringList.append(fileName);
111 }
112 }
113 if (m_pStringList.count() > 0)
114 Q_EMIT q->getPathList(m_pStringList);
115 } else {
116 QString fileName = selectFiles.first();
117 QFileInfo fileinfo(fileName);
118 if (m_FileDialog->nameFilters().first().contains("(*)"))
119 Q_EMIT q->getPath(fileName);
120 else if (fileinfo.isFile() && !fileName.isEmpty())
121 Q_EMIT q->getPath(fileName);
122 }
123 }
124 }
125
126 void KDragWidgetPrivate::changeTheme()
127 {
128 Q_Q(KDragWidget);
129 initThemeStyle();
130 }
131
132 KDragWidget::KDragWidget(QWidget *parent)
133 : QWidget(parent)
134 , d_ptr(new KDragWidgetPrivate(this))
135 {
136 Q_D(KDragWidget);
137 setAcceptDrops(true); // 设置接受拖拽
138
139 QVBoxLayout *mainLayout = new QVBoxLayout(this);
140 QHBoxLayout *hLayout = new QHBoxLayout();
141 QHBoxLayout *hLayout1 = new QHBoxLayout();
142 // d->m_pIconButton = new KPushButton();
143 d->m_pIconButton->setFixedSize(54, 54);
144 d->m_pIconButton->setTranslucent(true);
145 d->m_pIconButton->setButtonType(KPushButton::CircleType);
146 d->m_pIconButton->setIcon(QIcon::fromTheme("list-add.symbolic"));
147
148 // d->m_pTextLabel = new QLabel();
149 d->m_pTextLabel->setText(tr("Select or drag and drop the folder identification path"));
150 hLayout->setContentsMargins(0, 0, 0, 0);
151 hLayout->addStretch();
152 hLayout->addWidget(d->m_pIconButton);
153 hLayout->addStretch();
154
155 hLayout1->setContentsMargins(0, 0, 0, 0);
156 hLayout1->addStretch();
157 hLayout1->addWidget(d->m_pTextLabel);
158 hLayout1->addStretch();
159
160 mainLayout->setSpacing(0);
161 mainLayout->addStretch();
162 mainLayout->addLayout(hLayout);
163 mainLayout->addSpacing(16);
164 mainLayout->addLayout(hLayout1);
165 mainLayout->addStretch();
166
167 d->changeTheme();
168
169 connect(d->m_pIconButton, &KPushButton::clicked, this, [=]() {
170 d->openFile();
171 });
172 connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){
173 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
174 {
175 d->changeTheme();
176 }
177 });
178
179 installEventFilter(this);
180 d->m_FileDialog->installEventFilter(this);
181 }
182
183 KPushButton *KDragWidget::iconButton()
184 {
185 Q_D(KDragWidget);
186 return d->m_pIconButton;
187 }
188
189 QLabel *KDragWidget::textLabel()
190 {
191 Q_D(KDragWidget);
192 return d->m_pTextLabel;
193 }
194
195 QFileDialog *KDragWidget::fileDialog()
196 {
197 Q_D(KDragWidget);
198 return dynamic_cast<QFileDialog *>(d->m_FileDialog);
199 }
200
201 void KDragWidget::setNameFilter(const QString &filter)
202 {
203 Q_D(KDragWidget);
204 QString f(filter);
205 if (f.isEmpty())
206 return;
207 QString sep(QLatin1String(";;"));
208 int i = f.indexOf(sep, 0);
209 if (i == -1) {
210 if (f.indexOf(QLatin1Char('\n'), 0) != -1) {
211 sep = QLatin1Char('\n');
212 i = f.indexOf(sep, 0);
213 }
214 }
215 setNameFilters(f.split(sep));
216 }
217
218 void KDragWidget::setNameFilters(const QStringList &filters)
219 {
220 Q_D(KDragWidget);
221 d->m_FileDialog->setNameFilters(filters);
222
223 for (int i = 0; i < filters.size(); i++) {
224 if (!d->m_NameFilterList.contains(filters.at(i))) {
225 d->m_NameFilterList.append(filters.at(i));
226 }
227 }
228 }
229
230 void KDragWidget::mouseMoveEvent(QMouseEvent *event)
231 {
232 Q_D(KDragWidget);
233 return QWidget::mouseMoveEvent(event);
234 }
235
236 void KDragWidget::mousePressEvent(QMouseEvent *event)
237 {
238 Q_D(KDragWidget);
239 return QWidget::mousePressEvent(event);
240 }
241
242 void KDragWidget::mouseReleaseEvent(QMouseEvent *event)
243 {
244 Q_D(KDragWidget);
245 return QWidget::mouseReleaseEvent(event);
246 }
247
248 void KDragWidget::keyPressEvent(QKeyEvent *event)
249 {
250 return QWidget::keyPressEvent(event);
251 }
252
253 void KDragWidget::keyReleaseEvent(QKeyEvent *event)
254 {
255 return QWidget::keyReleaseEvent(event);
256 }
257
258 void KDragWidget::paintEvent(QPaintEvent *event)
259 {
260 Q_D(KDragWidget);
261 QPainter painter(this);
262
263 QPen pen = painter.pen();
264 pen.setColor(d->lineColor);
265 pen.setStyle(d->mainState);
266 pen.setWidth(1);
267 painter.setPen(pen);
268 painter.setBrush(Qt::NoBrush);
269 int radius = ThemeController::getRadiusFromDT("kradius-normal");
270 if (radius == -1)
271 radius = 6;
272 painter.drawRoundedRect(rect().adjusted(1, 1, -1, -1), radius, radius);
273 }
274
275 bool KDragWidget::eventFilter(QObject *watched, QEvent *event)
276 {
277 Q_D(KDragWidget);
278 if (watched == this) {
279 switch (event->type()) {
280 case QEvent::MouseButtonPress: {
281 d->m_pIconButton->setAttribute(Qt::WA_UnderMouse, true);
282 d->lineColor = ThemeController::getCustomColorFromDT("tooltiptext-active");
283 d->openFile();
284
285 update();
286 break;
287 }
288 case QEvent::MouseButtonRelease: {
289 d->m_pIconButton->setAttribute(Qt::WA_UnderMouse, true);
290 d->lineColor = ThemeController::getCustomColorFromDT("tooltiptext-active");
291 update();
292 break;
293 }
294 case QEvent::Leave: {
295 d->m_pIconButton->setAttribute(Qt::WA_UnderMouse, false);
296 d->lineColor = ThemeController::getCustomColorFromDT("placeholdertext-active");
297 if (d->m_cursorFlag) {
298 d->m_cursorFlag = false;
299 setCursor(Qt::ArrowCursor);
300 }
301 update();
302 break;
303 }
304 case QEvent::Enter: {
305 d->m_pIconButton->setAttribute(Qt::WA_UnderMouse, true);
306 d->lineColor = ThemeController::getCustomColorFromDT("tooltiptext-active");
307 if (d->m_cursorFlag) {
308 d->m_cursorFlag = false;
309 setCursor(Qt::ArrowCursor);
310 }
311 update();
312 break;
313 }
314 case QEvent::DragEnter: {
315 d->m_pIconButton->setAttribute(Qt::WA_UnderMouse, true);
316 d->lineColor = ThemeController::getCustomColorFromDT("highlight-active");
317 update();
318 break;
319 }
320 case QEvent::DragLeave: {
321 d->m_pIconButton->setAttribute(Qt::WA_UnderMouse, false);
322 d->lineColor = ThemeController::getCustomColorFromDT("placeholdertext-active");
323 update();
324 break;
325 }
326 default:
327 break;
328 }
329 }
330
331 if (watched == d->m_FileDialog) {
332
333 if (event->type() == QEvent::Show) {
334 QWidget *parentWindow = qobject_cast<QWidget *>(this->parent());
335
336 while (parentWindow && parentWindow->parent())
337 parentWindow = qobject_cast<QWidget *>(parentWindow->parent());
338
339 if (parentWindow && parentWindow->isActiveWindow()) {
340 const QPoint parentWindowCenter = parentWindow->geometry().center();
341 d->m_FileDialog->move(parentWindowCenter.x() - d->m_FileDialog->width() / 2, parentWindowCenter.y() - d->m_FileDialog->height() / 2 + 40);
342 } else {
343 QScreen *screen = QGuiApplication::primaryScreen();
344 int screenWidth = screen->geometry().width();
345 int screenHeight = screen->geometry().height();
346
347 int messageBox_X = (screenWidth - this->width()) / 2;
348 int messageBox_Y = (screenHeight - this->height()) / 2;
349 d->m_FileDialog->move(messageBox_X, messageBox_Y);
350 }
351 }
352 }
353 return QWidget::eventFilter(watched, event);
354 }
355
356 void KDragWidget::dragEnterEvent(QDragEnterEvent *event)
357 {
358 Q_D(KDragWidget);
359
360 if (event->mimeData()->hasUrls()) {
361 QList<QUrl> urls = event->mimeData()->urls();
362 for (int i = 0; i < urls.count(); i++) {
363 QString filePath = urls.at(i).toLocalFile();
364 if (!filePath.isEmpty()) {
365 QFileInfo fileInfo(filePath); // filePath 路径
366 QString suffix = fileInfo.suffix(); // suffix 后缀
367 QStringList nameFilters = d->m_FileDialog->nameFilters();
368 for (QString str : nameFilters) {
369 if (!str.contains("(*)")) {
370 if (!fileInfo.isDir()) {
371 if (!suffix.isEmpty() && str.contains(suffix, Qt::CaseInsensitive)) {
372 d->m_cursorFlag = false;
373 break;
374 } else
375 d->m_cursorFlag = true;
376 } else {
377 d->m_cursorFlag = true;
378 }
379 }
380 }
381 }
382 }
383
384 if (d->m_cursorFlag) {
385 setCursor(Qt::ForbiddenCursor);
386 event->setDropAction(Qt::IgnoreAction);
387 } else {
388 setCursor(Qt::ArrowCursor);
389 event->acceptProposedAction();
390 }
391 } else {
392 event->ignore();
393 }
394 }
395
396 void KDragWidget::dragLeaveEvent(QDragLeaveEvent *event)
397 {
398 Q_D(KDragWidget);
399 return QWidget::dragLeaveEvent(event);
400 }
401
402 void KDragWidget::dragMoveEvent(QDragMoveEvent *event)
403 {
404 Q_D(KDragWidget);
405 return QWidget::dragMoveEvent(event);
406 }
407
408 void KDragWidget::dropEvent(QDropEvent *event)
409 {
410 Q_D(KDragWidget);
411
412 const QMimeData *mimeData = event->mimeData();
413 if (mimeData->hasUrls()) {
414 QList<QUrl> urlList = mimeData->urls();
415 if (urlList.size() > 1) {
416 d->m_pStringList.clear();
417 for (int i = 0; i < urlList.size(); i++) // 选中的文件
418 {
419 QString fileName = urlList.at(i).toLocalFile();
420
421 QFileInfo fileinfo(fileName);
422 if (d->m_FileDialog->nameFilters().count() == 1 && d->m_FileDialog->nameFilters().first().contains("(*)")) {
423 d->m_pStringList.append(fileName);
424 } else {
425 if (fileinfo.isFile()) {
426 QString endStr = fileName.mid(fileName.lastIndexOf("."), fileName.size() - fileName.lastIndexOf("."));
427 if (d->m_NameFilterList.isEmpty()) // 若无过滤则正常选中
428 d->m_pStringList.append(fileName);
429 else {
430 for (int temp = 0; temp < d->m_NameFilterList.size(); temp++) // 遍历是否符合筛选条件
431 {
432 QString curString = d->m_NameFilterList.at(temp);
433 if (curString.contains(endStr)) {
434 d->m_pStringList.append(fileName);
435 }
436 }
437 }
438 }
439 }
440 }
441 Q_EMIT getPathList(d->m_pStringList);
442 } else {
443 QString fileName = urlList.first().toLocalFile();
444 QFileInfo fileinfo(fileName);
445 if (d->m_FileDialog->nameFilters().count() == 1 && d->m_FileDialog->nameFilters().first().contains("(*)")) {
446 Q_EMIT getPath(fileName);
447 } else {
448 if (fileinfo.isFile()) {
449 QString endStr = fileName.mid(fileName.lastIndexOf("."), fileName.size() - fileName.lastIndexOf("."));
450 if (d->m_NameFilterList.isEmpty()) // 若无过滤则正常选中
451 Q_EMIT getPath(fileName);
452 else {
453 for (int temp = 0; temp < d->m_NameFilterList.size(); temp++) // 遍历是否符合筛选条件
454 {
455 QString curString = d->m_NameFilterList.at(temp);
456 if (curString.contains(endStr)) {
457 Q_EMIT getPath(fileName);
458 }
459 }
460 }
461 }
462 }
463 }
464 }
465 if (d->m_cursorFlag) {
466 d->m_cursorFlag = false;
467 setCursor(Qt::ArrowCursor);
468 event->ignore();
469 }
470 }
471
472 FileDialog::FileDialog(QWidget *parent)
473 : QFileDialog(parent)
474 {
475 this->setFileMode(QFileDialog::ExistingFiles);
476 }
477
478 FileDialog::~FileDialog()
479 {
480 }
481
482 void FileDialog::goAccept()
483 {
484 accept();
485 }
486
487 void FileDialog::showEvent(QShowEvent *event)
488 {
489 Q_UNUSED(event)
490
491 if (this->isVisible()) {
492 for (QWidget *widget : this->findChildren<QWidget *>()) {
493 if (widget->objectName() == "acceptButton") {
494 QPushButton *btn = qobject_cast<QPushButton *>(widget);
495 disconnect(btn, &QPushButton::clicked, 0, 0);
496 connect(btn, &QPushButton::clicked, [=](bool checked) {
497 Q_UNUSED(checked)
498 this->goAccept();
499 });
500 }
501 }
502 }
503 }
504
505 }
506
507 #include "kdragwidget.moc"
508 #include "moc_kdragwidget.cpp"