File indexing completed on 2024-09-29 12:08:02

0001 /*
0002     SPDX-FileCopyrightText: 2009 Rahman Duran <rahman.duran@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "kurlnavigatormenu_p.h"
0008 
0009 #include <QApplication>
0010 #include <QKeyEvent>
0011 #include <QMimeData>
0012 
0013 namespace KDEPrivate
0014 {
0015 KUrlNavigatorMenu::KUrlNavigatorMenu(QWidget *parent)
0016     : QMenu(parent)
0017     , m_initialMousePosition(QCursor::pos())
0018     , m_mouseMoved(false)
0019 {
0020     setAcceptDrops(true);
0021     setMouseTracking(true);
0022 }
0023 
0024 KUrlNavigatorMenu::~KUrlNavigatorMenu()
0025 {
0026 }
0027 
0028 void KUrlNavigatorMenu::dragEnterEvent(QDragEnterEvent *event)
0029 {
0030     if (event->mimeData()->hasUrls()) {
0031         event->acceptProposedAction();
0032     }
0033 }
0034 
0035 void KUrlNavigatorMenu::dragMoveEvent(QDragMoveEvent *event)
0036 {
0037     QMouseEvent mouseEvent(QMouseEvent(QEvent::MouseMove, event->pos(), Qt::LeftButton, event->mouseButtons(), event->keyboardModifiers()));
0038     mouseMoveEvent(&mouseEvent);
0039 }
0040 
0041 void KUrlNavigatorMenu::dropEvent(QDropEvent *event)
0042 {
0043     QAction *action = actionAt(event->pos());
0044     if (action != nullptr) {
0045         Q_EMIT urlsDropped(action, event);
0046     }
0047 }
0048 
0049 void KUrlNavigatorMenu::mouseMoveEvent(QMouseEvent *event)
0050 {
0051     if (!m_mouseMoved) {
0052         QPoint moveDistance = mapToGlobal(event->pos()) - m_initialMousePosition;
0053         m_mouseMoved = (moveDistance.manhattanLength() >= QApplication::startDragDistance());
0054     }
0055     // Don't pass the event to the base class until we consider
0056     // that the mouse has moved. This prevents menu items from
0057     // being highlighted too early.
0058     if (m_mouseMoved) {
0059         QMenu::mouseMoveEvent(event);
0060     }
0061 }
0062 
0063 void KUrlNavigatorMenu::mouseReleaseEvent(QMouseEvent *event)
0064 {
0065     Qt::MouseButton btn = event->button();
0066     // Since menu is opened on mouse press, we may receive
0067     // the corresponding mouse release event. Let's ignore
0068     // it unless mouse was moved.
0069     if (m_mouseMoved || (btn != Qt::LeftButton)) {
0070         QAction *action = actionAt(event->pos());
0071         if (action != nullptr) {
0072             Q_EMIT mouseButtonClicked(action, btn);
0073 
0074             // Prevent QMenu default activation, in case
0075             // triggered signal is used
0076             setActiveAction(nullptr);
0077         }
0078         QMenu::mouseReleaseEvent(event);
0079     }
0080     m_mouseMoved = true;
0081 }
0082 
0083 } // namespace KDEPrivate
0084 
0085 #include "moc_kurlnavigatormenu_p.cpp"