mirror of
https://git.deluge-torrent.org/deluge
synced 2025-08-21 09:38:44 +00:00
[GTK] Refactor out get_pixbuf_at_size
The functionality of get_pixbuf and get_pixbuf_at_size is almost identical so reuse get_pixbuf with an optional size arg.
This commit is contained in:
parent
e87236514d
commit
24a3987c3a
4 changed files with 29 additions and 21 deletions
|
@ -59,12 +59,31 @@ def create_blank_pixbuf(size=16):
|
||||||
return pix
|
return pix
|
||||||
|
|
||||||
|
|
||||||
def get_pixbuf(filename):
|
def get_pixbuf(filename: str, size: int = 0) -> Pixbuf:
|
||||||
|
"""Creates a new pixbuf by loading an image from file
|
||||||
|
|
||||||
|
Args:
|
||||||
|
filename: An image file to load
|
||||||
|
size: Specify a size constraint (equal aspect ratio)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A newly created pixbuf
|
||||||
|
|
||||||
|
"""
|
||||||
|
if not os.path.isabs(filename):
|
||||||
|
filename = get_pixmap(filename)
|
||||||
|
|
||||||
|
pixbuf = None
|
||||||
try:
|
try:
|
||||||
return Pixbuf.new_from_file(get_pixmap(filename))
|
if size:
|
||||||
|
pixbuf = Pixbuf.new_from_file_at_size(filename, size, size)
|
||||||
|
else:
|
||||||
|
pixbuf = Pixbuf.new_from_file(filename)
|
||||||
except GError as ex:
|
except GError as ex:
|
||||||
|
# Failed to load the pixbuf (Bad image file), so return a blank pixbuf.
|
||||||
log.warning(ex)
|
log.warning(ex)
|
||||||
return create_blank_pixbuf()
|
|
||||||
|
return pixbuf or create_blank_pixbuf(size or 16)
|
||||||
|
|
||||||
|
|
||||||
# Status icons.. Create them from file only once to avoid constantly re-creating them.
|
# Status icons.. Create them from file only once to avoid constantly re-creating them.
|
||||||
|
@ -76,17 +95,6 @@ icon_queued = get_pixbuf('queued16.png')
|
||||||
icon_checking = get_pixbuf('checking16.png')
|
icon_checking = get_pixbuf('checking16.png')
|
||||||
|
|
||||||
|
|
||||||
def get_pixbuf_at_size(filename, size):
|
|
||||||
if not os.path.isabs(filename):
|
|
||||||
filename = get_pixmap(filename)
|
|
||||||
try:
|
|
||||||
return Pixbuf.new_from_file_at_size(filename, size, size)
|
|
||||||
except GError as ex:
|
|
||||||
# Failed to load the pixbuf (Bad image file), so return a blank pixbuf.
|
|
||||||
log.warning(ex)
|
|
||||||
return create_blank_pixbuf(size)
|
|
||||||
|
|
||||||
|
|
||||||
def get_logo(size):
|
def get_logo(size):
|
||||||
"""A Deluge logo.
|
"""A Deluge logo.
|
||||||
|
|
||||||
|
@ -99,7 +107,7 @@ def get_logo(size):
|
||||||
filename = 'deluge.svg'
|
filename = 'deluge.svg'
|
||||||
if windows_check():
|
if windows_check():
|
||||||
filename = 'deluge.png'
|
filename = 'deluge.png'
|
||||||
return get_pixbuf_at_size(filename, size)
|
return get_pixbuf(filename, size)
|
||||||
|
|
||||||
|
|
||||||
def build_menu_radio_list(
|
def build_menu_radio_list(
|
||||||
|
|
|
@ -14,7 +14,7 @@ from twisted.internet import defer
|
||||||
import deluge.component as component
|
import deluge.component as component
|
||||||
from deluge.common import windows_check
|
from deluge.common import windows_check
|
||||||
|
|
||||||
from .common import get_deluge_icon, get_pixbuf_at_size
|
from .common import get_deluge_icon, get_pixbuf
|
||||||
|
|
||||||
|
|
||||||
class BaseDialog(Gtk.Dialog):
|
class BaseDialog(Gtk.Dialog):
|
||||||
|
@ -52,7 +52,7 @@ class BaseDialog(Gtk.Dialog):
|
||||||
# Hack for Windows since it doesn't support svg
|
# Hack for Windows since it doesn't support svg
|
||||||
if icon.endswith('.svg') and windows_check():
|
if icon.endswith('.svg') and windows_check():
|
||||||
icon = icon.rpartition('.svg')[0] + '16.png'
|
icon = icon.rpartition('.svg')[0] + '16.png'
|
||||||
image.set_from_pixbuf(get_pixbuf_at_size(icon, 24))
|
image.set_from_pixbuf(get_pixbuf(icon, 24))
|
||||||
else:
|
else:
|
||||||
image.set_from_icon_name(icon, Gtk.IconSize.LARGE_TOOLBAR)
|
image.set_from_icon_name(icon, Gtk.IconSize.LARGE_TOOLBAR)
|
||||||
image.set_alignment(0.5, 0.0)
|
image.set_alignment(0.5, 0.0)
|
||||||
|
|
|
@ -21,7 +21,7 @@ from deluge.common import TORRENT_STATE, decode_bytes, resource_filename
|
||||||
from deluge.configmanager import ConfigManager
|
from deluge.configmanager import ConfigManager
|
||||||
from deluge.ui.client import client
|
from deluge.ui.client import client
|
||||||
|
|
||||||
from .common import get_pixbuf, get_pixbuf_at_size
|
from .common import get_pixbuf
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -253,7 +253,7 @@ class FilterTreeView(component.Component):
|
||||||
return get_pixbuf('%s16.png' % pix)
|
return get_pixbuf('%s16.png' % pix)
|
||||||
|
|
||||||
def set_row_image(self, cat, value, filename):
|
def set_row_image(self, cat, value, filename):
|
||||||
pix = get_pixbuf_at_size(filename, 16)
|
pix = get_pixbuf(filename, size=16)
|
||||||
row = self.filters[(cat, value)]
|
row = self.filters[(cat, value)]
|
||||||
self.treestore.set_value(row, 4, pix)
|
self.treestore.set_value(row, 4, pix)
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -14,7 +14,7 @@ import deluge.component as component
|
||||||
|
|
||||||
from .common import (
|
from .common import (
|
||||||
create_blank_pixbuf,
|
create_blank_pixbuf,
|
||||||
get_pixbuf_at_size,
|
get_pixbuf,
|
||||||
icon_alert,
|
icon_alert,
|
||||||
icon_checking,
|
icon_checking,
|
||||||
icon_downloading,
|
icon_downloading,
|
||||||
|
@ -83,7 +83,7 @@ def set_tracker_icon(tracker_icon, cell):
|
||||||
if tracker_icon:
|
if tracker_icon:
|
||||||
pixbuf = tracker_icon.get_cached_icon()
|
pixbuf = tracker_icon.get_cached_icon()
|
||||||
if pixbuf is None:
|
if pixbuf is None:
|
||||||
pixbuf = get_pixbuf_at_size(tracker_icon.get_filename(), 16)
|
pixbuf = get_pixbuf(tracker_icon.get_filename(), 16)
|
||||||
tracker_icon.set_cached_icon(pixbuf)
|
tracker_icon.set_cached_icon(pixbuf)
|
||||||
else:
|
else:
|
||||||
pixbuf = create_blank_pixbuf()
|
pixbuf = create_blank_pixbuf()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue