Issue
Background
I'm working on a graphical ssh client in Python. I'm using PySide for the GUI, a fork of Paramiko for the ssh interaction and a library called Pyte for the terminal emulation.
The problem
I can't figure out how to correctly resize the pyte terminal in relation to the PySide QTextEdit being resized. I can only seem to get the width and height of the QTextEdit in pixels, not columns and rows which is required by the pyte library's Screen.resize() function.
Is there anyway to either 1. get the column and line count for the QTextEdit or 2. convert the pixel width and height to columns and lines in a manner that is accurate across all systems?
Proposed Solution
Replace the QTextEdit.resizeEvent() function with a custom resize event handler which will call the Screen.resize() function of pyte to resize the terminal to match the new size of the QTextEdit widget.
If there is an easier solution I'm very open to ideas.
Solution
Solution
I ended up pulling font metrics and geometry information for the widget I was working with (QTextEdit). I then implemented a custom resize event for the QTextEdit which handled catching states on different events that caused an update (see the first if statement in my resizeConsoleEvent in the code block below), then, calculated the new available columns and lines and passed those to the ssh object (self.shell below).
The Code
def resizeConsoleEvent(self, resizeObject):
if not self.keyPressDown and not self.blockResizing:
# calculate maximum columns and lines based on a '|' character
font = self.ui.console.currentFont()
fmetric = QtGui.QFontMetrics(font)
fontPixelWidth = fmetric.width("|")
fontPixelHeight = fmetric.height()
availableWidthPixels = int(self.ui.console.geometry().width())
availableHeightPixels = int(self.ui.console.geometry().height())
# Calculate columns and lines w/ adjustments for rounding
self.consoleColumns = int(availableWidthPixels / fontPixelWidth) + 1
self.consoleLines = int(availableHeightPixels / fontPixelHeight) - 3
# resize the pyte screen I'm using with the calculated information
self.shell.resizeConsole(self.consoleLines, self.consoleColumns)
# block double resize event
self.keyPressDown = False
return False
else:
return True
Answered By - Benjamin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.