author | wenzelm |
Wed, 12 Feb 2025 00:40:57 +0100 | |
changeset 82142 | 508a673c87ac |
parent 81398 | f92ea68473f2 |
permissions | -rw-r--r-- |
59202 | 1 |
/* Title: Tools/Graphview/graph_panel.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 |
|
59461 | 5 |
GUI panel for graph layout. |
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 |
|
55618 | 10 |
|
49558 | 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 |
|
59441 | 13 |
import java.awt.{Dimension, Graphics2D, Point, Rectangle} |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
14 |
import java.awt.geom.{AffineTransform, Point2D} |
82142 | 15 |
import javax.swing.{JScrollPane, SwingUtilities} |
49729 | 16 |
|
59408 | 17 |
import scala.swing.{BorderPanel, Button, CheckBox, Action, FileChooser, Panel, ScrollPane} |
82142 | 18 |
import scala.swing.event.{Event, Key, MousePressed, MouseDragged, MouseClicked} |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
19 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
20 |
|
75393 | 21 |
class Graph_Panel(val graphview: Graphview) extends BorderPanel { |
59408 | 22 |
graph_panel => |
23 |
||
49729 | 24 |
|
59398 | 25 |
|
59408 | 26 |
/** graph **/ |
59398 | 27 |
|
59408 | 28 |
/* painter */ |
59243
21ef04bd4e17
recovered tooltip from 6e77ddb1e3fb: non-null default is required as prerequisite;
wenzelm
parents:
59241
diff
changeset
|
29 |
|
75393 | 30 |
private val painter = new Panel { |
31 |
override def paint(gfx: Graphics2D): Unit = { |
|
59408 | 32 |
super.paintComponent(gfx) |
33 |
||
59459 | 34 |
gfx.setColor(graphview.background_color) |
59408 | 35 |
gfx.fillRect(0, 0, peer.getWidth, peer.getHeight) |
36 |
||
37 |
gfx.transform(Transform()) |
|
59460 | 38 |
graphview.paint(gfx) |
59408 | 39 |
} |
49729 | 40 |
} |
41 |
||
75393 | 42 |
def set_preferred_size(): Unit = { |
59459 | 43 |
val box = graphview.bounding_box() |
59408 | 44 |
val s = Transform.scale_discrete |
45 |
painter.preferredSize = new Dimension((box.width * s).ceil.toInt, (box.height * s).ceil.toInt) |
|
46 |
painter.revalidate() |
|
47 |
} |
|
59398 | 48 |
|
75393 | 49 |
def refresh(): Unit = { |
59408 | 50 |
if (painter != null) { |
51 |
set_preferred_size() |
|
52 |
painter.repaint() |
|
53 |
} |
|
54 |
} |
|
59237
ac135eff1ffb
clarified mouse wheel: conventional scrolling, not scaling;
wenzelm
parents:
59234
diff
changeset
|
55 |
|
75393 | 56 |
def scroll_to_node(node: Graph_Display.Node): Unit = { |
59459 | 57 |
val gap = graphview.metrics.gap |
58 |
val info = graphview.layout.get_node(node) |
|
59398 | 59 |
|
60 |
val t = Transform() |
|
61 |
val p = |
|
62 |
t.transform(new Point2D.Double(info.x - info.width2 - gap, info.y - info.height2 - gap), null) |
|
63 |
val q = |
|
64 |
t.transform(new Point2D.Double(info.x + info.width2 + gap, info.y + info.height2 + gap), null) |
|
65 |
||
59408 | 66 |
painter.peer.scrollRectToVisible( |
59398 | 67 |
new Rectangle(p.getX.toInt, p.getY.toInt, |
68 |
(q.getX - p.getX).ceil.toInt, (q.getY - p.getY).ceil.toInt)) |
|
49735
30e2f3f1c623
more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents:
49733
diff
changeset
|
69 |
} |
30e2f3f1c623
more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents:
49733
diff
changeset
|
70 |
|
50470 | 71 |
|
59408 | 72 |
/* scrollable graph pane */ |
73 |
||
74 |
private val graph_pane: ScrollPane = new ScrollPane(painter) { |
|
75 |
tooltip = "" |
|
50478 | 76 |
|
59408 | 77 |
override lazy val peer: JScrollPane = new JScrollPane with SuperMixin { |
78 |
override def getToolTipText(event: java.awt.event.MouseEvent): String = |
|
59459 | 79 |
graphview.find_node(Transform.pane_to_graph_coordinates(event.getPoint)) match { |
59408 | 80 |
case Some(node) => |
59459 | 81 |
graphview.model.full_graph.get_node(node) match { |
59408 | 82 |
case Nil => null |
81398
f92ea68473f2
clarified signature with subtle change of semantics: output consists of individual messages that are formatted (and separated) internally;
wenzelm
parents:
81382
diff
changeset
|
83 |
case List(tip: XML.Elem) => |
f92ea68473f2
clarified signature with subtle change of semantics: output consists of individual messages that are formatted (and separated) internally;
wenzelm
parents:
81382
diff
changeset
|
84 |
graphview.make_tooltip(graph_pane.peer, event.getX, event.getY, tip) |
f92ea68473f2
clarified signature with subtle change of semantics: output consists of individual messages that are formatted (and separated) internally;
wenzelm
parents:
81382
diff
changeset
|
85 |
case body => |
f92ea68473f2
clarified signature with subtle change of semantics: output consists of individual messages that are formatted (and separated) internally;
wenzelm
parents:
81382
diff
changeset
|
86 |
val tip = Pretty.block(body, indent = 0) |
f92ea68473f2
clarified signature with subtle change of semantics: output consists of individual messages that are formatted (and separated) internally;
wenzelm
parents:
81382
diff
changeset
|
87 |
graphview.make_tooltip(graph_pane.peer, event.getX, event.getY, tip) |
59408 | 88 |
} |
89 |
case None => null |
|
90 |
} |
|
59218 | 91 |
} |
50470 | 92 |
|
59408 | 93 |
horizontalScrollBarPolicy = ScrollPane.BarPolicy.Always |
94 |
verticalScrollBarPolicy = ScrollPane.BarPolicy.Always |
|
95 |
||
96 |
listenTo(mouse.moves) |
|
97 |
listenTo(mouse.clicks) |
|
98 |
reactions += |
|
75393 | 99 |
{ |
100 |
case MousePressed(_, p, _, _, _) => |
|
101 |
Mouse_Interaction.pressed(p) |
|
102 |
painter.repaint() |
|
103 |
case MouseDragged(_, to, _) => |
|
104 |
Mouse_Interaction.dragged(to) |
|
105 |
painter.repaint() |
|
106 |
case e @ MouseClicked(_, p, m, n, _) => |
|
107 |
Mouse_Interaction.clicked(p, m, n, SwingUtilities.isRightMouseButton(e.peer)) |
|
108 |
painter.repaint() |
|
109 |
} |
|
50470 | 110 |
|
59408 | 111 |
contents = painter |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
112 |
} |
59408 | 113 |
graph_pane.peer.getVerticalScrollBar.setUnitIncrement(10) |
50470 | 114 |
|
59408 | 115 |
|
116 |
/* transform */ |
|
49733
38a68e6593be
prefer synchronous Mutator_Event.Bus on Swing_Thread;
wenzelm
parents:
49732
diff
changeset
|
117 |
|
75393 | 118 |
def rescale(s: Double): Unit = { |
59398 | 119 |
Transform.scale = s |
120 |
if (zoom != null) zoom.set_item((Transform.scale_discrete * 100).floor.toInt) |
|
121 |
refresh() |
|
122 |
} |
|
50470 | 123 |
|
75393 | 124 |
def fit_to_window(): Unit = { |
59399 | 125 |
Transform.fit_to_window() |
126 |
refresh() |
|
127 |
} |
|
128 |
||
75393 | 129 |
private object Transform { |
50474
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50470
diff
changeset
|
130 |
private var _scale: Double = 1.0 |
50477 | 131 |
def scale: Double = _scale |
75393 | 132 |
def scale_=(s: Double): Unit = { |
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
133 |
_scale = (s min 10.0) max 0.1 |
50468 | 134 |
} |
59255
db265648139c
clarified fit_to_window: floor scale within window bounds;
wenzelm
parents:
59253
diff
changeset
|
135 |
|
75393 | 136 |
def scale_discrete: Double = { |
59459 | 137 |
val font_height = GUI.line_metrics(graphview.metrics.font).getHeight.toInt |
59286
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59262
diff
changeset
|
138 |
(scale * font_height).floor / font_height |
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59262
diff
changeset
|
139 |
} |
50470 | 140 |
|
75393 | 141 |
def apply(): AffineTransform = { |
59459 | 142 |
val box = graphview.bounding_box() |
59397 | 143 |
val t = AffineTransform.getScaleInstance(scale_discrete, scale_discrete) |
144 |
t.translate(- box.x, - box.y) |
|
145 |
t |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
146 |
} |
50470 | 147 |
|
75393 | 148 |
def fit_to_window(): Unit = { |
59459 | 149 |
if (graphview.visible_graph.is_empty) |
50491 | 150 |
rescale(1.0) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
151 |
else { |
59459 | 152 |
val box = graphview.bounding_box() |
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
153 |
rescale((size.width / box.width) min (size.height / box.height)) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
154 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
155 |
} |
50470 | 156 |
|
75393 | 157 |
def pane_to_graph_coordinates(at: Point2D): Point2D = { |
50477 | 158 |
val s = Transform.scale_discrete |
59408 | 159 |
val p = Transform().inverseTransform(graph_pane.peer.getViewport.getViewPosition, null) |
50470 | 160 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
161 |
p.setLocation(p.getX + at.getX / s, p.getY + at.getY / s) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
162 |
p |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
163 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
164 |
} |
50470 | 165 |
|
59398 | 166 |
|
167 |
/* interaction */ |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
168 |
|
59459 | 169 |
graphview.model.Colors.events += { case _ => painter.repaint() } |
170 |
graphview.model.Mutators.events += { case _ => painter.repaint() } |
|
59398 | 171 |
|
75393 | 172 |
private object Mouse_Interaction { |
59398 | 173 |
private var draginfo: (Point, List[Graph_Display.Node], List[Layout.Dummy]) = |
174 |
(new Point(0, 0), Nil, Nil) |
|
59253 | 175 |
|
75393 | 176 |
def pressed(at: Point): Unit = { |
59253 | 177 |
val c = Transform.pane_to_graph_coordinates(at) |
178 |
val l = |
|
59459 | 179 |
graphview.find_node(c) match { |
59253 | 180 |
case Some(node) => |
59459 | 181 |
if (graphview.Selection.contains(node)) graphview.Selection.get() |
59253 | 182 |
else List(node) |
183 |
case None => Nil |
|
184 |
} |
|
185 |
val d = |
|
186 |
l match { |
|
59459 | 187 |
case Nil => graphview.find_dummy(c).toList |
59253 | 188 |
case _ => Nil |
189 |
} |
|
190 |
draginfo = (at, l, d) |
|
191 |
} |
|
50470 | 192 |
|
75393 | 193 |
def dragged(to: Point): Unit = { |
59398 | 194 |
val (from, p, d) = draginfo |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
195 |
|
59253 | 196 |
val s = Transform.scale_discrete |
59398 | 197 |
val dx = to.x - from.x |
198 |
val dy = to.y - from.y |
|
199 |
||
59253 | 200 |
(p, d) match { |
201 |
case (Nil, Nil) => |
|
59408 | 202 |
val r = graph_pane.peer.getViewport.getViewRect |
59302
4d985afc0565
explict layout graph structure, with dummies and coordinates;
wenzelm
parents:
59294
diff
changeset
|
203 |
r.translate(- dx, - dy) |
59408 | 204 |
painter.peer.scrollRectToVisible(r) |
50470 | 205 |
|
59253 | 206 |
case (Nil, ds) => |
59459 | 207 |
ds.foreach(d => graphview.translate_vertex(d, dx / s, dy / s)) |
59253 | 208 |
|
209 |
case (ls, _) => |
|
59459 | 210 |
ls.foreach(l => graphview.translate_vertex(Layout.Node(l), dx / s, dy / s)) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
211 |
} |
59398 | 212 |
|
213 |
draginfo = (to, p, d) |
|
214 |
} |
|
215 |
||
75393 | 216 |
def clicked(at: Point, m: Key.Modifiers, clicks: Int, right_click: Boolean): Unit = { |
59398 | 217 |
val c = Transform.pane_to_graph_coordinates(at) |
218 |
||
219 |
if (clicks < 2) { |
|
220 |
if (right_click) { |
|
59403 | 221 |
// FIXME |
222 |
if (false) { |
|
59459 | 223 |
val menu = Popups(graph_panel, graphview.find_node(c), graphview.Selection.get()) |
59408 | 224 |
menu.show(graph_pane.peer, at.x, at.y) |
59403 | 225 |
} |
59398 | 226 |
} |
227 |
else { |
|
59459 | 228 |
(graphview.find_node(c), m) match { |
229 |
case (Some(node), Key.Modifier.Control) => graphview.Selection.add(node) |
|
59398 | 230 |
case (None, Key.Modifier.Control) => |
231 |
||
59459 | 232 |
case (Some(node), Key.Modifier.Shift) => graphview.Selection.add(node) |
59398 | 233 |
case (None, Key.Modifier.Shift) => |
234 |
||
235 |
case (Some(node), _) => |
|
59459 | 236 |
graphview.Selection.clear() |
237 |
graphview.Selection.add(node) |
|
59398 | 238 |
case (None, _) => |
59459 | 239 |
graphview.Selection.clear() |
59398 | 240 |
} |
241 |
} |
|
242 |
} |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
243 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
244 |
} |
59408 | 245 |
|
246 |
||
247 |
||
248 |
/** controls **/ |
|
249 |
||
250 |
private val mutator_dialog = |
|
59459 | 251 |
new Mutator_Dialog(graphview, graphview.model.Mutators, "Filters", "Hide", false) |
59408 | 252 |
private val color_dialog = |
59459 | 253 |
new Mutator_Dialog(graphview, graphview.model.Colors, "Colorations") |
59408 | 254 |
|
255 |
private val chooser = new FileChooser { |
|
256 |
fileSelectionMode = FileChooser.SelectionMode.FilesOnly |
|
257 |
title = "Save image (.png or .pdf)" |
|
258 |
} |
|
259 |
||
59409 | 260 |
private val show_content = new CheckBox() { |
59459 | 261 |
selected = graphview.show_content |
59409 | 262 |
action = Action("Show content") { |
59459 | 263 |
graphview.show_content = selected |
264 |
graphview.update_layout() |
|
59409 | 265 |
refresh() |
266 |
} |
|
267 |
tooltip = "Show full node content within graph layout" |
|
268 |
} |
|
269 |
||
270 |
private val show_arrow_heads = new CheckBox() { |
|
59459 | 271 |
selected = graphview.show_arrow_heads |
59409 | 272 |
action = Action("Show arrow heads") { |
59459 | 273 |
graphview.show_arrow_heads = selected |
59409 | 274 |
painter.repaint() |
275 |
} |
|
276 |
tooltip = "Draw edges with explicit arrow heads" |
|
277 |
} |
|
278 |
||
279 |
private val show_dummies = new CheckBox() { |
|
59459 | 280 |
selected = graphview.show_dummies |
59409 | 281 |
action = Action("Show dummies") { |
59459 | 282 |
graphview.show_dummies = selected |
59409 | 283 |
painter.repaint() |
284 |
} |
|
285 |
tooltip = "Draw dummy nodes within graph layout, for easier mouse dragging" |
|
286 |
} |
|
287 |
||
288 |
private val save_image = new Button { |
|
289 |
action = Action("Save image") { |
|
290 |
chooser.showSaveDialog(this) match { |
|
59441 | 291 |
case FileChooser.Result.Approve => |
59459 | 292 |
try { Graph_File.write(chooser.selectedFile, graphview) } |
59441 | 293 |
catch { |
294 |
case ERROR(msg) => GUI.error_dialog(this.peer, "Error", GUI.scrollable_text(msg)) |
|
295 |
} |
|
59409 | 296 |
case _ => |
297 |
} |
|
298 |
} |
|
299 |
tooltip = "Save current graph layout as PNG or PDF" |
|
300 |
} |
|
301 |
||
81382 | 302 |
private val zoom = new GUI.Zoom { override def changed(): Unit = rescale(scale) } |
59409 | 303 |
|
304 |
private val fit_window = new Button { |
|
305 |
action = Action("Fit to window") { fit_to_window() } |
|
306 |
tooltip = "Zoom to fit window width and height" |
|
307 |
} |
|
308 |
||
309 |
private val update_layout = new Button { |
|
310 |
action = Action("Update layout") { |
|
59459 | 311 |
graphview.update_layout() |
59409 | 312 |
refresh() |
313 |
} |
|
314 |
tooltip = "Regenerate graph layout according to built-in algorithm" |
|
315 |
} |
|
316 |
||
61176 | 317 |
private val editor_style = new CheckBox() { |
318 |
selected = graphview.editor_style |
|
319 |
action = Action("Editor style") { |
|
320 |
graphview.editor_style = selected |
|
321 |
graphview.update_layout() |
|
322 |
refresh() |
|
323 |
} |
|
324 |
tooltip = "Use editor font and colors for painting" |
|
325 |
} |
|
326 |
||
73367 | 327 |
private val colorations = new Button { action = Action("Colorations") { color_dialog.open() } } |
328 |
private val filters = new Button { action = Action("Filters") { mutator_dialog.open() } } |
|
59409 | 329 |
|
59408 | 330 |
private val controls = |
66205 | 331 |
Wrap_Panel( |
332 |
List(show_content, show_arrow_heads, show_dummies, |
|
66206 | 333 |
save_image, zoom, fit_window, update_layout, editor_style)) // FIXME colorations, filters |
59408 | 334 |
|
335 |
||
336 |
||
337 |
/** main layout **/ |
|
338 |
||
339 |
layout(graph_pane) = BorderPanel.Position.Center |
|
340 |
layout(controls) = BorderPanel.Position.North |
|
341 |
||
342 |
rescale(1.0) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
343 |
} |