author | wenzelm |
Sun, 04 Jan 2015 21:01:27 +0100 | |
changeset 59262 | 5cd92c743958 |
parent 59260 | c8bd83f8dad9 |
child 59265 | 962ad3942ea7 |
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 |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
41 |
} |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
42 |
} |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
43 |
|
50465 | 44 |
class Visualizer(val model: Model) |
45 |
{ |
|
50472
bad1a1ca61e1
separate instance of class Parameters for each Main_Panel -- avoid global program state;
wenzelm
parents:
50471
diff
changeset
|
46 |
visualizer => |
bad1a1ca61e1
separate instance of class Parameters for each Main_Panel -- avoid global program state;
wenzelm
parents:
50471
diff
changeset
|
47 |
|
50476
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
48 |
|
59233 | 49 |
/* tooltips */ |
50 |
||
51 |
def make_tooltip(parent: JComponent, x: Int, y: Int, body: XML.Body): String = null |
|
52 |
||
53 |
||
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
54 |
/* main colors */ |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
55 |
|
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
56 |
def foreground_color: Color = Color.BLACK |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
57 |
def background_color: Color = Color.WHITE |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
58 |
def selection_color: Color = Color.GREEN |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
59 |
def error_color: Color = Color.RED |
59251 | 60 |
def dummy_color: Color = Color.GRAY |
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
61 |
|
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
62 |
|
50475 | 63 |
/* font rendering information */ |
64 |
||
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
65 |
def font_size: Int = 12 |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
66 |
def font(): Font = new Font("Helvetica", Font.PLAIN, font_size) |
50475 | 67 |
|
68 |
val rendering_hints = |
|
69 |
new RenderingHints( |
|
70 |
RenderingHints.KEY_ANTIALIASING, |
|
71 |
RenderingHints.VALUE_ANTIALIAS_ON) |
|
72 |
||
59231
6dea47cf6c6b
more dynamic visualizer -- re-use jEdit font info;
wenzelm
parents:
59228
diff
changeset
|
73 |
val font_render_context = new FontRenderContext(null, true, false) |
6dea47cf6c6b
more dynamic visualizer -- re-use jEdit font info;
wenzelm
parents:
59228
diff
changeset
|
74 |
|
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
75 |
def metrics(): Visualizer.Metrics = |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
76 |
Visualizer.Metrics(font(), font_render_context) |
50475 | 77 |
|
78 |
||
79 |
/* rendering parameters */ |
|
80 |
||
81 |
var arrow_heads = false |
|
82 |
||
83 |
object Colors |
|
84 |
{ |
|
85 |
private val filter_colors = List( |
|
59220 | 86 |
new Color(0xD9, 0xF2, 0xE2), // blue |
87 |
new Color(0xFF, 0xE7, 0xD8), // orange |
|
88 |
new Color(0xFF, 0xFF, 0xE5), // yellow |
|
89 |
new Color(0xDE, 0xCE, 0xFF), // lilac |
|
90 |
new Color(0xCC, 0xEB, 0xFF), // turquoise |
|
91 |
new Color(0xFF, 0xE5, 0xE5), // red |
|
92 |
new Color(0xE5, 0xE5, 0xD9) // green |
|
50475 | 93 |
) |
94 |
||
95 |
private var curr : Int = -1 |
|
59220 | 96 |
def next(): Color = |
50475 | 97 |
{ |
98 |
curr = (curr + 1) % filter_colors.length |
|
99 |
filter_colors(curr) |
|
100 |
} |
|
101 |
} |
|
102 |
||
50472
bad1a1ca61e1
separate instance of class Parameters for each Main_Panel -- avoid global program state;
wenzelm
parents:
50471
diff
changeset
|
103 |
|
50465 | 104 |
object Coordinates |
105 |
{ |
|
59262 | 106 |
// owned by GUI thread |
59232 | 107 |
private var layout = Layout.empty |
50465 | 108 |
|
59262 | 109 |
def get_node(node: Graph_Display.Node): Layout.Point = layout.get_node(node) |
110 |
def get_dummies(edge: Graph_Display.Edge): List[Layout.Point] = layout.get_dummies(edge) |
|
50465 | 111 |
|
59262 | 112 |
def translate_node(node: Graph_Display.Node, dx: Double, dy: Double) |
50465 | 113 |
{ |
59262 | 114 |
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
|
115 |
} |
50465 | 116 |
|
59262 | 117 |
def translate_dummy(d: (Graph_Display.Edge, Int), dx: Double, dy: Double) |
50465 | 118 |
{ |
59262 | 119 |
val (edge, index) = d |
120 |
layout = layout.map_dummies(edge, |
|
121 |
ds => { |
|
122 |
val p = ds(index) |
|
123 |
(ds.zipWithIndex :\ List.empty[Layout.Point]) { |
|
124 |
case ((t, i), n) => if (index == i) Layout.Point(p.x + dx, p.y + dy) :: n else t :: n |
|
125 |
} |
|
126 |
}) |
|
50465 | 127 |
} |
128 |
||
50470 | 129 |
def update_layout() |
50465 | 130 |
{ |
59252 | 131 |
// FIXME avoid expensive operation on GUI thread |
59260 | 132 |
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
|
133 |
} |
50465 | 134 |
|
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
135 |
def bounding_box(): Rectangle2D.Double = |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
136 |
{ |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
137 |
val m = metrics() |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
138 |
var x0 = 0.0 |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
139 |
var y0 = 0.0 |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
140 |
var x1 = 0.0 |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
141 |
var y1 = 0.0 |
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59258
diff
changeset
|
142 |
for (node <- model.make_visible_graph().keys_iterator) { |
59258 | 143 |
val shape = Shapes.Node.shape(m, visualizer, node) |
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
144 |
x0 = x0 min shape.getMinX |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
145 |
y0 = y0 min shape.getMinY |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
146 |
x1 = x1 max shape.getMaxX |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
147 |
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
|
148 |
} |
59242 | 149 |
x0 = (x0 - m.gap).floor |
150 |
y0 = (y0 - m.gap).floor |
|
151 |
x1 = (x1 + m.gap).ceil |
|
152 |
y1 = (y1 + m.gap).ceil |
|
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
153 |
new Rectangle2D.Double(x0, y0, x1 - x0, y1 - y0) |
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
154 |
} |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
155 |
} |
50465 | 156 |
|
50464 | 157 |
object Drawer |
158 |
{ |
|
59250 | 159 |
def apply(gfx: Graphics2D, node: Graph_Display.Node): Unit = |
59258 | 160 |
if (!node.is_dummy) Shapes.Node.paint(gfx, visualizer, node) |
50465 | 161 |
|
59250 | 162 |
def apply(gfx: Graphics2D, edge: Graph_Display.Edge, head: Boolean, dummies: Boolean): Unit = |
163 |
Shapes.Cardinal_Spline_Edge.paint(gfx, visualizer, edge, head, dummies) |
|
50465 | 164 |
|
59250 | 165 |
def paint_all_visible(gfx: Graphics2D, dummies: Boolean) |
50464 | 166 |
{ |
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59258
diff
changeset
|
167 |
gfx.setFont(font()) |
59250 | 168 |
gfx.setRenderingHints(rendering_hints) |
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59258
diff
changeset
|
169 |
val visible_graph = model.make_visible_graph() |
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59258
diff
changeset
|
170 |
visible_graph.edges_iterator.foreach(apply(gfx, _, arrow_heads, dummies)) |
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59258
diff
changeset
|
171 |
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
|
172 |
} |
50465 | 173 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
174 |
def shape(m: Visualizer.Metrics, node: Graph_Display.Node): Shape = |
59251 | 175 |
if (node.is_dummy) Shapes.Dummy.shape(m, visualizer) |
59258 | 176 |
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
|
177 |
} |
50465 | 178 |
|
179 |
object Selection |
|
180 |
{ |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
181 |
// owned by GUI thread |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
182 |
private var state: List[Graph_Display.Node] = Nil |
50465 | 183 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
184 |
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
|
185 |
def contains(node: Graph_Display.Node): Boolean = get().contains(node) |
50465 | 186 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
187 |
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
|
188 |
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
|
189 |
} |
50465 | 190 |
|
59220 | 191 |
sealed case class Node_Color(border: Color, background: Color, foreground: Color) |
50464 | 192 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
193 |
def node_color(node: Graph_Display.Node): Node_Color = |
59251 | 194 |
if (Selection.contains(node)) |
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
195 |
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
|
196 |
else |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
197 |
Node_Color(foreground_color, model.colors.getOrElse(node, background_color), foreground_color) |
59220 | 198 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59242
diff
changeset
|
199 |
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
|
200 |
} |