barely functioning drag and drop over graphs

This commit is contained in:
Dominik Demuth 2023-12-26 17:01:47 +01:00
parent a4b4c8109e
commit 969a400820
3 changed files with 33 additions and 1 deletions

View File

@ -27,6 +27,7 @@ class QGraphWindow(QtWidgets.QGraphicsView, Ui_GraphWindow):
mouseDoubleClicked = QtCore.pyqtSignal()
positionClicked = QtCore.pyqtSignal(tuple, bool)
aboutToClose = QtCore.pyqtSignal(list)
newData = QtCore.pyqtSignal(list, str)
counter = itertools.count()
@ -87,6 +88,9 @@ class QGraphWindow(QtWidgets.QGraphicsView, Ui_GraphWindow):
self.bwbutton.toggled.connect(self.change_background)
self.setAcceptDrops(True)
self.graphic.installEventFilter(self)
def _init_gui(self):
self.setWindowTitle('Graph ' + str(next(QGraphWindow.counter)))
@ -119,6 +123,34 @@ class QGraphWindow(QtWidgets.QGraphicsView, Ui_GraphWindow):
for lineedit in [self.xmin_lineedit, self.xmax_lineedit, self.ymin_lineedit, self.ymax_lineedit]:
lineedit.setValidator(QtGui.QDoubleValidator())
def eventFilter(self, obj: QtCore.QObject, evt: QtCore.QEvent):
"""
Catch drag and Drop to prevent anything inside self.graphic to accept the events.
Without event filter, we cannot process it here and start file reading
"""
if evt.type() == QtCore.QEvent.Type.DragEnter:
evt.accept()
return True
elif evt.type() == QtCore.QEvent.Type.Drop:
return self._handle_drop(evt)
else:
return False
def dropEvent(self, evt: QtGui.QDropEvent):
return self._handle_drop(evt)
def _handle_drop(self, evt: QtGui.QDropEvent):
if evt.mimeData().hasUrls():
files = [str(url.toLocalFile()) for url in evt.mimeData().urls()]
self.newData.emit(files, self.id)
evt.accept()
return True
return False
def __contains__(self, item: str):
return item in self.sets

View File

@ -471,6 +471,7 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
w.mousePositionChanged.connect(self.mousemoved)
w.aboutToClose.connect(self.management.delete_sets)
w.positionClicked.connect(self.point_selected)
w.newData.connect(lambda x, y: self.management.load_files(x, new_plot=y))
w.show()
graph_list = self.management.graphs.list()

View File

@ -8,7 +8,6 @@ UNSIGNED_NUMBER_RE = re.compile(r'[-+]?\d*[+p.]?\d+([eE][-+]?\d+)?', re.MULTILIN
def numbers_from_string(any_string: str) -> list[float]:
print(any_string)
matches = []
for m in NUMBER_RE.finditer(any_string):
matches.append(float(m.group().replace('p', '.')))