From 01d20cba49a66c66322c71877d6a486f15a1c24a Mon Sep 17 00:00:00 2001 From: Axel Gembe Date: Wed, 1 Jun 2022 18:24:37 +0200 Subject: [PATCH] qt PayToEdit: various fixes, incl icon size/pos, field size, scrollbar this ports https://github.com/Electron-Cash/Electron-Cash/pull/1371 including commits: --- https://github.com/Electron-Cash/Electron-Cash/commit/bab816e2c3ace1624f6e981e947bcf1440728852 Buttons 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. --- https://github.com/Electron-Cash/Electron-Cash/commit/2cb698affc3ddf1049f1fdd5d55a842b9b7c192b Pay 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. --- https://github.com/Electron-Cash/Electron-Cash/commit/1b7a70f4f5b9362ca83d4d35627135bbd0d19aec Pay to editor: Increase height by one if cursor is under buttons --- https://github.com/Electron-Cash/Electron-Cash/commit/abd42d9f664960210d0c2df29c6ff9dfd6d44015 Buttons Editor: Always center if the document is just one line high --- https://github.com/Electron-Cash/Electron-Cash/commit/33bd0b82e0fac5bfd2447b06668889fa249213c9 Pay to editor: Make button movement on scrollbar change reliable --- https://github.com/Electron-Cash/Electron-Cash/commit/94f8476c2e89584275280de280a93a18ad0194a3 Pay to editor: Use document lineCount instead of height --- https://github.com/Electron-Cash/Electron-Cash/commit/5bedfce392a0aac0345a1c070c72d75d9449ea18 Buttons Editor: Improve vertical centering of the buttons, needs to take into account the frame width --- https://github.com/Electron-Cash/Electron-Cash/commit/0cd0b490c45620a5700b0c1ccfad5d3593dbe6ff Buttons Editor: Add transparent border which is somehow needed for correct macOS layout --- electrum/gui/qt/paytoedit.py | 38 +++++++++++++++++++++++++++--------- electrum/gui/qt/util.py | 23 +++++++++++++++++----- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/electrum/gui/qt/paytoedit.py b/electrum/gui/qt/paytoedit.py index 23c26faca..7afed1fe8 100644 --- a/electrum/gui/qt/paytoedit.py +++ b/electrum/gui/qt/paytoedit.py @@ -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 diff --git a/electrum/gui/qt/util.py b/electrum/gui/qt/util.py index ce42d7db6..1b3b6635f 100644 --- a/electrum/gui/qt/util.py +++ b/electrum/gui/qt/util.py @@ -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))