diff --git a/lib/python/glnav.py b/lib/python/glnav.py index ff8dfa6da9a..658359d8672 100644 --- a/lib/python/glnav.py +++ b/lib/python/glnav.py @@ -251,6 +251,11 @@ def reset(self): self._totalx = 0.0 self._totaly = 0.0 + def winfo_scale(self): + """Device pixels per logical (mouse event) pixel. Widgets whose + winfo_width/winfo_height are in device pixels override this.""" + return 1.0 + def recordMouse(self, x, y): self.xmouse = x self.ymouse = y @@ -301,7 +306,8 @@ def translate(self, x, y): obj = unproject((win[0], win[1] + 0.5 * win_height, win[2]), self.modelview, projection, (0, 0, win_width, win_height)) dist = math.sqrt( v3distsq( obj, obj_c ) ) - scale = abs( dist / ( 0.5 * win_height ) ) + # winfo_* are device pixels, the mouse moves in logical pixels + scale = abs( dist / ( 0.5 * win_height ) ) * self.winfo_scale() glTranslateScene(self, scale, x, y, self.xmouse, self.ymouse) # keep track of all translations since view reset diff --git a/lib/python/qtvcp/lib/qt_vismach/primitives.py b/lib/python/qtvcp/lib/qt_vismach/primitives.py index 118211666f7..070d29a4003 100644 --- a/lib/python/qtvcp/lib/qt_vismach/primitives.py +++ b/lib/python/qtvcp/lib/qt_vismach/primitives.py @@ -6,7 +6,9 @@ from math import * # functions copied from glnav.py -def use_pango_font(font, start, count, will_call_prepost=False): +# scale: device pixels per logical pixel (HiDPI). The glyphs are rendered +# that much larger; the returned metrics stay in logical pixels. +def use_pango_font(font, start, count, will_call_prepost=False, scale=1.0): import gi gi.require_version('Pango','1.0') gi.require_version('PangoCairo','1.0') @@ -16,6 +18,11 @@ def use_pango_font(font, start, count, will_call_prepost=False): import cairo fontDesc = Pango.FontDescription(font) + if scale != 1.0: + if fontDesc.get_size_is_absolute(): + fontDesc.set_absolute_size(fontDesc.get_size() * scale) + else: + fontDesc.set_size(int(fontDesc.get_size() * scale)) a = array.array('b', itertools.repeat(0, 256*256)) surface = cairo.ImageSurface.create_for_data(a, cairo.FORMAT_A8, 256, 256) context = cairo.Context(surface) @@ -80,7 +87,24 @@ def use_pango_font(font, start, count, will_call_prepost=False): GL.glEndList() GL.glPopClientAttrib() - return base, int(width / Pango.SCALE), int(linespace / Pango.SCALE) + width, linespace = int(width / Pango.SCALE), int(linespace / Pango.SCALE) + if scale != 1.0: + width, linespace = ceil(width / scale), ceil(linespace / scale) + return base, width, linespace + +def hud_font(hud): + '''(Re)build a HUD's font for its window's scale factor (HiDPI) and + return that factor. The HUD is laid out in logical pixels; the window's + winfo_width/winfo_height are device pixels. Needs the GL context.''' + winfo_scale = getattr(hud.app, 'winfo_scale', None) + scale = winfo_scale() if winfo_scale else 1.0 + if not hud.fontbase or scale != getattr(hud, '_font_scale', None): + if hud.fontbase: + GL.glDeleteLists(hud.fontbase, 128) + hud.fontbase, hud._width, linespace = use_pango_font( + hud._font, 0, 128, scale=scale) + hud._font_scale = scale + return scale def pango_font_pre(rgba=(1., 1., 0., 1.)): GL.glPushAttrib(GL.GL_COLOR_BUFFER_BIT) @@ -787,11 +811,10 @@ def draw(self): GL.glPushMatrix() GL.glLoadIdentity() - if not self.fontbase: - self.fontbase, self._width, linespace = use_pango_font(self._font, 0, 128) + scale = hud_font(self) xmargin, ymargin = 5, 5 - ypos = float(self.app.winfo_height()) - winWidth = int(self.app.winfo_width()) + ypos = self.app.winfo_height() / scale + winWidth = int(self.app.winfo_width() / scale) GL.glOrtho(0.0, winWidth, 0.0, ypos, -1.0, 1.0) GL.glMatrixMode(GL.GL_MODELVIEW) @@ -937,12 +960,11 @@ def draw(self): GL.glPushMatrix() GL.glLoadIdentity() - if not self.fontbase: - self.fontbase, self._width, linespace = use_pango_font(self._font, 0, 128) + scale = hud_font(self) xmargin,ymargin = 5,5 - ypos = float(self.app.winfo_height()) - winWidth = int(self.app.winfo_width()) + ypos = self.app.winfo_height() / scale + winWidth = int(self.app.winfo_width() / scale) GL.glOrtho(0.0, winWidth, 0.0, ypos, -1.0, 1.0) GL.glMatrixMode(GL.GL_MODELVIEW) diff --git a/lib/python/qtvcp/lib/qt_vismach/qt_vismach.py b/lib/python/qtvcp/lib/qt_vismach/qt_vismach.py index 598ae1f79d1..b0806a86d6f 100644 --- a/lib/python/qtvcp/lib/qt_vismach/qt_vismach.py +++ b/lib/python/qtvcp/lib/qt_vismach/qt_vismach.py @@ -180,11 +180,16 @@ def minimumSizeHint(self): def sizeHint(self): return QSize(400, 400) + # Device pixels: the framebuffer is allocated at the window scale (HiDPI) + def winfo_scale(self): + # Device pixels per logical pixel; may be fractional. + return self.devicePixelRatioF() + def winfo_width(self): - return self.geometry().width() + return round(self.geometry().width() * self.winfo_scale()) def winfo_height(self): - return self.geometry().height() + return round(self.geometry().height() * self.winfo_scale()) def setXRotation(self, angle): angle = self.normalizeAngle(angle) diff --git a/lib/python/qtvcp/widgets/gcode_graphics.py b/lib/python/qtvcp/widgets/gcode_graphics.py index 838616812df..ab089aca291 100644 --- a/lib/python/qtvcp/widgets/gcode_graphics.py +++ b/lib/python/qtvcp/widgets/gcode_graphics.py @@ -365,7 +365,9 @@ def select_fire(self): x, y = self.select_primed self.select_primed = None self.makeCurrent() - self.select(x, y) + # Events are logical pixels, the pick target is device pixels. + scale = self.winfo_scale() + self.select(x * scale, y * scale) # override user plot -One could add gl commands to plot static objects here def user_plot(self): diff --git a/src/emc/usr_intf/gremlin/gremlin.py b/src/emc/usr_intf/gremlin/gremlin.py index b013e4e0430..42ef7079104 100755 --- a/src/emc/usr_intf/gremlin/gremlin.py +++ b/src/emc/usr_intf/gremlin/gremlin.py @@ -264,6 +264,10 @@ def swapbuffers(self): def deactivate(self): return + def winfo_scale(self): + # Device pixels per logical (mouse event) pixel, for glnav's panning. + return self.get_scale_factor() + def winfo_width(self): # Device pixels: the area's framebuffer is allocated at the window # scale, and the viewport and pick target are sized from these. diff --git a/src/emc/usr_intf/gremlin/qt5_graphics.py b/src/emc/usr_intf/gremlin/qt5_graphics.py index c84a27f86bd..dd20e9da5db 100644 --- a/src/emc/usr_intf/gremlin/qt5_graphics.py +++ b/src/emc/usr_intf/gremlin/qt5_graphics.py @@ -27,6 +27,7 @@ import glnav from rs274 import glcanon +from rs274 import glcanon_gl from rs274 import interpret from rs274.program_time import MachineLimits, ProgramTime, format_seconds import linuxcnc @@ -406,6 +407,7 @@ def C(s): self.sf_in = "SFM: %4d" self.sf_mm = "SMM: %4d" self._font = 'monospace bold 16' + self._font_scale = None self._fontLarge = 'monospace bold 22' self._largeFontState = False self.addTimer() @@ -610,7 +612,10 @@ def set_font(self, large=None): else: font = self._font self._largeFontState = False - self.font_base, width, linespace = glnav.use_pango_font(font, 0, 128) + scale = self.winfo_scale() + self.font_base, width, linespace = glnav.use_pango_font( + font, 0, 128, scale=scale) + self._font_scale = scale self.font_linespace = linespace self.font_charwidth = width self.update() @@ -699,11 +704,28 @@ def clear_live_plotter(self): self.logger.clear() self.update() + def winfo_scale(self): + # Device pixels per logical pixel (HiDPI); may be fractional. + return self.devicePixelRatioF() + def winfo_width(self): - return self.geometry().width() + # Device pixels: the widget's framebuffer is allocated at the window + # scale, and the viewport and pick target are sized from these. + return round(self.geometry().width() * self.winfo_scale()) def winfo_height(self): - return self.geometry().height() + return round(self.geometry().height() * self.winfo_scale()) + + def update_hidpi(self): + """Follow the window's scale factor (HiDPI): the GL framebuffer is in + device pixels, so line widths and the overlay font are scaled by it. + Checked every frame, since the factor changes when the window moves + to a screen with a different scale. Needs the context. + """ + scale = self.winfo_scale() + glcanon_gl.set_line_scale(scale) + if scale != self._font_scale: + self.set_font() # trick - we are not gtk def activate(self): @@ -922,6 +944,7 @@ def paintGL(self): try: + self.update_hidpi() if self.perspective: self.redraw_perspective() else: @@ -990,7 +1013,7 @@ def redraw_ortho(self): def resizeGL(self, width, height): # redraw sets the viewport and projection from the camera each frame; # nothing to configure on the (now core) GL matrix stack here. - GL.glViewport(0, 0, width, height) + GL.glViewport(0, 0, self.winfo_width(), self.winfo_height()) #################################### # Property setting functions @@ -1069,7 +1092,9 @@ def select_fire(self): x, y = self.select_primed self.select_primed = None self.makeCurrent() - self.select(x, y) + # Events are logical pixels, the pick target is device pixels. + scale = self.winfo_scale() + self.select(x * scale, y * scale) def select_cancel(self, widget=None, event=None): self.select_primed = None diff --git a/src/emc/usr_intf/qtvcp/qtvcp.py b/src/emc/usr_intf/qtvcp/qtvcp.py index e4f5166fb7a..d19df38a2d9 100644 --- a/src/emc/usr_intf/qtvcp/qtvcp.py +++ b/src/emc/usr_intf/qtvcp/qtvcp.py @@ -110,6 +110,21 @@ def __init__(self): # so web engine can load local images sys.argv.append("--disable-web-security") + # Qt5 leaves high-DPI scaling off: on a scaled display the fonts + # follow the screen DPI while widget sizes stay in raw pixels, so + # text outgrows its widgets. Qt6 always scales and has no switch. + import qtpy + if qtpy.QT5: + QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) + QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps) + # Qt5 rounds the factor to an integer (150% becomes 2x); pass + # it through unrounded as Qt6 does. Needs Qt 5.14. + try: + QtGui.QGuiApplication.setHighDpiScaleFactorRoundingPolicy( + QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough) + except AttributeError: + pass + # initialize QApp so we can pop up dialogs now. global APP APP = MyApplication(sys.argv)