1 """The only file where icon filenames are mentioned"""
6 from PyQt4
import QtGui
8 from cola
import qtcompat
9 from cola
import resources
10 from cola
.compat
import ustr
11 from cola
.decorators
import memoize
14 KNOWN_FILE_MIME_TYPES
= [
15 ('text', 'file-code.svg'),
16 ('image', 'file-media.svg'),
17 ('python', 'file-code.svg'),
18 ('ruby', 'file-code.svg'),
19 ('shell', 'file-code.svg'),
20 ('perl', 'file-code.svg'),
21 ('octet', 'file-binary.svg'),
24 KNOWN_FILE_EXTENSIONS
= {
25 '.java': 'file-code.svg',
26 '.groovy': 'file-code.svg',
27 '.cpp': 'file-code.svg',
28 '.c': 'file-code.svg',
29 '.h': 'file-code.svg',
30 '.cxx': 'file-code.svg',
35 icon_dir
= resources
.icon_dir()
36 qtcompat
.add_search_path('icons', icon_dir
)
39 def name_from_basename(basename
):
40 """Prefix the basename with "icons:" so that git-cola's icons are found
42 "icons" is registered with Qt's resource system during install().
45 return 'icons:' + basename
50 """Return a QIcon from an absolute filename or "icons:basename.svg" name"""
51 return QtGui
.QIcon(name
)
55 """Given a basename returns a QIcon from the corresponding cola icon"""
56 return from_name(name_from_basename(basename
))
60 def from_theme(name
, fallback
=None):
61 """Grab an icon from the current theme with a fallback
63 Support older versions of Qt checking for fromTheme's availability.
66 if hasattr(QtGui
.QIcon
, 'fromTheme'):
67 base
, ext
= os
.path
.splitext(name
)
69 qicon
= QtGui
.QIcon
.fromTheme(base
, icon(fallback
))
71 qicon
= QtGui
.QIcon
.fromTheme(base
)
72 if not qicon
.isNull():
74 return icon(fallback
or name
)
77 def basename_from_filename(filename
):
78 """Returns an icon name based on the filename"""
79 mimetype
= mimetypes
.guess_type(filename
)[0]
80 if mimetype
is not None:
81 mimetype
= mimetype
.lower()
82 for filetype
, icon_name
in KNOWN_FILE_MIME_TYPES
:
83 if filetype
in mimetype
:
85 extension
= os
.path
.splitext(filename
)[1]
86 return KNOWN_FILE_EXTENSIONS
.get(extension
.lower(), 'file-text.svg')
89 def from_filename(filename
):
90 basename
= basename_from_filename(filename
)
91 return from_name(name_from_basename(basename
))
94 def mkicon(icon
, default
=None):
95 if icon
is None and default
is not None:
97 elif icon
and isinstance(icon
, (str, ustr
)):
98 icon
= QtGui
.QIcon(icon
)
104 """Maintain a cache of standard icons and return cache entries."""
105 style
= QtGui
.QApplication
.instance().style()
106 return style
.standardIcon(key
)
109 def status(filename
, deleted
, staged
, untracked
):
111 icon_name
= 'circle-slash-red.svg'
113 icon_name
= 'staged.svg'
115 icon_name
= 'question-plain.svg'
117 icon_name
= basename_from_filename(filename
)
121 # Icons creators and SVG file references
124 return from_theme('list-add', fallback
='plus.svg')
128 return icon('git-branch.svg')
132 return name_from_basename('check.svg')
140 return from_theme('git-cola.svg')
144 return icon('git-compare.svg')
148 return from_theme('configure', fallback
='gear.svg')
152 return from_theme('edit-copy.svg')
156 return icon('telescope.svg')
160 return name_from_basename('primitive-dot.svg')
164 return icon('file-download.svg')
168 return icon('trashcan.svg')
170 # folder vs directory: directory is opaque, folder is just an outline
171 # directory is used for the File Browser, where more contrast with the file
175 return icon('folder.svg')
178 return icon('file-directory.svg')
182 return icon('diff.svg')
186 return icon('pencil.svg')
190 return icon('ellipsis.svg')
194 return icon('link-external.svg')
198 return icon('file-code.svg')
202 return icon('file-text.svg')
206 return icon('file-zip.svg')
210 return icon('fold.svg')
214 return icon('git-merge.svg')
218 return name_from_basename('modified.svg')
222 return icon('folder-new.svg')
226 return icon('check.svg')
229 def open_directory():
230 return icon('folder.svg')
234 return name_from_basename('partial.svg')
238 return icon('repo-pull.svg')
242 return icon('repo-push.svg')
246 return icon('question.svg')
250 return from_theme('list-remove', fallback
='circle-slash.svg')
254 return icon('repo.svg')
258 return icon('desktop-download.svg')
262 return icon('search.svg')
266 return from_theme('edit-select-all.svg')
270 return icon('staged.svg')
274 return name_from_basename('staged.svg')
278 return icon('star.svg')
282 return icon('sync.svg')
286 return icon('tag.svg')
290 return from_theme('edit-undo', fallback
='edit-undo.svg')
294 return icon('unfold.svg')
298 return icon('eye.svg')
302 return name_from_basename('upstream.svg')
306 return from_theme('zoom-fit-best.svg')
310 return from_theme('zoom-in.svg')
314 return from_theme('zoom-out.svg')