| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 5 | */ |
| 6 | |
| 7 | #include "klineediturldropeventfilter.h" |
| 8 | |
| 9 | #include <QDropEvent> |
| 10 | #include <QLineEdit> |
| 11 | #include <QMimeData> |
| 12 | |
| 13 | static const char s_kdeUriListMime[] = "application/x-kde4-urilist" ; // keep this name "kde4" for compat. |
| 14 | |
| 15 | KLineEditUrlDropEventFilter::KLineEditUrlDropEventFilter(QObject *parent) |
| 16 | : QObject(parent) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | KLineEditUrlDropEventFilter::~KLineEditUrlDropEventFilter() = default; |
| 21 | |
| 22 | bool KLineEditUrlDropEventFilter::eventFilter(QObject *object, QEvent *event) |
| 23 | { |
| 24 | // Handle only drop events |
| 25 | if (event->type() != QEvent::Drop) { |
| 26 | return false; |
| 27 | } |
| 28 | auto *dropEvent = static_cast<QDropEvent *>(event); |
| 29 | |
| 30 | // Handle only url drops, we check the MIME type for the standard or kde's urllist |
| 31 | // It would be interesting to handle urls that don't have any MIME type set (like a drag and drop from kate) |
| 32 | const QMimeData *data = dropEvent->mimeData(); |
| 33 | if (!data->hasUrls() && !data->hasFormat(mimetype: QLatin1String(s_kdeUriListMime))) { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | // Our object should be a QLineEdit |
| 38 | auto *line = qobject_cast<QLineEdit *>(object); |
| 39 | if (!line) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | QString content = data->text(); |
| 44 | line->setText(content); |
| 45 | line->setCursorPosition(content.length()); |
| 46 | |
| 47 | event->accept(); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | #include "moc_klineediturldropeventfilter.cpp" |
| 52 | |