summaryrefslogtreecommitdiff
path: root/lib/taurus/qt/qtgui/resource/taurus_resource_utils.py
blob: 20af909d5b98c7f7167d3b57410412924b1aa61a (plain)
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
#!/usr/bin/env python

# ###########################################################################
#
# This file is part of Taurus
#
# http://taurus-scada.org
#
# Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
#
# Taurus is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Taurus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Taurus.  If not, see <http://www.gnu.org/licenses/>.
#
# ###########################################################################

"""This module provides widgets that display the database in a tree format
"""

__all__ = [
    "getPixmap",
    "getIcon",
    "getThemePixmap",
    "getThemeIcon",
    "getThemeMembers",
    # the following are actually from taurus.qt.qtgui.icon
    "getStandardIcon",
    "getElementTypeToolTip",
    "getElementTypeSize",
    "getElementTypeIcon",
    "getElementTypeIconName",
    "getElementTypePixmap",
    "getDevStateToolTip",
    "getDevStateIcon",
    "getDevStatePixmap",
]

__docformat__ = "restructuredtext"

import os

from taurus.external.qt import Qt

from taurus.core.util.log import (
    deprecated,
    taurus4_deprecation,
    deprecation_decorator,
)

from taurus.qt.qtgui.icon import *  # noqa: F403,F401


@deprecation_decorator(
    alt="QIcon.hasThemeIcon to test individual names", rel="4.0"
)
def getThemeMembers():
    """Returns the current icon theme elements

    .. note:: Since its depredation, it returns an empty dict (there is no
    reasonable way of introspecting the list of available icon names).
    Alternatively Just test a given name using

    :return: the current icon theme elements in a dictionary where each key is
        a group name and the value is a sequence of theme icon name.
    :rtype: dict<str,seq<str>>
    """
    return {}


def getPixmap(key, size=None):
    # handle resource syntax (deprecated)
    if key.startswith(":"):
        head, tail = os.path.split(key[1:])
        # logos used to be in the resource root. Now they are in 'logos'
        prefix = sanitizePrefix(head or "logos")
        alt = 'getCachedPixmap("%s:%s [, size]")' % (prefix, tail)
        ret = getCachedPixmap("%s:%s" % (prefix, tail), size)
    deprecated(dep='getPixmap("%s" [, size])' % key, alt=alt, rel="4.0")
    return ret


def getIcon(key):
    """Returns a PyQt5.Qt.QIcon object for the given key. It supports QDir's
    searchPath prefixes (see :meth:`QDir.setSearchPaths`).
    Note that taurus.qt.qtgui.resource already sets several search paths based
    on .path files

    :param key: the pixmap file name. (optionally with a prefix)
    :type key: str
    :return: a PyQt5.Qt.QIcon for the given key
    :rtype: PyQt5.Qt.QIcon
    """

    # handle resource syntax (deprecated)
    if key.startswith(":"):
        head, tail = os.path.split(key[1:])
        # logos used to be in the resource root. Now they are in 'logos'
        prefix = sanitizePrefix(head or "logos")
        alt = 'Qt.QIcon("%s:%s")' % (prefix, tail)
        ret = Qt.QIcon("%s:%s" % (prefix, tail))
    elif not Qt.QFile.exists(key) and Qt.QIcon.hasThemeIcon(key):
        alt = 'QIcon.fromTheme("%s")' % key
        ret = Qt.QIcon.fromTheme(key)
    else:
        alt = 'QIcon("%s")' % key
        ret = Qt.QIcon(key)
    deprecated(dep='getIcon("%s")' % key, alt=alt, rel="4.0")
    return ret


@deprecation_decorator(
    alt="QIcon.fromTheme(key).pixmap(size, size)", rel="4.0"
)
def getThemePixmap(key, size=48):
    """Returns a PyQt5.Qt.QPixmap object for the given key and size.
    Key should be a valid theme icon key. See :meth:`PyQt5.Qt.QIcon.fromTheme`.

    Note that if the OS does not define a theme, taurus.qt.qtgui.resource will
    use the bundled 'Tango' icons theme. See:
    `Tango Icon Library <http://tango.freedesktop.org/Tango_Icon_Library>`_.

    If the key cannot be found, it will return a null content Pixmap.

    :param key: a string with the pixmap theme key (ex.: 'folder_open')
    :type key: str
    :param size: the pixmap size in pixels (will get a square pixmap). Default
        size=48
    :type size: int
    :return:
    :rtype: PyQt5.Qt.QPixmap
    """
    return Qt.QIcon.fromTheme(key).pixmap(size, size)


@deprecation_decorator(alt="QIcon.fromTheme", rel="4.0")
def getThemeIcon(key):
    """Returns the theme icon corresponding to the given key.
    Key should be a valid theme icon key. See :meth:`PyQt5.Qt.QIcon.fromTheme`.

    Note that if the OS does not define a theme, taurus.qt.qtgui.resource will
    use the bundled 'Tango' icons theme. See:
    `Tango Icon Library <http://tango.freedesktop.org/Tango_Icon_Library>`_.

    :param key: a string with the icon theme key (e.g.: 'folder_open')
    :type key: str
    :return: a PyQt5.Qt.QIcon for the given theme key
    :rtype: PyQt5.Qt.QIcon
    """
    return Qt.QIcon.fromTheme(key)


@taurus4_deprecation(alt="getDevStateToolTip")
def getSWDevHealthToolTip(state):
    return getDevStateToolTip(state)


@taurus4_deprecation(alt="getDevStateIcon")
def getSWDevHealthIcon(state, fallback=None):
    return getDevStateIcon(state, fallback=fallback)


@taurus4_deprecation(alt="getDevStatePixmap")
def getSWDevHealthPixmap(state, fallback=None):
    return getDevStatePixmap(state, fallback=fallback)


if __name__ == "__main__":

    import sys
    from taurus.qt.qtgui.application import TaurusApplication

    app = TaurusApplication(cmd_line_parser=None)

    themekey = "computer"
    b = Qt.QIcon.fromTheme(themekey)

    icons = [
        getIcon("actions:edit-cut.svg"),
        getIcon(":/actions/edit-cut.svg"),
        getIcon(":/apps/preferences-system-session.svg"),
        getIcon(":/designer/devs_tree.png"),
        getIcon(":/actions/process-stop.svg"),  # from tango-icons/actions
        getIcon(":/actions/add.svg"),  # from rrze-icons/actions
        getIcon(":/actions/stop.svg"),  # from extra-icons/actions
        getIcon(":taurus.svg"),
        getIcon("computer"),  # theme Icon via getIcon
        getThemeIcon("computer"),  # theme Icon via getThemeIcon
    ]

    w = Qt.QWidget()
    lyt = Qt.QVBoxLayout()
    w.setLayout(lyt)
    for icon in icons:
        button = Qt.QPushButton(icon, "kk")
        lyt.addWidget(button)

    w.show()

    sys.exit(app.exec_())