File indexing completed on 2024-04-28 03:59:06

0001 /*
0002     SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "klineediturldropeventfilter.h"
0008 
0009 #include <QDropEvent>
0010 #include <QLineEdit>
0011 #include <QMimeData>
0012 
0013 static const char s_kdeUriListMime[] = "application/x-kde4-urilist"; // keep this name "kde4" for compat.
0014 
0015 KLineEditUrlDropEventFilter::KLineEditUrlDropEventFilter(QObject *parent)
0016     : QObject(parent)
0017 {
0018 }
0019 
0020 KLineEditUrlDropEventFilter::~KLineEditUrlDropEventFilter() = default;
0021 
0022 bool KLineEditUrlDropEventFilter::eventFilter(QObject *object, QEvent *event)
0023 {
0024     // Handle only drop events
0025     if (event->type() != QEvent::Drop) {
0026         return false;
0027     }
0028     auto *dropEvent = static_cast<QDropEvent *>(event);
0029 
0030     // Handle only url drops, we check the MIME type for the standard or kde's urllist
0031     // It would be interesting to handle urls that don't have any MIME type set (like a drag and drop from kate)
0032     const QMimeData *data = dropEvent->mimeData();
0033     if (!data->hasUrls() && !data->hasFormat(QLatin1String(s_kdeUriListMime))) {
0034         return false;
0035     }
0036 
0037     // Our object should be a QLineEdit
0038     auto *line = qobject_cast<QLineEdit *>(object);
0039     if (!line) {
0040         return false;
0041     }
0042 
0043     QString content = data->text();
0044     line->setText(content);
0045     line->setCursorPosition(content.length());
0046 
0047     event->accept();
0048     return true;
0049 }
0050 
0051 #include "moc_klineediturldropeventfilter.cpp"