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 "lineediturldropeventfilter.h" 8 9 #include <QDropEvent> 10 #include <QEvent> 11 #include <QLineEdit> 12 #include <qmimedata.h> 13 14 static const char s_kdeUriListMime[] = "application/x-kde4-urilist"; // keep this name "kde4" for compat. 15 LineEditUrlDropEventFilter(QObject * parent)16LineEditUrlDropEventFilter::LineEditUrlDropEventFilter(QObject *parent) 17 : QObject(parent) 18 { 19 } 20 ~LineEditUrlDropEventFilter()21LineEditUrlDropEventFilter::~LineEditUrlDropEventFilter() 22 { 23 } 24 eventFilter(QObject * obj,QEvent * ev)25bool LineEditUrlDropEventFilter::eventFilter(QObject *obj, QEvent *ev) 26 { 27 // Handle only drop events 28 if (ev->type() != QEvent::Drop) { 29 return false; 30 } 31 QDropEvent *dropEv = static_cast<QDropEvent *>(ev); 32 33 // Handle only url drops, we check the MIME type for the standard or kde's urllist 34 // It would be interesting to handle urls that don't have any MIME type set (like a drag and drop from kate) 35 const QMimeData *data = dropEv->mimeData(); 36 if (!data->hasUrls() && !data->hasFormat(QLatin1String(s_kdeUriListMime))) { 37 return false; 38 } 39 40 // Our object should be a QLineEdit 41 QLineEdit *line = qobject_cast<QLineEdit *>(obj); 42 if (!line) { 43 return false; 44 } 45 46 QString content = data->text(); 47 line->setText(content); 48 line->setCursorPosition(content.length()); 49 50 ev->accept(); 51 return true; 52 } 53