File: gui_utils.py

package info (click to toggle)
qgis 3.40.10%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,183,672 kB
  • sloc: cpp: 1,595,771; python: 372,544; xml: 23,474; sh: 3,761; perl: 3,664; ansic: 2,257; sql: 2,137; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 161
file content (104 lines) | stat: -rw-r--r-- 3,086 bytes parent folder | download | duplicates (6)
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
# /***************************************************************************
#  *                                                                         *
#  *   This program is free software; you can redistribute it and/or modify  *
#  *   it under the terms of the GNU General Public License as published by  *
#  *   the Free Software Foundation; either version 2 of the License, or     *
#  *   (at your option) any later version.                                   *
#  *                                                                         *
#  ***************************************************************************/

"""
GUI Utilities
"""

import os
from typing import Optional, Dict

from qgis.PyQt.QtGui import QIcon, QImage, QPixmap


class GuiUtils:
    """
    Utilities for GUI plugin components
    """

    ICON_CACHE: dict[str, QIcon] = {}

    @staticmethod
    def get_icon(icon: str) -> QIcon:
        """
        Returns a plugin icon
        :param icon: icon name (base part of file name)
        :return: QIcon
        """
        if icon in GuiUtils.ICON_CACHE:
            return GuiUtils.ICON_CACHE[icon]

        # prefer SVG files if present
        path = GuiUtils.get_icon_svg(icon)
        if path:
            res = QIcon(path)
            GuiUtils.ICON_CACHE[icon] = res
            return res

        pixmap = GuiUtils.get_icon_as_pixmap(icon)
        if pixmap is not None:
            res = QIcon(pixmap)
            GuiUtils.ICON_CACHE[icon] = res
            return res

        # return an invalid icon
        GuiUtils.ICON_CACHE[icon] = QIcon()
        return QIcon()

    @staticmethod
    def get_icon_svg(icon: str) -> str:
        """
        Returns a plugin icon's SVG file path
        :param icon: icon name (base part of file name)
        :return: icon svg path
        """
        path = os.path.join(os.path.dirname(__file__), "icons", icon + ".svg")
        if not os.path.exists(path):
            return ""

        return path

    @staticmethod
    def get_pixmap_path(icon: str) -> Optional[str]:
        """
        Returns the path to a pixmap icon
        """
        for suffix in (".png", ".gif", ".xpm"):
            path = os.path.join(os.path.dirname(__file__), "icons", icon + suffix)
            if os.path.exists(path):
                return path

        return None

    @staticmethod
    def get_icon_as_pixmap(icon: str) -> Optional[QPixmap]:
        """
        Returns a plugin icon's PNG file path
        :param icon: icon name (png file name)
        :return: icon png path
        """
        path = GuiUtils.get_pixmap_path(icon)
        if path is not None:
            im = QImage(path)
            return QPixmap.fromImage(im)

        return None

    @staticmethod
    def get_ui_file_path(file: str) -> str:
        """
        Returns a UI file's path
        :param file: file name (uifile name)
        :return: ui file path
        """
        path = os.path.join(os.path.dirname(__file__), "ui", file)
        if not os.path.exists(path):
            return ""

        return path