-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create utils.py and the to_qfont() function
- Loading branch information
Showing
6 changed files
with
105 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
msl.qt.utils module | ||
=================== | ||
|
||
.. automodule:: msl.qt.utils | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,3 +68,4 @@ def application(*args): | |
DoubleSpinBox, | ||
SpinBox, | ||
) | ||
from . import utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
General helper functions. | ||
""" | ||
from . import QtGui | ||
|
||
|
||
def to_qfont(font): | ||
"""Convert the input argument into a :class:`QtGui.QFont` object. | ||
Parameters | ||
---------- | ||
font : :class:`int`, :class:`float`, :class:`str` or :class:`tuple` | ||
The value to convert to a :class:`QtGui.QFont`. | ||
* If :class:`int` or :class:`float` then the point size. | ||
* If :class:`str` then the font family name. | ||
* If :class:`tuple` then (family name, point size). | ||
Returns | ||
------- | ||
:class:`QtGui.QFont` | ||
The input `value` converted to a :class:`QtGui.QFont` object. | ||
""" | ||
if isinstance(font, QtGui.QFont): | ||
return font | ||
elif isinstance(font, int): | ||
f = QtGui.QFont() | ||
f.setPointSize(font) | ||
return f | ||
elif isinstance(font, float): | ||
f = QtGui.QFont() | ||
f.setPointSizeF(font) | ||
return f | ||
elif isinstance(font, str): | ||
return QtGui.QFont(font) | ||
elif isinstance(font, (tuple, list)): | ||
if len(font) < 2: | ||
raise ValueError('The font must be a (family name, point size) tuple') | ||
return QtGui.QFont(font[0], pointSize=int(font[1])) | ||
else: | ||
raise TypeError('Must specify a QFont object') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import pytest | ||
from msl.qt import QtGui, utils | ||
|
||
|
||
def test_to_qfont(): | ||
|
||
with pytest.raises(TypeError): | ||
utils.to_qfont(None) | ||
|
||
# the tuple/list must have length == 2 | ||
for obj in [(), [], (1,), [1]]: | ||
with pytest.raises(ValueError): | ||
utils.to_qfont(obj) | ||
|
||
font = QtGui.QFont() | ||
assert utils.to_qfont(font) is font | ||
|
||
font = utils.to_qfont(100) | ||
assert font.pointSize() == 100 | ||
|
||
font = utils.to_qfont(12.3) | ||
assert font.pointSize() == 12 | ||
assert font.pointSizeF() == 12.3 | ||
|
||
font = utils.to_qfont('Papyrus') | ||
assert font.family() == 'Papyrus' | ||
|
||
font = utils.to_qfont(('Ariel', 48)) | ||
assert font.family() == 'Ariel' | ||
assert font.pointSize() == 48 | ||
|
||
# the second item in the list will be cast to an integer | ||
# also test that one can pass in a tuple or a list | ||
for obj in [('Comic Sans MS', 36), ['Comic Sans MS', 36.6], ('Comic Sans MS', '36')]: | ||
font = utils.to_qfont(obj) | ||
assert font.family() == 'Comic Sans MS' | ||
assert font.pointSize() == 36 | ||
|
||
# the second item in the list cannot be cast to an integer | ||
with pytest.raises(ValueError): | ||
utils.to_qfont(['Ariel', 'xxx']) | ||
|
||
# the first item in the list must be a string | ||
for obj in [1, 23.4, 5j, True, [1]]: | ||
with pytest.raises(TypeError): | ||
utils.to_qfont((obj, 12)) |