mirror of
https://github.com/wassname/scikit-image.git
synced 2026-08-14 12:50:39 +08:00
Dock plugins to image viewer.
This commit is contained in:
@@ -78,10 +78,13 @@ class Plugin(QtGui.QDialog):
|
||||
image_updated = pyqtSignal(np.ndarray)
|
||||
_started = pyqtSignal()
|
||||
|
||||
def __init__(self, image_filter=None, height=0, width=400, useblit=True):
|
||||
def __init__(self, image_filter=None, height=0, width=400, useblit=True,
|
||||
dock='bottom'):
|
||||
init_qtapp()
|
||||
super(Plugin, self).__init__()
|
||||
|
||||
self.dock = dock
|
||||
|
||||
self.image_viewer = None
|
||||
# If subclass defines `image_filter` method ignore input.
|
||||
if not hasattr(self, 'image_filter'):
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
ImageViewer class for viewing and interacting with images.
|
||||
"""
|
||||
from ..qt import QtGui
|
||||
from ..qt import QtCore
|
||||
from ..qt.QtCore import Qt
|
||||
|
||||
from skimage import io, img_as_float
|
||||
from skimage.util.dtype import dtype_range
|
||||
@@ -17,6 +17,12 @@ from ..plugins.base import Plugin
|
||||
__all__ = ['ImageViewer', 'CollectionViewer']
|
||||
|
||||
|
||||
dock_areas = {'top': Qt.TopDockWidgetArea,
|
||||
'bottom': Qt.BottomDockWidgetArea,
|
||||
'left': Qt.LeftDockWidgetArea,
|
||||
'right': Qt.RightDockWidgetArea}
|
||||
|
||||
|
||||
def mpl_image_to_rgba(mpl_image):
|
||||
"""Return RGB image from the given matplotlib image object.
|
||||
|
||||
@@ -67,16 +73,16 @@ class ImageViewer(QtGui.QMainWindow):
|
||||
|
||||
#TODO: Add ImageViewer to skimage.io window manager
|
||||
|
||||
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
|
||||
self.setAttribute(Qt.WA_DeleteOnClose)
|
||||
self.setWindowTitle("Image Viewer")
|
||||
|
||||
self.file_menu = QtGui.QMenu('&File', self)
|
||||
self.file_menu.addAction('Open file', self.open_file,
|
||||
QtCore.Qt.CTRL + QtCore.Qt.Key_O)
|
||||
Qt.CTRL + Qt.Key_O)
|
||||
self.file_menu.addAction('Save to file', self.save_to_file,
|
||||
QtCore.Qt.CTRL + QtCore.Qt.Key_S)
|
||||
Qt.CTRL + Qt.Key_S)
|
||||
self.file_menu.addAction('Quit', self.close,
|
||||
QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
|
||||
Qt.CTRL + Qt.Key_Q)
|
||||
self.menuBar().addMenu(self.file_menu)
|
||||
|
||||
self.main_widget = QtGui.QWidget()
|
||||
@@ -113,8 +119,33 @@ class ImageViewer(QtGui.QMainWindow):
|
||||
def __add__(self, plugin):
|
||||
"""Add plugin to ImageViewer"""
|
||||
plugin.attach(self)
|
||||
if plugin.dock:
|
||||
location = dock_areas[plugin.dock]
|
||||
dock_location = Qt.DockWidgetArea(location)
|
||||
dock = QtGui.QDockWidget()
|
||||
dock.setWidget(plugin)
|
||||
self.addDockWidget(dock_location, dock)
|
||||
|
||||
horiz = (dock_areas['left'], dock_areas['right'])
|
||||
dimension = 'width' if location in horiz else 'height'
|
||||
self._add_widget_size(plugin, dimension=dimension)
|
||||
|
||||
return self
|
||||
|
||||
def _add_widget_size(self, widget, dimension='width'):
|
||||
widget_size = widget.sizeHint()
|
||||
viewer_size = self.frameGeometry()
|
||||
|
||||
dx = dy = 0
|
||||
if dimension == 'width':
|
||||
dx = widget_size.width()
|
||||
elif dimension == 'height':
|
||||
dy = widget_size.height()
|
||||
|
||||
w = viewer_size.width()
|
||||
h = viewer_size.height()
|
||||
self.resize(w + dx, h + dy)
|
||||
|
||||
def open_file(self):
|
||||
"""Open image file and display in viewer."""
|
||||
filename = dialogs.open_file_dialog()
|
||||
@@ -160,14 +191,16 @@ class ImageViewer(QtGui.QMainWindow):
|
||||
|
||||
def auto_layout(self):
|
||||
"""Move viewer to top-left and align plugin on right edge of viewer."""
|
||||
size = self.geometry()
|
||||
size = self.frameGeometry()
|
||||
self.move(0, 0)
|
||||
w = size.width()
|
||||
y = 0
|
||||
#TODO: Layout isn't quite correct for multiple plugins (overlaps).
|
||||
for p in self.plugins:
|
||||
p.move(w, y)
|
||||
y += p.geometry().height()
|
||||
# w = size.width()
|
||||
# h = size.height()
|
||||
# x = 0
|
||||
# y = h
|
||||
# #TODO: Layout isn't quite correct for multiple plugins (overlaps).
|
||||
# for p in self.plugins:
|
||||
# p.move(x, y)
|
||||
# y += p.frameGeometry().height()
|
||||
|
||||
def _show(self):
|
||||
self.auto_layout()
|
||||
|
||||
@@ -5,5 +5,5 @@ from skimage import data
|
||||
|
||||
image = data.load('color.png')
|
||||
viewer = ImageViewer(image)
|
||||
viewer += ColorHistogram()
|
||||
viewer += ColorHistogram(dock='right')
|
||||
viewer.show()
|
||||
|
||||
Reference in New Issue
Block a user