Skip to content
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
8 changes: 7 additions & 1 deletion lib/python/glnav.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
42 changes: 32 additions & 10 deletions lib/python/qtvcp/lib/qt_vismach/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions lib/python/qtvcp/lib/qt_vismach/qt_vismach.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/python/qtvcp/widgets/gcode_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions src/emc/usr_intf/gremlin/gremlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 30 additions & 5 deletions src/emc/usr_intf/gremlin/qt5_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -922,6 +944,7 @@ def paintGL(self):


try:
self.update_hidpi()
if self.perspective:
self.redraw_perspective()
else:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qtvcp's GCodeGraphics overrides select_fire in lib/python/qtvcp/widgets/gcode_graphics.py and still calls self.select(x, y), so qtdragon and qtaxis don't get this scaling. At 200% clicking a program line selects nothing in qtdragon; with the scaling in the override it works. Could the override get it too, or call this one after its own checks?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed in fc6e7c0: the override in gcode_graphics.py now scales the position the same way. I have not clicked through it in qtdragon myself yet, so a check on your side would be welcome.


def select_cancel(self, widget=None, event=None):
self.select_primed = None
Expand Down
15 changes: 15 additions & 0 deletions src/emc/usr_intf/qtvcp/qtvcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Qt5 rounds the scale factor to the nearest integer, so a 150% desktop becomes 2x and a 1920x1080 screen looks like 960x540 to qtvcp. Adding QtGui.QGuiApplication.setHighDpiScaleFactorRoundingPolicy(QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough) here gives 1.5, same as Qt6's default. Worth adding?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 9df60bb, inside the Qt5 block and guarded for Qt older than 5.14. I could not reproduce the 150% rounding on my machine, so I have only confirmed that PyQt5 5.15 accepts the call.

# 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)
Expand Down