commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 92/101: grc: gtk3: fix block layout sizes w


From: git
Subject: [Commit-gnuradio] [gnuradio] 92/101: grc: gtk3: fix block layout sizes when zooming
Date: Thu, 16 Mar 2017 14:58:14 +0000 (UTC)

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

jcorgan pushed a commit to branch python3
in repository gnuradio.

commit 9460011e1834c07456ebad9b3549b06a0ad18fa6
Author: Sebastian Koslowski <address@hidden>
Date:   Sat Jan 7 11:38:53 2017 +0100

    grc: gtk3: fix block layout sizes when zooming
---
 grc/gui/DrawingArea.py      | 31 +++++++++++++----------
 grc/gui/Notebook.py         |  1 -
 grc/gui/Utils.py            |  2 +-
 grc/gui/canvas/block.py     | 60 ++++++++++++++++++++++++++-------------------
 grc/gui/canvas/drawable.py  |  5 ++--
 grc/gui/canvas/flowgraph.py |  9 +++----
 grc/gui/canvas/port.py      | 18 ++++++++------
 7 files changed, 71 insertions(+), 55 deletions(-)

diff --git a/grc/gui/DrawingArea.py b/grc/gui/DrawingArea.py
index 746666c..2e26c26 100644
--- a/grc/gui/DrawingArea.py
+++ b/grc/gui/DrawingArea.py
@@ -43,6 +43,7 @@ class DrawingArea(Gtk.DrawingArea):
         self._flow_graph = flow_graph
 
         self.zoom_factor = 1.0
+        self._update_after_zoom = False
         self.ctrl_mask = False
         self.mod1_mask = False
         self.button_state = [False] * 10
@@ -61,7 +62,7 @@ class DrawingArea(Gtk.DrawingArea):
             Gdk.EventMask.SCROLL_MASK |
             Gdk.EventMask.LEAVE_NOTIFY_MASK |
             Gdk.EventMask.ENTER_NOTIFY_MASK
-            #Gdk.EventMask.FOCUS_CHANGE_MASK
+            # Gdk.EventMask.FOCUS_CHANGE_MASK
         )
 
         # setup drag and drop
@@ -73,7 +74,9 @@ class DrawingArea(Gtk.DrawingArea):
         # setup the focus flag
         self._focus_flag = False
         self.get_focus_flag = lambda: self._focus_flag
-        def _handle_notify_event(widget, event, focus_flag): self._focus_flag 
= focus_flag
+
+        def _handle_notify_event(widget, event, focus_flag):
+            self._focus_flag = focus_flag
         self.connect('leave-notify-event', _handle_notify_event, False)
         self.connect('enter-notify-event', _handle_notify_event, True)
         # todo: fix
@@ -81,7 +84,7 @@ class DrawingArea(Gtk.DrawingArea):
 #        self.connect('focus-out-event', self._handle_focus_lost_event)
 
     ##########################################################################
-    ## Handlers
+    # Handlers
     ##########################################################################
     def _handle_drag_data_received(self, widget, drag_context, x, y, 
selection_data, info, time):
         """
@@ -90,19 +93,13 @@ class DrawingArea(Gtk.DrawingArea):
         self._flow_graph.add_new_block(selection_data.get_text(), (x, y))
 
     def _handle_mouse_scroll(self, widget, event):
-        if event.get_state() & Gdk.ModifierType.SHIFT_MASK:
-            if event.direction == Gdk.ScrollDirection.UP:
-                event.direction = Gdk.ScrollDirection.LEFT
-            else:
-                event.direction = Gdk.ScrollDirection.RIGHT
-
-        elif event.get_state() & Gdk.ModifierType.CONTROL_MASK:
+        if event.get_state() & Gdk.ModifierType.CONTROL_MASK:
             change = 1.2 if event.direction == Gdk.ScrollDirection.UP else 
1/1.2
             zoom_factor = min(max(self.zoom_factor * change, 0.1), 5.0)
 
             if zoom_factor != self.zoom_factor:
                 self.zoom_factor = zoom_factor
-                self._update_size()
+                self._update_after_zoom = True
                 self.queue_draw()
             return True
 
@@ -157,7 +154,7 @@ class DrawingArea(Gtk.DrawingArea):
         )
 
     def _update_size(self):
-        w, h = self._flow_graph.extent[2:]
+        w, h = self._flow_graph.get_extents()[2:]
         self.set_size_request(w * self.zoom_factor + 100, h * self.zoom_factor 
+ 100)
 
     def _auto_scroll(self, event):
@@ -191,11 +188,19 @@ class DrawingArea(Gtk.DrawingArea):
     def draw(self, widget, cr):
         width = widget.get_allocated_width()
         height = widget.get_allocated_height()
+
         cr.set_source_rgba(*Colors.FLOWGRAPH_BACKGROUND_COLOR)
         cr.rectangle(0, 0, width, height)
+        cr.fill()
 
         cr.scale(self.zoom_factor, self.zoom_factor)
-        cr.fill()
+        cr.set_line_width(2.0 / self.zoom_factor)
+
+        if self._update_after_zoom:
+            self._flow_graph.create_labels(cr)
+            self._flow_graph.create_shapes()
+            self._update_size()
+            self._update_after_zoom = False
 
         self._flow_graph.draw(cr)
 
diff --git a/grc/gui/Notebook.py b/grc/gui/Notebook.py
index 080505a..e78b748 100644
--- a/grc/gui/Notebook.py
+++ b/grc/gui/Notebook.py
@@ -37,7 +37,6 @@ class Notebook(Gtk.Notebook):
         self.set_scrollable(True)
         self.connect('switch-page', self._handle_page_change)
 
-
     def _handle_page_change(self, notebook, page, page_num):
         """
         Handle a page change. When the user clicks on a new tab,
diff --git a/grc/gui/Utils.py b/grc/gui/Utils.py
index 97c648c..9c19daf 100644
--- a/grc/gui/Utils.py
+++ b/grc/gui/Utils.py
@@ -114,7 +114,7 @@ def make_screenshot(flow_graph, file_path, 
transparent_bg=False):
     if not file_path:
         return
 
-    x_min, y_min, x_max, y_max = flow_graph.extent
+    x_min, y_min, x_max, y_max = flow_graph.get_extents()
     padding = Constants.CANVAS_GRID_SIZE
     width = x_max - x_min + 2 * padding
     height = y_max - y_min + 2 * padding
diff --git a/grc/gui/canvas/block.py b/grc/gui/canvas/block.py
index 182e29d..3c1d7da 100644
--- a/grc/gui/canvas/block.py
+++ b/grc/gui/canvas/block.py
@@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 """
 
-from __future__ import absolute_import
+from __future__ import absolute_import, division
 
 import math
 
@@ -51,10 +51,10 @@ class Block(CoreBlock, Drawable):
         Drawable.__init__(self)  # needs the states and initial sizes
 
         self._surface_layouts = [
-            Gtk.DrawingArea().create_pango_layout(''),  # title
-            Gtk.DrawingArea().create_pango_layout(''),  # params
+            None,  # title
+            None,  # params
         ]
-        self._surface_layout_offsets = 0, 0
+        self._surface_layouts_offsets = 0, 0
         self._comment_layout = None
 
         self._area = []
@@ -149,9 +149,18 @@ class Block(CoreBlock, Drawable):
                 port.connector_length = Constants.CONNECTOR_EXTENSION_MINIMAL 
+ \
                                         
Constants.CONNECTOR_EXTENSION_INCREMENT * index
 
-    def create_labels(self):
+    def create_labels(self, cr=None):
         """Create the labels for the signal block."""
-        title_layout, params_layout = self._surface_layouts
+
+        # (Re-)creating layouts here, because layout.context_changed() doesn't 
seems to work (after zoom)
+        title_layout, params_layout = self._surface_layouts = [
+            Gtk.DrawingArea().create_pango_layout(''),  # title
+            Gtk.DrawingArea().create_pango_layout(''),  # params
+        ]
+
+        if cr:  # to fix up extents after zooming
+            PangoCairo.update_layout(cr, title_layout)
+            PangoCairo.update_layout(cr, params_layout)
 
         title_layout.set_markup(
             '<span {foreground} 
font_desc="{font}"><b>{name}</b></span>'.format(
@@ -159,7 +168,7 @@ class Block(CoreBlock, Drawable):
                 name=Utils.encode(self.name)
             )
         )
-        title_width, title_height = title_layout.get_pixel_size()
+        title_width, title_height = title_layout.get_size()
 
         # update the params layout
         if not self.is_dummy_block:
@@ -170,13 +179,12 @@ class Block(CoreBlock, Drawable):
 
         params_layout.set_spacing(LABEL_SEPARATION * Pango.SCALE)
         params_layout.set_markup('\n'.join(markups))
-        params_width, params_height = params_layout.get_pixel_size() if 
markups else (0, 0)
-
-        label_width = max(title_width, params_width)
-        label_height = title_height + LABEL_SEPARATION + params_height
+        params_width, params_height = params_layout.get_size() if markups else 
(0, 0)
 
-        title_layout.set_width(label_width * Pango.SCALE)
-        title_layout.set_alignment(Pango.Alignment.CENTER)
+        label_width = max(title_width, params_width) / Pango.SCALE
+        label_height = title_height / Pango.SCALE
+        if markups:
+            label_height += LABEL_SEPARATION + params_height / Pango.SCALE
 
         # calculate width and height needed
         width = label_width + 2 * BLOCK_LABEL_PADDING
@@ -207,11 +215,15 @@ class Block(CoreBlock, Drawable):
 
         self.width, self.height = width, height = Utils.align_to_grid((width, 
height))
 
-        self._surface_layout_offsets = [
-            (width - label_width) / 2.0,
-            (height - label_height) / 2.0
+        self._surface_layouts_offsets = [
+            (0, (height - label_height) / 2.0),
+            (0, (height - label_height) / 2.0 + LABEL_SEPARATION + 
title_height / Pango.SCALE)
         ]
 
+        title_layout.set_width(width * Pango.SCALE)
+        title_layout.set_alignment(Pango.Alignment.CENTER)
+        params_layout.set_indent((width - label_width) / 2.0 * Pango.SCALE)
+
         self.create_comment_layout()
 
     def create_port_labels(self):
@@ -269,14 +281,13 @@ class Block(CoreBlock, Drawable):
         if self.is_vertical():
             cr.rotate(-math.pi / 2)
             cr.translate(-self.width, 0)
-        cr.translate(*self._surface_layout_offsets)
-
         cr.set_source_rgba(*self._font_color)
-        for layout in self._surface_layouts:
+        for layout, offset in zip(self._surface_layouts, 
self._surface_layouts_offsets):
+            cr.save()
+            cr.translate(*offset)
             PangoCairo.update_layout(cr, layout)
             PangoCairo.show_layout(cr, layout)
-            _, h = layout.get_pixel_size()
-            cr.translate(0, h + LABEL_SEPARATION)
+            cr.restore()
 
     def what_is_selected(self, coor, coor_m=None):
         """
@@ -314,13 +325,12 @@ class Block(CoreBlock, Drawable):
         PangoCairo.show_layout(cr, self._comment_layout)
         cr.restore()
 
-    @property
-    def extent(self):
-        extent = Drawable.extent.fget(self)
+    def get_extents(self):
+        extent = Drawable.get_extents(self)
         x, y = self.coordinate
         for port in self.active_ports():
             extent = (min_or_max(xy, offset + p_xy) for offset, min_or_max, 
xy, p_xy in zip(
-                (x, y, x, y), (min, min, max, max), extent, port.extent
+                (x, y, x, y), (min, min, max, max), extent, port.get_extents()
             ))
         return tuple(extent)
 
diff --git a/grc/gui/canvas/drawable.py b/grc/gui/canvas/drawable.py
index 12f88c2..d755d44 100644
--- a/grc/gui/canvas/drawable.py
+++ b/grc/gui/canvas/drawable.py
@@ -96,7 +96,7 @@ class Drawable(object):
         dx, dy = delta_coor
         self.coordinate = (x + dx, y + dy)
 
-    def create_labels(self):
+    def create_labels(self, cr=None):
         """
         Create labels (if applicable) and call on all children.
         Call this base method before creating labels in the element.
@@ -168,8 +168,7 @@ class Drawable(object):
                 if x <= x1 <= x_m and y <= y1 <= y_m:
                     return self
 
-    @property
-    def extent(self):
+    def get_extents(self):
         x_min, y_min = x_max, y_max = self.coordinate
         x_min += min(x for x, y in self._bounding_points)
         y_min += min(y for x, y in self._bounding_points)
diff --git a/grc/gui/canvas/flowgraph.py b/grc/gui/canvas/flowgraph.py
index ab432b3..2aa0d7c 100644
--- a/grc/gui/canvas/flowgraph.py
+++ b/grc/gui/canvas/flowgraph.py
@@ -464,9 +464,9 @@ class FlowGraph(CoreFlowgraph, Drawable):
                 continue  # skip hidden disabled blocks and connections
             self._elements_to_draw.append(element)
 
-    def create_labels(self):
+    def create_labels(self, cr=None):
         for element in self._elements_to_draw:
-            element.create_labels()
+            element.create_labels(cr)
 
     def create_shapes(self):
         for element in self._elements_to_draw:
@@ -743,11 +743,10 @@ class FlowGraph(CoreFlowgraph, Drawable):
                 self.coordinate = (x, y)
                 self.drawing_area.queue_draw()
 
-    @property
-    def extent(self):
+    def get_extents(self):
         extent = 100000, 100000, 0, 0
         for element in self._elements_to_draw:
             extent = (min_or_max(xy, e_xy) for min_or_max, xy, e_xy in zip(
-                (min, min, max, max), extent, element.extent
+                (min, min, max, max), extent, element.get_extents()
             ))
         return tuple(extent)
diff --git a/grc/gui/canvas/port.py b/grc/gui/canvas/port.py
index bc40c9c..2ea55aa 100644
--- a/grc/gui/canvas/port.py
+++ b/grc/gui/canvas/port.py
@@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 """
 
-from __future__ import absolute_import
+from __future__ import absolute_import, division
 
 import math
 
@@ -55,8 +55,7 @@ class Port(CorePort, Drawable):
         self.width_with_label = self.height = 0
         self.connector_length = 0
 
-        self.label_layout = Gtk.DrawingArea().create_pango_layout('')
-        self.label_layout.set_alignment(Pango.Alignment.CENTER)
+        self.label_layout = None
 
     @property
     def width(self):
@@ -103,8 +102,13 @@ class Port(CorePort, Drawable):
             270: (self.height / 2, self.width)
         }[self.get_connector_direction()]
 
-    def create_labels(self):
+    def create_labels(self, cr=None):
         """Create the labels for the socket."""
+        self.label_layout = Gtk.DrawingArea().create_pango_layout('')
+        self.label_layout.set_alignment(Pango.Alignment.CENTER)
+
+        if cr:
+            PangoCairo.update_layout(cr, self.label_layout)
 
         if self.domain in (Constants.GR_MESSAGE_DOMAIN, 
Constants.DEFAULT_DOMAIN):
             self._line_width_factor = 1.0
@@ -117,10 +121,10 @@ class Port(CorePort, Drawable):
         layout.set_markup('<span font_desc="{font}">{name}</span>'.format(
             name=Utils.encode(self.name), font=Constants.PORT_FONT
         ))
-        label_width, label_height = self.label_layout.get_pixel_size()
+        label_width, label_height = self.label_layout.get_size()
 
-        self.width = 2 * Constants.PORT_LABEL_PADDING + label_width
-        self.height = 2 * Constants.PORT_LABEL_PADDING + label_height
+        self.width = 2 * Constants.PORT_LABEL_PADDING + label_width / 
Pango.SCALE
+        self.height = 2 * Constants.PORT_LABEL_PADDING + label_height / 
Pango.SCALE
         self._label_layout_offsets = [0, Constants.PORT_LABEL_PADDING]
         if self.get_type() == 'bus':
             self.height += Constants.PORT_EXTRA_BUS_HEIGHT



reply via email to

[Prev in Thread] Current Thread [Next in Thread]