qt PayToEdit: various fixes, incl icon size/pos, field size, scrollbar
this ports https://github.com/Electron-Cash/Electron-Cash/pull/1371 including commits: ---bab816e2c3Buttons Editor: Make background non-transparent and change to push button There were some issues with transparent backgrounds with QToolButton on Linux and as there is no real reason for them to be transparent we just make them opaque. ---2cb698affcPay to editor: Fix size computations to use the proper values Previously this did not take into account the spacing between lines nor the margins of the control and the document. There is also a sensible minimum height of one line now and it expands to up to 10 lines before we show the scroll bar. When the scroll bar is active, we move the buttons so they do not obscure the scroll bar. ---1b7a70f4f5Pay to editor: Increase height by one if cursor is under buttons ---abd42d9f66Buttons Editor: Always center if the document is just one line high ---33bd0b82e0Pay to editor: Make button movement on scrollbar change reliable ---94f8476c2ePay to editor: Use document lineCount instead of height ---5bedfce392Buttons Editor: Improve vertical centering of the buttons, needs to take into account the frame width ---0cd0b490c4Buttons Editor: Add transparent border which is somehow needed for correct macOS layout
This commit is contained in:
@@ -68,9 +68,21 @@ class PayToEdit(CompletionTextEdit, ScanQRTextEdit, Logger):
|
||||
self.win = win
|
||||
self.amount_edit = win.amount_e
|
||||
self.setFont(QFont(MONOSPACE_FONT))
|
||||
self.document().contentsChanged.connect(self.update_size)
|
||||
self.heightMin = 0
|
||||
self.heightMax = 150
|
||||
document = self.document()
|
||||
document.contentsChanged.connect(self.update_size)
|
||||
|
||||
fontMetrics = QFontMetrics(document.defaultFont())
|
||||
self.fontSpacing = fontMetrics.lineSpacing()
|
||||
|
||||
margins = self.contentsMargins()
|
||||
documentMargin = document.documentMargin()
|
||||
self.verticalMargins = margins.top() + margins.bottom()
|
||||
self.verticalMargins += self.frameWidth() * 2
|
||||
self.verticalMargins += documentMargin * 2
|
||||
|
||||
self.heightMin = self.fontSpacing + self.verticalMargins
|
||||
self.heightMax = (self.fontSpacing * 10) + self.verticalMargins
|
||||
|
||||
self.c = None
|
||||
self.textChanged.connect(self.check_text)
|
||||
self.outputs = [] # type: List[PartialTxOutput]
|
||||
@@ -248,13 +260,21 @@ class PayToEdit(CompletionTextEdit, ScanQRTextEdit, Logger):
|
||||
self.update_size()
|
||||
|
||||
def update_size(self):
|
||||
lineHeight = QFontMetrics(self.document().defaultFont()).height()
|
||||
docHeight = self.document().size().height()
|
||||
h = round(docHeight * lineHeight + 11)
|
||||
docLineCount = self.document().lineCount()
|
||||
if self.cursorRect().right() + 1 >= self.overlay_widget.pos().x():
|
||||
# Add a line if we are under the overlay widget
|
||||
docLineCount += 1
|
||||
docHeight = docLineCount * self.fontSpacing
|
||||
|
||||
h = docHeight + self.verticalMargins
|
||||
h = min(max(h, self.heightMin), self.heightMax)
|
||||
self.setMinimumHeight(h)
|
||||
self.setMaximumHeight(h)
|
||||
self.verticalScrollBar().hide()
|
||||
self.setMinimumHeight(int(h))
|
||||
self.setMaximumHeight(int(h))
|
||||
|
||||
self.verticalScrollBar().setHidden(docHeight + self.verticalMargins < self.heightMax)
|
||||
|
||||
# The scrollbar visibility can have changed so we update the overlay position here
|
||||
self._updateOverlayPos()
|
||||
|
||||
def resolve(self):
|
||||
self.is_alias = False
|
||||
|
||||
@@ -832,12 +832,12 @@ class MySortModel(QSortFilterProxyModel):
|
||||
|
||||
class OverlayControlMixin:
|
||||
STYLE_SHEET_COMMON = '''
|
||||
QWidget { background-color: transparent; }
|
||||
QToolButton { border-width: 1px; padding: 0px; margin: 0px; }
|
||||
QPushButton { border-width: 1px; padding: 0px; margin: 0px; }
|
||||
'''
|
||||
|
||||
STYLE_SHEET_LIGHT = '''
|
||||
QToolButton:hover { border: 1px solid #3daee9; }
|
||||
QPushButton { border: 1px solid transparent; }
|
||||
QPushButton:hover { border: 1px solid #3daee9; }
|
||||
'''
|
||||
|
||||
def __init__(self, middle: bool = False):
|
||||
@@ -852,14 +852,27 @@ class OverlayControlMixin:
|
||||
self.overlay_layout = QHBoxLayout(self.overlay_widget)
|
||||
self.overlay_layout.setContentsMargins(0, 0, 0, 0)
|
||||
self.overlay_layout.setSpacing(1)
|
||||
self._updateOverlayPos()
|
||||
|
||||
def resizeEvent(self, e):
|
||||
super().resizeEvent(e)
|
||||
self._updateOverlayPos()
|
||||
|
||||
def _updateOverlayPos(self):
|
||||
frame_width = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth)
|
||||
overlay_size = self.overlay_widget.sizeHint()
|
||||
x = self.rect().right() - frame_width - overlay_size.width()
|
||||
y = self.rect().bottom() - overlay_size.height()
|
||||
y = y / 2 if self.middle else y - frame_width
|
||||
middle = self.middle
|
||||
if hasattr(self, 'document'):
|
||||
# Keep the buttons centered if we have less than 2 lines in the editor
|
||||
line_spacing = QFontMetrics(self.document().defaultFont()).lineSpacing()
|
||||
if self.rect().height() < (line_spacing * 2):
|
||||
middle = True
|
||||
y = (y / 2) + frame_width if middle else y - frame_width
|
||||
if hasattr(self, 'verticalScrollBar') and self.verticalScrollBar().isVisible():
|
||||
scrollbar_width = self.style().pixelMetric(QStyle.PM_ScrollBarExtent)
|
||||
x -= scrollbar_width
|
||||
self.overlay_widget.move(int(x), int(y))
|
||||
|
||||
def addWidget(self, widget: QWidget):
|
||||
@@ -867,7 +880,7 @@ class OverlayControlMixin:
|
||||
self.overlay_layout.insertWidget(0, widget)
|
||||
|
||||
def addButton(self, icon_name: str, on_click, tooltip: str) -> QAbstractButton:
|
||||
button = QToolButton(self.overlay_widget)
|
||||
button = QPushButton(self.overlay_widget)
|
||||
button.setToolTip(tooltip)
|
||||
button.setIcon(read_QIcon(icon_name))
|
||||
button.setCursor(QCursor(Qt.PointingHandCursor))
|
||||
|
||||
Reference in New Issue
Block a user