diff options
author | Sebastian Koslowski <koslowski@kit.edu> | 2016-08-30 16:28:24 +0200 |
---|---|---|
committer | Sebastian Koslowski <koslowski@kit.edu> | 2016-09-12 12:15:35 -0600 |
commit | a867a290194228d09ba93f0f46e3a4e4523f5396 (patch) | |
tree | df3d40d363831d204f28e97718c942d6312f4856 | |
parent | eeea99b45c0a0dccd935dc5203b71e0adf1430fe (diff) |
grc: gtk3: make screnshots as png, pdf and svg
-rw-r--r-- | grc/gui/ActionHandler.py | 9 | ||||
-rw-r--r-- | grc/gui/Actions.py | 2 | ||||
-rw-r--r-- | grc/gui/FlowGraph.py | 4 | ||||
-rw-r--r-- | grc/gui/Utils.py | 44 |
4 files changed, 48 insertions, 11 deletions
diff --git a/grc/gui/ActionHandler.py b/grc/gui/ActionHandler.py index 85b68a9629..913eb5a8d1 100644 --- a/grc/gui/ActionHandler.py +++ b/grc/gui/ActionHandler.py @@ -24,9 +24,8 @@ import os import subprocess from gi.repository import Gtk -from gi.repository import GObject -from . import Dialogs, Preferences, Actions, Executor, Constants, FileDialogs +from . import Dialogs, Preferences, Actions, Executor, FileDialogs, Utils from .MainWindow import MainWindow from .ParserErrorsDialog import ParserErrorsDialog from .PropsDialog import PropsDialog @@ -548,8 +547,10 @@ class ActionHandler: elif action == Actions.FLOW_GRAPH_SCREEN_CAPTURE: file_path, background_transparent = FileDialogs.SaveScreenShot(main, page.file_path).run() if file_path is not None: - pixbuf = flow_graph.get_drawing_area().get_screenshot(background_transparent) - pixbuf.save(file_path, Constants.IMAGE_FILE_EXTENSION[1:]) + try: + Utils.make_screenshot(flow_graph, file_path, background_transparent) + except ValueError: + Messages.send('Failed to generate screen shot\n') ################################################## # Gen/Exec/Stop ################################################## diff --git a/grc/gui/Actions.py b/grc/gui/Actions.py index 3a51e80918..4759e56294 100644 --- a/grc/gui/Actions.py +++ b/grc/gui/Actions.py @@ -466,7 +466,7 @@ FLOW_GRAPH_SCREEN_CAPTURE = Action( label='Screen Ca_pture', tooltip='Create a screen capture of the flow graph', stock_id=Gtk.STOCK_PRINT, - keypresses=(Gdk.KEY_Print, NO_MODS_MASK), + keypresses=(Gdk.KEY_p, Gdk.ModifierType.CONTROL_MASK), ) PORT_CONTROLLER_DEC = Action( keypresses=(Gdk.KEY_minus, NO_MODS_MASK, Gdk.KEY_KP_Subtract, NO_MODS_MASK), diff --git a/grc/gui/FlowGraph.py b/grc/gui/FlowGraph.py index df8e668eed..f04383f32c 100644 --- a/grc/gui/FlowGraph.py +++ b/grc/gui/FlowGraph.py @@ -30,7 +30,7 @@ from six.moves import filter from gi.repository import GLib -from . import Actions, Colors, Utils, Bars, Dialogs +from . import Actions, Colors, Constants, Utils, Bars, Dialogs from .Element import Element from .external_editor import ExternalEditor @@ -735,7 +735,7 @@ class FlowGraph(CoreFlowgraph, Element): X, Y = self.coordinate dX, dY = int(x - X), int(y - Y) active = Actions.TOGGLE_SNAP_TO_GRID.get_active() or self.drawing_area.mod1_mask - if not active or abs(dX) >= Utils.CANVAS_GRID_SIZE or abs(dY) >= Utils.CANVAS_GRID_SIZE: + if not active or abs(dX) >= Constants.CANVAS_GRID_SIZE or abs(dY) >= Constants.CANVAS_GRID_SIZE: self.move_selected((dX, dY)) self.coordinate = (x, y) # queue draw for animation diff --git a/grc/gui/Utils.py b/grc/gui/Utils.py index d474c66f19..782a7e3a01 100644 --- a/grc/gui/Utils.py +++ b/grc/gui/Utils.py @@ -20,8 +20,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA from __future__ import absolute_import from gi.repository import GLib +import cairo -from .Constants import POSSIBLE_ROTATIONS, CANVAS_GRID_SIZE, COMPLEX_TYPES +from . import Colors, Constants def get_rotated_coordinate(coor, rotation): @@ -37,7 +38,7 @@ def get_rotated_coordinate(coor, rotation): """ # handles negative angles rotation = (rotation + 360) % 360 - if rotation not in POSSIBLE_ROTATIONS: + if rotation not in Constants.POSSIBLE_ROTATIONS: raise ValueError('unusable rotation angle "%s"'%str(rotation)) # determine the number of degrees to rotate cos_r, sin_r = { @@ -68,7 +69,7 @@ def get_angle_from_coordinates(p1, p2): def align_to_grid(coor, mode=round): def align(value): - return int(mode(value / (1.0 * CANVAS_GRID_SIZE)) * CANVAS_GRID_SIZE) + return int(mode(value / (1.0 * Constants.CANVAS_GRID_SIZE)) * Constants.CANVAS_GRID_SIZE) try: return [align(c) for c in coor] except TypeError: @@ -88,7 +89,7 @@ def num_to_str(num): return template.format(value / factor, symbol.strip()) return template.format(value, '') - if isinstance(num, COMPLEX_TYPES): + if isinstance(num, Constants.COMPLEX_TYPES): num = complex(num) # Cast to python complex if num == 0: return '0' @@ -107,3 +108,38 @@ def encode(value): """ valid_utf8 = value.decode('utf-8', errors='replace').encode('utf-8') return GLib.markup_escape_text(valid_utf8) + + +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.extend + padding = Constants.CANVAS_GRID_SIZE + width = x_max - x_min + 2 * padding + height = y_max - y_min + 2 * padding + + if file_path.endswith('.png'): + psurf = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) + elif file_path.endswith('.pdf'): + psurf = cairo.PDFSurface(file_path, width, height) + elif file_path.endswith('.svg'): + psurf = cairo.SVGSurface(file_path, width, height) + else: + raise ValueError('Unknown file format') + + cr = cairo.Context(psurf) + + if not transparent_bg: + cr.set_source_rgba(*Colors.FLOWGRAPH_BACKGROUND_COLOR) + cr.rectangle(0, 0, width, height) + cr.fill() + + cr.translate(padding - x_min, padding - y_min) + flow_graph.draw(cr) + + if file_path.endswith('.png'): + psurf.write_to_png(file_path) + if file_path.endswith('.pdf') or file_path.endswith('.svg'): + cr.show_page() + psurf.finish() |