File indexing completed on 2024-04-21 16:30:36

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2017 Martin Koller, kollix@aon.at
0004 
0005   This file is part of liquidshell.
0006 
0007   liquidshell is free software: you can redistribute it and/or modify
0008   it under the terms of the GNU General Public License as published by
0009   the Free Software Foundation, either version 3 of the License, or
0010   (at your option) any later version.
0011 
0012   liquidshell is distributed in the hope that it will be useful,
0013   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015   GNU General Public License for more details.
0016 
0017   You should have received a copy of the GNU General Public License
0018   along with liquidshell.  If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include <PopupMenu.hxx>
0022 
0023 #include <QMouseEvent>
0024 #include <QDrag>
0025 #include <QMimeData>
0026 #include <QGuiApplication>
0027 #include <QStyleHints>
0028 #include <QDebug>
0029 
0030 //--------------------------------------------------------------------------------
0031 
0032 void PopupMenu::mousePressEvent(QMouseEvent *event)
0033 {
0034   if ( event->button() == Qt::LeftButton )
0035     dragStartPos = event->pos();
0036 
0037   QMenu::mousePressEvent(event);
0038 }
0039 
0040 //--------------------------------------------------------------------------------
0041 
0042 void PopupMenu::mouseMoveEvent(QMouseEvent *event)
0043 {
0044   if ( (event->buttons() == Qt::LeftButton) && (dragStartPos != QPoint(-1, -1)) &&
0045        (event->pos() - dragStartPos).manhattanLength() > QGuiApplication::styleHints()->startDragDistance() )
0046   {
0047     dragStartPos = QPoint(-1, -1);
0048     QAction *action = actionAt(event->pos());
0049     if ( action && !action->menu() && action->data().isValid() )
0050     {
0051       event->accept();
0052 
0053       QDrag *drag = new QDrag(this);
0054       QMimeData *mimeData = new QMimeData;
0055       Qt::DropActions dropAction;
0056 
0057       if ( static_cast<QMetaType::Type>(action->data().type()) == QMetaType::QUrl )
0058       {
0059         mimeData->setUrls(QList<QUrl>() << action->data().toUrl());
0060         dropAction = Qt::CopyAction;
0061       }
0062       else if ( static_cast<QMetaType::Type>(action->data().type()) == QMetaType::Int )
0063       {
0064         mimeData->setData("application/x-winId", QByteArray::number(action->data().toInt()));
0065         dropAction = Qt::MoveAction;
0066       }
0067       else
0068         qDebug() << "illegal data in PopupMenu action";  // should never come here
0069 
0070       drag->setMimeData(mimeData);
0071       drag->setPixmap(action->icon().pixmap(32, 32));
0072       drag->exec(dropAction);
0073     }
0074   }
0075   else
0076     QMenu::mouseMoveEvent(event);
0077 }
0078 
0079 //--------------------------------------------------------------------------------