author | wenzelm |
Mon, 05 Jan 2015 14:13:38 +0100 | |
changeset 59286 | ac74eedb910a |
parent 59265 | 962ad3942ea7 |
child 59290 | 569a8109eeb2 |
permissions | -rw-r--r-- |
59202 | 1 |
/* Title: Tools/Graphview/visualizer.scala |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
2 |
Author: Markus Kaiser, TU Muenchen |
59240 | 3 |
Author: Makarius |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
4 |
|
50475 | 5 |
Graph visualization parameters and interface state. |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
6 |
*/ |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
7 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
8 |
package isabelle.graphview |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
9 |
|
50464 | 10 |
|
49565
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents:
49557
diff
changeset
|
11 |
import isabelle._ |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
12 |
|
59220 | 13 |
import java.awt.{Font, FontMetrics, Color, Shape, RenderingHints, Graphics2D} |
59231
6dea47cf6c6b
more dynamic visualizer -- re-use jEdit font info;
wenzelm
parents:
59228
diff
changeset
|
14 |
import java.awt.font.FontRenderContext |
50476
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
15 |
import java.awt.image.BufferedImage |
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
16 |
import java.awt.geom.Rectangle2D |
49729 | 17 |
import javax.swing.JComponent |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
18 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
19 |
|
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
20 |
object Visualizer |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
21 |
{ |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
22 |
object Metrics |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
23 |
{ |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
24 |
def apply(font: Font, font_render_context: FontRenderContext) = |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
25 |
new Metrics(font, font_render_context) |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
26 |
|
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
27 |
def apply(gfx: Graphics2D): Metrics = |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
28 |
new Metrics(gfx.getFont, gfx.getFontRenderContext) |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
29 |
} |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
30 |
|
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
31 |
class Metrics private(font: Font, font_render_context: FontRenderContext) |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
32 |
{ |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
33 |
def string_bounds(s: String) = font.getStringBounds(s, font_render_context) |
59242 | 34 |
private val mix = string_bounds("mix") |
35 |
val space_width = string_bounds(" ").getWidth |
|
36 |
def char_width: Double = mix.getWidth / 3 |
|
37 |
def height: Double = mix.getHeight |
|
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
38 |
def ascent: Double = font.getLineMetrics("", font_render_context).getAscent |
59242 | 39 |
def gap: Double = mix.getWidth |
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
40 |
def pad: Double = char_width |
59265
962ad3942ea7
more metrics, with integer coordinates for layout;
wenzelm
parents:
59262
diff
changeset
|
41 |
|
962ad3942ea7
more metrics, with integer coordinates for layout;
wenzelm
parents:
59262
diff
changeset
|
42 |
def box_width2(node: Graph_Display.Node): Double = |
962ad3942ea7
more metrics, with integer coordinates for layout;
wenzelm
parents:
59262
diff
changeset
|
43 |
((string_bounds(node.toString).getWidth + pad) / 2).ceil |
962ad3942ea7
more metrics, with integer coordinates for layout;
wenzelm
parents:
59262
diff
changeset
|
44 |
def box_gap: Double = gap.ceil |
962ad3942ea7
more metrics, with integer coordinates for layout;
wenzelm
parents:
59262
diff
changeset
|
45 |
def box_height(n: Int): Double = (char_width * 1.5 * (5 max n)).ceil |
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
46 |
} |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
47 |
} |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
48 |
|
59286
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59265
diff
changeset
|
49 |
class Visualizer(options: => Options, val model: Model) |
50465 | 50 |
{ |
50472
bad1a1ca61e1
separate instance of class Parameters for each Main_Panel -- avoid global program state;
wenzelm
parents:
50471
diff
changeset
|
51 |
visualizer => |
bad1a1ca61e1
separate instance of class Parameters for each Main_Panel -- avoid global program state;
wenzelm
parents:
50471
diff
changeset
|
52 |
|
50476
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
53 |
|
59233 | 54 |
/* tooltips */ |
55 |
||
56 |
def make_tooltip(parent: JComponent, x: Int, y: Int, body: XML.Body): String = null |
|
57 |
||
58 |
||
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
59 |
/* main colors */ |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
60 |
|
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
61 |
def foreground_color: Color = Color.BLACK |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
62 |
def background_color: Color = Color.WHITE |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
63 |
def selection_color: Color = Color.GREEN |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
64 |
def error_color: Color = Color.RED |
59251 | 65 |
def dummy_color: Color = Color.GRAY |
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
66 |
|
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
67 |
|
50475 | 68 |
/* font rendering information */ |
69 |
||
59286
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59265
diff
changeset
|
70 |
def font(): Font = |
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59265
diff
changeset
|
71 |
{ |
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59265
diff
changeset
|
72 |
val family = options.string("graphview_font_family") |
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59265
diff
changeset
|
73 |
val size = options.int("graphview_font_size") |
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59265
diff
changeset
|
74 |
new Font(family, Font.PLAIN, size) |
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59265
diff
changeset
|
75 |
} |
50475 | 76 |
|
77 |
val rendering_hints = |
|
78 |
new RenderingHints( |
|
79 |
RenderingHints.KEY_ANTIALIASING, |
|
80 |
RenderingHints.VALUE_ANTIALIAS_ON) |
|
81 |
||
59231
6dea47cf6c6b
more dynamic visualizer -- re-use jEdit font info;
wenzelm
parents:
59228
diff
changeset
|
82 |
val font_render_context = new FontRenderContext(null, true, false) |
6dea47cf6c6b
more dynamic visualizer -- re-use jEdit font info;
wenzelm
parents:
59228
diff
changeset
|
83 |
|
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
84 |
def metrics(): Visualizer.Metrics = |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
85 |
Visualizer.Metrics(font(), font_render_context) |
50475 | 86 |
|
87 |
||
88 |
/* rendering parameters */ |
|
89 |
||
90 |
var arrow_heads = false |
|
91 |
||
92 |
object Colors |
|
93 |
{ |
|
94 |
private val filter_colors = List( |
|
59220 | 95 |
new Color(0xD9, 0xF2, 0xE2), // blue |
96 |
new Color(0xFF, 0xE7, 0xD8), // orange |
|
97 |
new Color(0xFF, 0xFF, 0xE5), // yellow |
|
98 |
new Color(0xDE, 0xCE, 0xFF), // lilac |
|
99 |
new Color(0xCC, 0xEB, 0xFF), // turquoise |
|
100 |
new Color(0xFF, 0xE5, 0xE5), // red |
|
101 |
new Color(0xE5, 0xE5, 0xD9) // green |
|
50475 | 102 |
) |
103 |
||
104 |
private var curr : Int = -1 |
|
59220 | 105 |
def next(): Color = |
50475 | 106 |
{ |
107 |
curr = (curr + 1) % filter_colors.length |
|
108 |
filter_colors(curr) |
|
109 |
} |
|
110 |
} |
|
111 |
||
50472
bad1a1ca61e1
separate instance of class Parameters for each Main_Panel -- avoid global program state;
wenzelm
parents:
50471
diff
changeset
|
112 |
|
50465 | 113 |
object Coordinates |
114 |
{ |
|
59262 | 115 |
// owned by GUI thread |
59232 | 116 |
private var layout = Layout.empty |
50465 | 117 |
|
59262 | 118 |
def get_node(node: Graph_Display.Node): Layout.Point = layout.get_node(node) |
119 |
def get_dummies(edge: Graph_Display.Edge): List[Layout.Point] = layout.get_dummies(edge) |
|
50465 | 120 |
|
59262 | 121 |
def translate_node(node: Graph_Display.Node, dx: Double, dy: Double) |
50465 | 122 |
{ |
59262 | 123 |
layout = layout.map_node(node, p => Layout.Point(p.x + dx, p.y + dy)) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
124 |
} |
50465 | 125 |
|
59262 | 126 |
def translate_dummy(d: (Graph_Display.Edge, Int), dx: Double, dy: Double) |
50465 | 127 |
{ |
59262 | 128 |
val (edge, index) = d |
129 |
layout = layout.map_dummies(edge, |
|
130 |
ds => { |
|
131 |
val p = ds(index) |
|
132 |
(ds.zipWithIndex :\ List.empty[Layout.Point]) { |
|
133 |
case ((t, i), n) => if (index == i) Layout.Point(p.x + dx, p.y + dy) :: n else t :: n |
|
134 |
} |
|
135 |
}) |
|
50465 | 136 |
} |
137 |
||
50470 | 138 |
def update_layout() |
50465 | 139 |
{ |
59252 | 140 |
// FIXME avoid expensive operation on GUI thread |
59260 | 141 |
layout = Layout.make(metrics(), model.make_visible_graph()) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
142 |
} |
50465 | 143 |
|
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
144 |
def bounding_box(): Rectangle2D.Double = |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
145 |
{ |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
146 |
val m = metrics() |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
147 |
var x0 = 0.0 |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
148 |
var y0 = 0.0 |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
149 |
var x1 = 0.0 |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
150 |
var y1 = 0.0 |
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59258
diff
changeset
|
151 |
for (node <- model.make_visible_graph().keys_iterator) { |
59258 | 152 |
val shape = Shapes.Node.shape(m, visualizer, node) |
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
153 |
x0 = x0 min shape.getMinX |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
154 |
y0 = y0 min shape.getMinY |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
155 |
x1 = x1 max shape.getMaxX |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
156 |
y1 = y1 max shape.getMaxY |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
157 |
} |
59242 | 158 |
x0 = (x0 - m.gap).floor |
159 |
y0 = (y0 - m.gap).floor |
|
160 |
x1 = (x1 + m.gap).ceil |
|
161 |
y1 = (y1 + m.gap).ceil |
|
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
162 |
new Rectangle2D.Double(x0, y0, x1 - x0, y1 - y0) |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
163 |
} |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
164 |
} |
50465 | 165 |
|
50464 | 166 |
object Drawer |
167 |
{ |
|
59250 | 168 |
def apply(gfx: Graphics2D, node: Graph_Display.Node): Unit = |
59258 | 169 |
if (!node.is_dummy) Shapes.Node.paint(gfx, visualizer, node) |
50465 | 170 |
|
59250 | 171 |
def apply(gfx: Graphics2D, edge: Graph_Display.Edge, head: Boolean, dummies: Boolean): Unit = |
172 |
Shapes.Cardinal_Spline_Edge.paint(gfx, visualizer, edge, head, dummies) |
|
50465 | 173 |
|
59250 | 174 |
def paint_all_visible(gfx: Graphics2D, dummies: Boolean) |
50464 | 175 |
{ |
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59258
diff
changeset
|
176 |
gfx.setFont(font()) |
59250 | 177 |
gfx.setRenderingHints(rendering_hints) |
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59258
diff
changeset
|
178 |
val visible_graph = model.make_visible_graph() |
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59258
diff
changeset
|
179 |
visible_graph.edges_iterator.foreach(apply(gfx, _, arrow_heads, dummies)) |
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59258
diff
changeset
|
180 |
visible_graph.keys_iterator.foreach(apply(gfx, _)) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
181 |
} |
50465 | 182 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
183 |
def shape(m: Visualizer.Metrics, node: Graph_Display.Node): Shape = |
59251 | 184 |
if (node.is_dummy) Shapes.Dummy.shape(m, visualizer) |
59258 | 185 |
else Shapes.Node.shape(m, visualizer, node) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
186 |
} |
50465 | 187 |
|
188 |
object Selection |
|
189 |
{ |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
190 |
// owned by GUI thread |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
191 |
private var state: List[Graph_Display.Node] = Nil |
50465 | 192 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
193 |
def get(): List[Graph_Display.Node] = GUI_Thread.require { state } |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
194 |
def contains(node: Graph_Display.Node): Boolean = get().contains(node) |
50465 | 195 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
196 |
def add(node: Graph_Display.Node): Unit = GUI_Thread.require { state = node :: state } |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
197 |
def clear(): Unit = GUI_Thread.require { state = Nil } |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
198 |
} |
50465 | 199 |
|
59220 | 200 |
sealed case class Node_Color(border: Color, background: Color, foreground: Color) |
50464 | 201 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
202 |
def node_color(node: Graph_Display.Node): Node_Color = |
59251 | 203 |
if (Selection.contains(node)) |
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
204 |
Node_Color(foreground_color, selection_color, foreground_color) |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
205 |
else |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
206 |
Node_Color(foreground_color, model.colors.getOrElse(node, background_color), foreground_color) |
59220 | 207 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
208 |
def edge_color(edge: Graph_Display.Edge): Color = foreground_color |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
209 |
} |