From patchwork Thu Jun 20 15:49:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 20356 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 56A5FBE175 for ; Thu, 20 Jun 2024 15:49:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 96CAA654A3; Thu, 20 Jun 2024 17:49:24 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Mfmhv36U"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 953F7619ED for ; Thu, 20 Jun 2024 17:49:23 +0200 (CEST) Received: from Monstersaurus.local (cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 4E61C2B3; Thu, 20 Jun 2024 17:49:04 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1718898544; bh=FRt3L0pA3I8E+IOKOZKHbGTww5ZLIcl5zKHEgLTszpI=; h=From:To:Cc:Subject:Date:From; b=Mfmhv36UHoCdyavFgxG3NWDopaGaMrCvN2FA/IW7hBAjY210yrXAUg1NfXKdN9QQW VpLw6jlgpYOwxNVZyu6cAL/kpRMIpaY4fsCrPFH6HS0iUwEQJSjV88pwPIsm+7qW7E ihZ6t2YX4G7Jdb9x3bpzMrU5XeTy+uXPBnbGOl6g= From: Kieran Bingham To: libcamera devel Cc: Kieran Bingham , Laurent Pinchart Subject: [PATCH v3] qcam: viewfinder_qt: Maintain aspect ratio Date: Thu, 20 Jun 2024 16:49:19 +0100 Message-Id: <20240620154919.1923755-1-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Keep the image aspect ratio when displaying in the viewfinder. When the window is adjusted to a size that differs in aspect ratio to the image, keep the image centered in the main window. Signed-off-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- v2: (fixed up from Laurent's review comments) - No longer accept event on an invalid size. - centering simplifed v3: - use QWidget::resizeEvent(event); - simplify commit message. src/apps/qcam/viewfinder_qt.cpp | 16 ++++++++++++++-- src/apps/qcam/viewfinder_qt.h | 2 ++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/apps/qcam/viewfinder_qt.cpp b/src/apps/qcam/viewfinder_qt.cpp index 4821c27d826e..492648cfa2ff 100644 --- a/src/apps/qcam/viewfinder_qt.cpp +++ b/src/apps/qcam/viewfinder_qt.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include "../common/image.h" @@ -40,7 +41,7 @@ static const QMap nativeFormats }; ViewFinderQt::ViewFinderQt(QWidget *parent) - : QWidget(parent), buffer_(nullptr) + : QWidget(parent), place_(rect()), buffer_(nullptr) { icon_ = QIcon(":camera-off.svg"); } @@ -148,7 +149,7 @@ void ViewFinderQt::paintEvent(QPaintEvent *) /* If we have an image, draw it. */ if (!image_.isNull()) { - painter.drawImage(rect(), image_, image_.rect()); + painter.drawImage(place_, image_, image_.rect()); return; } @@ -181,3 +182,14 @@ QSize ViewFinderQt::sizeHint() const { return size_.isValid() ? size_ : QSize(640, 480); } + +void ViewFinderQt::resizeEvent(QResizeEvent *event) +{ + if (!size_.isValid()) + return; + + place_.setSize(size_.scaled(event->size(), Qt::KeepAspectRatio)); + place_.moveCenter(rect().center()); + + QWidget::resizeEvent(event); +} diff --git a/src/apps/qcam/viewfinder_qt.h b/src/apps/qcam/viewfinder_qt.h index 4f4b9f117e37..50fde88e163e 100644 --- a/src/apps/qcam/viewfinder_qt.h +++ b/src/apps/qcam/viewfinder_qt.h @@ -44,6 +44,7 @@ Q_SIGNALS: protected: void paintEvent(QPaintEvent *) override; + void resizeEvent(QResizeEvent *) override; QSize sizeHint() const override; private: @@ -51,6 +52,7 @@ private: libcamera::PixelFormat format_; QSize size_; + QRect place_; /* Camera stopped icon */ QSize vfSize_;