File indexing completed on 2024-04-28 15:32:17

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 "lineediturldropeventfilter.h"
0008 
0009 #include <QDropEvent>
0010 #include <QEvent>
0011 #include <QLineEdit>
0012 #include <qmimedata.h>
0013 
0014 static const char s_kdeUriListMime[] = "application/x-kde4-urilist"; // keep this name "kde4" for compat.
0015 
0016 // KF6 TODO: add K-prefix to class name
0017 LineEditUrlDropEventFilter::LineEditUrlDropEventFilter(QObject *parent)
0018     : QObject(parent)
0019 {
0020 }
0021 
0022 LineEditUrlDropEventFilter::~LineEditUrlDropEventFilter()
0023 {
0024 }
0025 
0026 bool LineEditUrlDropEventFilter::eventFilter(QObject *obj, QEvent *ev)
0027 {
0028     // Handle only drop events
0029     if (ev->type() != QEvent::Drop) {
0030         return false;
0031     }
0032     QDropEvent *dropEv = static_cast<QDropEvent *>(ev);
0033 
0034     // Handle only url drops, we check the MIME type for the standard or kde's urllist
0035     // It would be interesting to handle urls that don't have any MIME type set (like a drag and drop from kate)
0036     const QMimeData *data = dropEv->mimeData();
0037     if (!data->hasUrls() && !data->hasFormat(QLatin1String(s_kdeUriListMime))) {
0038         return false;
0039     }
0040 
0041     // Our object should be a QLineEdit
0042     QLineEdit *line = qobject_cast<QLineEdit *>(obj);
0043     if (!line) {
0044         return false;
0045     }
0046 
0047     QString content = data->text();
0048     line->setText(content);
0049     line->setCursorPosition(content.length());
0050 
0051     ev->accept();
0052     return true;
0053 }
0054 
0055 #include "moc_lineediturldropeventfilter.cpp"