Skip to content

Commit

Permalink
Make positive input prompt drag resizable.
Browse files Browse the repository at this point in the history
  • Loading branch information
FeepingCreature committed Sep 30, 2024
1 parent 4e17a5e commit 4cebcb5
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions ai_diffusion/ui/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,34 @@ def keyPressEvent(self, a0: QKeyEvent | None):
super().keyPressEvent(a0)


class TextPromptResizeWidget(QWidget):
"""Handles drag-based resizing for TextPromptWidget."""

def __init__(self, text_prompt: TextPromptWidget):
super().__init__(text_prompt)
self.setCursor(Qt.CursorShape.SizeVerCursor)
self.setFixedHeight(5)
self._dragging = False
self._text_prompt = text_prompt

def mousePressEvent(self, a0: QMouseEvent | None) -> None:
if ensure(a0).button() == Qt.MouseButton.LeftButton:
self._dragging = True

def mouseReleaseEvent(self, a0: QMouseEvent | None) -> None:
self._dragging = False

def mouseMoveEvent(self, a0: QMouseEvent | None) -> None:
if not self._dragging:
return
new_height = self.mapToParent(ensure(a0).pos()).y() - self._text_prompt.contentsRect().top()
fm = QFontMetrics(ensure(self._text_prompt._multi.document()).defaultFont())
new_line_count = round((new_height - 13) / fm.lineSpacing())
if 1 <= new_line_count <= 10:
settings.prompt_line_count = new_line_count
self._text_prompt.line_count = new_line_count


class TextPromptWidget(QFrame):
"""Wraps a single or multi-line text widget, with ability to switch between them.
Using QPlainTextEdit set to a single line doesn't work properly because it still
Expand Down Expand Up @@ -471,6 +499,9 @@ def __init__(self, line_count=2, is_negative=False, parent=None):
self._layout.addWidget(self._multi)
self._layout.addWidget(self._single)

if not is_negative:
self._layout.addWidget(TextPromptResizeWidget(self))

palette: QPalette = self._multi.palette()
self._base_color = palette.color(QPalette.ColorRole.Base)
self.is_negative = self._is_negative
Expand Down

0 comments on commit 4cebcb5

Please sign in to comment.