Skip to content

Commit

Permalink
Add ability to change window icon in MDIWidget and TopLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
ejeschke committed May 21, 2024
1 parent 757a991 commit 2f2d45a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
15 changes: 10 additions & 5 deletions ginga/gtk3w/GtkHelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def on_button_release(self, widget, event):

class MDISubWindow(Callback.Callbacks):

def __init__(self, widget, label):
def __init__(self, widget, label, iconpath=None):
super(MDISubWindow, self).__init__()

self.widget = widget
Expand All @@ -286,10 +286,11 @@ def __init__(self, widget, label):
hbox = Gtk.HBox()

# set window icon
iconfile = os.path.join(icondir, "ginga.svg")
pixbuf = pixbuf_new_from_file_at_size(iconfile, 32, 32)
image = Gtk.Image.new_from_pixbuf(pixbuf)
hbox.pack_start(image, False, False, 2)
if iconpath is None:
iconpath = os.path.join(icondir, "ginga.svg")
pixbuf = pixbuf_new_from_file_at_size(iconpath, 32, 32)
self.image = Gtk.Image.new_from_pixbuf(pixbuf)
hbox.pack_start(self.image, False, False, 2)

# titlebar label
evbox = Gtk.EventBox()
Expand Down Expand Up @@ -347,6 +348,10 @@ def __init__(self, widget, label):
minim.connect('clicked', lambda *args: self.make_callback('minimize'))
close.connect('clicked', lambda *args: self.make_callback('close'))

def set_icon(self, iconpath):
pixbuf = pixbuf_new_from_file_at_size(iconpath, 32, 32)
self.image.set_from_pixbuf(pixbuf)

def raise_(self):
window = self.frame.get_window()
if window is not None:
Expand Down
20 changes: 15 additions & 5 deletions ginga/gtk3w/Widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ def use_tabs(self, tf):


class MDIWindow(WidgetBase):
def __init__(self, parent, child, title=''):
def __init__(self, parent, child, title='', iconpath=None):
"""NOTE: this widget is not meant to be instantiated except *inside*
of MDIWidget implementation.
"""
Expand All @@ -1582,7 +1582,10 @@ def __init__(self, parent, child, title=''):

child_w = child.get_widget()
label = Gtk.Label(title)
subwin = GtkHelp.MDISubWindow(child_w, label)
if iconpath is None:
iconpath = os.path.join(icondir, "ginga.svg")

subwin = GtkHelp.MDISubWindow(child_w, label, iconpath=iconpath)
self.widget = subwin
# attach title to child
child.extdata.tab_title = title
Expand Down Expand Up @@ -2264,14 +2267,15 @@ def set_title(self, title):

class TopLevel(TopLevelMixin, ContainerBase):

def __init__(self, title=None):
def __init__(self, title=None, iconpath=None):
ContainerBase.__init__(self)

self._fullscreen = False

widget = GtkHelp.TopLevel()
ginga_icon = os.path.join(icondir, "ginga.svg")
widget.set_icon(GtkHelp.get_icon(ginga_icon))
if iconpath is None:
iconpath = os.path.join(icondir, "ginga.svg")
widget.set_icon(GtkHelp.get_icon(iconpath))
self.widget = widget
widget.set_border_width(0)

Expand All @@ -2282,6 +2286,12 @@ def set_widget(self, child):
child_w = child.get_widget()
self.widget.add(child_w)

def set_icon(self, iconpath):
# NOTE: not guaranteed to work after the window is created because
# this can be rendered by the window manager. Better to use the
# constructor 'iconpath' parameter
self.widget.set_icon(GtkHelp.get_icon(iconpath))


class Application(Callback.Callbacks):

Expand Down
24 changes: 18 additions & 6 deletions ginga/qtw/Widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1949,16 +1949,17 @@ def set_title(self, title):


class MDIWindow(TopLevelMixin, WidgetBase):
def __init__(self, parent, child, title=''):
def __init__(self, parent, child, title='', iconpath=None):
"""NOTE: this widget is not meant to be instantiated except *inside*
of MDIWidget implementation.
"""
WidgetBase.__init__(self)
self.parent = parent
w = QtGui.QMdiSubWindow(parent.get_widget())
# replace Qt logo from subwindow
ginga_icon = os.path.join(icondir, "ginga.svg")
w.setWindowIcon(QIcon(ginga_icon))
if iconpath is None:
iconpath = os.path.join(icondir, "ginga.svg")
w.setWindowIcon(QIcon(iconpath))
self.widget = w

child_w = child.get_widget()
Expand Down Expand Up @@ -2007,6 +2008,9 @@ def _focus_cb(event):
child.show()
w.show()

def set_icon(self, iconpath):
self.widget.setWindowIcon(QIcon(iconpath))

def _window_resized(self, event, subwin, child):
qsize = event.size()
wd, ht = qsize.width(), qsize.height()
Expand All @@ -2032,12 +2036,14 @@ def _window_closed(self, event, subwin, widget):

class TopLevel(TopLevelMixin, ContainerBase):

def __init__(self, title=None):
def __init__(self, title=None, iconpath=None):
ContainerBase.__init__(self)

widget = QtHelp.TopLevel()
ginga_icon = os.path.join(icondir, "ginga.svg")
widget.setWindowIcon(QIcon(ginga_icon))

if iconpath is None:
iconpath = os.path.join(icondir, "ginga.svg")
widget.setWindowIcon(QIcon(iconpath))
self.widget = widget
box = QtGui.QVBoxLayout()
box.setContentsMargins(0, 0, 0, 0)
Expand All @@ -2051,6 +2057,12 @@ def set_widget(self, child):
child_w = child.get_widget()
self.widget.layout().addWidget(child_w)

def set_icon(self, iconpath):
# NOTE: not guaranteed to work after the window is created because
# this can be rendered by the window manager. Better to use the
# constructor 'iconpath' parameter
self.widget.setWindowIcon(QIcon(iconpath))


class Application(Callback.Callbacks):

Expand Down
8 changes: 7 additions & 1 deletion ginga/web/pgw/Widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3121,7 +3121,7 @@ class TopLevel(ContainerBase):
</script>
"""

def __init__(self, title="", parent=None):
def __init__(self, title="", parent=None, iconpath=None):

super(TopLevel, self).__init__()

Expand All @@ -3130,6 +3130,8 @@ def __init__(self, title="", parent=None):

self.title = title
self.parent = parent
# NOTE: iconpath is TODO
self.iconpath = iconpath

for name in ('open', 'close', 'resize'):
self.enable_callback(name)
Expand Down Expand Up @@ -3159,6 +3161,10 @@ def set_widget(self, child):
self.remove_all()
self.add_ref(child)

def set_icon(self, iconpath):
# TODO!
pass

def show(self):
if self._rendered:
app = self.get_app()
Expand Down

0 comments on commit 2f2d45a

Please sign in to comment.