| 1 |
r""" |
| 2 |
qtawesome |
| 3 |
========= |
| 4 |
|
| 5 |
Font-Awesome and other iconic fonts for PyQt / PySide applications. |
| 6 |
|
| 7 |
.. currentmodule:: qtawesome |
| 8 |
|
| 9 |
.. autosummary:: |
| 10 |
:toctree: _generate |
| 11 |
|
| 12 |
get_fonts_info |
| 13 |
install_bundled_fonts_system_wide |
| 14 |
icon |
| 15 |
load_font |
| 16 |
charmap |
| 17 |
font |
| 18 |
set_defaults |
| 19 |
""" |
| 20 |
|
| 21 |
import hashlib |
| 22 |
import os |
| 23 |
|
| 24 |
|
| 25 |
from qtpy import QtCore, QtWidgets, QtGui |
| 26 |
|
| 27 |
|
| 28 |
from ._version import __version__ as __version__, version_info as version_info |
| 29 |
from .animation import Pulse as Pulse, Spin as Spin |
| 30 |
from .iconic_font import IconicFont, set_global_defaults, FontError |
| 31 |
from .iconic_font import SYSTEM_FONTS as _SYSTEM_FONTS |
| 32 |
from .styles import dark as dark, light as light |
| 33 |
|
| 34 |
|
| 35 |
_resource = {"iconic": None} |
| 36 |
|
| 37 |
_BUNDLED_FONTS = ( |
| 38 |
( |
| 39 |
"fa5", |
| 40 |
"fontawesome5-regular-webfont-5.15.4.ttf", |
| 41 |
"fontawesome5-regular-webfont-charmap-5.15.4.json", |
| 42 |
), |
| 43 |
( |
| 44 |
"fa5s", |
| 45 |
"fontawesome5-solid-webfont-5.15.4.ttf", |
| 46 |
"fontawesome5-solid-webfont-charmap-5.15.4.json", |
| 47 |
), |
| 48 |
( |
| 49 |
"fa5b", |
| 50 |
"fontawesome5-brands-webfont-5.15.4.ttf", |
| 51 |
"fontawesome5-brands-webfont-charmap-5.15.4.json", |
| 52 |
), |
| 53 |
( |
| 54 |
"fa6", |
| 55 |
"fontawesome6-regular-webfont-6.7.2.ttf", |
| 56 |
"fontawesome6-regular-webfont-charmap-6.7.2.json", |
| 57 |
), |
| 58 |
( |
| 59 |
"fa6s", |
| 60 |
"fontawesome6-solid-webfont-6.7.2.ttf", |
| 61 |
"fontawesome6-solid-webfont-charmap-6.7.2.json", |
| 62 |
), |
| 63 |
( |
| 64 |
"fa6b", |
| 65 |
"fontawesome6-brands-webfont-6.7.2.ttf", |
| 66 |
"fontawesome6-brands-webfont-charmap-6.7.2.json", |
| 67 |
), |
| 68 |
("ei", "elusiveicons-webfont-2.0.ttf", "elusiveicons-webfont-charmap-2.0.json"), |
| 69 |
( |
| 70 |
"mdi", |
| 71 |
"materialdesignicons5-webfont-5.9.55.ttf", |
| 72 |
"materialdesignicons5-webfont-charmap-5.9.55.json", |
| 73 |
), |
| 74 |
( |
| 75 |
"mdi6", |
| 76 |
"materialdesignicons6-webfont-6.9.96.ttf", |
| 77 |
"materialdesignicons6-webfont-charmap-6.9.96.json", |
| 78 |
), |
| 79 |
("ph", "phosphor-1.3.0.ttf", "phosphor-charmap-1.3.0.json"), |
| 80 |
("ri", "remixicon-2.5.0.ttf", "remixicon-charmap-2.5.0.json"), |
| 81 |
("msc", "codicon-0.0.36.ttf", "codicon-charmap-0.0.36.json"), |
| 82 |
) |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
_MD5_HASHES = { |
| 87 |
"fontawesome5-regular-webfont-5.15.4.ttf": "dc47e4089f5bcb25f241bdeb2de0fb58", |
| 88 |
"fontawesome5-solid-webfont-5.15.4.ttf": "5de19800fc9ae73438c2e5c61d041b48", |
| 89 |
"fontawesome5-brands-webfont-5.15.4.ttf": "513aa607d398efaccc559916c3431403", |
| 90 |
"fontawesome6-regular-webfont-6.7.2.ttf": "2b9e6cb53822f6a9b42f15229a36811a", |
| 91 |
"fontawesome6-solid-webfont-6.7.2.ttf": "07312c769a05b2c17133da1a09db4ccf", |
| 92 |
"fontawesome6-brands-webfont-6.7.2.ttf": "15d54d142da2f2d6f2e90ed1d55121af", |
| 93 |
"elusiveicons-webfont-2.0.ttf": "207966b04c032d5b873fd595a211582e", |
| 94 |
"materialdesignicons5-webfont-5.9.55.ttf": "b7d40e7ef80c1d4af6d94902af66e524", |
| 95 |
"materialdesignicons6-webfont-6.9.96.ttf": "ecaabfbb23fdac4ddbaf897b97257a92", |
| 96 |
"phosphor-1.3.0.ttf": "5b8dc57388b2d86243566b996cc3a789", |
| 97 |
"remixicon-2.5.0.ttf": "888e61f04316f10bddfff7bee10c6dd0", |
| 98 |
"codicon-0.0.36.ttf": "5fcbaf4e245dde222bd74022e5c50bc7", |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
def get_fonts_info(): |
| 103 |
""" |
| 104 |
Return tuple with information about the bundled fonts being used. |
| 105 |
|
| 106 |
Notes |
| 107 |
----- |
| 108 |
The tuple contains the fonts directory path and a list of font filenames. |
| 109 |
""" |
| 110 |
fonts_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), "fonts") |
| 111 |
return ( |
| 112 |
fonts_directory, |
| 113 |
[entry for entry in os.listdir(fonts_directory) if entry.endswith(".ttf")], |
| 114 |
) |
| 115 |
|
| 116 |
|
| 117 |
def install_bundled_fonts_system_wide(): |
| 118 |
_instance().install_fonts_system_wide() |
| 119 |
|
| 120 |
|
| 121 |
def has_valid_font_ids(inst): |
| 122 |
"""Validate instance's font ids are loaded to QFontDatabase. |
| 123 |
|
| 124 |
It is possible that QFontDatabase was reset or QApplication was recreated |
| 125 |
in both cases it is possible that font is not available. |
| 126 |
""" |
| 127 |
|
| 128 |
for font_id in inst.fontids.values(): |
| 129 |
font_families = QtGui.QFontDatabase.applicationFontFamilies(font_id) |
| 130 |
if not font_families: |
| 131 |
return False |
| 132 |
return True |
| 133 |
|
| 134 |
|
| 135 |
def _instance(): |
| 136 |
""" |
| 137 |
Return the singleton instance of IconicFont. |
| 138 |
|
| 139 |
Functions ``icon``, ``load_font``, ``charmap``, ``font`` and |
| 140 |
``set_defaults`` all rebind to methods of the singleton instance of IconicFont. |
| 141 |
""" |
| 142 |
if _resource["iconic"] is not None and not has_valid_font_ids(_resource["iconic"]): |
| 143 |
|
| 144 |
_resource["iconic"] = None |
| 145 |
|
| 146 |
if _resource["iconic"] is None: |
| 147 |
|
| 148 |
if not _SYSTEM_FONTS: |
| 149 |
for fargs in _BUNDLED_FONTS: |
| 150 |
ttf_filename = fargs[1] |
| 151 |
ttf_hash = _MD5_HASHES.get(ttf_filename, None) |
| 152 |
if ttf_hash is None: |
| 153 |
continue |
| 154 |
ttf_filepath = os.path.join( |
| 155 |
os.path.dirname(os.path.realpath(__file__)), "fonts", ttf_filename |
| 156 |
) |
| 157 |
with open(ttf_filepath, "rb") as f: |
| 158 |
ttf_calculated_hash_code = hashlib.md5(f.read()).hexdigest() |
| 159 |
if ttf_calculated_hash_code != ttf_hash: |
| 160 |
raise FontError(f"Font is corrupt at: '{ttf_filepath}'") |
| 161 |
|
| 162 |
_resource["iconic"] = IconicFont(*_BUNDLED_FONTS) |
| 163 |
return _resource["iconic"] |
| 164 |
|
| 165 |
|
| 166 |
def reset_cache(): |
| 167 |
if _resource["iconic"] is not None: |
| 168 |
_resource["iconic"].icon_cache = {} |
| 169 |
|
| 170 |
|
| 171 |
def icon(*names, **kwargs): |
| 172 |
""" |
| 173 |
Return a QIcon object corresponding to the provided icon name(s). |
| 174 |
|
| 175 |
This function is the main interface of qtawesome. |
| 176 |
|
| 177 |
It can be used to create a QIcon instance from a single glyph |
| 178 |
or from a list of glyphs that are displayed on the top of each other. |
| 179 |
Such icon stacks are generally used to combine multiple glyphs to make |
| 180 |
more complex icons. |
| 181 |
|
| 182 |
Glyph names are specified by strings, of the form ``prefix.name``. |
| 183 |
The ``prefix`` corresponds to the font to be used and ``name`` is the |
| 184 |
name of the icon. |
| 185 |
|
| 186 |
- The prefix corresponding to Font-Awesome 6.x (regular) is 'fa6' |
| 187 |
- The prefix corresponding to Font-Awesome 6.x (solid) is 'fa6s' |
| 188 |
- The prefix corresponding to Font-Awesome 6.x (brands) is 'fa6b' |
| 189 |
- The prefix corresponding to Font-Awesome 5 (regular) is 'fa5' |
| 190 |
- The prefix corresponding to Font-Awesome 5 (solid) is 'fa5s' |
| 191 |
- The prefix corresponding to Font-Awesome 5 (brands) is 'fa5b' |
| 192 |
- The prefix corresponding to Elusive-Icons is 'ei' |
| 193 |
- The prefix corresponding to Material-Design-Icons 5.x is 'mdi' |
| 194 |
- The prefix corresponding to Material-Design-Icons 6.x is 'mdi6' |
| 195 |
- The prefix corresponding to Phosphor is 'ph' |
| 196 |
- The prefix corresponding to Remix-Icon is 'ri' |
| 197 |
- The prefix corresponding to Microsoft's Codicons is 'msc' |
| 198 |
|
| 199 |
When requesting a single glyph, options (such as color or positional offsets) |
| 200 |
can be passed as keyword arguments:: |
| 201 |
|
| 202 |
import qtawesome as qta |
| 203 |
|
| 204 |
music_icon = qta.icon( |
| 205 |
'fa5s.music', |
| 206 |
color='blue', |
| 207 |
color_active='orange') |
| 208 |
|
| 209 |
When requesting multiple glyphs, the `options` keyword argument contains |
| 210 |
the list of option dictionaries to be used for each glyph:: |
| 211 |
|
| 212 |
camera_ban = qta.icon('fa5s.camera', 'fa5s.ban', options=[{ |
| 213 |
'scale_factor': 0.5, |
| 214 |
'active': 'fa5s.balance-scale' |
| 215 |
}, { |
| 216 |
'color': 'red', |
| 217 |
'opacity': 0.7 |
| 218 |
}]) |
| 219 |
|
| 220 |
Qt's ``QIcon`` object has four modes |
| 221 |
|
| 222 |
- ``Normal``: The user is not interacting with the icon, but the |
| 223 |
functionality represented by the icon is available. |
| 224 |
- ``Disabled``: The functionality corresponding to the icon is not |
| 225 |
available. |
| 226 |
- ``Active``: The functionality corresponding to the icon is available. |
| 227 |
The user is interacting with the icon, for example, moving the mouse |
| 228 |
over it or clicking it. |
| 229 |
- ``Selected``: The item represented by the icon is selected. |
| 230 |
|
| 231 |
The glyph for the Normal mode is the one specified with the main positional |
| 232 |
argument. |
| 233 |
|
| 234 |
- ``color``: icon color in the ``Normal`` mode. |
| 235 |
|
| 236 |
The following four options will apply to the icon regardless of the mode. |
| 237 |
|
| 238 |
- ``offset``: tuple (x, y) corresponding to the horizontal and vertical |
| 239 |
offsets for the glyph, specified as a proportion of the icon size. |
| 240 |
- ``animation``: animation object for the icon. |
| 241 |
- ``scale_factor``: multiplicative scale factor to be used for the glyph. |
| 242 |
|
| 243 |
The following options apply to the different modes of the icon |
| 244 |
|
| 245 |
- ``active``: name of the glyph to be used when the icon is ``Active``. |
| 246 |
- ``color_active``: the corresponding icon color. |
| 247 |
|
| 248 |
- ``disabled``: name of the glyph to be used when the icon is ``Disabled``. |
| 249 |
- ``color_disabled``: the corresponding icon color. |
| 250 |
|
| 251 |
- ``selected``: name of the glyph to be used when the icon is ``Selected``. |
| 252 |
- ``color_selected``: the corresponding icon color. |
| 253 |
|
| 254 |
Default values for these options can be specified via the function |
| 255 |
``set_defaults``. If unspecified, the default defaults are:: |
| 256 |
|
| 257 |
{ |
| 258 |
'opacity': 1.0, |
| 259 |
'scale_factor': 1.0 |
| 260 |
'color': QColor(50, 50, 50), |
| 261 |
'color_disabled': QColor(150, 150, 150), |
| 262 |
} |
| 263 |
|
| 264 |
If no default value is provided for ``active``, ``disabled`` or ``selected`` |
| 265 |
the glyph specified for the ``Normal`` mode will be used. |
| 266 |
|
| 267 |
""" |
| 268 |
return _instance().icon(*names, **kwargs) |
| 269 |
|
| 270 |
|
| 271 |
def load_font(prefix, ttf_filename, charmap_filename, directory=None): |
| 272 |
""" |
| 273 |
Loads a font file and the associated charmap. |
| 274 |
|
| 275 |
If ``directory`` is passed, the files will be looked for in the qtawesome |
| 276 |
``fonts`` directory. |
| 277 |
|
| 278 |
Parameters |
| 279 |
---------- |
| 280 |
prefix: str |
| 281 |
Prefix string to be used when accessing a given font set |
| 282 |
ttf_filename: str |
| 283 |
Ttf font filename |
| 284 |
charmap_filename: str |
| 285 |
Character map filename |
| 286 |
directory: str or None, optional |
| 287 |
Directory path for font and charmap files |
| 288 |
|
| 289 |
Example |
| 290 |
------- |
| 291 |
If you want to load a font ``myicon.tff`` with a ``myicon-charmap.json`` |
| 292 |
charmap added to the qtawesome ``fonts`` directory (usually located at |
| 293 |
``</path/to/lib/python>/site-packages/qtawesome/fonts/``) you can use:: |
| 294 |
|
| 295 |
qta.load_font( |
| 296 |
'myicon', |
| 297 |
'myicon.ttf', |
| 298 |
'myicon-charmap.json' |
| 299 |
) |
| 300 |
|
| 301 |
However, if you want to load a font ``myicon.tff`` with a |
| 302 |
``myicon-charmap.json`` charmap located in a specific path outside the |
| 303 |
qtawesome ``font`` directory like for example ``/path/to/myproject/fonts`` |
| 304 |
you can use:: |
| 305 |
|
| 306 |
qta.load_font( |
| 307 |
'myicon', |
| 308 |
'myicon.ttf', |
| 309 |
'myicon-charmap.json', |
| 310 |
directory='/path/to/myproject/fonts' |
| 311 |
) |
| 312 |
|
| 313 |
""" |
| 314 |
return _instance().load_font(prefix, ttf_filename, charmap_filename, directory) |
| 315 |
|
| 316 |
|
| 317 |
def charmap(prefixed_name): |
| 318 |
""" |
| 319 |
Return the character map used for a given font. |
| 320 |
|
| 321 |
Returns |
| 322 |
------- |
| 323 |
return_value: dict |
| 324 |
The dictionary mapping the icon names to the corresponding unicode character. |
| 325 |
|
| 326 |
""" |
| 327 |
prefix, name = prefixed_name.split(".") |
| 328 |
return _instance().charmap[prefix][name] |
| 329 |
|
| 330 |
|
| 331 |
def font(prefix, size): |
| 332 |
""" |
| 333 |
Return the font corresponding to the specified prefix. |
| 334 |
|
| 335 |
This can be used to render text using the iconic font directly:: |
| 336 |
|
| 337 |
import qtawesome as qta |
| 338 |
from qtpy import QtWidgets |
| 339 |
|
| 340 |
label = QtWidgets.QLabel(unichr(0xf19c) + ' ' + 'Label') |
| 341 |
label.setFont(qta.font('fa', 16)) |
| 342 |
|
| 343 |
Parameters |
| 344 |
---------- |
| 345 |
prefix: str |
| 346 |
prefix string of the loaded font |
| 347 |
size: int |
| 348 |
size for the font |
| 349 |
|
| 350 |
""" |
| 351 |
return _instance().font(prefix, size) |
| 352 |
|
| 353 |
|
| 354 |
def set_defaults(**kwargs): |
| 355 |
""" |
| 356 |
Set default options for icons. |
| 357 |
|
| 358 |
The valid keyword arguments are: |
| 359 |
|
| 360 |
'active', 'animation', 'color', 'color_active', 'color_disabled', |
| 361 |
'color_selected', 'disabled', 'offset', 'scale_factor', 'selected'. |
| 362 |
|
| 363 |
""" |
| 364 |
return set_global_defaults(**kwargs) |
| 365 |
|
| 366 |
|
| 367 |
class IconWidget(QtWidgets.QLabel): |
| 368 |
""" |
| 369 |
IconWidget gives the ability to display an icon as a widget |
| 370 |
|
| 371 |
It supports the same arguments as `icon()`, |
| 372 |
for example, |
| 373 |
|
| 374 |
music_icon = qta.IconWidget('fa5s.music', |
| 375 |
color='blue', |
| 376 |
color_active='orange') |
| 377 |
|
| 378 |
The exceptions are `parent` and `size` keyword-only arguments, |
| 379 |
which allow setting the widget parent and initial size, correspondingly. |
| 380 |
|
| 381 |
It also has `setIcon()` and `setIconSize()` functions. |
| 382 |
""" |
| 383 |
|
| 384 |
def __init__(self, *names, **kwargs): |
| 385 |
super().__init__(parent=kwargs.get("parent")) |
| 386 |
self._icon = None |
| 387 |
self._size = kwargs.get("size", QtCore.QSize(16, 16)) |
| 388 |
self.setIcon(icon(*names, **kwargs)) |
| 389 |
|
| 390 |
def setIcon(self, _icon): |
| 391 |
""" |
| 392 |
set a new icon() |
| 393 |
|
| 394 |
Parameters |
| 395 |
---------- |
| 396 |
_icon: qtawesome.icon |
| 397 |
icon to set |
| 398 |
""" |
| 399 |
self._icon = _icon |
| 400 |
self.setPixmap(_icon.pixmap(self._size)) |
| 401 |
|
| 402 |
def setIconSize(self, size): |
| 403 |
""" |
| 404 |
set icon size |
| 405 |
|
| 406 |
Parameters |
| 407 |
---------- |
| 408 |
size: QtCore.QSize |
| 409 |
size of the icon |
| 410 |
""" |
| 411 |
self._size = size |
| 412 |
|
| 413 |
def update(self, *args, **kwargs): |
| 414 |
if self._icon: |
| 415 |
self.setPixmap(self._icon.pixmap(self._size)) |
| 416 |
return super().update(*args, **kwargs) |