Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customizableplot.available_font_families: Fix for non-existent default family #5037

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Orange/widgets/visualize/utils/customizableplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ def available_font_families() -> List:
if not QApplication.instance():
_ = QApplication(sys.argv)
fonts = QFontDatabase().families()
default = fonts.pop(fonts.index(default_font_family()))
default = default_font_family()

defaults = [default]
if default in fonts:
fonts.remove(default)

guessed_name = default.split()[0]
i = 0
Expand Down Expand Up @@ -263,7 +266,7 @@ def update_axis(axis, **settings):
self.getAxis(axis), settings[self.TITLE_LABEL])

self.FONT_FAMILY_SETTING: SettingsType = { # pylint: disable=invalid-name
Updater.FONT_FAMILY_LABEL: (available_font_families(), QFont().family()),
Updater.FONT_FAMILY_LABEL: (available_font_families(), default_font_family()),
}

self.FONT_SETTING: SettingsType = { # pylint: disable=invalid-name
Expand Down
38 changes: 38 additions & 0 deletions Orange/widgets/visualize/utils/tests/test_customizableplot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 regular")

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

db.return_value = Mock()
db.return_value.families = Mock(
return_value=["a", ".d", "e", ".b", "mock regular",
"mock bold", "mock italic", "c", "mock semi"])
self.assertEqual(customizableplot.available_font_families(),
["mock regular", "mock bold", "mock italic",
"mock semi", "",
"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(),
["mock regular", "", "a", ".b", "c", ".d", "e"])
self.assertIn(customizableplot.default_font_family(),
customizableplot.available_font_families())

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