|
2 | 2 | import logging |
3 | 3 | from itertools import chain |
4 | 4 | from urllib.parse import urlparse |
5 | | -from typing import List |
| 5 | +from typing import List, Dict, Any |
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 | from AnyQt.QtWidgets import \ |
9 | 9 | QStyle, QComboBox, QMessageBox, QGridLayout, QLabel, \ |
10 | 10 | QLineEdit, QSizePolicy as Policy, QCompleter |
11 | | -from AnyQt.QtCore import Qt, QTimer, QSize |
| 11 | +from AnyQt.QtCore import Qt, QTimer, QSize, QUrl |
| 12 | + |
| 13 | +from orangewidget.workflow.drophandler import SingleUrlDropHandler |
12 | 14 |
|
13 | 15 | from Orange.data.table import Table, get_sample_datasets_dir |
14 | 16 | from Orange.data.io import FileFormat, UrlReader, class_from_qualified_name |
|
28 | 30 | # module's namespace so that old saved settings still work |
29 | 31 | from Orange.widgets.utils.filedialogs import RecentPath |
30 | 32 |
|
31 | | - |
32 | 33 | log = logging.getLogger(__name__) |
33 | 34 |
|
34 | 35 |
|
@@ -571,5 +572,34 @@ def workflowEnvChanged(self, key, value, oldvalue): |
571 | 572 | self.update_file_list(key, value, oldvalue) |
572 | 573 |
|
573 | 574 |
|
| 575 | +class OWFileDropHandler(SingleUrlDropHandler): |
| 576 | + WIDGET = OWFile |
| 577 | + |
| 578 | + def canDropUrl(self, url: QUrl) -> bool: |
| 579 | + if url.isLocalFile(): |
| 580 | + try: |
| 581 | + FileFormat.get_reader(url.toLocalFile()) |
| 582 | + return True |
| 583 | + except Exception: # noqa # pylint:disable=broad-except |
| 584 | + return False |
| 585 | + else: |
| 586 | + return url.scheme().lower() in ("http", "https", "ftp") |
| 587 | + |
| 588 | + def parametersFromUrl(self, url: QUrl) -> Dict[str, Any]: |
| 589 | + if url.isLocalFile(): |
| 590 | + path = url.toLocalFile() |
| 591 | + r = RecentPath(os.path.abspath(path), None, None, |
| 592 | + os.path.basename(path)) |
| 593 | + return { |
| 594 | + "recent_paths": [r], |
| 595 | + "source": OWFile.LOCAL_FILE, |
| 596 | + } |
| 597 | + else: |
| 598 | + return { |
| 599 | + "recent_urls": [url.toString()], |
| 600 | + "source": OWFile.URL, |
| 601 | + } |
| 602 | + |
| 603 | + |
574 | 604 | if __name__ == "__main__": # pragma: no cover |
575 | 605 | WidgetPreview(OWFile).run() |
0 commit comments