Mantid
Loading...
Searching...
No Matches
DropEventHelper.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <QFileInfo>
10#include <QMimeData>
11#include <QStringList>
12#include <QUrl>
13
14// Compile on OSX only.
15#if defined(__APPLE__)
16#include <CoreFoundation/CoreFoundation.h>
17#endif // defined(__APPLE__)
18
19using namespace MantidQt::MantidWidgets;
20
31QUrl fixupURL(const QUrl &url) {
32#if defined(__APPLE__)
33 QString localFileQString = url.toLocalFile();
34 // Compile on OSX only.
35 if (localFileQString.startsWith("/.file/id=")) {
36 CFStringRef relCFStringRef =
37 CFStringCreateWithCString(kCFAllocatorDefault, localFileQString.toUtf8().constData(), kCFStringEncodingUTF8);
38 CFURLRef relCFURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, relCFStringRef, kCFURLPOSIXPathStyle,
39 false // isDirectory
40 );
41 CFErrorRef error = nullptr;
42 CFURLRef absCFURL = CFURLCreateFilePathURL(kCFAllocatorDefault, relCFURL, &error);
43 if (!error) {
44 static const CFIndex maxAbsPathCStrBufLen = 4096;
45 char absPathCStr[maxAbsPathCStrBufLen];
46 if (CFURLGetFileSystemRepresentation(absCFURL,
47 true, // resolveAgainstBase
48 reinterpret_cast<UInt8 *>(&absPathCStr[0]), maxAbsPathCStrBufLen)) {
49 localFileQString = QString(absPathCStr);
50 }
51 }
52 CFRelease(absCFURL);
53 CFRelease(relCFURL);
54 CFRelease(relCFStringRef);
55 }
56 return QUrl(localFileQString);
57#else
58 return url;
59#endif // defined(__APPLE__)
60}
61
70QStringList DropEventHelper::getFileNames(const QDropEvent *event) {
71 QStringList filenames;
72 const auto mimeData = event->mimeData();
73 if (mimeData->hasUrls()) {
74 const auto urlList = mimeData->urls();
75 for (const auto &url : urlList) {
76 const auto fileUrl = fixupURL(url);
77 const auto fName = fileUrl.toLocalFile();
78 if (fName.size() > 0) {
79 filenames.append(fName);
80 }
81 }
82 }
83 return filenames;
84}
85
94QStringList DropEventHelper::extractPythonFiles(const QDropEvent *event) {
95 QStringList filenames;
96
97 for (const auto &name : getFileNames(event)) {
98 QFileInfo fi(name);
99
100 if (fi.suffix().toUpper() == "PY") {
101 filenames.append(name);
102 }
103 }
104
105 return filenames;
106}
QUrl fixupURL(const QUrl &url)
Workaround for file path bug on OSX >=10.10 with Qt4.
double error
Definition: IndexPeaks.cpp:133
EXPORT_OPT_MANTIDQT_COMMON QStringList extractPythonFiles(const QDropEvent *event)
Get all python files from q QDropEvent.
EXPORT_OPT_MANTIDQT_COMMON QStringList getFileNames(const QDropEvent *event)
Get all filenames from a QDropEvent.