File: icons.py

package info (click to toggle)
git-cola 4.13.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 6,480 kB
  • sloc: python: 36,938; sh: 304; makefile: 223; xml: 100; tcl: 62
file content (487 lines) | stat: -rw-r--r-- 9,943 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
"""The only file where icon filenames are mentioned"""
import os

from qtpy import QtGui
from qtpy import QtWidgets

from . import core
from . import decorators
from . import qtcompat
from . import resources
from .compat import ustr
from .i18n import N_


KNOWN_FILE_MIME_TYPES = [
    ('text', 'file-code.svg'),
    ('image', 'file-media.svg'),
    ('octet', 'file-binary.svg'),
]

KNOWN_FILE_EXTENSIONS = {
    '.bash': 'file-code.svg',
    '.c': 'file-code.svg',
    '.cpp': 'file-code.svg',
    '.css': 'file-code.svg',
    '.cxx': 'file-code.svg',
    '.h': 'file-code.svg',
    '.hpp': 'file-code.svg',
    '.hs': 'file-code.svg',
    '.html': 'file-code.svg',
    '.java': 'file-code.svg',
    '.js': 'file-code.svg',
    '.ksh': 'file-code.svg',
    '.lisp': 'file-code.svg',
    '.perl': 'file-code.svg',
    '.pl': 'file-code.svg',
    '.py': 'file-code.svg',
    '.rb': 'file-code.svg',
    '.rs': 'file-code.svg',
    '.sh': 'file-code.svg',
    '.zsh': 'file-code.svg',
}


def install(themes):
    for theme in themes:
        icon_dir = resources.icon_dir(theme)
        qtcompat.add_search_path('icons', icon_dir)


def icon_themes():
    return (
        (N_('Default'), 'default'),
        (N_('Dark Theme'), 'dark'),
        (N_('Light Theme'), 'light'),
    )


def name_from_basename(basename):
    """Prefix the basename with "icons:" so that git-cola's icons are found

    "icons" is registered with the Qt resource system during install().

    """
    return 'icons:' + basename


@decorators.memoize
def from_name(name):
    """Return a QIcon from an absolute filename or "icons:basename.svg" name"""
    return QtGui.QIcon(name)


def icon(basename):
    """Given a basename returns a QIcon from the corresponding cola icon"""
    return from_name(name_from_basename(basename))


def from_theme(name, fallback=None):
    """Grab an icon from the current theme with a fallback

    Support older versions of Qt checking for fromTheme's availability.

    """
    if hasattr(QtGui.QIcon, 'fromTheme'):
        base, _ = os.path.splitext(name)
        if fallback:
            qicon = QtGui.QIcon.fromTheme(base, icon(fallback))
        else:
            qicon = QtGui.QIcon.fromTheme(base)
        if not qicon.isNull():
            return qicon
    return icon(fallback or name)


def basename_from_filename(filename):
    """Returns an icon name based on the filename"""
    mimetype = core.guess_mimetype(filename)
    if mimetype is not None:
        mimetype = mimetype.lower()
        for filetype, icon_name in KNOWN_FILE_MIME_TYPES:
            if filetype in mimetype:
                return icon_name
    extension = os.path.splitext(filename)[1]
    return KNOWN_FILE_EXTENSIONS.get(extension.lower(), 'file-text.svg')


def from_filename(filename):
    """Return a QIcon from a filename"""
    basename = basename_from_filename(filename)
    return from_name(name_from_basename(basename))


def mkicon(value, default=None):
    """Create an icon from a string value"""
    if value is None and default is not None:
        value = default()
    elif value and isinstance(value, (str, ustr)):
        value = QtGui.QIcon(value)
    return value


def from_style(key):
    """Maintain a cache of standard icons and return cache entries."""
    style = QtWidgets.QApplication.instance().style()
    return style.standardIcon(key)


def status(filename, deleted, is_staged, untracked):
    """Status icon for a file"""
    if deleted:
        icon_name = 'circle-slash-red.svg'
    elif is_staged:
        icon_name = 'staged.svg'
    elif untracked:
        icon_name = 'question-plain.svg'
    else:
        icon_name = basename_from_filename(filename)
    return icon_name


# Icons creators and SVG file references


def three_bars():
    """Three-bars icon"""
    return icon('three-bars.svg')


def add():
    """Add icon"""
    return from_theme('list-add', fallback='plus.svg')


def alphabetical():
    """Alphabetical icon"""
    return from_theme('view-sort', fallback='a-z-order.svg')


def branch():
    """Branch icon"""
    return icon('git-branch.svg')


def check_name():
    """Check mark icon name"""
    return name_from_basename('check.svg')


def cherry_pick():
    """Cherry-pick icon"""
    return icon('git-commit.svg')


def circle_slash_red():
    """A circle with a slash through it"""
    return icon('circle-slash-red.svg')


def close():
    """Close icon"""
    return icon('x.svg')


def cola():
    """Git Cola icon"""
    return icon('git-cola.svg')


def commit():
    """Commit icon"""
    return icon('document-save-symbolic.svg')


def compare():
    """Compare icon"""
    return icon('git-compare.svg')


def configure():
    """Configure icon"""
    return icon('gear.svg')


def cut():
    """Cut icon"""
    return from_theme('edit-cut', fallback='edit-cut.svg')


def copy():
    """Copy icon"""
    return from_theme('edit-copy', fallback='edit-copy.svg')


def paste():
    """Paste icon"""
    return from_theme('edit-paste', fallback='edit-paste.svg')


def play():
    """Play icon"""
    return icon('play.svg')


def delete():
    """Delete icon"""
    return from_theme('edit-delete', fallback='trashcan.svg')


def default_app():
    """Default app icon"""
    return icon('telescope.svg')


def dot_name():
    """Dot icon name"""
    return name_from_basename('primitive-dot.svg')


def download():
    """Download icon"""
    return icon('file-download.svg')


def discard():
    """Discard icon"""
    return from_theme('delete', fallback='trashcan.svg')


# folder vs directory: directory is opaque, folder is just an outline
# directory is used for the File Browser, where more contrast with the file
# icons are needed.


def folder():
    """Folder icon"""
    return from_theme('folder', fallback='folder.svg')


def directory():
    """Directory icon"""
    return from_theme('folder', fallback='file-directory.svg')


def diff():
    """Diff icon"""
    return icon('diff.svg')


def edit():
    """Edit icon"""
    return from_theme('document-edit', fallback='pencil.svg')


def ellipsis():
    """Ellipsis icon"""
    return icon('ellipsis.svg')


def external():
    """External link icon"""
    return icon('link-external.svg')


def file_code():
    """Code file icon"""
    return icon('file-code.svg')


def file_text():
    """Text file icon"""
    return icon('file-text.svg')


def file_zip():
    """Zip file / tarball icon"""
    return icon('file-zip.svg')


def fold():
    """Fold icon"""
    return icon('fold.svg')


def merge():
    """Merge icon"""
    return icon('git-merge.svg')


def modified():
    """Modified icon"""
    return icon('modified.svg')


def modified_name():
    """Modified icon name"""
    return name_from_basename('modified.svg')


def move_down():
    """Move down icon"""
    return from_theme('go-next', fallback='arrow-down.svg')


def move_up():
    """Move up icon"""
    return from_theme('go-previous', fallback='arrow-up.svg')


def new():
    """Add new/add-to-list icon"""
    return from_theme('list-add', fallback='folder-new.svg')


def ok():
    """Ok/accept icon"""
    return from_theme('checkmark', fallback='check.svg')


def open_directory():
    """Open directory icon"""
    return from_theme('folder', fallback='folder.svg')


def up():
    """Previous icon"""
    return icon('arrow-up.svg')


def down():
    """Go to next item icon"""
    return icon('arrow-down.svg')


def partial_name():
    """Partial icon name"""
    return name_from_basename('partial.svg')


def pull():
    """Pull icon"""
    return icon('repo-pull.svg')


def push():
    """Push icon"""
    return icon('repo-push.svg')


def question():
    """Question icon"""
    return icon('question.svg')


def remove():
    """Remove icon"""
    return from_theme('list-remove', fallback='circle-slash.svg')


def repo():
    """Repository icon"""
    return icon('repo.svg')


def reverse_chronological():
    """Reverse chronological icon"""
    return icon('last-first-order.svg')


def save():
    """Save icon"""
    return from_theme('document-save', fallback='desktop-download.svg')


def search():
    """Search icon"""
    return from_theme('search', fallback='search.svg')


def select_all():
    """Select all icon"""
    return from_theme('edit-select-all', fallback='edit-select-all')


def staged():
    """Staged icon"""
    return icon('staged.svg')


def staged_name():
    """Staged icon name"""
    return name_from_basename('staged.svg')


def star():
    """Star icon"""
    return icon('star.svg')


def sync():
    """Sync/update icon"""
    return icon('sync.svg')


def tag():
    """Tag icon"""
    return icon('tag.svg')


def terminal():
    """Terminal icon"""
    return icon('terminal.svg')


def undo():
    """Undo icon"""
    return from_theme('edit-undo', fallback='edit-undo.svg')


def redo():
    """Redo icon"""
    return from_theme('edit-redo', fallback='edit-redo.svg')


def style_dialog_apply():
    """Apply icon from the current style"""
    return from_style(QtWidgets.QStyle.SP_DialogApplyButton)


def style_dialog_discard():
    """Discard icon for the current style"""
    return from_style(QtWidgets.QStyle.SP_DialogDiscardButton)


def style_dialog_reset():
    """Reset icon for the current style"""
    return from_style(QtWidgets.QStyle.SP_DialogResetButton)


def unfold():
    """Expand/unfold icon"""
    return icon('unfold.svg')


def visualize():
    """An eye icon to represent visualization"""
    return icon('eye.svg')


def upstream_name():
    """Upstream branch icon name"""
    return name_from_basename('upstream.svg')


def zoom_fit_best():
    """Zoom-to-fit icon"""
    return from_theme('zoom-fit-best', fallback='zoom-fit-best.svg')


def zoom_in():
    """Zoom-in icon"""
    return from_theme('zoom-in', fallback='zoom-in.svg')


def zoom_out():
    """Zoom-out icon"""
    return from_theme('zoom-out', fallback='zoom-out.svg')