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))