1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
|
# Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 of the
# License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
#import cairo
import cairo_utils
def draw_varrow(cr, tip_pos, ah = 4, aw = 4):
cr.new_path()
x, y = tip_pos
cr.move_to(x, y)
cr.line_to(x - aw/2, y + ah - 0.5)
cr.line_to(x + aw/2, y + ah - 0.5)
cr.close_path()
def draw_harrow(cr, tip_pos, ah = 4, aw = 4):
cr.new_path()
x, y = tip_pos
cr.move_to(x, y)
cr.line_to(x - ah - 0.5, y - aw/2)
cr.line_to(x - ah - 0.5, y + aw/2)
cr.close_path()
#
class Settings:
default_font = "Helvetica"
settings = Settings()
class Canvas(object):
def __init__(self, set_needs_repaint_cb):
self._size = (0, 0)
self.tooltip = None
self.set_needs_repaint = set_needs_repaint_cb
self._figures = []
self._mouse_down_figures = [None] * 5
self._hover_figure = None
self._background_color = (1,1,1)
def deactivate(self):
if self.tooltip:
self.tooltip.close()
self.tooltip = None
def activate(self):
pass
def set_background_color(self, r, g, b):
self._background_color = r, g, b
def add(self, obj):
obj.set_canvas(self)
self._figures.append(obj)
obj.invalidate()
def remove(self, obj):
self.invalidate(*obj.bounds)
self.set_needs_repaint()
obj.set_canvas(None)
self._figures.remove(obj)
def repaint(self, cr, x, y, w, h):
cr.set_source_rgb(*self._background_color)
cr.rectangle(0, 0, w, h)
cr.fill()
cr.translate(x, y)
for f in self._figures:
f.relayout(cr)
f.render(cr)
def invalidate(self, x, y, w, h):
self.set_needs_repaint(x, y, w, h)
def figure_at(self, x, y):
for f in self._figures:
if f.contains_point(x, y):
return f
return None
# event handling
def mouse_down(self, button, x, y):
fig = self.figure_at(x, y)
if fig:
self._mouse_down_figures[button] = fig
fig.mouse_down(button, x, y)
return True
return False
def mouse_up(self, button, x, y):
if self._mouse_down_figures[button]:
self._mouse_down_figures[button].mouse_up(button, x, y)
self._mouse_down_figures[button] = None
def mouse_leave(self):
#if self.tooltip:
# self.tooltip.close()
# self.tooltip = None
pass
def mouse_move(self, x, y):
dragged = False
for b, f in enumerate(self._mouse_down_figures):
if f:
dragged = True
f.mouse_drag(b, x, y)
break
if not dragged:
fig = self.figure_at(x, y)
if fig != self._hover_figure:
if self._hover_figure:
self._hover_figure.hover_out(x, y)
if fig:
fig.hover_in(x, y)
self._hover_figure = fig
else:
if self._hover_figure:
self._hover_figure.hover(x, y)
if self._hover_figure:
self._hover_figure.mouse_move(x, y)
# Base Classes
class Element(object):
def __init__(self):
self.canvas = None
self.parent = None
self._layout_dirty = False
self._color = (0, 0, 0, 1)
self.on_hover_in = None
self.on_hover_out = None
def set_canvas(self, canvas):
self.canvas = canvas
def invalidate(self, repaint_only = False):
if not repaint_only:
self._layout_dirty = True
if self.canvas:
self.canvas.invalidate(*self.bounds)
def relayout(self, cr):
if self._layout_dirty:
self.do_relayout(cr)
self._layout_dirty = False
def do_relayout(self, cr):
pass
def render(self, cr):
pass
def set_color(self, r, g, b, a = 1.0):
self._color = (r, g, b, a)
# event handling
def mouse_down(self, button, x, y):
pass
def mouse_up(self, button, x, y):
pass
def mouse_drag(self, button, x, y):
pass
def mouse_move(self, x, y):
pass
def hover_in(self, x, y):
if self.on_hover_in:
self.on_hover_in(self, x, y)
def hover_out(self, x, y):
if self.on_hover_out:
self.on_hover_out(self, x, y)
def hover(self, x, y):
pass
HFill = 1 << 0
VFill = 1 << 1
class Figure(Element):
def __init__(self):
Element.__init__(self)
self._x = 0
self._y = 0
self._width = 0
self._height = 0
self._uwidth = None
self._uheight = None
self._fill_color = (1, 1, 1, 1)
self._line_width = 1
self._dash = (None, None)
self._padding = (0, 0, 0, 0)
self._layout_flags = HFill | VFill
@property
def x(self):
return self._x
@property
def y(self):
return self._y
@property
def root_x(self):
return self.x + (self.parent.root_x if self.parent else 0)
@property
def root_y(self):
return self.y + (self.parent.root_y if self.parent else 0)
@property
def width(self):
return self._width if self._uwidth is None else self._uwidth
@property
def height(self):
return self._height if self._uheight is None else self._uheight
@property
def pos(self):
return (self._x, self._y)
@property
def size(self):
return (self.width, self.height)
@property
def bounds(self):
return 0, 0, self.width, self.height
@property
def frame(self):
return self.x, self.y, self.width, self.height
@property
def padding_top(self):
return self._padding[0]
@property
def padding_left(self):
return self._padding[1]
@property
def padding_bottom(self):
return self._padding[2]
@property
def padding_right(self):
return self._padding[3]
def move(self, x, y):
if self.canvas:
self.canvas.invalidate(*self.frame)
self._x = x
self._y = y
if self.canvas:
self.canvas.invalidate(*self.frame)
def contains_point(self, x, y):
pos = self.pos
size = self.size
return x >= pos[0] and x < pos[0] + size[0] and y >= pos[1] and y < pos[1] + size[1]
def center(self):
x, y = self.pos
w, h = self.size
return int(x + w/2), int(y + h/2)
def top_vertex(self):
x, y = self.pos
w, h = self.size
return (x, y), (x+w, y)
def bottom_vertex(self):
x, y = self.pos
w, h = self.size
return (x, y+h), (x+w, y+h)
def right_vertex(self):
x, y = self.pos
w, h = self.size
return (x+w, y), (x+w, y+h)
def left_vertex(self):
x, y = self.pos
w, h = self.size
return (x, y), (x, y+h)
def set_padding(self, t, l, b, r):
self._padding = t, l, b, r
def set_layout_flags(self, flags):
self._layout_flags = flags
def set_line_dash(self, dash, offset):
self._dash = (dash, offset)
self.invalidate()
def set_line_width(self, l):
self._line_width = l
self.invalidate()
def set_fill_color(self, r, g, b, a = 1.0):
self._fill_color = (r, g, b, a)
self.invalidate()
def apply_attributes(self, c):
c.set_source_rgba(*self._color)
if self._dash[0]:
c.set_dash(self._dash[0], self._dash[1])
c.set_line_width(self._line_width)
def apply_fill_attributes(self, c):
c.set_source_rgba(*self._fill_color)
def set_usize(self, w, h):
self._uwidth = w
self._uheight = h
class Line(object):
def __init__(self):
Element.__init__(self)
self._end1 = None
self._end2 = None
@property
def bounds(self):
return 0, 0, 0, 0
def contains_point(self, x, y):
return None
#
#
class Container(Figure):
def __init__(self):
Figure.__init__(self)
self._items = []
def add(self, item):
item.parent = self
self._items.append(item)
def remove(self, item):
item.parent = None
self._items.remove(item)
def render(self, cr):
cr.save()
cr.translate(self.x, self.y)
for i in self._items:
i.render(cr)
cr.restore()
class VBoxFigure(Container):
def __init__(self):
Container.__init__(self)
self._spacing = 0
def set_spacing(self, sp):
self._spacing = sp
def do_relayout(self, cr):
lp, tp, rp, bp = self._padding
y = tp
x = lp
max_width = self._width
for i in self._items:
i.do_relayout(cr)
i.move(x, y)
max_width = max(max_width, i.width)
y += int(i.height) + self._spacing
if self._items:
y -= self._spacing
for i in self._items:
if i._layout_flags & HFill:
i.set_usize(max_width, i._uheight)
i.do_relayout(cr)
else:
i.move((max_width - i.width) / 2, i.y)
self._width = max_width + lp + rp
self._height = y + bp
self.invalidate()
#
#
class TextFigure(Figure):
def __init__(self, text=""):
Figure.__init__(self)
self._text = text
self._font = settings.default_font
self._font_size = 12
self._text_color = (0, 0, 0, 1)
self._line_spacing = 2
self._bold = False
self._xalignment = 0.0
self._yalignment = 0.0
self._line_height = 14
self._text_height = 0
def set_text_color(self, r, g, b, a=1.0):
self._text_color = r, g, b, a
self.invalidate()
def set_font_size(self, s):
self._font_size = s
def set_font_bold(self, s):
self._bold = s
def set_alignment(self, x, y):
self._xalignment = x
self._yalignment = y
self.invalidate(False)
def set_text(self, text):
self._text = text
self.invalidate(False)
def do_relayout(self, ctx):
ctx.save()
ctx.set_font(self._font, False, self._bold)
ctx.set_font_size(self._font_size)
if "\n" in self._text:
lines = self._text.split("\n")
w, h = 0, 0
lh = 0
self._text_height = 0
for line in lines:
ext = ctx.text_extents(line)
w = max(w, int(ext.x_bearing + ext.x_advance))
lh = max(lh, int(ext.height + ext.height + ext.y_bearing))
self._text_height += ext.height + self._line_spacing
if lines:
self._text_height -= self._line_spacing
self._line_height = int(lh)
t, l, b, r = self._padding
self._width, self._height = (w + r+l, self._text_height + t+b)
else:
ext = ctx.text_extents(self._text)
self._extents = ext
t, l, b, r = self._padding
self._line_height = int(ext.height) + int(ext.height + ext.y_bearing)
self._width = int(ext.x_bearing + ext.x_advance) + r+l
self._height = self._line_height + t+b
self._text_height = self._line_height
ctx.restore()
def render(self, ctx):
ctx.save()
ctx.translate(self.x, self.y)
ctx.set_source_rgba(*self._text_color)
ctx.set_font(self._font, False, self._bold)
ctx.set_font_size(self._font_size)
t, l, b, r = self._padding
w, h = self.size
x = int(l) + 0.5
y = int(t) + 0.5 + int((self.height - t - b - self._text_height) * self._yalignment)
for line in self._text.split("\n"):
extents = ctx.text_extents(line)
ctx.move_to(x + int((self.width - l - r - extents.width) * self._xalignment),
y + int(extents.height - (extents.height + extents.y_bearing)))
ctx.show_text(line)
y += extents.height + self._line_spacing
ctx.stroke()
ctx.restore()
class ImageFigure(Figure):
def __init__(self, file):
Figure.__init__(self)
self.set_line_width(1)
# if this is a @2x image (for hidpi), we need to scale it by half when painting on screen
if "@2x" in file:
self._scale = 0.5
else:
self._scale = 1.0
self._image = cairo_utils.ImageSurface.from_png(file)
self._width = self._image.get_width() * self._scale
self._height = self._image.get_height() * self._scale
def set_image_file(self, path):
self._image = cairo_utils.ImageSurface.from_png(path)
self._width = self._image.get_width() * self._scale
self._height = self._image.get_height() * self._scale
def render(self, ctx):
ctx.save()
ctx.translate(self.x, self.y)
if self._scale != 1.0:
ctx.scale(self._scale, self._scale)
ctx.set_source_surface(self._image, 0, 0)
ctx.paint()
ctx.restore()
class ShapeFigure(TextFigure):
def __init__(self, caption):
TextFigure.__init__(self, caption)
self.set_alignment(0.5, 0.5)
self.set_line_width(1)
def render(self, ctx):
ctx.save()
ctx.translate(self.x, self.y)
self.make_path(ctx)
self.apply_fill_attributes(ctx)
ctx.fill_preserve()
self.apply_attributes(ctx)
ctx.stroke()
ctx.restore()
TextFigure.render(self, ctx)
def make_path(self, ctx):
ctx.rectangle(*self.bounds)
class DiamondShapeFigure(ShapeFigure):
def make_path(self, ctx):
ctx.move_to(0, self.height/2)
ctx.line_to(self.width/2, 0)
ctx.line_to(self.width, self.height/2)
ctx.line_to(self.width/2, self.height)
ctx.close_path()
class RectangleShapeFigure(ShapeFigure):
_corner_radius = 0
def make_path(self, ctx):
if self._corner_radius == 0:
ctx.rectangle(0.5, 0.5, self.width, self.height)
else:
ctx.rounded_rect(0.5, 0.5, self.width, self.height, self._corner_radius)
def set_corner_radius(self, r):
self._corner_radius = r
#
#
class HoverTip(Figure):
def __init__(self, text):
Figure.__init__(self)
self.text = text
self.font_size = 12
self.bold = False
self.line_spacing = 4
self.line_height = 10
self.padding = (4, 4, 4, 4)
def calc(self, ctx):
ctx.save()
ctx.set_font_size(self.font_size)
if self.bold:
ctx.set_font("Helvetica", False, self.bold)
if "\n" in self.text:
lines = self.text.split("\n")
w, h = 0, 0
lh = 0
for line in lines:
ext = ctx.text_extents(line)
w = max(w, int(ext.x_bearing + ext.x_advance))
lh = max(lh, int(ext.height + (ext.height + ext.y_advance + ext.y_bearing)))
self.line_height = lh
h = lh * len(lines) + self.line_spacing * (len(lines)-1)
t, l, b, r = self.padding
self._width, self._height = (w + r+l, h + t+b)
else:
ext = ctx.text_extents(self.text)
self._extents = ext
t, l, b, r = self.padding
self.line_height = int(ext.height + (ext.height + ext.y_advance + ext.y_bearing))
self._width = int(ext.x_bearing + ext.x_advance) + r+l
self._height = self.line_height + t+b
ctx.restore()
def render(self, ctx):
ctx.save()
ctx.rectangle(*self.bounds)
ctx.set_source_rgb(0.9, 0.9, 0.8)
ctx.stroke_preserve()
ctx.set_source_rgb(1, 1, 0.9)
ctx.fill()
ctx.set_source_rgb(0.2, 0.2, 0.2)
ctx.set_font_size(self.font_size)
if self.bold:
ctx.set_font("Helvetica", False, self.bold)
t, l, b, r = self.padding
x, y = self.pos
w, h = self.size
ctx.show_text_lines_at(int(x+l)+0.5, int(y+t)+0.5, self.text, self.line_spacing, self.line_height)
ctx.stroke()
ctx.restore()
def do_relayout(self, cr):
self.calc(cr)
|