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
|
#!/usr/bin/env python3
"""
Syncthing-GTK - InfoBox
Colorful, expandable widget displaying folder/device data
"""
import logging
import math
import os
from gi.repository import Gdk, GLib, GObject, Gtk, Pango, Rsvg
from syncthing_gtk.ribar import RevealerClass
from syncthing_gtk.tools import _, escape_html_entities # _ is gettext function
log = logging.getLogger("InfoBox")
COLOR_CHANGE_TIMER = 10 # ms
COLOR_CHANGE_STEP = 0.05
HILIGHT_INTENSITY = 0.3 # 0.0 to 1.0
DARKEN_FACTOR = 0.75 # 0.0 to 1.0
svg_cache = {}
class InfoBox(Gtk.Container):
""" Expandable widget displaying folder/device data """
__gtype_name__ = "InfoBox"
__gsignals__ = {
# right-click(button, time)
"right-click": (GObject.SIGNAL_RUN_FIRST, None, (int, int)),
# doubleclick, no arguments
"doubleclick": (GObject.SIGNAL_RUN_FIRST, None, ())
}
# Initialization
def __init__(self, app, title, icon):
# Variables
self.app = app
self.child = None
self.header = None
self.str_title = None
self.str_status = None
self.header_inverted = False
self.values = {}
self.icons = {}
self.value_widgets = {}
self.hilight = False
self.hilight_factor = 0.0
self.timer_enabled = False
self.icon = icon
self.color = (1, 0, 1, 1) # rgba
self.background = (1, 1, 1, 1) # rgba
self.dark_color = None # Overrides background if set
self.text_color = (0, 0, 0, 1) # rgba (text color)
self.real_color = self.color # set color + hilight
self.border_width = 2
self.children = [self.header, self.child]
# Initialization
Gtk.Container.__init__(self)
self.init_header()
self.init_grid()
# Settings
self.set_title(title)
self.set_status(_("Disconnected"))
def init_header(self):
# Create widgets
eb = Gtk.EventBox()
self.title = Gtk.Label()
self.status = Gtk.Label()
hbox = Gtk.HBox()
# Set values
self.title.set_alignment(0.0, 0.5)
self.status.set_alignment(1.0, 0.5)
self.title.set_ellipsize(Pango.EllipsizeMode.START)
hbox.set_spacing(4)
# Connect signals
eb.connect("realize", self.set_header_cursor)
eb.connect("button-release-event", self.on_header_click)
eb.connect('enter-notify-event', self.on_enter_notify)
eb.connect('leave-notify-event', self.on_leave_notify)
# Pack together
hbox.pack_start(self.icon, False, False, 0)
hbox.pack_start(self.title, True, True, 0)
hbox.pack_start(self.status, False, False, 0)
hbox.override_background_color(
Gtk.StateFlags.NORMAL, Gdk.RGBA(*self.color))
eb.add(hbox)
# Update stuff
self.header_box = hbox
self.header = eb
self.header.set_parent(self)
self.children = [self.header, self.child]
def init_grid(self):
# Create widgets
self.grid = Gtk.Grid()
self.rev = RevealerClass()
align = Gtk.Alignment()
self.eb = Gtk.EventBox()
# Set values
self.grid.set_row_spacing(1)
self.grid.set_column_spacing(3)
self.rev.set_reveal_child(False)
align.set_padding(2, 2, 5, 5)
self.eb.override_background_color(
Gtk.StateType.NORMAL, Gdk.RGBA(*self.background))
self.grid.override_background_color(
Gtk.StateType.NORMAL, Gdk.RGBA(*self.background))
# Connect signals
self.eb.connect("button-release-event", self.on_grid_release)
self.eb.connect("button-press-event", self.on_grid_click)
self.eb.connect('enter-notify-event', self.on_enter_notify)
self.eb.connect('leave-notify-event', self.on_leave_notify)
# Pack together
align.add(self.grid)
self.eb.add(align)
self.rev.add(self.eb)
self.add(self.rev)
# GtkWidget-related stuff
def do_add(self, widget):
if widget is not None:
if self.child is None:
self.child = widget
self.children = [self.header, self.child]
widget.set_parent(self)
def do_remove(self, widget):
if self.child == widget:
self.child = None
self.children = [self.header, self.child]
widget.unparent()
def do_child_type(self):
return (Gtk.Widget.get_type())
def do_forall(self, include_internals, callback, *callback_parameters):
if callback is not None:
if hasattr(self, 'children'): # No idea why this happens...
for c in self.children:
if c is not None:
callback(c, *callback_parameters)
def do_get_request_mode(self):
return (Gtk.SizeRequestMode.CONSTANT_SIZE)
def do_get_preferred_height(self):
mw, nw, mh, nh = self.get_preferred_size()
return (mh, nh)
def do_get_preferred_width(self):
mw, nw, mh, nh = self.get_preferred_size()
return (mw, nw)
def get_preferred_size(self):
""" Returns (min_width, nat_width, min_height, nat_height) """
min_width, nat_width = 0, 0
min_height, nat_height = 0, 0
# Use max of preferred widths from children;
# Use sum of preferred height from children.
for c in self.children:
if c is not None:
if c != self.rev or self.rev.get_reveal_child() or self.rev.get_child_revealed():
mw, nw = c.get_preferred_width()
mh, nh = c.get_preferred_height()
min_width = max(min_width, mw)
nat_width = max(nat_width, nw)
min_height = min_height + mh
nat_height = nat_height + nh
# Add border size
min_width += self.border_width * 2 # Left + right border
nat_width += self.border_width * 2
min_height += self.border_width * 3 # Top + below header + bottom
nat_height += self.border_width * 3
return (min_width, nat_width, min_height, nat_height)
def do_size_allocate(self, allocation):
child_allocation = Gdk.Rectangle()
child_allocation.x = self.border_width
child_allocation.y = self.border_width
self.set_allocation(allocation)
if self.get_has_window():
if self.get_realized():
self.get_window().move_resize(allocation.x, allocation.y,
allocation.width, allocation.height)
# Allocate children as VBox does, always use all available width
for c in self.children:
if c is not None:
if c.get_visible():
min_size, nat_size = c.get_preferred_size()
child_allocation.width = allocation.width - \
(self.border_width * 2)
child_allocation.height = min_size.height
# TODO: Handle child that has window (where would i get it?)
c.size_allocate(child_allocation)
child_allocation.y += child_allocation.height + self.border_width
def do_realize(self):
allocation = self.get_allocation()
attr = Gdk.WindowAttr()
attr.window_type = Gdk.WindowType.CHILD
attr.x = allocation.x
attr.y = allocation.y
attr.width = allocation.width
attr.height = allocation.height
attr.visual = self.get_visual()
attr.event_mask = self.get_events() | Gdk.EventMask.EXPOSURE_MASK
WAT = Gdk.WindowAttributesType
mask = WAT.X | WAT.Y | WAT.VISUAL
window = Gdk.Window(self.get_parent_window(), attr, mask)
window.set_decorations(0)
self.set_window(window)
self.register_window(window)
self.set_realized(True)
def do_draw(self, cr):
allocation = self.get_allocation()
header_al = self.children[0].get_allocation()
# Border
cr.set_source_rgba(*self.real_color)
cr.move_to(0, self.border_width / 2.0)
cr.line_to(0, allocation.height)
cr.line_to(allocation.width, allocation.height)
cr.line_to(allocation.width, self.border_width / 2.0)
# Half of border is rendered outside of widget
cr.set_line_width(self.border_width * 2)
cr.stroke()
# Background
if self.background is not None:
# Use set background color
cr.set_source_rgba(*self.background)
cr.rectangle(
self.border_width,
self.border_width,
allocation.width - (2 * self.border_width),
allocation.height - (2 * self.border_width)
)
cr.fill()
# Header
cr.set_source_rgba(*self.real_color)
cr.rectangle(self.border_width / 2.0, 0, allocation.width -
self.border_width, header_al.height + (2 * self.border_width))
cr.fill()
for c in self.children:
if c is not None:
self.propagate_draw(c, cr)
# InfoBox logic
def set_header_cursor(self, eb, *a):
""" Sets cursor over top part of infobox to hand """
eb.get_window().set_cursor(Gdk.Cursor(Gdk.CursorType.HAND1))
def on_header_click(self, eventbox, event):
"""
Hides or reveals everything below header
Displays popup menu on right click
"""
if event.button == 1: # left
self.rev.set_reveal_child(not self.rev.get_reveal_child())
self.app.cb_open_closed(self)
elif event.button == 3: # right
self.emit('right-click', event.button, 0)
def on_grid_release(self, eventbox, event):
""" Displays popup menu on right click """
if event.button == 3: # right
self.emit('right-click', event.button, 0)
def on_grid_click(self, eventbox, event):
""" Emits 'doubleclick' signal """
if event.button == 1: # Left
if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS:
self.emit('doubleclick')
def hilight_timer(self, *a):
""" Called repeatedly while color is changing """
if self.hilight and self.hilight_factor < 1.0:
self.hilight_factor = min(
1.0, self.hilight_factor + COLOR_CHANGE_STEP)
elif not self.hilight and self.hilight_factor > 0.0:
self.hilight_factor = max(
0.0, self.hilight_factor - COLOR_CHANGE_STEP)
else:
self.timer_enabled = False
self.recolor()
return self.timer_enabled
def recolor(self, *a):
"""
Called to computes actual color every time when self.color or
self.hilight_factor changes.
"""
if self.dark_color is None:
self.real_color = tuple(
[min(1.0, x + HILIGHT_INTENSITY * math.sin(self.hilight_factor)) for x in self.color])
else:
# Darken colors when dark background is enabled
self.real_color = tuple(
[min(1.0, DARKEN_FACTOR * (x + HILIGHT_INTENSITY * math.sin(self.hilight_factor))) for x in self.color])
gdkcol = Gdk.RGBA(*self.real_color)
self.header.override_background_color(Gtk.StateType.NORMAL, gdkcol)
try:
self.header.get_children()[0].override_background_color(
Gtk.StateFlags.NORMAL, gdkcol)
except IndexError:
# Happens when recolor is called before header widget is created
pass
self.queue_draw()
# Translated events
def on_enter_notify(self, eb, event, *data):
self.emit("enter-notify-event", None, *data)
def on_leave_notify(self, eb, event, *data):
self.emit("leave-notify-event", None, *data)
# Methods
def set_title(self, t):
t = escape_html_entities(t)
self.str_title = t
inverted = self.header_inverted and self.dark_color is None
col = "black" if inverted else "white"
self.title.set_markup('<span color="%s" %s>%s</span>' % (
col,
self.app.config["infobox_style"],
t
))
def get_title(self):
return self.str_title
def get_icon(self):
""" Returns icon widget """
return self.icon
def set_icon(self, icon):
""" Sets new icon. Expects widget as parameter """
self.header_box.remove(self.icon)
self.header_box.pack_start(icon, False, False, 0)
self.header_box.reorder_child(icon, 0)
self.header_box.show_all()
self.icon = icon
def set_hilight(self, h):
if self.hilight != h:
self.hilight = h
if not self.timer_enabled:
GLib.timeout_add(COLOR_CHANGE_TIMER, self.hilight_timer)
self.timer_enabled = True
def invert_header(self, e):
self.header_inverted = e
self.set_title(self.str_title)
def set_status(self, t, percentage=0.0):
if percentage > 0.0 and percentage < 1.0:
percent = percentage * 100.0
self.status.set_markup('<span color="white" %s>%s (%.f%%)</span>' % (
self.app.config["infobox_style"],
t, percent))
log.debug("%s state changed to %s (%s%%)",
self.str_title, t, percent)
else:
self.status.set_markup('<span color="white" %s>%s</span>' % (
self.app.config["infobox_style"],
t))
if self.str_status != t:
log.debug("%s state changed to %s", self.str_title, t)
self.str_status = t
def get_status(self):
return self.str_status
@classmethod
def hex2color(self, hx):
"""
Converts color from AABBCC or #AABBCC format to tuple of floats
"""
hx = hx.lstrip('#')
hlen = len(hx)
color = [float(int(hx[i:i + hlen // 3], 16)) /
255.0 for i in range(0, hlen, hlen // 3)]
while len(color) < 4:
color.append(1.0)
return color
def set_color_hex(self, hx):
""" Expects AABBCC or #AABBCC format """
self.set_color(*InfoBox.hex2color(hx))
def set_color(self, r, g, b, a):
""" Expects floats """
self.color = (r, g, b, a)
self.recolor()
def compare_color_hex(self, hx):
"""
Returns True if specified color is same as color currently used.
Expects AABBCC or #AABBCC format
"""
return self.compare_color(*InfoBox.hex2color(hx))
def compare_color(self, r, g, b, a):
"""
Returns True if specified color is same as color currently used.
Expects floats.
"""
return (self.color == (r, g, b, a))
def set_dark_color(self, r, g, b, a):
"""
Overrides background color, inverts icon colors and darkens some borders
"""
# Override background
self.background = self.dark_color = (r, g, b, a)
self.set_bg_color(*self.background)
# Recolor existing widgets
self.text_color = (1, 1, 1, 1)
col = Gdk.RGBA(*self.text_color)
for key in self.value_widgets:
for w in self.value_widgets[key]:
if isinstance(w, Gtk.Image):
if (Gtk.get_major_version(), Gtk.get_minor_version()) <= (3, 10):
# Mint....
v1 = GObject.Value(int, 0)
v2 = GObject.Value(int, 0)
self.grid.child_get_property(w, "left-attach", v1)
self.grid.child_get_property(w, "top-attach", v2)
la, ta = v1.get_int(), v2.get_int()
else:
la = self.grid.child_get_property(w, "left-attach")
ta = self.grid.child_get_property(w, "top-attach")
vis = not w.get_no_show_all()
wIcon = self._prepare_icon(self.icons[key])
w.get_parent().remove(w)
self.grid.attach(wIcon, la, ta, 1, 1)
if not vis:
wIcon.set_no_show_all(True)
wIcon.set_visible(False)
wValue, trash, wTitle = self.value_widgets[key]
self.value_widgets[key] = (wValue, wIcon, wTitle)
else:
w.override_color(Gtk.StateFlags.NORMAL, col)
# Recolor borders
self.recolor()
# Recolor header
self.set_title(self.str_title)
def set_bg_color(self, r, g, b, a):
""" Expects floats """
if self.dark_color is None:
self.background = (r, g, b, a)
col = Gdk.RGBA(r, g, b, a)
for key in self.value_widgets:
for w in self.value_widgets[key]:
w.override_background_color(Gtk.StateFlags.NORMAL, col)
self.eb.override_background_color(
Gtk.StateType.NORMAL, Gdk.RGBA(*self.background))
self.grid.override_background_color(Gtk.StateType.NORMAL, col)
def set_border(self, width):
self.border_width = width
self.queue_resize()
def set_open(self, b):
self.rev.set_reveal_child(b)
def is_open(self):
""" Returns True if box is open """
return self.rev.get_reveal_child()
def _prepare_icon(self, icon):
if icon.endswith(".svg"):
# Icon is svg file
key = icon if self.dark_color is None else icon + "-dark"
if key not in svg_cache:
if self.dark_color is not None:
# Recolor svg for dark theme
with open(os.path.join(self.app.iconpath, icon), "r") as f:
svg_source = f.read()
svg_source = svg_source.replace(
'fill:rgb(0%,0%,0%)', 'fill:rgb(100%,100%,100%)')
svg = Rsvg.Handle.new_from_data(svg_source.encode("utf-8"))
else:
# Load svg directly
svg = Rsvg.Handle.new_from_file(
os.path.join(self.app.iconpath, icon))
pixbuf = svg.get_pixbuf()
svg_cache[key] = pixbuf
return Gtk.Image.new_from_pixbuf(svg_cache[key])
elif "." in icon:
# Icon is other image file (png)
return Gtk.Image.new_from_file(os.path.join(self.app.iconpath, icon))
else:
# Icon is theme icon name
return Gtk.Image.new_from_icon_name(icon, 1)
def add_value(self, key, icon, title, value="", visible=True):
""" Adds new line with provided properties """
wIcon = self._prepare_icon(icon)
wTitle, wValue = Gtk.Label(), Gtk.Label()
self.value_widgets[key] = (wValue, wIcon, wTitle)
self.set_value(key, value)
self.icons[key] = icon
wTitle.set_text(title)
wTitle.set_alignment(0.0, 0.5)
wValue.set_alignment(1.0, 0.5)
wValue.set_ellipsize(Pango.EllipsizeMode.START)
wTitle.set_property("expand", True)
line = len(self.value_widgets)
self.grid.attach(wIcon, 0, line, 1, 1)
self.grid.attach_next_to(wTitle, wIcon, Gtk.PositionType.RIGHT, 1, 1)
self.grid.attach_next_to(wValue, wTitle, Gtk.PositionType.RIGHT, 1, 1)
for w in self.value_widgets[key]:
w.override_background_color(
Gtk.StateFlags.NORMAL, Gdk.RGBA(*self.background))
w.override_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(*self.text_color))
if not visible:
w.set_no_show_all(True)
def clear_values(self):
""" Removes all lines from UI, effectively making all values hidden """
for ch in [] + self.grid.get_children():
self.grid.remove(ch)
self.value_widgets = {}
def add_hidden_value(self, key, value):
""" Adds value that is saved, but not shown on UI """
self.set_value(key, value)
def set_value(self, key, value):
""" Updates already existing value """
self.values[key] = value
if key in self.value_widgets:
if value is None:
self.value_widgets[key][0].set_text("?")
else:
self.value_widgets[key][0].set_text(value)
def hide_value(self, key):
""" Hides value added by add_value """
if key in self.value_widgets:
for w in self.value_widgets[key]:
w.set_no_show_all(True)
w.set_visible(False)
def show_value(self, key):
""" Shows value added by add_value """
if key in self.value_widgets:
for w in self.value_widgets[key]:
w.set_no_show_all(False)
w.set_visible(True)
def set_visible(self, key, show):
""" Sets value visibility """
if show:
self.show_value(key)
else:
self.hide_value(key)
def hide_values(self, *keys):
""" Same as hide_value, but for multiple keys at once """
for k in keys:
self.hide_value(k)
def show_values(self, *keys):
""" Same as show_value, but for multiple keys at once """
for k in keys:
self.show_value(k)
def get_value(self, key):
return self.values[key]
def __getitem__(self, key):
""" Shortcut to get_value """
return self.values[key]
def __setitem__(self, key, value):
""" Shortcut to set_value. Creates new hidden_value if key doesn't exist """
self.set_value(key, value)
|