Skip to content

Commit

Permalink
customizableplot.available_font_families: Fix for non-existent defaul…
Browse files Browse the repository at this point in the history
…t family
  • Loading branch information
janezd committed Oct 17, 2020
1 parent ac714e7 commit b0e4a4f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Orange/widgets/visualize/utils/customizableplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ def available_font_families() -> List:
if not QApplication.instance():
_ = QApplication(sys.argv)
fonts = QFontDatabase().families()
default = fonts.pop(fonts.index(default_font_family()))

default_family = default_font_family()
if default_family not in fonts:
return sorted(fonts, key=lambda s: s.replace(".", ""))

default = fonts.pop(fonts.index(default_family))
defaults = [default]

guessed_name = default.split()[0]
Expand Down
28 changes: 28 additions & 0 deletions Orange/widgets/visualize/utils/tests/test_customizableplot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import unittest
from unittest.mock import patch, Mock

from Orange.widgets.visualize.utils import customizableplot


class TestFonts(unittest.TestCase):
def test_available_font_families(self):
with patch.object(customizableplot, "QFont") as font, \
patch.object(customizableplot, "QFontDatabase") as db:
font.return_value = Mock()
font.return_value.family = Mock(return_value = "mock family")

db.return_value = Mock()
db.return_value.families = Mock(
return_value=["a", ".d", "e", ".b", "mock family", "c"])
self.assertEqual(customizableplot.available_font_families(),
["mock family", "", "a", ".b", "c", ".d", "e"])

# It seems it's possible that default font family does not exist
# (see gh-5036)
db.return_value.families.return_value = ["a", ".d", "e", ".b", "c"]
self.assertEqual(customizableplot.available_font_families(),
["a", ".b", "c", ".d", "e"])


if __name__ == "__main__":
unittest.main()

0 comments on commit b0e4a4f

Please sign in to comment.