-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3876 from silx-kit/fix_3807
gui: add WaiterOverlay. To display processing wheel on top of another widget
- Loading branch information
Showing
4 changed files
with
239 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#!/usr/bin/env python | ||
# /*########################################################################## | ||
# | ||
# Copyright (c) 2023 European Synchrotron Radiation Facility | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
# | ||
# ###########################################################################*/ | ||
""" | ||
Demonstration window that displays a wait icon until the plot is updated | ||
""" | ||
|
||
import numpy.random | ||
from silx.gui import qt | ||
from silx.gui.widgets.WaitingOverlay import WaitingOverlay | ||
from silx.gui.plot import Plot2D | ||
|
||
|
||
class MyMainWindow(qt.QMainWindow): | ||
|
||
WAITING_TIME = 2000 # ms | ||
|
||
def __init__(self, parent=None): | ||
super().__init__(parent) | ||
|
||
# central plot | ||
self._plot = Plot2D() | ||
self._waitingOverlay = WaitingOverlay(self._plot) | ||
self.setCentralWidget(self._plot) | ||
|
||
# button to trigger image generation | ||
self._rightPanel = qt.QWidget(self) | ||
self._rightPanel.setLayout(qt.QVBoxLayout()) | ||
self._button = qt.QPushButton("generate image", self) | ||
self._rightPanel.layout().addWidget(self._button) | ||
|
||
self._dockWidget = qt.QDockWidget() | ||
self._dockWidget.setWidget(self._rightPanel) | ||
self.addDockWidget(qt.Qt.RightDockWidgetArea, self._dockWidget) | ||
|
||
# set up | ||
self._waitingOverlay.hide() | ||
self._waitingOverlay.setIconSize(qt.QSize(60, 60)) | ||
# connect signal / slot | ||
self._button.clicked.connect(self._triggerImageCalculation) | ||
|
||
def _generateRandomData(self): | ||
self.setData(numpy.random.random(1000 * 500).reshape((1000, 500))) | ||
self._button.setEnabled(True) | ||
|
||
def setData(self, data): | ||
self._plot.addImage(data) | ||
self._waitingOverlay.hide() | ||
|
||
def _triggerImageCalculation(self): | ||
self._plot.clear() | ||
self._button.setEnabled(False) | ||
self._waitingOverlay.show() | ||
qt.QTimer.singleShot(self.WAITING_TIME, self._generateRandomData) | ||
|
||
|
||
qapp = qt.QApplication([]) | ||
window = MyMainWindow() | ||
window.show() | ||
qapp.exec_() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import weakref | ||
from typing import Optional | ||
from silx.gui.widgets.WaitingPushButton import WaitingPushButton | ||
from silx.gui import qt | ||
from silx.gui.qt import inspect as qt_inspect | ||
from silx.gui.plot import PlotWidget | ||
|
||
|
||
class WaitingOverlay(qt.QWidget): | ||
"""Widget overlaying another widget with a processing wheel icon. | ||
:param parent: widget on top of which to display the "processing/waiting wheel" | ||
""" | ||
|
||
def __init__(self, parent: qt.QWidget) -> None: | ||
super().__init__(parent) | ||
self.setContentsMargins(0, 0, 0, 0) | ||
|
||
self._waitingButton = WaitingPushButton(self) | ||
self._waitingButton.setDown(True) | ||
self._waitingButton.setWaiting(True) | ||
self._waitingButton.setStyleSheet("QPushButton { background-color: rgba(150, 150, 150, 40); border: 0px; border-radius: 10px; }") | ||
self._registerParent(parent) | ||
|
||
def text(self) -> str: | ||
"""Returns displayed text""" | ||
return self._waitingButton.text() | ||
|
||
def setText(self, text: str): | ||
"""Set displayed text""" | ||
self._waitingButton.setText(text) | ||
self._resize() | ||
|
||
def _listenedWidget(self, parent: qt.QWidget) -> qt.QWidget: | ||
"""Returns widget to register event filter to according to parent""" | ||
if isinstance(parent, PlotWidget): | ||
return parent.getWidgetHandle() | ||
return parent | ||
|
||
def _backendChanged(self): | ||
self._listenedWidget(self.parent()).installEventFilter(self) | ||
self._resizeLater() | ||
|
||
def _registerParent(self, parent: Optional[qt.QWidget]): | ||
if parent is None: | ||
return | ||
self._listenedWidget(parent).installEventFilter(self) | ||
if isinstance(parent, PlotWidget): | ||
parent.sigBackendChanged.connect(self._backendChanged) | ||
self._resize() | ||
|
||
def _unregisterParent(self, parent: Optional[qt.QWidget]): | ||
if parent is None: | ||
return | ||
if isinstance(parent, PlotWidget): | ||
parent.sigBackendChanged.disconnect(self._backendChanged) | ||
self._listenedWidget(parent).removeEventFilter(self) | ||
|
||
def setParent(self, parent: qt.QWidget): | ||
self._unregisterParent(self.parent()) | ||
super().setParent(parent) | ||
self._registerParent(parent) | ||
|
||
def showEvent(self, event: qt.QShowEvent): | ||
super().showEvent(event) | ||
self._waitingButton.setVisible(True) | ||
|
||
def hideEvent(self, event: qt.QHideEvent): | ||
super().hideEvent(event) | ||
self._waitingButton.setVisible(False) | ||
|
||
def _resize(self): | ||
if not qt_inspect.isValid(self): | ||
return # For _resizeLater in case the widget has been deleted | ||
|
||
parent = self.parent() | ||
if parent is None: | ||
return | ||
|
||
size = self._waitingButton.sizeHint() | ||
if isinstance(parent, PlotWidget): | ||
offset = parent.getWidgetHandle().mapTo(parent, qt.QPoint(0, 0)) | ||
left, top, width, height = parent.getPlotBoundsInPixels() | ||
rect = qt.QRect( | ||
qt.QPoint( | ||
int(offset.x() + left + width / 2 - size.width() / 2), | ||
int(offset.y() + top + height / 2 - size.height() / 2), | ||
), | ||
size, | ||
) | ||
else: | ||
position = parent.size() | ||
position = (position - size) / 2 | ||
rect = qt.QRect(qt.QPoint(position.width(), position.height()), size) | ||
self.setGeometry(rect) | ||
self.raise_() | ||
|
||
def _resizeLater(self): | ||
qt.QTimer.singleShot(0, self._resize) | ||
|
||
def eventFilter(self, watched: qt.QWidget, event: qt.QEvent): | ||
if event.type() == qt.QEvent.Resize: | ||
self._resize() | ||
self._resizeLater() # Defer resize for the receiver to have handled it | ||
return super().eventFilter(watched, event) | ||
|
||
# expose Waiting push button API | ||
def setIconSize(self, size): | ||
self._waitingButton.setIconSize(size) |
Oops, something went wrong.