Browse code

Simple test script.

Jose Fonseca authored on 16/10/2015 23:09:03
Showing 1 changed files

1 1
new file mode 100755
... ...
@@ -0,0 +1,98 @@
1
+#!/usr/bin/env python
2
+#
3
+# Copyright 2015 Jose Fonseca
4
+#
5
+# This program is free software: you can redistribute it and/or modify it
6
+# under the terms of the GNU Lesser General Public License as published
7
+# by the Free Software Foundation, either version 3 of the License, or
8
+# (at your option) any later version.
9
+#
10
+# This program is distributed in the hope that it will be useful,
11
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+# GNU Lesser General Public License for more details.
14
+#
15
+# You should have received a copy of the GNU Lesser General Public License
16
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
+#
18
+
19
+
20
+import sys
21
+import os.path
22
+import traceback
23
+
24
+from gi.repository import Gtk
25
+from gi.repository import Gdk
26
+from gi.repository import GdkPixbuf
27
+
28
+from xdot import DotWidget, DotWindow
29
+
30
+
31
+class TestDotWidget(DotWidget):
32
+
33
+    def __init__(self, name):
34
+        DotWidget.__init__(self)
35
+        self.name = name
36
+
37
+    def on_draw(self, widget, cr):
38
+        DotWidget.on_draw(self, widget, cr)
39
+
40
+        if True:
41
+            # Cairo screenshot
42
+
43
+            import cairo
44
+
45
+            dpi = 96.0
46
+            zoom_ratio = dpi/72.0
47
+            w = int(self.graph.width*zoom_ratio)
48
+            h = int(self.graph.height*zoom_ratio)
49
+
50
+            surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
51
+
52
+            cr = cairo.Context(surface)
53
+
54
+            cr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
55
+            cr.paint()
56
+
57
+            cr.scale(zoom_ratio, zoom_ratio)
58
+
59
+            self.graph.draw(cr, highlight_items=self.highlight)
60
+
61
+            surface.write_to_png(self.name + '.png')
62
+
63
+        if False:
64
+            # GTK 3 screenshot
65
+
66
+            window = self.get_window()
67
+
68
+            w = window.get_width()
69
+            h = window.get_height()
70
+
71
+            pixbuf = Gdk.pixbuf_get_from_window(window, 0, 0, w, h)
72
+
73
+            pixbuf.savev(self.name + '.png', 'png', (), ())
74
+
75
+        Gtk.main_quit()
76
+
77
+
78
+def main():
79
+    for arg in sys.argv[1:]:
80
+        sys.stdout.write(arg + '\n')
81
+        sys.stdout.flush()
82
+        name, ext = os.path.splitext(os.path.basename(arg))
83
+        dotcode = open(arg).read()
84
+        widget = TestDotWidget(name)
85
+        window = DotWindow(widget)
86
+        window.connect('delete-event', Gtk.main_quit)
87
+        try:
88
+            window.set_dotcode(dotcode)
89
+        except:
90
+            exc_info = sys.exc_info()
91
+            traceback.print_exception(*exc_info)
92
+        window.show()
93
+        Gtk.main()
94
+        window.destroy()
95
+
96
+
97
+if __name__ == '__main__':
98
+    main()