File indexing completed on 2024-04-21 04:58:14

0001 /* This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2002 John Firebaugh <jfirebaugh@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "konqdraggablelabel.h"
0008 #include "konqmainwindow.h"
0009 #include "konqview.h"
0010 #include <kiconloader.h>
0011 #include <QMouseEvent>
0012 #include <QApplication>
0013 #include <QMimeData>
0014 #include <QDrag>
0015 
0016 #include <KUrlMimeData>
0017 
0018 KonqDraggableLabel::KonqDraggableLabel(KonqMainWindow *mw, const QString &text)
0019     : QLabel(text)
0020     , m_mw(mw)
0021 {
0022     setBackgroundRole(QPalette::Button);
0023     setAlignment((QApplication::isRightToLeft() ? Qt::AlignRight : Qt::AlignLeft) |
0024                  Qt::AlignVCenter);
0025     setAcceptDrops(true);
0026     adjustSize();
0027     validDrag = false;
0028 }
0029 
0030 void KonqDraggableLabel::mousePressEvent(QMouseEvent *ev)
0031 {
0032     validDrag = true;
0033     startDragPos = ev->pos();
0034 }
0035 
0036 void KonqDraggableLabel::mouseMoveEvent(QMouseEvent *ev)
0037 {
0038     if ((startDragPos - ev->pos()).manhattanLength() > QApplication::startDragDistance()) {
0039         validDrag = false;
0040         if (m_mw->currentView()) {
0041             QList<QUrl> lst;
0042             //TODO KF6: check whether requestedUrl or realUrl is more suitable here
0043             lst.append(m_mw->currentView()->url());
0044             QDrag *drag = new QDrag(m_mw);
0045             QMimeData *md = new QMimeData;
0046             md->setUrls(lst);
0047             drag->setMimeData(md);
0048             QString iconName = KIO::iconNameForUrl(lst.first());
0049 
0050             const QIcon icon = QIcon::fromTheme(iconName, QIcon::fromTheme(QStringLiteral("application-octet-stream")));
0051             drag->setPixmap(icon.pixmap(KIconLoader::SizeSmall));
0052 
0053             drag->exec();
0054         }
0055     }
0056 }
0057 
0058 void KonqDraggableLabel::mouseReleaseEvent(QMouseEvent *)
0059 {
0060     validDrag = false;
0061 }
0062 
0063 void KonqDraggableLabel::dragEnterEvent(QDragEnterEvent *ev)
0064 {
0065     if (ev->mimeData()->hasUrls()) {
0066         ev->accept();
0067     }
0068 }
0069 
0070 void KonqDraggableLabel::dropEvent(QDropEvent *ev)
0071 {
0072     _savedLst.clear();
0073     _savedLst = KUrlMimeData::urlsFromMimeData(ev->mimeData());
0074     if (!_savedLst.isEmpty()) {
0075         QMetaObject::invokeMethod(this, "delayedOpenURL", Qt::QueuedConnection);
0076     }
0077 }
0078 
0079 void KonqDraggableLabel::delayedOpenURL()
0080 {
0081     m_mw->openUrl(nullptr, _savedLst.first());
0082 }