File indexing completed on 2024-09-29 06:32:30

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     const QPointF eventPosition = event->position();
0038     const QPointF globalEventPosition = mapToGlobal(eventPosition);
0039     QMouseEvent mouseEvent(QMouseEvent(QEvent::MouseMove, eventPosition, globalEventPosition, Qt::LeftButton, event->buttons(), event->modifiers()));
0040     mouseMoveEvent(&mouseEvent);
0041 }
0042 
0043 void KUrlNavigatorMenu::dropEvent(QDropEvent *event)
0044 {
0045     QAction *action = actionAt(event->position().toPoint());
0046     if (action != nullptr) {
0047         Q_EMIT urlsDropped(action, event);
0048     }
0049 }
0050 
0051 void KUrlNavigatorMenu::mouseMoveEvent(QMouseEvent *event)
0052 {
0053     if (!m_mouseMoved) {
0054         QPoint moveDistance = mapToGlobal(event->pos()) - m_initialMousePosition;
0055         m_mouseMoved = (moveDistance.manhattanLength() >= QApplication::startDragDistance());
0056     }
0057     // Don't pass the event to the base class until we consider
0058     // that the mouse has moved. This prevents menu items from
0059     // being highlighted too early.
0060     if (m_mouseMoved) {
0061         QMenu::mouseMoveEvent(event);
0062     }
0063 }
0064 
0065 void KUrlNavigatorMenu::mouseReleaseEvent(QMouseEvent *event)
0066 {
0067     Qt::MouseButton btn = event->button();
0068     // Since menu is opened on mouse press, we may receive
0069     // the corresponding mouse release event. Let's ignore
0070     // it unless mouse was moved.
0071     if (m_mouseMoved || (btn != Qt::LeftButton)) {
0072         QAction *action = actionAt(event->pos());
0073         if (action != nullptr) {
0074             Q_EMIT mouseButtonClicked(action, btn);
0075 
0076             // Prevent QMenu default activation, in case
0077             // triggered signal is used
0078             setActiveAction(nullptr);
0079         }
0080         QMenu::mouseReleaseEvent(event);
0081     }
0082     m_mouseMoved = true;
0083 }
0084 
0085 } // namespace KDEPrivate
0086 
0087 #include "moc_kurlnavigatormenu_p.cpp"