Hip shot: try making low dpi font sizes "just work"

readme_bumpz
Tyler Goodlet 2021-03-29 16:01:05 -04:00
parent 8c39ff307e
commit 3bd87caf4b
5 changed files with 44 additions and 46 deletions

View File

@ -170,10 +170,10 @@ class AxisLabel(pg.GraphicsObject):
parent: pg.GraphicsItem, parent: pg.GraphicsItem,
digits: int = 2, digits: int = 2,
font_size_inches: Optional[float] = None,
bg_color: str = 'bracket', bg_color: str = 'bracket',
fg_color: str = 'black', fg_color: str = 'black',
opacity: int = 1, # XXX: seriously don't set this to 0 opacity: int = 1, # XXX: seriously don't set this to 0
font_size: str = 'default',
use_arrow: bool = True, use_arrow: bool = True,
@ -195,7 +195,7 @@ class AxisLabel(pg.GraphicsObject):
self._txt_br: QtCore.QRect = None self._txt_br: QtCore.QRect = None
self._dpifont = DpiAwareFont(size_in_inches=font_size_inches) self._dpifont = DpiAwareFont(font_size=font_size)
self._dpifont.configure_to_dpi() self._dpifont.configure_to_dpi()
self.bg_color = pg.mkColor(hcolor(bg_color)) self.bg_color = pg.mkColor(hcolor(bg_color))

View File

@ -27,10 +27,7 @@ from PyQt5.QtCore import QPointF
from .._annotate import mk_marker, qgo_draw_markers from .._annotate import mk_marker, qgo_draw_markers
from .._label import Label, vbr_left, right_axis from .._label import Label, vbr_left, right_axis
from .._style import ( from .._style import hcolor
hcolor,
_down_2_font_inches_we_like,
)
# TODO: probably worth investigating if we can # TODO: probably worth investigating if we can
@ -157,7 +154,6 @@ class LevelLine(pg.InfiniteLine):
side_of_axis: str = 'left', side_of_axis: str = 'left',
x_offset: float = 0, x_offset: float = 0,
font_size_inches: float = _down_2_font_inches_we_like,
color: str = None, color: str = None,
bg_color: str = None, bg_color: str = None,
avoid_book: bool = True, avoid_book: bool = True,
@ -535,9 +531,6 @@ def level_line(
level: float, level: float,
color: str = 'default', color: str = 'default',
# size 4 font on 4k screen scaled down, so small-ish.
font_size_inches: float = _down_2_font_inches_we_like,
# whether or not the line placed in view should highlight # whether or not the line placed in view should highlight
# when moused over (aka "hovered") # when moused over (aka "hovered")
hl_on_hover: bool = True, hl_on_hover: bool = True,
@ -682,21 +675,21 @@ def order_line(
llabel.show() llabel.show()
else: else:
# left side label # # left side label
llabel = line.add_label( # llabel = line.add_label(
side='left', # side='left',
fmt_str=' {exec_type}-{order_type}:\n ${$value}', # fmt_str=' {exec_type}-{order_type}:\n ${$value}',
) # )
llabel.fields = { # llabel.fields = {
'order_type': order_type, # 'order_type': order_type,
'level': level, # 'level': level,
'$value': lambda f: f['level'] * f['size'], # '$value': lambda f: f['level'] * f['size'],
'size': size, # 'size': size,
'exec_type': exec_type, # 'exec_type': exec_type,
} # }
llabel.orient_v = orient_v # llabel.orient_v = orient_v
llabel.render() # llabel.render()
llabel.show() # llabel.show()
# right before L1 label # right before L1 label
rlabel = line.add_label( rlabel = line.add_label(
@ -770,7 +763,7 @@ def position_line(
vb.sigYRangeChanged.connect(update_pp_nav) vb.sigYRangeChanged.connect(update_pp_nav)
rlabel = line.add_label( rlabel = line.add_label(
side='left', side='right',
fmt_str='{direction}: {size} -> ${$:.2f}', fmt_str='{direction}: {size} -> ${$:.2f}',
) )
rlabel.fields = { rlabel.fields = {

View File

@ -25,10 +25,7 @@ from PyQt5 import QtCore, QtGui
from PyQt5.QtCore import QPointF from PyQt5.QtCore import QPointF
from ._axes import YAxisLabel from ._axes import YAxisLabel
from ._style import ( from ._style import hcolor
hcolor,
_down_2_font_inches_we_like,
)
class LevelLabel(YAxisLabel): class LevelLabel(YAxisLabel):
@ -248,7 +245,7 @@ class L1Labels:
chart: 'ChartPlotWidget', # noqa chart: 'ChartPlotWidget', # noqa
digits: int = 2, digits: int = 2,
size_digits: int = 3, size_digits: int = 3,
font_size_inches: float = _down_2_font_inches_we_like, font_size: str = 'small',
) -> None: ) -> None:
self.chart = chart self.chart = chart
@ -259,7 +256,7 @@ class L1Labels:
'parent': raxis, 'parent': raxis,
'opacity': 1, 'opacity': 1,
'font_size_inches': font_size_inches, 'font_size': font_size,
'fg_color': chart.pen_color, 'fg_color': chart.pen_color,
'bg_color': chart.view_color, 'bg_color': chart.view_color,
} }

View File

@ -28,7 +28,6 @@ from PyQt5.QtCore import QPointF, QRectF
from ._style import ( from ._style import (
DpiAwareFont, DpiAwareFont,
hcolor, hcolor,
_down_2_font_inches_we_like,
) )
@ -108,7 +107,7 @@ class Label:
fmt_str: str, fmt_str: str,
color: str = 'bracket', color: str = 'bracket',
x_offset: float = 0, x_offset: float = 0,
font_size_inches: float = _down_2_font_inches_we_like, font_size: str = 'small',
opacity: float = 0.666, opacity: float = 0.666,
fields: dict = {} fields: dict = {}
@ -125,7 +124,7 @@ class Label:
# configure font size based on DPI # configure font size based on DPI
dpi_font = DpiAwareFont( dpi_font = DpiAwareFont(
size_in_inches=font_size_inches font_size=font_size,
) )
dpi_font.configure_to_dpi() dpi_font.configure_to_dpi()
txt.setFont(dpi_font.font) txt.setFont(dpi_font.font)

View File

@ -17,7 +17,7 @@
""" """
Qt UI styling. Qt UI styling.
""" """
from typing import Optional from typing import Optional, Dict
import math import math
import pyqtgraph as pg import pyqtgraph as pg
@ -30,11 +30,16 @@ from ._exec import current_screen
log = get_logger(__name__) log = get_logger(__name__)
# chart-wide fonts specified in inches # chart-wide fonts specified in inches
_default_font_inches_we_like_low_dpi = 6 / 64 _font_sizes: Dict[str, Dict[str, float]] = {
_down_2_font_inches_we_like_low_dpi = 4 / 64 'hi': {
'default': 0.0616,
_default_font_inches_we_like = 0.0616 # 5 / 96 'small': 0.055,
_down_2_font_inches_we_like = 0.055 # 4 / 96 },
'lo': {
'default': 6.5 / 64,
'small': 6 / 64,
},
}
class DpiAwareFont: class DpiAwareFont:
@ -42,11 +47,13 @@ class DpiAwareFont:
self, self,
# TODO: move to config # TODO: move to config
name: str = 'Hack', name: str = 'Hack',
size_in_inches: Optional[float] = None, font_size: str = 'default',
# size_in_inches: Optional[float] = None,
) -> None: ) -> None:
self.name = name self.name = name
self._qfont = QtGui.QFont(name) self._qfont = QtGui.QFont(name)
self._iwl = size_in_inches or _default_font_inches_we_like # self._iwl = size_in_inches or _default_font_inches_we_like
self._font_size: str = font_size
self._qfm = QtGui.QFontMetrics(self._qfont) self._qfm = QtGui.QFontMetrics(self._qfont)
self._physical_dpi = None self._physical_dpi = None
self._screen = None self._screen = None
@ -91,10 +98,12 @@ class DpiAwareFont:
dpi = max(pdpi, ldpi) dpi = max(pdpi, ldpi)
# for low dpi scale everything down # for low dpi scale everything down
if dpi <= 96: if dpi <= 97:
self._iwl = _default_font_inches_we_like_low_dpi inches = _font_sizes['lo'][self._font_size]
else:
inches = _font_sizes['hi'][self._font_size]
font_size = math.floor(self._iwl * dpi) font_size = math.floor(inches * dpi)
log.info( log.info(
f"\nscreen:{screen.name()} with DPI: {dpi}" f"\nscreen:{screen.name()} with DPI: {dpi}"
f"\nbest font size is {font_size}\n" f"\nbest font size is {font_size}\n"