1 """The only file where icon filenames are mentioned"""
2 from __future__
import absolute_import
, division
, unicode_literals
6 from qtpy
import QtWidgets
10 from . import resources
11 from .compat
import ustr
15 KNOWN_FILE_MIME_TYPES
= [
16 ('text', 'file-code.svg'),
17 ('image', 'file-media.svg'),
18 ('octet', 'file-binary.svg'),
21 KNOWN_FILE_EXTENSIONS
= {
22 '.bash': 'file-code.svg',
23 '.c': 'file-code.svg',
24 '.cpp': 'file-code.svg',
25 '.css': 'file-code.svg',
26 '.cxx': 'file-code.svg',
27 '.h': 'file-code.svg',
28 '.hpp': 'file-code.svg',
29 '.hs': 'file-code.svg',
30 '.html': 'file-code.svg',
31 '.java': 'file-code.svg',
32 '.js': 'file-code.svg',
33 '.ksh': 'file-code.svg',
34 '.lisp': 'file-code.svg',
35 '.perl': 'file-code.svg',
36 '.pl': 'file-code.svg',
37 '.py': 'file-code.svg',
38 '.rb': 'file-code.svg',
39 '.rs': 'file-code.svg',
40 '.sh': 'file-code.svg',
41 '.zsh': 'file-code.svg',
47 icon_dir
= resources
.icon_dir(theme
)
48 qtcompat
.add_search_path('icons', icon_dir
)
53 (N_('Default'), 'default'),
54 (N_('Dark Theme'), 'dark'),
55 (N_('Light Theme'), 'light'),
59 def name_from_basename(basename
):
60 """Prefix the basename with "icons:" so that git-cola's icons are found
62 "icons" is registered with Qt's resource system during install().
65 return 'icons:' + basename
69 """Return a QIcon from an absolute filename or "icons:basename.svg" name"""
70 return QtGui
.QIcon(name
)
74 """Given a basename returns a QIcon from the corresponding cola icon"""
75 return from_name(name_from_basename(basename
))
78 def from_theme(name
, fallback
=None):
79 """Grab an icon from the current theme with a fallback
81 Support older versions of Qt checking for fromTheme's availability.
84 if hasattr(QtGui
.QIcon
, 'fromTheme'):
85 base
, _
= os
.path
.splitext(name
)
87 qicon
= QtGui
.QIcon
.fromTheme(base
, icon(fallback
))
89 qicon
= QtGui
.QIcon
.fromTheme(base
)
90 if not qicon
.isNull():
92 return icon(fallback
or name
)
95 def basename_from_filename(filename
):
96 """Returns an icon name based on the filename"""
97 mimetype
= core
.guess_mimetype(filename
)
98 if mimetype
is not None:
99 mimetype
= mimetype
.lower()
100 for filetype
, icon_name
in KNOWN_FILE_MIME_TYPES
:
101 if filetype
in mimetype
:
103 extension
= os
.path
.splitext(filename
)[1]
104 return KNOWN_FILE_EXTENSIONS
.get(extension
.lower(), 'file-text.svg')
107 def from_filename(filename
):
108 basename
= basename_from_filename(filename
)
109 return from_name(name_from_basename(basename
))
112 def mkicon(value
, default
=None):
113 if value
is None and default
is not None:
115 elif value
and isinstance(value
, (str, ustr
)):
116 value
= QtGui
.QIcon(value
)
121 """Maintain a cache of standard icons and return cache entries."""
122 style
= QtWidgets
.QApplication
.instance().style()
123 return style
.standardIcon(key
)
126 def status(filename
, deleted
, is_staged
, untracked
):
128 icon_name
= 'circle-slash-red.svg'
130 icon_name
= 'staged.svg'
132 icon_name
= 'question-plain.svg'
134 icon_name
= basename_from_filename(filename
)
138 # Icons creators and SVG file references
142 return icon('three-bars.svg')
146 return from_theme('list-add', fallback
='plus.svg')
150 return icon('a-z-order.svg')
154 return icon('git-branch.svg')
158 return name_from_basename('check.svg')
166 return icon('git-cola.svg')
170 return icon('document-save-symbolic.svg')
174 return icon('git-compare.svg')
178 return icon('gear.svg')
182 return from_theme('edit-copy.svg')
186 return icon('telescope.svg')
190 return name_from_basename('primitive-dot.svg')
194 return icon('file-download.svg')
198 return icon('trashcan.svg')
201 # folder vs directory: directory is opaque, folder is just an outline
202 # directory is used for the File Browser, where more contrast with the file
207 return icon('folder.svg')
211 return icon('file-directory.svg')
215 return icon('diff.svg')
219 return icon('pencil.svg')
223 return icon('ellipsis.svg')
227 return icon('link-external.svg')
231 return icon('file-code.svg')
235 return icon('file-text.svg')
239 return icon('file-zip.svg')
243 return icon('fold.svg')
247 return icon('git-merge.svg')
251 return icon('modified.svg')
255 return name_from_basename('modified.svg')
259 return icon('folder-new.svg')
263 return icon('check.svg')
266 def open_directory():
267 return icon('folder.svg')
271 return name_from_basename('partial.svg')
275 return icon('repo-pull.svg')
279 return icon('repo-push.svg')
283 return icon('question.svg')
287 return from_theme('list-remove', fallback
='circle-slash.svg')
291 return icon('repo.svg')
294 def reverse_chronological():
295 return icon('last-first-order.svg')
299 return icon('desktop-download.svg')
303 return icon('search.svg')
307 return from_theme('edit-select-all.svg')
311 return icon('staged.svg')
315 return name_from_basename('staged.svg')
319 return icon('star.svg')
323 return icon('sync.svg')
327 return icon('tag.svg')
331 return from_theme('edit-undo', fallback
='edit-undo.svg')
334 def style_dialog_apply():
335 return from_style(QtWidgets
.QStyle
.SP_DialogApplyButton
)
338 def style_dialog_discard():
339 return from_style(QtWidgets
.QStyle
.SP_DialogDiscardButton
)
342 def style_dialog_reset():
343 return from_style(QtWidgets
.QStyle
.SP_DialogResetButton
)
347 return icon('unfold.svg')
351 return icon('eye.svg')
355 return name_from_basename('upstream.svg')
359 return icon('zoom-fit-best.svg')
363 return icon('zoom-in.svg')
367 return icon('zoom-out.svg')