[segyio] 130/376: Added LayoutCanvas widget

Jørgen Kvalsvik jokva-guest at moszumanska.debian.org
Wed Sep 20 08:04:20 UTC 2017


This is an automated email from the git hooks/post-receive script.

jokva-guest pushed a commit to branch debian
in repository segyio.

commit a2cd21f62f4c6a3b3a6a2cd96efcad9457c3cff6
Author: Jean-Paul Balabanian <jepebe at users.noreply.github.com>
Date:   Thu Nov 17 13:46:47 2016 +0100

    Added LayoutCanvas widget
---
 examples/showcase_gui.py        |   9 +--
 python/segyview/CMakeLists.txt  |   2 +
 python/segyview/__init__.py     |   2 +
 python/segyview/layoutcanvas.py | 130 ++++++++++++++++++++++++++++++++++++++++
 python/segyview/layoutfigure.py |  31 ++++++++++
 5 files changed, 170 insertions(+), 4 deletions(-)

diff --git a/examples/showcase_gui.py b/examples/showcase_gui.py
index e16c8ed..19f694d 100644
--- a/examples/showcase_gui.py
+++ b/examples/showcase_gui.py
@@ -4,7 +4,7 @@ import sys
 from PyQt4.QtCore import Qt
 from PyQt4.QtGui import QMainWindow, QWidget, QApplication, QLabel, QVBoxLayout
 
-from segyview import LayoutCombo, ColormapCombo
+from segyview import LayoutCombo, ColormapCombo, LayoutCanvas
 
 
 class TestGUI(QMainWindow):
@@ -29,8 +29,9 @@ class TestGUI(QMainWindow):
         layout = QVBoxLayout()
         central_widget.setLayout(layout)
 
-        self._layout_label = QLabel()
-        layout.addWidget(self._layout_label)
+        self._layout_canvas = LayoutCanvas(width=5, height=5)
+        self._layout_canvas.set_plot_layout(layout_combo.get_current_layout())
+        layout.addWidget(self._layout_canvas)
 
         self._colormap_label = QLabel()
         layout.addWidget(self._colormap_label)
@@ -38,7 +39,7 @@ class TestGUI(QMainWindow):
         self.setCentralWidget(central_widget)
 
     def _layout_changed(self, layout):
-        self._layout_label.setText("Layout selected: %s" % str(layout))
+        self._layout_canvas.set_plot_layout(layout)
 
     def _colormap_changed(self, index):
         colormap = str(self._colormap_combo.itemText(index))
diff --git a/python/segyview/CMakeLists.txt b/python/segyview/CMakeLists.txt
index 8de829f..ddbffed 100644
--- a/python/segyview/CMakeLists.txt
+++ b/python/segyview/CMakeLists.txt
@@ -8,6 +8,8 @@ set(PYTHON_SOURCES
     viewer.py
     layoutcombo.py
     colormapcombo.py
+    layoutfigure.py
+    layoutcanvas.py
     )
 
 add_python_package(segyview segyview "${PYTHON_SOURCES}")
diff --git a/python/segyview/__init__.py b/python/segyview/__init__.py
index f248bac..295852a 100644
--- a/python/segyview/__init__.py
+++ b/python/segyview/__init__.py
@@ -23,6 +23,8 @@ def resource_icon(name):
 try:
     from .colormapcombo import ColormapCombo
     from .layoutcombo import LayoutCombo
+    from .layoutfigure import LayoutFigure
+    from .layoutcanvas import LayoutCanvas
     from .progresswidget import ProgressWidget
     from .slicewidget import SliceWidget, ColorBarWidget
     from .segyiowrapper import SegyIOWrapper, SlicesWrapper
diff --git a/python/segyview/layoutcanvas.py b/python/segyview/layoutcanvas.py
new file mode 100644
index 0000000..9da61f0
--- /dev/null
+++ b/python/segyview/layoutcanvas.py
@@ -0,0 +1,130 @@
+from PyQt4 import QtGui
+from PyQt4.QtCore import pyqtSignal, Qt
+
+from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
+
+from segyview import LayoutFigure
+
+
+class Keys(object):
+    def __init__(self, key=None, ctrl=False, alt=False, shift=False, meta=False):
+        super(Keys, self).__init__()
+        self.key = key
+        self.alt = alt
+        self.ctrl = ctrl
+        self.super = meta
+        self.shift = shift
+
+    def state(self, ctrl=False, alt=False, shift=False, meta=False):
+        return self.ctrl == ctrl and self.alt == alt and self.shift == shift and self.super == meta
+
+    def __bool__(self):
+        return not (self.key is None and not self.alt and not self.ctrl and not self.super and not self.shift)
+
+    __nonzero__ = __bool__
+
+    def __str__(self):
+        return "%s ctrl: %s shift: %s alt: %s super: %s self: %s" % (self.key, self.ctrl, self.shift, self.alt, self.super, bool(self))
+
+
+class LayoutCanvas(FigureCanvas):
+    layout_changed = pyqtSignal()
+    subplot_pressed = pyqtSignal(dict)
+    subplot_released = pyqtSignal(dict)
+    subplot_motion = pyqtSignal(dict)
+    subplot_scrolled = pyqtSignal(dict)
+
+    def __init__(self, width=11.7, height=8.3, dpi=100, parent=None):
+        self._figure = LayoutFigure(width, height, dpi)
+
+        FigureCanvas.__init__(self, self._figure)
+        self.setParent(parent)
+
+        self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
+        self.updateGeometry()
+        self.setFocusPolicy(Qt.WheelFocus)
+        self.setFocus()
+
+        self.mpl_connect('scroll_event', self._mouse_scrolled)
+        self.mpl_connect('button_press_event', self._mouse_pressed)
+        self.mpl_connect('button_release_event', self._mouse_released)
+        self.mpl_connect('motion_notify_event', self._mouse_motion)
+        self.mpl_connect('key_press_event', self._key_press_event)
+        self.mpl_connect('key_release_event', self._key_release_event)
+
+        self._start_x = None
+        self._start_y = None
+        self._start_subplot_index = None
+        self._keys = Keys()
+
+    def _create_event(self, event):
+        data = {
+            "x": event.xdata,
+            "y": event.ydata,
+            "mx": event.x,
+            "my": event.y,
+            "dx": None if self._start_x is None else event.xdata - self._start_x,
+            "dy": None if self._start_y is None else event.ydata - self._start_y,
+            "button": event.button,
+            "key": self._keys,
+            "step": event.step,
+            "subplot_index": self._start_subplot_index or self._figure.index(event.inaxes),
+            "gui_event": event.guiEvent
+        }
+        return data
+
+    def _key_press_event(self, event):
+        if len(event.key) > 0:
+            self._keys.ctrl = 'ctrl' in event.key or 'control' in event.key
+            self._keys.shift = 'shift' in event.key
+            self._keys.alt = 'alt' in event.key
+            self._keys.meta = 'super' in event.key
+            keys = event.key.replace('ctrl', '')
+            keys = keys.replace('control', '')
+            keys = keys.replace('alt', '')
+            keys = keys.replace('super', '')
+            keys = keys.replace('+', '')
+            self._keys.keys = keys
+
+    def _key_release_event(self, event):
+        # the event is unclear on what key is actually released -> wiping every key :)
+        self._keys.keys = None
+        self._keys.ctrl = False
+        self._keys.alt = False
+        self._keys.shift = False
+        self._keys.super = False
+
+    def _mouse_scrolled(self, event):
+        if event.inaxes is not None:
+            data = self._create_event(event)
+            self.subplot_scrolled.emit(data)
+
+    def _mouse_pressed(self, event):
+        if event.inaxes is not None:
+            data = self._create_event(event)
+            self._start_x = data['x']
+            self._start_y = data['y']
+            self._start_subplot_index = data['subplot_index']
+            self.subplot_pressed.emit(data)
+
+    def _mouse_released(self, event):
+        if event.inaxes is not None:
+            self._start_x = None
+            self._start_y = None
+            self._start_subplot_index = None
+            data = self._create_event(event)
+            self.subplot_released.emit(data)
+
+    def _mouse_motion(self, event):
+        if event.inaxes is not None:
+            data = self._create_event(event)
+            self.subplot_motion.emit(data)
+
+    def set_plot_layout(self, layout_spec):
+        self._figure.set_plot_layout(layout_spec)
+        self.layout_changed.emit()
+        self.draw()
+
+    def layout_figure(self):
+        """ :rtype: LayoutFigure """
+        return self._figure
diff --git a/python/segyview/layoutfigure.py b/python/segyview/layoutfigure.py
new file mode 100644
index 0000000..c81a79f
--- /dev/null
+++ b/python/segyview/layoutfigure.py
@@ -0,0 +1,31 @@
+from matplotlib import gridspec
+from matplotlib.figure import Figure
+from matplotlib.axes import Axes
+
+
+class LayoutFigure(Figure):
+    def __init__(self, width=11.7, height=8.3, dpi=100, tight_layout=True, **kwargs):
+        super(LayoutFigure, self).__init__(figsize=(width, height), dpi=dpi, tight_layout=tight_layout, **kwargs)
+
+        self._axes = []
+        """ :type: list[Axes] """
+
+    def set_plot_layout(self, layout_spec):
+        grid_spec = gridspec.GridSpec(*layout_spec['dims'])
+
+        for axes in self._axes:
+            self.delaxes(axes)
+
+        self._axes = [self.add_subplot(grid_spec[sub_spec]) for sub_spec in layout_spec['grid']]
+
+    def index(self, axes):
+        """
+        :param axes: The Axes instance to find the index of.
+        :type axes: Axes
+        :rtype: int
+        """
+        return self._axes.index(axes)
+
+    def layout_axes(self):
+        """ :rtype: list[Axes] """
+        return list(self._axes)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/segyio.git



More information about the debian-science-commits mailing list