From 3bd87caf4b34c858157aabf0893332a37d562cf6 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 29 Mar 2021 16:01:05 -0400 Subject: [PATCH] Hip shot: try making low dpi font sizes "just work" --- piker/ui/_axes.py | 4 ++-- piker/ui/_graphics/_lines.py | 41 +++++++++++++++--------------------- piker/ui/_l1.py | 9 +++----- piker/ui/_label.py | 5 ++--- piker/ui/_style.py | 31 +++++++++++++++++---------- 5 files changed, 44 insertions(+), 46 deletions(-) diff --git a/piker/ui/_axes.py b/piker/ui/_axes.py index 72de9f48..18647e70 100644 --- a/piker/ui/_axes.py +++ b/piker/ui/_axes.py @@ -170,10 +170,10 @@ class AxisLabel(pg.GraphicsObject): parent: pg.GraphicsItem, digits: int = 2, - font_size_inches: Optional[float] = None, bg_color: str = 'bracket', fg_color: str = 'black', opacity: int = 1, # XXX: seriously don't set this to 0 + font_size: str = 'default', use_arrow: bool = True, @@ -195,7 +195,7 @@ class AxisLabel(pg.GraphicsObject): 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.bg_color = pg.mkColor(hcolor(bg_color)) diff --git a/piker/ui/_graphics/_lines.py b/piker/ui/_graphics/_lines.py index 63ca4f53..6277bc3b 100644 --- a/piker/ui/_graphics/_lines.py +++ b/piker/ui/_graphics/_lines.py @@ -27,10 +27,7 @@ from PyQt5.QtCore import QPointF from .._annotate import mk_marker, qgo_draw_markers from .._label import Label, vbr_left, right_axis -from .._style import ( - hcolor, - _down_2_font_inches_we_like, -) +from .._style import hcolor # TODO: probably worth investigating if we can @@ -157,7 +154,6 @@ class LevelLine(pg.InfiniteLine): side_of_axis: str = 'left', x_offset: float = 0, - font_size_inches: float = _down_2_font_inches_we_like, color: str = None, bg_color: str = None, avoid_book: bool = True, @@ -535,9 +531,6 @@ def level_line( level: float, 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 # when moused over (aka "hovered") hl_on_hover: bool = True, @@ -682,21 +675,21 @@ def order_line( llabel.show() else: - # left side label - llabel = line.add_label( - side='left', - fmt_str=' {exec_type}-{order_type}:\n ${$value}', - ) - llabel.fields = { - 'order_type': order_type, - 'level': level, - '$value': lambda f: f['level'] * f['size'], - 'size': size, - 'exec_type': exec_type, - } - llabel.orient_v = orient_v - llabel.render() - llabel.show() + # # left side label + # llabel = line.add_label( + # side='left', + # fmt_str=' {exec_type}-{order_type}:\n ${$value}', + # ) + # llabel.fields = { + # 'order_type': order_type, + # 'level': level, + # '$value': lambda f: f['level'] * f['size'], + # 'size': size, + # 'exec_type': exec_type, + # } + # llabel.orient_v = orient_v + # llabel.render() + # llabel.show() # right before L1 label rlabel = line.add_label( @@ -770,7 +763,7 @@ def position_line( vb.sigYRangeChanged.connect(update_pp_nav) rlabel = line.add_label( - side='left', + side='right', fmt_str='{direction}: {size} -> ${$:.2f}', ) rlabel.fields = { diff --git a/piker/ui/_l1.py b/piker/ui/_l1.py index 02e3a49a..683f46b9 100644 --- a/piker/ui/_l1.py +++ b/piker/ui/_l1.py @@ -25,10 +25,7 @@ from PyQt5 import QtCore, QtGui from PyQt5.QtCore import QPointF from ._axes import YAxisLabel -from ._style import ( - hcolor, - _down_2_font_inches_we_like, -) +from ._style import hcolor class LevelLabel(YAxisLabel): @@ -248,7 +245,7 @@ class L1Labels: chart: 'ChartPlotWidget', # noqa digits: int = 2, size_digits: int = 3, - font_size_inches: float = _down_2_font_inches_we_like, + font_size: str = 'small', ) -> None: self.chart = chart @@ -259,7 +256,7 @@ class L1Labels: 'parent': raxis, 'opacity': 1, - 'font_size_inches': font_size_inches, + 'font_size': font_size, 'fg_color': chart.pen_color, 'bg_color': chart.view_color, } diff --git a/piker/ui/_label.py b/piker/ui/_label.py index fe629c23..8f776279 100644 --- a/piker/ui/_label.py +++ b/piker/ui/_label.py @@ -28,7 +28,6 @@ from PyQt5.QtCore import QPointF, QRectF from ._style import ( DpiAwareFont, hcolor, - _down_2_font_inches_we_like, ) @@ -108,7 +107,7 @@ class Label: fmt_str: str, color: str = 'bracket', x_offset: float = 0, - font_size_inches: float = _down_2_font_inches_we_like, + font_size: str = 'small', opacity: float = 0.666, fields: dict = {} @@ -125,7 +124,7 @@ class Label: # configure font size based on DPI dpi_font = DpiAwareFont( - size_in_inches=font_size_inches + font_size=font_size, ) dpi_font.configure_to_dpi() txt.setFont(dpi_font.font) diff --git a/piker/ui/_style.py b/piker/ui/_style.py index a0232a08..f8f77950 100644 --- a/piker/ui/_style.py +++ b/piker/ui/_style.py @@ -17,7 +17,7 @@ """ Qt UI styling. """ -from typing import Optional +from typing import Optional, Dict import math import pyqtgraph as pg @@ -30,11 +30,16 @@ from ._exec import current_screen log = get_logger(__name__) # chart-wide fonts specified in inches -_default_font_inches_we_like_low_dpi = 6 / 64 -_down_2_font_inches_we_like_low_dpi = 4 / 64 - -_default_font_inches_we_like = 0.0616 # 5 / 96 -_down_2_font_inches_we_like = 0.055 # 4 / 96 +_font_sizes: Dict[str, Dict[str, float]] = { + 'hi': { + 'default': 0.0616, + 'small': 0.055, + }, + 'lo': { + 'default': 6.5 / 64, + 'small': 6 / 64, + }, +} class DpiAwareFont: @@ -42,11 +47,13 @@ class DpiAwareFont: self, # TODO: move to config name: str = 'Hack', - size_in_inches: Optional[float] = None, + font_size: str = 'default', + # size_in_inches: Optional[float] = None, ) -> None: self.name = 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._physical_dpi = None self._screen = None @@ -91,10 +98,12 @@ class DpiAwareFont: dpi = max(pdpi, ldpi) # for low dpi scale everything down - if dpi <= 96: - self._iwl = _default_font_inches_we_like_low_dpi + if dpi <= 97: + 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( f"\nscreen:{screen.name()} with DPI: {dpi}" f"\nbest font size is {font_size}\n"