GraphIcons: Extend and deprecate previous pixmaps (#1534)

* GraphPixmaps: deprecate pixmaps

* Deprecation print

* More doc strings and deprecation warning
This commit is contained in:
Dennis Göries 2021-02-06 06:09:59 +01:00 committed by GitHub
parent c2daa00714
commit bac2ff5b4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 49 additions and 56 deletions

View File

@ -5,7 +5,7 @@ import weakref
import numpy as np
import os
from ...Qt import QtGui, QtCore, QT_LIB
from ... import pixmaps
from ... import icons
from ... import functions as fn
from ...widgets.FileDialog import FileDialog
from .. PlotDataItem import PlotDataItem
@ -120,7 +120,7 @@ class PlotItem(GraphicsWidget):
## Set up control buttons
path = os.path.dirname(__file__)
self.autoBtn = ButtonItem(pixmaps.getPixmap('auto'), 14, self)
self.autoBtn = ButtonItem(icons.getGraphPixmap('auto'), 14, self)
self.autoBtn.mode = 'auto'
self.autoBtn.clicked.connect(self.autoBtnClicked)
self.buttonsHidden = False ## whether the user has requested buttons to be hidden

View File

@ -1,25 +1,43 @@
import os.path as op
import warnings
from ..Qt import QtGui
__all__ = ['getGraphIcon']
__all__ = ['getGraphIcon', 'getGraphPixmap']
_ICON_REGISTRY = {}
class GraphIcon:
"""An icon place holder for lazy loading of QIcons"""
"""An icon place holder for lazy loading of QIcons
The icon must reside in the icons folder and the path refers to the full
name including suffix of the icon file, e.g.:
tiny = GraphIcon("tiny.png")
Icons can be later retrieved via the function `getGraphIcon` and providing
the name:
tiny = getGraphIcon("tiny")
"""
def __init__(self, path):
self._path = path
self._icon = None
name = path.split('.')[0]
_ICON_REGISTRY[name] = self
self._icon = None
def _build_qicon(self):
icon = QtGui.QIcon(op.join(op.dirname(__file__), self._path))
name = self._path.split('.')[0]
_ICON_REGISTRY[name] = icon
self._icon = icon
@property
def qicon(self):
if self._icon is None:
self._icon = QtGui.QIcon(op.join(op.dirname(__file__), self._path))
self._build_qicon()
return self._icon
@ -34,5 +52,28 @@ def getGraphIcon(name):
return icon
def getGraphPixmap(name, size=(20, 20)):
"""Return a `QPixmap` from the registry by `name`"""
icon = getGraphIcon(name)
return icon.pixmap(*size)
def getPixmap(name, size=(20, 20)):
"""Historic `getPixmap` function
(eg. getPixmap('auto') loads pyqtgraph/icons/auto.png)
"""
warnings.warn(
"'getPixmap' is deprecated and will be removed soon, "
"please use `getGraphPixmap` in the future",
DeprecationWarning, stacklevel=2)
return getGraphPixmap(name, size=size)
# Note: List all graph icons here ...
auto = GraphIcon("auto.png")
ctrl = GraphIcon("ctrl.png")
default = GraphIcon("default.png")
invisibleEye = GraphIcon("invisibleEye.svg")
lock = GraphIcon("lock.png")

View File

Before

Width:  |  Height:  |  Size: 1022 B

After

Width:  |  Height:  |  Size: 1022 B

View File

Before

Width:  |  Height:  |  Size: 934 B

After

Width:  |  Height:  |  Size: 934 B

View File

Before

Width:  |  Height:  |  Size: 810 B

After

Width:  |  Height:  |  Size: 810 B

View File

Before

Width:  |  Height:  |  Size: 6.2 KiB

After

Width:  |  Height:  |  Size: 6.2 KiB

View File

Before

Width:  |  Height:  |  Size: 913 B

After

Width:  |  Height:  |  Size: 913 B

View File

@ -6,7 +6,7 @@ from .ParameterItem import ParameterItem
from ..widgets.SpinBox import SpinBox
from ..widgets.ColorButton import ColorButton
from ..colormap import ColorMap
from .. import pixmaps as pixmaps
from .. import icons as icons
from .. import functions as fn
from ..pgcollections import OrderedDict
@ -55,7 +55,7 @@ class WidgetParameterItem(ParameterItem):
self.defaultBtn.setFixedWidth(20)
self.defaultBtn.setFixedHeight(20)
modDir = os.path.dirname(__file__)
self.defaultBtn.setIcon(QtGui.QIcon(pixmaps.getPixmap('default')))
self.defaultBtn.setIcon(icons.getGraphIcon('default'))
self.defaultBtn.clicked.connect(self.defaultClicked)
self.displayLabel = QtGui.QLabel()

View File

@ -1,27 +0,0 @@
"""
Allows easy loading of pixmaps used in UI elements.
Provides support for frozen environments as well.
"""
import os, sys, pickle
from ..functions import makeQImage
from ..Qt import QtGui
from ..python2_3 import basestring
if sys.version_info[0] == 2:
from . import pixmapData_2 as pixmapData
else:
from . import pixmapData_3 as pixmapData
def getPixmap(name):
"""
Return a QPixmap corresponding to the image file with the given name.
(eg. getPixmap('auto') loads pyqtgraph/pixmaps/auto.png)
"""
key = name+'.png'
data = pixmapData.pixmapData[key]
if isinstance(data, basestring) or isinstance(data, bytes):
pixmapData.pixmapData[key] = pickle.loads(data)
arr = pixmapData.pixmapData[key]
return QtGui.QPixmap(makeQImage(arr, alpha=True))

View File

@ -1,19 +0,0 @@
# -*- coding: utf-8 -*-
import numpy as np
from PyQt4 import QtGui
import os, pickle, sys
path = os.path.abspath(os.path.split(__file__)[0])
pixmaps = {}
for f in os.listdir(path):
if not f.endswith('.png'):
continue
print(f)
img = QtGui.QImage(os.path.join(path, f))
ptr = img.bits()
ptr.setsize(img.byteCount())
arr = np.asarray(ptr).reshape(img.height(), img.width(), 4).transpose(1,0,2)
pixmaps[f] = pickle.dumps(arr)
ver = sys.version_info[0]
with open(os.path.join(path, 'pixmapData_%d.py' % (ver, )), 'w') as fh:
fh.write("import numpy as np; pixmapData=%s" % (repr(pixmaps), ))

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long