Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
1 change: 1 addition & 0 deletions changes/3914.feature.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ImageView widget is now supported in the Qt backend.
1 change: 0 additions & 1 deletion docs/en/reference/data/apis_by_platform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ General widgets:
unsupported:
- web
- textual
- qt

Label:
description: A text label for annotating forms or interfaces.
Expand Down
2 changes: 2 additions & 0 deletions qt/src/toga_qt/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .widgets.activityindicator import ActivityIndicator
from .widgets.box import Box
from .widgets.button import Button
from .widgets.imageview import ImageView
from .widgets.label import Label
from .widgets.textinput import TextInput
from .window import MainWindow, Window
Expand Down Expand Up @@ -44,6 +45,7 @@
"Box",
"Label",
"TextInput",
"ImageView",
"dialogs",
]

Expand Down
2 changes: 1 addition & 1 deletion qt/src/toga_qt/widgets/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def create(self):
self.native.setAutoFillBackground(True)
# Background is not autofilled by default; but since we're
# enabling it here, let the default color be transparent
# so it autofills nothing.
# so it autofills nothing by default.
self._default_background_color = TRANSPARENT

def rehint(self):
Expand Down
46 changes: 46 additions & 0 deletions qt/src/toga_qt/widgets/imageview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from PySide6.QtCore import Qt
from PySide6.QtGui import QPixmap
from PySide6.QtWidgets import QLabel
from travertino.constants import TRANSPARENT

from toga.widgets.imageview import rehint_imageview

from .base import Widget


class ImageView(Widget):
def create(self):
self.native = QLabel()
self.native.setAlignment(Qt.AlignCenter)
self._aspect_ratio = None
self.native.setAutoFillBackground(True)
# Background is not autofilled by default; but since we're
# enabling it here, let the default color be transparent
# so it autofills nothing by default.
self._default_background_color = TRANSPARENT

def set_image(self, image):
if image:
self.set_scaled_pixbuf(image._impl.native)
else:
self.native.setPixmap(QPixmap())

def set_scaled_pixbuf(self, image):
scaled = QPixmap.fromImage(image).scaled(
self.native.size(),
Qt.KeepAspectRatio if self._aspect_ratio else Qt.IgnoreAspectRatio,
Qt.SmoothTransformation,
)
self.native.setPixmap(scaled)

def set_bounds(self, *args):
super().set_bounds(*args)
if self.interface.image:
self.set_scaled_pixbuf(self.interface.image._impl.native)

def rehint(self):
width, height, self._aspect_ratio = rehint_imageview(
image=self.interface.image, style=self.interface.style
)
self.interface.intrinsic.width = width
self.interface.intrinsic.height = height
4 changes: 4 additions & 0 deletions qt/tests_backend/widgets/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
class BoxProbe(SimpleProbe):
native_class = QWidget
invalid_size_while_hidden = False

def __init__(self, widget):
super().__init__(widget)
assert self.native.autoFillBackground()
19 changes: 19 additions & 0 deletions qt/tests_backend/widgets/imageview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from PySide6.QtWidgets import QLabel

from .base import SimpleProbe


class ImageViewProbe(SimpleProbe):
native_class = QLabel

def __init__(self, widget):
super().__init__(widget)
assert self.native.autoFillBackground()

@property
def preserve_aspect_ratio(self):
return self.impl._aspect_ratio is not None

def assert_image_size(self, width, height):
pixmap = self.native.pixmap()
assert (pixmap.width(), pixmap.height()) == (width, height)
4 changes: 4 additions & 0 deletions qt/tests_backend/widgets/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
class LabelProbe(SimpleProbe):
native_class = QLabel

def __init__(self, widget):
super().__init__(widget)
assert self.native.autoFillBackground()

@property
def text(self):
return self.native.text()
Expand Down