| 1 |
from __future__ import print_function, absolute_import |
| 2 |
import os, time, sys |
| 3 |
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" |
| 4 |
import pygame |
| 5 |
from pygame.locals import * |
| 6 |
from dotviewer.drawgraph import GraphRenderer, FIXEDFONT |
| 7 |
from dotviewer.drawgraph import Node, Edge |
| 8 |
from dotviewer.drawgraph import EventQueue, wait_for_events |
| 9 |
from dotviewer.strunicode import forceunicode, forcestr, unicode |
| 10 |
|
| 11 |
|
| 12 |
METAKEYS = dict([ |
| 13 |
(ident[len('KMOD_'):].lower(), getattr(pygame.locals, ident)) |
| 14 |
for ident in dir(pygame.locals) if ident.startswith('KMOD_') and ident != 'KMOD_NONE' |
| 15 |
]) |
| 16 |
|
| 17 |
if sys.platform == 'darwin': |
| 18 |
PMETA = 'lmeta', 'rmeta' |
| 19 |
else: |
| 20 |
PMETA = 'lalt', 'ralt', 'lctrl', 'rctrl' |
| 21 |
|
| 22 |
METAKEYS['meta'] = PMETA |
| 23 |
METAKEYS['shift'] = 'lshift', 'rshift' |
| 24 |
|
| 25 |
KEYS = dict([ |
| 26 |
(ident[len('K_'):].lower(), getattr(pygame.locals, ident)) |
| 27 |
for ident in dir(pygame.locals) if ident.startswith('K_') |
| 28 |
]) |
| 29 |
|
| 30 |
|
| 31 |
KEYS['plus'] = ('=', '+', '.') |
| 32 |
KEYS['quit'] = ('q', 'escape') |
| 33 |
KEYS['help'] = ('h', '?', 'f1') |
| 34 |
|
| 35 |
def GET_KEY(key): |
| 36 |
if len(key) == 1: |
| 37 |
return key |
| 38 |
return KEYS[key] |
| 39 |
|
| 40 |
def permute_mods(base, args): |
| 41 |
if not args: |
| 42 |
yield base |
| 43 |
return |
| 44 |
first, rest = args[0], args[1:] |
| 45 |
for val in first: |
| 46 |
for rval in permute_mods(base | val, rest): |
| 47 |
yield rval |
| 48 |
|
| 49 |
class Display(object): |
| 50 |
|
| 51 |
def __init__(self, size=(800,680)): |
| 52 |
w, h = size |
| 53 |
|
| 54 |
|
| 55 |
pygame.display.init() |
| 56 |
self.highdpi = pygame.display.Info().current_h > 1500 |
| 57 |
if self.highdpi: |
| 58 |
w *= 2 |
| 59 |
h *= 2 |
| 60 |
pygame.font.init() |
| 61 |
self.resize((w,h)) |
| 62 |
|
| 63 |
def resize(self, size): |
| 64 |
w, h = size |
| 65 |
self.width = w |
| 66 |
self.height = h |
| 67 |
self.screen = pygame.display.set_mode((w, h), HWSURFACE|RESIZABLE, 32) |
| 68 |
|
| 69 |
class GraphDisplay(Display): |
| 70 |
STATUSBARFONT = FIXEDFONT |
| 71 |
ANIM_STEP = 0.03 |
| 72 |
KEY_REPEAT = (500, 50) |
| 73 |
STATUSBAR_ALPHA = 0.75 |
| 74 |
STATUSBAR_FGCOLOR = (255, 255, 80) |
| 75 |
STATUSBAR_BGCOLOR = (128, 0, 0) |
| 76 |
STATUSBAR_OVERFLOWCOLOR = (255, 0, 0) |
| 77 |
HELP_ALPHA = 0.95 |
| 78 |
HELP_FGCOLOR = (255, 255, 80) |
| 79 |
HELP_BGCOLOR = (0, 128, 0) |
| 80 |
INPUT_ALPHA = 0.75 |
| 81 |
INPUT_FGCOLOR = (255, 255, 80) |
| 82 |
INPUT_BGCOLOR = (0, 0, 128) |
| 83 |
|
| 84 |
KEYS = { |
| 85 |
'meta -' : ('zoom', 0.5), |
| 86 |
'-' : ('zoom', 0.5), |
| 87 |
'meta plus' : ('zoom', 2.0), |
| 88 |
'plus' : ('zoom', 2.0), |
| 89 |
'meta 0' : 'zoom_actual_size', |
| 90 |
'0' : 'zoom_actual_size', |
| 91 |
'meta 1' : 'zoom_to_fit', |
| 92 |
'1' : 'zoom_to_fit', |
| 93 |
'meta f4' : 'quit', |
| 94 |
'meta quit' : 'quit', |
| 95 |
'quit' : 'quit', |
| 96 |
'meta right' : 'layout_forward', |
| 97 |
'meta left': 'layout_back', |
| 98 |
'backspace' : 'layout_back', |
| 99 |
'f': 'search', |
| 100 |
'/': 'search', |
| 101 |
'n': 'find_next', |
| 102 |
'p': 'find_prev', |
| 103 |
'r': 'reload', |
| 104 |
'left' : ('pan', (-1, 0)), |
| 105 |
'right' : ('pan', (1, 0)), |
| 106 |
'up' : ('pan', (0, -1)), |
| 107 |
'down' : ('pan', (0, 1)), |
| 108 |
'shift left' : ('fast_pan', (-1, 0)), |
| 109 |
'shift right' : ('fast_pan', (1, 0)), |
| 110 |
'shift up' : ('fast_pan', (0, -1)), |
| 111 |
'shift down' : ('fast_pan', (0, 1)), |
| 112 |
'help': 'help', |
| 113 |
'space': 'hit', |
| 114 |
} |
| 115 |
|
| 116 |
HELP_MSG = """ |
| 117 |
Key bindings: |
| 118 |
|
| 119 |
+, = or . Zoom in |
| 120 |
- Zoom out |
| 121 |
1 Zoom to fit |
| 122 |
0 Actual size |
| 123 |
|
| 124 |
Arrows Scroll |
| 125 |
Shift+Arrows Scroll faster |
| 126 |
|
| 127 |
Space Follow word link |
| 128 |
|
| 129 |
Backspace Go back in history |
| 130 |
Meta Left Go back in history |
| 131 |
Meta Right Go forward in history |
| 132 |
R Reload the page |
| 133 |
|
| 134 |
F or / Search for text |
| 135 |
N Find next occurrence |
| 136 |
P Find previous occurrence |
| 137 |
|
| 138 |
F1, H or ? This help message |
| 139 |
|
| 140 |
Q or Esc Quit |
| 141 |
|
| 142 |
Mouse bindings: |
| 143 |
|
| 144 |
Click on objects to move around |
| 145 |
Drag with the left mouse button to zoom in/out |
| 146 |
Drag with the right mouse button to scroll |
| 147 |
Use scroll wheel do scroll up or down |
| 148 |
""".replace('\n ', '\n').strip() |
| 149 |
|
| 150 |
|
| 151 |
def __init__(self, layout): |
| 152 |
super(GraphDisplay, self).__init__() |
| 153 |
if self.highdpi: |
| 154 |
fontsize = 32 |
| 155 |
else: |
| 156 |
fontsize = 16 |
| 157 |
self.font = pygame.font.Font(self.STATUSBARFONT, fontsize) |
| 158 |
self.viewers_history = [] |
| 159 |
self.forward_viewers_history = [] |
| 160 |
self.highlight_word = None |
| 161 |
self.highlight_obj = None |
| 162 |
self.viewer = None |
| 163 |
self.method_cache = {} |
| 164 |
self.key_cache = {} |
| 165 |
self.ascii_key_cache = {} |
| 166 |
self.status_bar_height = 0 |
| 167 |
self.searchstr = None |
| 168 |
self.searchpos = 0 |
| 169 |
self.searchresults = [] |
| 170 |
self.initialize_keys() |
| 171 |
self.setlayout(layout) |
| 172 |
|
| 173 |
def initialize_keys(self): |
| 174 |
pygame.key.set_repeat(*self.KEY_REPEAT) |
| 175 |
|
| 176 |
mask = 0 |
| 177 |
|
| 178 |
for strnames, methodname in self.KEYS.items(): |
| 179 |
names = strnames.split() |
| 180 |
if not isinstance(methodname, (bytes, unicode)): |
| 181 |
methodname, args = methodname[0], methodname[1:] |
| 182 |
else: |
| 183 |
args = () |
| 184 |
method = getattr(self, methodname, None) |
| 185 |
if method is None: |
| 186 |
continue |
| 187 |
|
| 188 |
mods = [] |
| 189 |
basemod = 0 |
| 190 |
keys = [] |
| 191 |
for name in names: |
| 192 |
if name in METAKEYS: |
| 193 |
val = METAKEYS[name] |
| 194 |
if not isinstance(val, int): |
| 195 |
mods.append(tuple([METAKEYS[k] for k in val])) |
| 196 |
else: |
| 197 |
basemod |= val |
| 198 |
else: |
| 199 |
val = GET_KEY(name) |
| 200 |
assert len(keys) == 0 |
| 201 |
if not isinstance(val, (int, bytes, unicode)): |
| 202 |
keys.extend([GET_KEY(k) for k in val]) |
| 203 |
else: |
| 204 |
keys.append(val) |
| 205 |
assert keys |
| 206 |
for key in keys: |
| 207 |
if isinstance(key, int): |
| 208 |
for mod in permute_mods(basemod, mods): |
| 209 |
self.key_cache[(key, mod)] = (method, args) |
| 210 |
mask |= mod |
| 211 |
else: |
| 212 |
for mod in permute_mods(basemod, mods): |
| 213 |
char = key.lower() |
| 214 |
mod = mod & ~KMOD_SHIFT |
| 215 |
self.ascii_key_cache[(char, mod)] = (method, args) |
| 216 |
mask |= mod |
| 217 |
|
| 218 |
self.key_mask = mask |
| 219 |
|
| 220 |
def help(self): |
| 221 |
"""Show a help window and wait for a key or a mouse press.""" |
| 222 |
margin_x = margin_y = 64 |
| 223 |
padding_x = padding_y = 8 |
| 224 |
fgcolor = self.HELP_FGCOLOR |
| 225 |
bgcolor = self.HELP_BGCOLOR |
| 226 |
helpmsg = self.HELP_MSG |
| 227 |
width = self.width - 2*margin_x |
| 228 |
height = self.height - 2*margin_y |
| 229 |
lines = rendertext(helpmsg, self.font, fgcolor, width - 2*padding_x, |
| 230 |
height - 2*padding_y) |
| 231 |
block = pygame.Surface((width, height), SWSURFACE | SRCALPHA) |
| 232 |
block.fill(bgcolor) |
| 233 |
sx = padding_x |
| 234 |
sy = padding_y |
| 235 |
for img in lines: |
| 236 |
w, h = img.get_size() |
| 237 |
block.blit(img, (sx, sy)) |
| 238 |
sy += h |
| 239 |
block.set_alpha(int(255 * self.HELP_ALPHA)) |
| 240 |
self.screen.blit(block, (margin_x, margin_y)) |
| 241 |
|
| 242 |
pygame.display.flip() |
| 243 |
while True: |
| 244 |
wait_for_events() |
| 245 |
e = EventQueue.pop(0) |
| 246 |
if e.type in (MOUSEBUTTONDOWN, KEYDOWN, QUIT): |
| 247 |
break |
| 248 |
if e.type == QUIT: |
| 249 |
EventQueue.insert(0, e) |
| 250 |
self.must_redraw = True |
| 251 |
|
| 252 |
def input(self, prompt): |
| 253 |
"""Ask the user to input something. |
| 254 |
|
| 255 |
Returns the string that the user entered, or None if the user pressed |
| 256 |
Esc. |
| 257 |
""" |
| 258 |
|
| 259 |
def draw(text): |
| 260 |
margin_x = margin_y = 0 |
| 261 |
padding_x = padding_y = 8 |
| 262 |
fgcolor = self.INPUT_FGCOLOR |
| 263 |
bgcolor = self.INPUT_BGCOLOR |
| 264 |
width = self.width - 2*margin_x |
| 265 |
lines = renderline(text, self.font, fgcolor, width - 2*padding_x) |
| 266 |
height = totalheight(lines) + 2 * padding_y |
| 267 |
block = pygame.Surface((width, height), SWSURFACE | SRCALPHA) |
| 268 |
block.fill(bgcolor) |
| 269 |
sx = padding_x |
| 270 |
sy = padding_y |
| 271 |
for img in lines: |
| 272 |
w, h = img.get_size() |
| 273 |
block.blit(img, (sx, sy)) |
| 274 |
sy += h |
| 275 |
block.set_alpha(int(255 * self.INPUT_ALPHA)) |
| 276 |
|
| 277 |
|
| 278 |
self.viewer.render() |
| 279 |
if self.statusbarinfo: |
| 280 |
self.drawstatusbar() |
| 281 |
self.screen.blit(block, (margin_x, margin_y)) |
| 282 |
pygame.display.flip() |
| 283 |
|
| 284 |
draw(prompt) |
| 285 |
text = "" |
| 286 |
self.must_redraw = True |
| 287 |
while True: |
| 288 |
wait_for_events() |
| 289 |
old_text = text |
| 290 |
events = EventQueue[:] |
| 291 |
del EventQueue[:] |
| 292 |
for e in events: |
| 293 |
if e.type == QUIT: |
| 294 |
EventQueue.insert(0, e) |
| 295 |
return None |
| 296 |
elif e.type == KEYDOWN: |
| 297 |
if e.key == K_ESCAPE: |
| 298 |
return None |
| 299 |
elif e.key == K_RETURN: |
| 300 |
return forceunicode(text) |
| 301 |
elif e.key == K_BACKSPACE: |
| 302 |
text = text[:-1] |
| 303 |
elif e.unicode and ord(e.unicode) >= ord(' '): |
| 304 |
text += e.unicode |
| 305 |
if text != old_text: |
| 306 |
draw(prompt + text) |
| 307 |
|
| 308 |
def hit(self): |
| 309 |
word = self.highlight_word |
| 310 |
if word is not None: |
| 311 |
if word in self.layout.links: |
| 312 |
self.setstatusbar('loading...') |
| 313 |
self.redraw_now() |
| 314 |
self.layout.request_followlink(word) |
| 315 |
|
| 316 |
|
| 317 |
def search(self): |
| 318 |
searchstr = self.input('Find: ') |
| 319 |
if not searchstr: |
| 320 |
return |
| 321 |
self.searchstr = searchstr |
| 322 |
self.viewer.highlightwords[searchstr] = searchstr |
| 323 |
self.searchpos = -1 |
| 324 |
self.searchresults = list(self.viewer.findall(self.searchstr)) |
| 325 |
self.find_next() |
| 326 |
|
| 327 |
def find_next(self): |
| 328 |
if not self.searchstr: |
| 329 |
return |
| 330 |
if self.searchpos + 1 >= len(self.searchresults): |
| 331 |
self.setstatusbar('Not found: %s' % self.searchstr) |
| 332 |
return |
| 333 |
self.searchpos += 1 |
| 334 |
self.highlight_found_item() |
| 335 |
|
| 336 |
def find_prev(self): |
| 337 |
if not self.searchstr: |
| 338 |
return |
| 339 |
if self.searchpos - 1 < 0: |
| 340 |
self.setstatusbar('Not found: %s' % self.searchstr) |
| 341 |
return |
| 342 |
self.searchpos -= 1 |
| 343 |
self.highlight_found_item() |
| 344 |
|
| 345 |
def highlight_found_item(self): |
| 346 |
item = self.searchresults[self.searchpos] |
| 347 |
self.sethighlight(obj=item) |
| 348 |
msg = 'Found %%s containing %s (%d/%d)' % ( |
| 349 |
self.searchstr.replace('%', '%%'), |
| 350 |
self.searchpos+1, len(self.searchresults)) |
| 351 |
if isinstance(item, Node): |
| 352 |
self.setstatusbar(msg % 'node') |
| 353 |
self.look_at_node(item, keep_highlight=True) |
| 354 |
elif isinstance(item, Edge): |
| 355 |
self.setstatusbar(msg % 'edge') |
| 356 |
self.look_at_edge(item, keep_highlight=True) |
| 357 |
else: |
| 358 |
|
| 359 |
self.setstatusbar(msg % item) |
| 360 |
|
| 361 |
def setlayout(self, layout): |
| 362 |
if self.viewer and getattr(self.viewer.graphlayout, 'key', True) is not None: |
| 363 |
self.viewers_history.append(self.viewer) |
| 364 |
del self.forward_viewers_history[:] |
| 365 |
self.layout = layout |
| 366 |
self.viewer = GraphRenderer(self.screen, layout, highdpi=self.highdpi) |
| 367 |
self.searchpos = 0 |
| 368 |
self.searchresults = [] |
| 369 |
self.zoom_to_fit() |
| 370 |
|
| 371 |
def zoom_actual_size(self): |
| 372 |
self.viewer.shiftscale(float(self.viewer.SCALEMAX) / self.viewer.scale) |
| 373 |
self.updated_viewer() |
| 374 |
|
| 375 |
def calculate_zoom_to_fit(self): |
| 376 |
return min(float(self.width) / self.viewer.width, |
| 377 |
float(self.height) / self.viewer.height, |
| 378 |
float(self.viewer.SCALEMAX) / self.viewer.scale) |
| 379 |
|
| 380 |
def zoom_to_fit(self): |
| 381 |
""" |
| 382 |
center and scale to view the whole graph |
| 383 |
""" |
| 384 |
|
| 385 |
f = self.calculate_zoom_to_fit() |
| 386 |
self.viewer.shiftscale(f) |
| 387 |
self.updated_viewer() |
| 388 |
|
| 389 |
def zoom(self, scale): |
| 390 |
self.viewer.shiftscale(max(scale, self.calculate_zoom_to_fit())) |
| 391 |
self.updated_viewer() |
| 392 |
|
| 393 |
def reoffset(self): |
| 394 |
self.viewer.reoffset(self.width, self.height) |
| 395 |
|
| 396 |
def pan(self, p): |
| 397 |
x, y = p |
| 398 |
self.viewer.shiftoffset(x * (self.width // 8), y * (self.height // 8)) |
| 399 |
self.updated_viewer() |
| 400 |
|
| 401 |
def fast_pan(self, p): |
| 402 |
x, y = p |
| 403 |
self.pan((x * 4, y * 4)) |
| 404 |
|
| 405 |
def update_status_bar(self): |
| 406 |
self.statusbarinfo = None |
| 407 |
self.must_redraw = True |
| 408 |
if self.viewers_history: |
| 409 |
info = 'Press Backspace to go back to previous screen' |
| 410 |
else: |
| 411 |
info = 'Press H for help' |
| 412 |
self.setstatusbar(info) |
| 413 |
|
| 414 |
def updated_viewer(self, keep_highlight=False): |
| 415 |
self.reoffset() |
| 416 |
if not keep_highlight: |
| 417 |
self.sethighlight() |
| 418 |
self.update_status_bar() |
| 419 |
self.must_redraw = True |
| 420 |
|
| 421 |
def layout_back(self): |
| 422 |
if self.viewers_history: |
| 423 |
self.forward_viewers_history.append(self.viewer) |
| 424 |
self.viewer = self.viewers_history.pop() |
| 425 |
self.layout = self.viewer.graphlayout |
| 426 |
self.updated_viewer() |
| 427 |
|
| 428 |
def layout_forward(self): |
| 429 |
if self.forward_viewers_history: |
| 430 |
self.viewers_history.append(self.viewer) |
| 431 |
self.viewer = self.forward_viewers_history.pop() |
| 432 |
self.layout = self.viewer.graphlayout |
| 433 |
self.updated_viewer() |
| 434 |
|
| 435 |
def reload(self): |
| 436 |
self.setstatusbar('reloading...') |
| 437 |
self.redraw_now() |
| 438 |
self.layout.request_reload() |
| 439 |
|
| 440 |
def setstatusbar(self, text, fgcolor=None, bgcolor=None): |
| 441 |
info = (forceunicode(text), fgcolor or self.STATUSBAR_FGCOLOR, bgcolor or self.STATUSBAR_BGCOLOR) |
| 442 |
if info != self.statusbarinfo: |
| 443 |
self.statusbarinfo = info |
| 444 |
self.must_redraw = True |
| 445 |
|
| 446 |
def drawstatusbar(self): |
| 447 |
text, fgcolor, bgcolor = self.statusbarinfo |
| 448 |
maxheight = self.height / 2 |
| 449 |
lines = rendertext(text, self.font, fgcolor, self.width, maxheight, |
| 450 |
self.STATUSBAR_OVERFLOWCOLOR) |
| 451 |
totalh = totalheight(lines) |
| 452 |
y = self.height - totalh |
| 453 |
self.status_bar_height = totalh + 16 |
| 454 |
block = pygame.Surface((self.width, self.status_bar_height), SWSURFACE | SRCALPHA) |
| 455 |
block.fill(bgcolor) |
| 456 |
sy = 16 |
| 457 |
for img in lines: |
| 458 |
w, h = img.get_size() |
| 459 |
block.blit(img, ((self.width-w)//2, sy-8)) |
| 460 |
sy += h |
| 461 |
block.set_alpha(int(255 * self.STATUSBAR_ALPHA)) |
| 462 |
self.screen.blit(block, (0, y-16)) |
| 463 |
|
| 464 |
def notifymousepos(self, pos): |
| 465 |
word = self.viewer.at_position(pos) |
| 466 |
if word in self.layout.links: |
| 467 |
info = self.layout.links[word] |
| 468 |
if isinstance(info, tuple): |
| 469 |
info = info[0] |
| 470 |
self.setstatusbar(info) |
| 471 |
self.sethighlight(word) |
| 472 |
return |
| 473 |
node = self.viewer.node_at_position(pos) |
| 474 |
if node: |
| 475 |
self.setstatusbar(shortlabel(node.label)) |
| 476 |
self.sethighlight(obj=node) |
| 477 |
return |
| 478 |
edge = self.viewer.edge_at_position(pos) |
| 479 |
if edge: |
| 480 |
info = '%s -> %s' % (shortlabel(edge.tail.label), |
| 481 |
shortlabel(edge.head.label)) |
| 482 |
if edge.label: |
| 483 |
info += '\n' + shortlabel(edge.label) |
| 484 |
self.setstatusbar(info) |
| 485 |
self.sethighlight(obj=edge) |
| 486 |
return |
| 487 |
self.sethighlight() |
| 488 |
|
| 489 |
def notifyclick(self, pos): |
| 490 |
word = self.viewer.at_position(pos) |
| 491 |
if word in self.layout.links: |
| 492 |
self.setstatusbar('loading...') |
| 493 |
self.redraw_now() |
| 494 |
self.layout.request_followlink(word) |
| 495 |
return |
| 496 |
node = self.viewer.node_at_position(pos) |
| 497 |
if node: |
| 498 |
self.look_at_node(node) |
| 499 |
else: |
| 500 |
edge = self.viewer.edge_at_position(pos) |
| 501 |
if edge: |
| 502 |
if (self.distance_to_node(edge.head) >= |
| 503 |
self.distance_to_node(edge.tail)): |
| 504 |
self.look_at_node(edge.head) |
| 505 |
else: |
| 506 |
self.look_at_node(edge.tail) |
| 507 |
|
| 508 |
def sethighlight(self, word=None, obj=None): |
| 509 |
if word == self.highlight_word and obj is self.highlight_obj: |
| 510 |
return |
| 511 |
|
| 512 |
self.viewer.highlight_word = word |
| 513 |
if self.highlight_obj is not None: |
| 514 |
self.highlight_obj.sethighlight(False) |
| 515 |
if obj is not None: |
| 516 |
obj.sethighlight(True) |
| 517 |
self.highlight_word = word |
| 518 |
self.highlight_obj = obj |
| 519 |
self.must_redraw = True |
| 520 |
|
| 521 |
def animation(self, expectedtime=0.6): |
| 522 |
start = time.time() |
| 523 |
step = 0.0 |
| 524 |
n = 0 |
| 525 |
while True: |
| 526 |
step += self.ANIM_STEP |
| 527 |
if step >= expectedtime: |
| 528 |
break |
| 529 |
yield step / expectedtime |
| 530 |
n += 1 |
| 531 |
now = time.time() |
| 532 |
frametime = (now-start) / n |
| 533 |
self.ANIM_STEP = self.ANIM_STEP * 0.9 + frametime * 0.1 |
| 534 |
yield 1.0 |
| 535 |
|
| 536 |
def distance_to_node(self, node): |
| 537 |
cx1, cy1 = self.viewer.getcenter() |
| 538 |
cx2, cy2 = node.x, node.y |
| 539 |
return (cx2-cx1)*(cx2-cx1) + (cy2-cy1)*(cy2-cy1) |
| 540 |
|
| 541 |
def look_at_node(self, node, keep_highlight=False): |
| 542 |
"""Shift the node in view.""" |
| 543 |
self.look_at(node.x, node.y, node.w, node.h, keep_highlight) |
| 544 |
|
| 545 |
def look_at_edge(self, edge, keep_highlight=False): |
| 546 |
"""Shift the edge's label into view.""" |
| 547 |
points = edge.bezierpoints() |
| 548 |
xmin = min([x for (x, y) in points]) |
| 549 |
xmax = max([x for (x, y) in points]) |
| 550 |
ymin = min([y for (x, y) in points]) |
| 551 |
ymax = max([y for (x, y) in points]) |
| 552 |
x = (xmin + xmax) / 2 |
| 553 |
y = (ymin + ymax) / 2 |
| 554 |
w = max(1, xmax - xmin) |
| 555 |
h = max(1, ymax - ymin) |
| 556 |
self.look_at(x, y, w, h, keep_highlight) |
| 557 |
|
| 558 |
def look_at(self, targetx, targety, targetw, targeth, |
| 559 |
keep_highlight=False): |
| 560 |
"""Shift the node in view.""" |
| 561 |
endscale = min(float(self.width-40) / targetw, |
| 562 |
float(self.height-40) / targeth, |
| 563 |
75) |
| 564 |
startscale = self.viewer.scale |
| 565 |
cx1, cy1 = self.viewer.getcenter() |
| 566 |
cx2, cy2 = targetx, targety |
| 567 |
moving = (abs(startscale-endscale) + abs(cx1-cx2) + abs(cy1-cy2) |
| 568 |
> 0.4) |
| 569 |
if moving: |
| 570 |
|
| 571 |
tx, ty = self.viewer.map(cx2, cy2) |
| 572 |
offview = max(-tx, -ty, tx-self.width, ty-self.height) |
| 573 |
middlescale = endscale * (0.999 ** offview) |
| 574 |
if offview > 150 and middlescale < startscale: |
| 575 |
bumpscale = 4.0 * (middlescale - 0.5*(startscale+endscale)) |
| 576 |
else: |
| 577 |
bumpscale = 0.0 |
| 578 |
if not keep_highlight: |
| 579 |
self.statusbarinfo = None |
| 580 |
self.sethighlight() |
| 581 |
for t in self.animation(): |
| 582 |
self.viewer.setscale(startscale*(1-t) + endscale*t + |
| 583 |
bumpscale*t*(1-t)) |
| 584 |
self.viewer.setcenter(cx1*(1-t) + cx2*t, cy1*(1-t) + cy2*t) |
| 585 |
self.updated_viewer(keep_highlight=keep_highlight) |
| 586 |
self.redraw_now() |
| 587 |
return moving |
| 588 |
|
| 589 |
def peek(self, typ): |
| 590 |
for event in EventQueue: |
| 591 |
if event.type == typ: |
| 592 |
return True |
| 593 |
return False |
| 594 |
|
| 595 |
def process_event(self, event): |
| 596 |
method = self.method_cache.get(event.type, KeyError) |
| 597 |
if method is KeyError: |
| 598 |
method = getattr(self, 'process_%s' % (pygame.event.event_name(event.type),), None) |
| 599 |
self.method_cache[event.type] = method |
| 600 |
if method is not None: |
| 601 |
method(event) |
| 602 |
|
| 603 |
def process_MouseMotion(self, event): |
| 604 |
if self.peek(MOUSEMOTION): |
| 605 |
return |
| 606 |
if self.dragging: |
| 607 |
if (abs(event.pos[0] - self.click_origin[0]) + |
| 608 |
abs(event.pos[1] - self.click_origin[1])) > 12: |
| 609 |
self.click_time = None |
| 610 |
dx = event.pos[0] - self.dragging[0] |
| 611 |
dy = event.pos[1] - self.dragging[1] |
| 612 |
if event.buttons[0]: |
| 613 |
self.zoom(1.003 ** (dx+dy)) |
| 614 |
else: |
| 615 |
self.viewer.shiftoffset(-2*dx, -2*dy) |
| 616 |
self.updated_viewer() |
| 617 |
self.dragging = event.pos |
| 618 |
self.must_redraw = True |
| 619 |
else: |
| 620 |
self.notifymousepos(event.pos) |
| 621 |
|
| 622 |
def process_MouseButtonDown(self, event): |
| 623 |
self.dragging = self.click_origin = event.pos |
| 624 |
self.click_time = time.time() |
| 625 |
|
| 626 |
|
| 627 |
def process_MouseButtonUp(self, event): |
| 628 |
self.dragging = None |
| 629 |
pygame.event.set_grab(False) |
| 630 |
|
| 631 |
if event.button == 4: |
| 632 |
self.pan((0, -1)) |
| 633 |
return |
| 634 |
if event.button == 5: |
| 635 |
self.pan((0, 1)) |
| 636 |
return |
| 637 |
if event.button == 6: |
| 638 |
self.pan((-1, 0)) |
| 639 |
return |
| 640 |
if event.button == 7: |
| 641 |
self.pan((1, 0)) |
| 642 |
return |
| 643 |
if self.click_time is not None and abs(time.time() - self.click_time) < 1: |
| 644 |
|
| 645 |
self.notifyclick(self.click_origin) |
| 646 |
self.click_time = None |
| 647 |
else: |
| 648 |
self.update_status_bar() |
| 649 |
self.click_time = None |
| 650 |
self.notifymousepos(event.pos) |
| 651 |
|
| 652 |
def process_KeyDown(self, event): |
| 653 |
mod = event.mod & self.key_mask |
| 654 |
method, args = self.key_cache.get((event.key, mod), (None, None)) |
| 655 |
if method is None and event.unicode: |
| 656 |
char = event.unicode.lower() |
| 657 |
mod = mod & ~ KMOD_SHIFT |
| 658 |
method, args = self.ascii_key_cache.get((char, mod), (None, None)) |
| 659 |
if method is not None: |
| 660 |
method(*args) |
| 661 |
|
| 662 |
def process_VideoResize(self, event): |
| 663 |
|
| 664 |
if self.peek(VIDEORESIZE): |
| 665 |
return |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
if event.size[1] <= 32: |
| 673 |
return |
| 674 |
self.resize(event.size) |
| 675 |
self.must_redraw = True |
| 676 |
|
| 677 |
def process_Quit(self, event): |
| 678 |
self.quit() |
| 679 |
|
| 680 |
def process_UserEvent(self, event): |
| 681 |
if hasattr(event, 'layout'): |
| 682 |
if event.layout is None: |
| 683 |
self.setstatusbar('cannot follow this link') |
| 684 |
else: |
| 685 |
self.setlayout(event.layout) |
| 686 |
elif hasattr(event, 'say'): |
| 687 |
self.setstatusbar(event.say) |
| 688 |
|
| 689 |
def quit(self): |
| 690 |
raise StopIteration |
| 691 |
|
| 692 |
def redraw_now(self): |
| 693 |
self.viewer.render() |
| 694 |
if self.statusbarinfo: |
| 695 |
self.drawstatusbar() |
| 696 |
else: |
| 697 |
self.status_bar_height = 0 |
| 698 |
pygame.display.flip() |
| 699 |
self.must_redraw = False |
| 700 |
|
| 701 |
def run1(self): |
| 702 |
self.dragging = self.click_origin = self.click_time = None |
| 703 |
try: |
| 704 |
|
| 705 |
while True: |
| 706 |
|
| 707 |
if self.must_redraw and not EventQueue: |
| 708 |
self.redraw_now() |
| 709 |
|
| 710 |
if not EventQueue: |
| 711 |
wait_for_events() |
| 712 |
|
| 713 |
self.process_event(EventQueue.pop(0)) |
| 714 |
|
| 715 |
except StopIteration: |
| 716 |
pass |
| 717 |
|
| 718 |
def run(self): |
| 719 |
self.run1() |
| 720 |
|
| 721 |
|
| 722 |
pygame.display.set_mode((self.width,1)) |
| 723 |
|
| 724 |
|
| 725 |
def shortlabel(label): |
| 726 |
"""Shorten a graph node label.""" |
| 727 |
return label and label.replace('\\l', '\n').splitlines()[0] |
| 728 |
|
| 729 |
|
| 730 |
def renderline(text, font, fgcolor, width, maxheight=sys.maxsize, |
| 731 |
overflowcolor=None): |
| 732 |
"""Render a single line of text into a list of images. |
| 733 |
|
| 734 |
Performs word wrapping. |
| 735 |
""" |
| 736 |
if overflowcolor is None: |
| 737 |
overflowcolor = fgcolor |
| 738 |
words = text.split(' ') |
| 739 |
lines = [] |
| 740 |
while words: |
| 741 |
line = words.pop(0) |
| 742 |
img = font.render(line or ' ', True, fgcolor) |
| 743 |
while words: |
| 744 |
longerline = line + ' ' + words[0] |
| 745 |
longerimg = font.render(longerline, 1, fgcolor) |
| 746 |
w, h = longerimg.get_size() |
| 747 |
if w > width: |
| 748 |
break |
| 749 |
words.pop(0) |
| 750 |
line = longerline |
| 751 |
img = longerimg |
| 752 |
w, h = img.get_size() |
| 753 |
if h > maxheight: |
| 754 |
img = font.render('...', True, overflowcolor) |
| 755 |
w, h = img.get_size() |
| 756 |
while lines and h > maxheight: |
| 757 |
maxheight += lines.pop().get_size()[1] |
| 758 |
lines.append(img) |
| 759 |
break |
| 760 |
maxheight -= h |
| 761 |
lines.append(img) |
| 762 |
return lines |
| 763 |
|
| 764 |
|
| 765 |
def rendertext(text, font, fgcolor, width, maxheight=sys.maxsize, |
| 766 |
overflowcolor=None): |
| 767 |
"""Render a multiline string into a list of images. |
| 768 |
|
| 769 |
Performs word wrapping for each line individually.""" |
| 770 |
lines = [] |
| 771 |
for line in text.splitlines(): |
| 772 |
l = renderline(line, font, fgcolor, width, maxheight, overflowcolor) |
| 773 |
lines.extend(l) |
| 774 |
maxheight -= totalheight(l) |
| 775 |
if maxheight <= 0: |
| 776 |
break |
| 777 |
return lines |
| 778 |
|
| 779 |
|
| 780 |
def totalheight(lines): |
| 781 |
"""Calculate the total height of a list of images.""" |
| 782 |
totalh = 0 |
| 783 |
for img in lines: |
| 784 |
w, h = img.get_size() |
| 785 |
totalh += h |
| 786 |
return totalh |