Debian Codegraph @

src/gui/dragAndDrop/DragAndDropProcessor.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 Damir Porobic <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "DragAndDropProcessor.h"
21
22 DragAndDropProcessor::DragAndDropProcessor(IDragContentProvider *dragContentProvider, const QSharedPointer<ITempFileProvider> &tempFileProvider) :
23 mDragContentProvider(dragContentProvider),
24 mTempFileProvider(tempFileProvider)
25 {
26
27 }
28
29 bool DragAndDropProcessor::eventFilter(QObject *object, QEvent *event)
30 {
31 if(event->type() == QEvent::DragEnter) {
32 return handleDragEnter(dynamic_cast<QDragEnterEvent *>(event));
33 } else if(event->type() == QEvent::Drop) {
34 return handleDrop(dynamic_cast<QDropEvent *>(event));
35 } else if(event->type() == QEvent::GraphicsSceneDrop) {
36 return handleDrop(dynamic_cast<QGraphicsSceneDragDropEvent *>(event));
37 } else if (event->type() == QEvent::GraphicsSceneDragEnter) {
38 return handleDragEnter(dynamic_cast<QGraphicsSceneDragDropEvent *>(event));
39 } else if (event->type() == QEvent::MouseButtonPress) {
40 return handleDragStart(dynamic_cast<QMouseEvent *>(event));
41 } else if (event->type() == QEvent::MouseMove) {
42 return handleDragMove(dynamic_cast<QMouseEvent *>(event));
43 }
44 return QObject::eventFilter(object, event);
45 }
46
47 bool DragAndDropProcessor::handleDragEnter(QDragEnterEvent *event)
48 {
49 if (event->mimeData()->hasUrls()) {
50 event->acceptProposedAction();
51 return true;
52 }
53 return false;
54 }
55
56 bool DragAndDropProcessor::handleDragEnter(QGraphicsSceneDragDropEvent *event)
57 {
58 if (event->mimeData()->hasUrls()) {
59 event->acceptProposedAction();
60 return true;
61 }
62 return false;
63 }
64
65 bool DragAndDropProcessor::handleDrop(QDropEvent *event)
66 {
67 auto paths = getUrlsFromMimeData(event->mimeData());
68 processDroppedImagePaths(paths);
69
70 event->acceptProposedAction();
71 return true;
72 }
73
74 bool DragAndDropProcessor::handleDrop(QGraphicsSceneDragDropEvent *event)
75 {
76 auto paths = getUrlsFromMimeData(event->mimeData());
77 processDroppedImagePaths(paths);
78
79 event->acceptProposedAction();
80 return true;
81 }
82
83 bool DragAndDropProcessor::handleDragStart(QMouseEvent *event)
84 {
85 if (isDragStarting(event)) {
86 mDragStartPosition = event->pos();
87 return true;
88 }
89 return false;
90 }
91
92 bool DragAndDropProcessor::handleDragMove(QMouseEvent *event)
93 {
94 if (isDragStarting(event) && isMinDragDistanceReached(event)) {
95 auto dragContent = mDragContentProvider->dragContent();
96 if (dragContent.isValid()) {
97 createDrag(dragContent);
98 return true;
99 }
100 }
101 return false;
102 }
103
104 QStringList DragAndDropProcessor::getUrlsFromMimeData(const QMimeData *mimeData)
105 {
106 QStringList urls;
107
108 for (const auto &url : mimeData->urls()) {
109 urls.push_back(FileUrlHelper::toPath(url.toString()));
110 }
111
112 return urls;
113 }
114
115 bool DragAndDropProcessor::isDragStarting(const QMouseEvent *event)
116 {
117 return event->buttons() & Qt::LeftButton && event->modifiers() & Qt::ShiftModifier;
118 }
119
120 bool DragAndDropProcessor::isMinDragDistanceReached(const QMouseEvent *event) const
121 {
122 return (event->pos() - mDragStartPosition).manhattanLength() >= QApplication::startDragDistance();
123 }
124
125 void DragAndDropProcessor::createDrag(const DragContent &dragContent)
126 {
127 auto imagePath = getPathToDraggedImage(dragContent);
128 auto mimeData = new QMimeData;
129 mimeData->setUrls( { QUrl(FileUrlHelper::toFileUrl(imagePath)) });
130 mimeData->setData(QLatin1String("text/uri-list"), FileUrlHelper::toFileUrl(imagePath).toLatin1());
131
132 auto icon = QPixmap::fromImage(dragContent.image).scaled(256, 256, Qt::KeepAspectRatio, Qt::FastTransformation);
133 auto drag = new QDrag(this);
134 drag->setMimeData(mimeData);
135 drag->setPixmap(icon);
136 drag->exec();
137 }
138
139 QString DragAndDropProcessor::getPathToDraggedImage(const DragContent &dragContent)
140 {
141 QString imagePath;
142 if (dragContent.isSaved) {
143 imagePath = dragContent.path;
144 } else{
145 imagePath = mTempFileProvider->tempFile();
146 if(!dragContent.image.save(imagePath)){
147 qWarning("Failed to save temporary dragImage %s for drag and drop operation.", qPrintable(imagePath));
148 }
149 }
150 return imagePath;
151 }
152
153 void DragAndDropProcessor::processDroppedImagePaths(const QStringList &paths)
154 {
155 for(const auto& path: paths) {
156 if (PathHelper::isTempDirectory(path)) {
157 emit imageDropped(QPixmap(path));
158 } else {
159 emit fileDropped(path);
160 }
161 }
162 }