Skip to content

Commit

Permalink
Merge pull request OpenShot#4630 from ferdnyc/fix_viewport_logic
Browse files Browse the repository at this point in the history
Fix viewport zooming and region selection
  • Loading branch information
ferdnyc authored Feb 7, 2022
2 parents 502a7d4 + 1433b21 commit 9c442fb
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions src/windows/video_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,23 +509,17 @@ def paintEvent(self, event, *args):
def centeredViewport(self, width, height):
""" Calculate size of viewport to maintain aspect ratio """

# Calculate padding
top_padding = (height - (height * self.zoom)) / 2.0
left_padding = (width - (width * self.zoom)) / 2.0

# Adjust parameters to zoom
width = width * self.zoom
height = height * self.zoom

# Calculate which direction to scale (for perfect centering)
aspectRatio = self.aspect_ratio.ToFloat()
heightFromWidth = width / aspectRatio
widthFromHeight = height * aspectRatio

if heightFromWidth <= height:
return QRect(left_padding, ((height - heightFromWidth) / 2) + top_padding, width, heightFromWidth)
else:
return QRect(((width - widthFromHeight) / 2.0) + left_padding, top_padding, widthFromHeight, height)
window_size = QSizeF(width, height)
window_rect = QRectF(QPointF(0, 0), window_size)

aspectRatio = self.aspect_ratio.ToFloat() * self.pixel_ratio.ToFloat()
viewport_size = QSizeF(aspectRatio, 1).scaled(
window_size, Qt.KeepAspectRatio
) * self.zoom
viewport_rect = QRectF(QPointF(0, 0), viewport_size)
viewport_rect.moveCenter(window_rect.center())
# Always round up to next whole integer value
return viewport_rect.toAlignedRect()

def present(self, image, *args):
""" Present the current frame """
Expand Down Expand Up @@ -1273,12 +1267,8 @@ def keyFrameTransformTriggered(self, effect_id, clip_id):

def regionTriggered(self, clip_id):
"""Handle the 'select region' signal when it's emitted"""
if self and not clip_id:
# Clear transform
self.region_enabled = False
else:
self.region_enabled = True

# Clear transform
self.region_enabled = bool(clip_id)
get_app().window.refreshFrameSignal.emit()

def resizeEvent(self, event):
Expand Down

0 comments on commit 9c442fb

Please sign in to comment.