Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
Add icon support to the Qt Page implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sccolbert committed Jan 7, 2013
1 parent 0d407f4 commit b6426b7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 10 deletions.
11 changes: 2 additions & 9 deletions enaml/qt/qt_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,7 @@ def addPage(self, page):
The QPage instance to add to the notebook.
"""
if page.isOpen():
self.addTab(page, page.title())
index = self.indexOf(page)
self.setTabToolTip(index, page.toolTip())
self.setTabEnabled(index, page.isTabEnabled())
self.setTabCloseButtonVisible(index, page.isClosable())
else:
page.hide()
self._hidden_pages[page] = self.count()
self.insertPage(self.count(), page)

def insertPage(self, index, page):
""" Insert a QPage instance into the notebook.
Expand All @@ -168,6 +160,7 @@ def insertPage(self, index, page):
if page.isOpen():
index = min(index, self.count())
self.insertTab(index, page, page.title())
self.setTabIcon(index, page.icon())
self.setTabToolTip(index, page.toolTip())
self.setTabEnabled(index, page.isTabEnabled())
self.setTabCloseButtonVisible(index, page.isClosable())
Expand Down
83 changes: 82 additions & 1 deletion enaml/qt/qt_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
# Copyright (c) 2012, Enthought, Inc.
# All rights reserved.
#------------------------------------------------------------------------------
import logging

from .qt.QtCore import Signal
from .qt.QtGui import QFrame
from .qt.QtGui import QFrame, QIcon, QImage, QPixmap
from .q_single_widget_layout import QSingleWidgetLayout
from .qt_container import QtContainer
from .qt_widget import QtWidget


logger = logging.getLogger(__name__)


class QPage(QFrame):
""" A QFrame subclass which acts as a page in a QNotebook.
Expand All @@ -29,6 +34,7 @@ def __init__(self, *args, **kwargs):
super(QPage, self).__init__(*args, **kwargs)
self._title = u''
self._tool_tip = u''
self._icon = QIcon()
self._closable = True
self._is_enabled = True
self._is_open = True
Expand Down Expand Up @@ -246,11 +252,39 @@ def closure(nb, index):
nb.setTabToolTip(index, tool_tip)
self._pageIndexOperation(closure)

def icon(self):
""" Get the icon for the page.
Returns
-------
result : QIcon
The icon for the page.
"""
return self._icon

def setIcon(self, icon):
""" Set the icon for the page.
Parameters
----------
icon : QIcon
The icon for the page.
"""
self._icon = icon
def closure(nb, index):
nb.setTabIcon(index, icon)
self._pageIndexOperation(closure)


class QtPage(QtWidget):
""" A Qt implementation of an Enaml notebook Page.
"""
#: Temporary internal storage for the icon source url.
_icon_source = ''

#--------------------------------------------------------------------------
# Setup Methods
#--------------------------------------------------------------------------
Expand All @@ -267,6 +301,7 @@ def create(self, tree):
super(QtPage, self).create(tree)
self.set_title(tree['title'])
self.set_closable(tree['closable'])
self._icon_source = tree['icon_source']
self.widget().pageClosed.connect(self.on_page_closed)

def init_layout(self):
Expand All @@ -276,6 +311,13 @@ def init_layout(self):
super(QtPage, self).init_layout()
self.widget().setPageWidget(self.page_widget())

def activate(self):
""" Activate the page widget.
"""
self.set_icon_source(self._icon_source)
super(QtPage, self).activate()

#--------------------------------------------------------------------------
# Utility Methods
#--------------------------------------------------------------------------
Expand Down Expand Up @@ -336,6 +378,12 @@ def on_action_set_closable(self, content):
"""
self.set_closable(content['closable'])

def on_action_set_icon_source(self, content):
""" Handle the 'set_icon_source' action from the Enaml widget.
"""
self.set_icon_source(content['icon_source'])

def on_action_open(self, content):
""" Handle the 'open' action from the Enaml widget.
Expand Down Expand Up @@ -381,3 +429,36 @@ def set_closable(self, closable):
"""
self.widget().setClosable(closable)

def set_icon_source(self, icon_source):
""" Sets the widget's icon to the provided image.
"""
if icon_source:
loader = self._session.load_resource(icon_source)
loader.on_load(self._on_icon_load)
else:
self._on_icon_load(QIcon())

#--------------------------------------------------------------------------
# Private API
#--------------------------------------------------------------------------
def _on_icon_load(self, icon):
""" A private resource loader callback.
This method is invoked when the requested icon is successfully
loaded. It will update the icon on the page widget.
Parameters
----------
icon : QIcon or QImage
The icon or image that was loaded by the request.
"""
if isinstance(icon, QImage):
icon = QIcon(QPixmap.fromImage(icon))
elif not isinstance(icon, QIcon):
msg = 'got incorrect type for icon: `%s`'
logger.error(msg % type(icon).__name__)
icon = QIcon()
self.widget().setIcon(icon)

0 comments on commit b6426b7

Please sign in to comment.