Skip to content

Commit

Permalink
Add dataset directly from silx + drop overlay on plot
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeloAlexis committed Jun 24, 2024
1 parent a774f64 commit 0175e0c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
54 changes: 54 additions & 0 deletions src/silx/app/view/CustomPlotSelectionWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ def __init__(self, parent=None):
super().__init__(parent)
self.setAcceptDrops(True)
self._treeView = None
self.dropOverlay = DropOverlay(self)

def setTreeView(self, treeView: qt.QTreeView):
"""Set the TreeView widget for the plot."""
Expand All @@ -509,9 +510,19 @@ def setTreeView(self, treeView: qt.QTreeView):
def dragEnterEvent(self, event):
super().dragEnterEvent(event)
self._treeView.acceptDragEvent(event)
self._showDropOverlay(event)

def dragMoveEvent(self, event):
super().dragMoveEvent(event)
self._showDropOverlay(event)

def dragLeaveEvent(self, event):
super().dragLeaveEvent(event)
self.dropOverlay.hideOverlay()

def dropEvent(self, event):
super().dropEvent(event)
self.dropOverlay.hideOverlay()
byteString = event.mimeData().data("application/x-silx-uri")
url = silx.io.url.DataUrl(byteString.data().decode("utf-8"))

Expand All @@ -535,8 +546,26 @@ def setXAxisLabel(self, label: str):
xAxis = self.getXAxis()
xAxis.setLabel(label)

def _showDropOverlay(self, event):
"""Show the drop overlay at the drop position."""
plotArea = self.getWidgetHandle()
dropPosition = plotArea.mapFrom(self, event.pos())
offset = plotArea.mapTo(self, qt.QPoint(0, 0))

plotBounds = self.getPlotBoundsInPixels()
left, top, width, height = plotBounds
yAreaTop = top + height

if dropPosition.y() > yAreaTop:
rect = qt.QRect(left + offset.x(), yAreaTop + offset.y(), width, 20)
else:
rect = qt.QRect(left + offset.x(), top + offset.y(), width, height)

self.dropOverlay.showOverlay(rect)


class _PlotToolBar(qt.QToolBar):
"""Toolbar widget for the plot."""
def __init__(self, parent=None):
super().__init__(parent)

Expand All @@ -547,6 +576,31 @@ def addClearAction(self, treeView: qt.QTreeView):
clearAction.triggered.connect(treeView.clear)
self.addAction(clearAction)

class DropOverlay(qt.QWidget):
"""Overlay widget for displaying drop zones on the plot."""
def __init__(self, parent=None):
super().__init__(parent)
self.setAttribute(qt.Qt.WA_TransparentForMouseEvents)
self.setAttribute(qt.Qt.WA_NoSystemBackground)
self.hide()

def showOverlay(self, rect):
"""Show the overlay at the given rectangle."""
self.setGeometry(rect)
self.show()

def hideOverlay(self):
"""Hide the overlay."""
self.hide()

def paintEvent(self, event):
"""Paint the overlay."""
painter = qt.QPainter(self)
painter.setRenderHint(qt.QPainter.Antialiasing)
painter.setPen(qt.QPen(qt.Qt.red, 3, qt.Qt.DashLine))
painter.setBrush(qt.QBrush(qt.Qt.transparent))
painter.drawRect(self.rect())


class CustomPlotSelectionWindow(qt.QMainWindow):
sigVisibilityChanged = qt.Signal(bool)
Expand Down
24 changes: 23 additions & 1 deletion src/silx/app/view/Viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,11 +962,24 @@ def __makeSureCustomNxDataWindowIsVisible(self):
self.__customNxdataWindow.setVisible(True)
self._displayCustomNxdataWindow.setChecked(True)

def __makeSureCustomPlotSelectionWindowIsVisible(self):
if not self._customPlotSelectionWindow.isVisible():
self._customPlotSelectionWindow.setVisible(True)
self._displayCustomPlotSelectionWindow.setChecked(True)

def useAsNewCustomSignal(self, h5dataset):
self.__makeSureCustomNxDataWindowIsVisible()
model = self.__customNxdata.model()
model.createFromSignal(h5dataset)

def setToPlotSelection(self, h5dataset):
self.__makeSureCustomPlotSelectionWindowIsVisible()
self._customPlotSelectionWindow.treeView.setX(h5dataset)

def addToPlotSelection(self, h5dataset):
self.__makeSureCustomPlotSelectionWindowIsVisible()
self._customPlotSelectionWindow.treeView.addY(h5dataset)

def useAsNewCustomNxdata(self, h5nxdata):
self.__makeSureCustomNxDataWindowIsVisible()
model = self.__customNxdata.model()
Expand All @@ -986,7 +999,7 @@ def customContextMenu(self, event):

for obj in selectedObjects:
h5 = obj.h5py_object

name = obj.name
if name.startswith("/"):
name = name[1:]
Expand All @@ -1002,6 +1015,15 @@ def customContextMenu(self, event):
action.triggered.connect(lambda: self.useAsNewCustomSignal(h5))
menu.addAction(action)

if h5.ndim == 1:
action = qt.QAction("Set to plot selection (X)", event.source())
action.triggered.connect(lambda: self.setToPlotSelection(obj.data_url))
menu.addAction(action)

action = qt.QAction("Add to plot selection (Y)", event.source())
action.triggered.connect(lambda: self.addToPlotSelection(obj.data_url))
menu.addAction(action)

if silx.io.is_group(h5) and silx.io.nxdata.is_valid_nxdata(h5):
action = qt.QAction("Use as a new custom NXdata", event.source())
action.triggered.connect(lambda: self.useAsNewCustomNxdata(h5))
Expand Down

0 comments on commit 0175e0c

Please sign in to comment.