Skip to content

Commit

Permalink
qtutils: memoize() icon creation and gettext translations
Browse files Browse the repository at this point in the history
Eliminate more local caches in favor of the memoize() solution.
This makes the code easier to read and simpler in implementation.

Signed-off-by: David Aguilar <davvid@gmail.com>
  • Loading branch information
davvid committed Jun 8, 2010
1 parent 2ea8270 commit 6c47625
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions cola/qtutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,22 @@ def create_listwidget_item(text, filename):
item.setText(text)
return item

_tree_icon_cache = {}

@memoize
def create_treewidget_item(text, filename):
"""Creates a QTreeWidgetItem with text and the icon at filename."""
if filename not in _tree_icon_cache:
_tree_icon_cache[filename] = QtGui.QIcon(filename)
icon = _tree_icon_cache[filename]
icon = cached_icon_from_path(filename)
item = QtGui.QTreeWidgetItem()
item.setIcon(0, icon)
item.setText(0, text)
return item


@memoize
def cached_icon_from_path(filename):
return QtGui.QIcon(filename)


def information(title, message=None):
"""Launches a QMessageBox information with the
provided title and message."""
Expand Down Expand Up @@ -217,6 +222,8 @@ def set_items(widget, items):
widget.clear()
add_items(widget, items)


@memoize
def tr(txt):
"""Translate a string into a local language."""
if type(txt) is QtCore.QString:
Expand Down Expand Up @@ -269,13 +276,12 @@ def set_listwidget_strings(widget, items):
widget.clear()
add_items(widget, [ QtGui.QListWidgetItem(i) for i in items ])

_icon_cache = {}
@memoize
def cached_icon(key):
"""Maintain a cache of standard icons and return cache entries."""
if key not in _icon_cache:
style = QtGui.QApplication.instance().style()
_icon_cache[key] = style.standardIcon(key)
return _icon_cache[key]
style = QtGui.QApplication.instance().style()
return style.standardIcon(key)


def dir_icon():
"""Return a standard icon for a directory."""
Expand Down

0 comments on commit 6c47625

Please sign in to comment.