From e19c96eff0c310c06c4f268c8b80cb33bd08996f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Picca=20Fr=C3=A9d=C3=A9ric-Emmanuel?= Date: Sat, 25 Nov 2017 16:55:20 +0100 Subject: New upstream version 0.6.1+dfsg --- examples/plot3dContextMenu.py | 112 +++++++++++++++++++++++ examples/viewer3DVolume.py | 202 ++++++++++++++++++++---------------------- 2 files changed, 209 insertions(+), 105 deletions(-) create mode 100644 examples/plot3dContextMenu.py (limited to 'examples') diff --git a/examples/plot3dContextMenu.py b/examples/plot3dContextMenu.py new file mode 100644 index 0000000..d33bb8f --- /dev/null +++ b/examples/plot3dContextMenu.py @@ -0,0 +1,112 @@ +# coding: utf-8 +# /*########################################################################## +# +# Copyright (c) 2017 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. +# +# ###########################################################################*/ +""" +This script adds a context menu to a :class:`silx.gui.plot3d.ScalarFieldView`. + +This is done by adding a custom context menu to the :class:`Plot3DWidget`: + +- set the context menu policy to Qt.CustomContextMenu. +- connect to the customContextMenuRequested signal. + +For more information on context menus, see Qt documentation. +""" + +from __future__ import absolute_import, division, unicode_literals + +__authors__ = ["T. Vincent"] +__license__ = "MIT" +__date__ = "03/10/2017" + + +import logging + +import numpy + +from silx.gui import qt + +from silx.gui.plot3d.ScalarFieldView import ScalarFieldView +from silx.gui.plot3d import actions + +logging.basicConfig() + +_logger = logging.getLogger(__name__) + + +class ScalarFieldViewWithContextMenu(ScalarFieldView): + """Subclass ScalarFieldView to add a custom context menu to its 3D area.""" + + def __init__(self, parent=None): + super(ScalarFieldViewWithContextMenu, self).__init__(parent) + self.setWindowTitle("Right-click to open the context menu") + + # Set Plot3DWidget custom context menu + self.getPlot3DWidget().setContextMenuPolicy(qt.Qt.CustomContextMenu) + self.getPlot3DWidget().customContextMenuRequested.connect( + self._contextMenu) + + def _contextMenu(self, pos): + """Handle plot area customContextMenuRequested signal. + + :param QPoint pos: Mouse position relative to plot area + """ + # Create the context menu + menu = qt.QMenu(self) + menu.addAction(actions.mode.PanAction( + parent=menu, plot3d=self.getPlot3DWidget())) + menu.addAction(actions.mode.RotateArcballAction( + parent=menu, plot3d=self.getPlot3DWidget())) + menu.addSeparator() + menu.addAction(actions.io.CopyAction( + parent=menu, plot3d=self.getPlot3DWidget())) + + # Displaying the context menu at the mouse position requires + # a global position. + # The position received as argument is relative to Plot3DWidget + # and needs to be converted. + globalPosition = self.getPlot3DWidget().mapToGlobal(pos) + menu.exec_(globalPosition) + + +# Start Qt QApplication +app = qt.QApplication([]) + +# Create the viewer main window +window = ScalarFieldViewWithContextMenu() + +# Create dummy data +coords = numpy.linspace(-10, 10, 64) +z = coords.reshape(-1, 1, 1) +y = coords.reshape(1, -1, 1) +x = coords.reshape(1, 1, -1) +data = numpy.sin(x * y * z) / (x * y * z) + +# Set ScalarFieldView data +window.setData(data) + +# Add an iso-surface +window.addIsosurface(0.2, '#FF0000FF') + +window.show() +app.exec_() diff --git a/examples/viewer3DVolume.py b/examples/viewer3DVolume.py index 82022f9..d030fba 100644 --- a/examples/viewer3DVolume.py +++ b/examples/viewer3DVolume.py @@ -22,7 +22,7 @@ # THE SOFTWARE. # # ###########################################################################*/ -"""This script illustrates the use of silx.gui.plot3d.ScalarFieldView. +"""This script illustrates the use of :class:`silx.gui.plot3d.ScalarFieldView`. It loads a 3D scalar data set from a file and displays iso-surfaces and an interactive cutting plane. @@ -39,6 +39,7 @@ __date__ = "05/01/2017" import argparse import logging import os.path +import sys import numpy @@ -114,107 +115,98 @@ def default_isolevel(data): return numpy.mean(data) + numpy.std(data) -def main(argv=None): - # Parse input arguments - parser = argparse.ArgumentParser( - description=__doc__) - parser.add_argument( - '-l', '--level', nargs='?', type=float, default=float('nan'), - help="The value at which to generate the iso-surface") - parser.add_argument( - '-sx', '--xscale', nargs='?', type=float, default=1., - help="The scale of the data on the X axis") - parser.add_argument( - '-sy', '--yscale', nargs='?', type=float, default=1., - help="The scale of the data on the Y axis") - parser.add_argument( - '-sz', '--zscale', nargs='?', type=float, default=1., - help="The scale of the data on the Z axis") - parser.add_argument( - '-ox', '--xoffset', nargs='?', type=float, default=0., - help="The offset of the data on the X axis") - parser.add_argument( - '-oy', '--yoffset', nargs='?', type=float, default=0., - help="The offset of the data on the Y axis") - parser.add_argument( - '-oz', '--zoffset', nargs='?', type=float, default=0., - help="The offset of the data on the Z axis") - parser.add_argument( - 'filename', - nargs='?', - default=None, - help="""Filename to open. - - It supports 3D volume saved as .npy or in .h5 files. - - It also support nD data set (n>=3) stored in a HDF5 file. - For HDF5, provide the filename and path as: ::. - If the data set has more than 3 dimensions, it is possible to choose a - 3D data set as a subset by providing the indices along the first n-3 - dimensions with '#': - ::#<1st_dim_index>...# - - E.g.: data.h5::/data_5D#1#1 - """) - args = parser.parse_args(args=argv) - - # Start GUI - global app # QApplication must be global to avoid seg fault on quit - app = qt.QApplication([]) - - # Create the viewer main window - window = ScalarFieldView() - window.setAttribute(qt.Qt.WA_DeleteOnClose) - - # Create a parameter tree for the scalar field view - treeView = SFViewParamTree.TreeView(window) - treeView.setSfView(window) # Attach the parameter tree to the view - - # Add the parameter tree to the main window in a dock widget - dock = qt.QDockWidget() - dock.setWindowTitle('Parameters') - dock.setWidget(treeView) - window.addDockWidget(qt.Qt.RightDockWidgetArea, dock) - - # Load data from file - if args.filename is not None: - data = load(args.filename) - _logger.info('Data:\n\tShape: %s\n\tRange: [%f, %f]', - str(data.shape), data.min(), data.max()) - else: - # Create dummy data - _logger.warning('Not data file provided, creating dummy data') - coords = numpy.linspace(-10, 10, 64) - z = coords.reshape(-1, 1, 1) - y = coords.reshape(1, -1, 1) - x = coords.reshape(1, 1, -1) - data = numpy.sin(x * y * z) / (x * y * z) - - # Set ScalarFieldView data - window.setData(data) - - # Set scale of the data - window.setScale(args.xscale, args.yscale, args.zscale) - - # Set offset of the data - window.setTranslation(args.xoffset, args.yoffset, args.zoffset) - - # Set axes labels - window.setAxesLabels('X', 'Y', 'Z') - - # Add an iso-surface - if not numpy.isnan(args.level): - # Add an iso-surface at the given iso-level - window.addIsosurface(args.level, '#FF0000FF') - else: - # Add an iso-surface from a function - window.addIsosurface(default_isolevel, '#FF0000FF') - - window.show() - return app.exec_() - - -if __name__ == '__main__': - import sys - - sys.exit(main(argv=sys.argv[1:])) +# Parse input arguments +parser = argparse.ArgumentParser( + description=__doc__) +parser.add_argument( + '-l', '--level', nargs='?', type=float, default=float('nan'), + help="The value at which to generate the iso-surface") +parser.add_argument( + '-sx', '--xscale', nargs='?', type=float, default=1., + help="The scale of the data on the X axis") +parser.add_argument( + '-sy', '--yscale', nargs='?', type=float, default=1., + help="The scale of the data on the Y axis") +parser.add_argument( + '-sz', '--zscale', nargs='?', type=float, default=1., + help="The scale of the data on the Z axis") +parser.add_argument( + '-ox', '--xoffset', nargs='?', type=float, default=0., + help="The offset of the data on the X axis") +parser.add_argument( + '-oy', '--yoffset', nargs='?', type=float, default=0., + help="The offset of the data on the Y axis") +parser.add_argument( + '-oz', '--zoffset', nargs='?', type=float, default=0., + help="The offset of the data on the Z axis") +parser.add_argument( + 'filename', + nargs='?', + default=None, + help="""Filename to open. + + It supports 3D volume saved as .npy or in .h5 files. + + It also support nD data set (n>=3) stored in a HDF5 file. + For HDF5, provide the filename and path as: ::. + If the data set has more than 3 dimensions, it is possible to choose a + 3D data set as a subset by providing the indices along the first n-3 + dimensions with '#': + ::#<1st_dim_index>...# + + E.g.: data.h5::/data_5D#1#1 + """) +args = parser.parse_args(args=sys.argv[1:]) + +# Start GUI +app = qt.QApplication([]) + +# Create the viewer main window +window = ScalarFieldView() + +# Create a parameter tree for the scalar field view +treeView = SFViewParamTree.TreeView(window) +treeView.setSfView(window) # Attach the parameter tree to the view + +# Add the parameter tree to the main window in a dock widget +dock = qt.QDockWidget() +dock.setWindowTitle('Parameters') +dock.setWidget(treeView) +window.addDockWidget(qt.Qt.RightDockWidgetArea, dock) + +# Load data from file +if args.filename is not None: + data = load(args.filename) + _logger.info('Data:\n\tShape: %s\n\tRange: [%f, %f]', + str(data.shape), data.min(), data.max()) +else: + # Create dummy data + _logger.warning('Not data file provided, creating dummy data') + coords = numpy.linspace(-10, 10, 64) + z = coords.reshape(-1, 1, 1) + y = coords.reshape(1, -1, 1) + x = coords.reshape(1, 1, -1) + data = numpy.sin(x * y * z) / (x * y * z) + +# Set ScalarFieldView data +window.setData(data) + +# Set scale of the data +window.setScale(args.xscale, args.yscale, args.zscale) + +# Set offset of the data +window.setTranslation(args.xoffset, args.yoffset, args.zoffset) + +# Set axes labels +window.setAxesLabels('X', 'Y', 'Z') + +# Add an iso-surface +if not numpy.isnan(args.level): + # Add an iso-surface at the given iso-level + window.addIsosurface(args.level, '#FF0000FF') +else: + # Add an iso-surface from a function + window.addIsosurface(default_isolevel, '#FF0000FF') + +window.show() +app.exec_() -- cgit v1.2.3