[libcamera-devel,v9,2/7] qcam: Support Hotplug for Camera Selection Dialog
diff mbox series

Message ID 20220831054938.21617-3-utkarsh02t@gmail.com
State Accepted
Headers show
Series
  • Introduce capture scripts to qcam
Related show

Commit Message

Utkarsh Tiwari Aug. 31, 2022, 5:49 a.m. UTC
Currently if there is HotPlug event when the user is on the Camera
selection dialog, the QComboBox doesn't update to reflect the change.

Add support for hotplugging / unplugging cameras.

Signed-off-by: Utkarsh Tiwari <utkarsh02t@gmail.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
Difference from v8:
    1. Removed the part about QPointer from commit message, no longer
    needed.
    2. s/didn't/doesn't commit msg.
    3. Rename cameraRemoved to removeCamera and cameraAdded to addCamera
    4. Pass QString to cameraRemoved, cameraAdded instead of libcamera::Camera
    5. Removed blank line between cameraAdded, cameraRemoved
    6. Optimize cameraId passing cameraCombo_ , cameraAdded,
    cameraRemoved.
 src/qcam/cam_select_dialog.cpp | 12 ++++++++++++
 src/qcam/cam_select_dialog.h   |  5 +++++
 src/qcam/main_window.cpp       |  7 +++++--
 3 files changed, 22 insertions(+), 2 deletions(-)

Comments

Laurent Pinchart Aug. 31, 2022, 9:08 a.m. UTC | #1
Hi Utkarsh,

Thank you for the patch.

On Wed, Aug 31, 2022 at 11:19:33AM +0530, Utkarsh Tiwari wrote:
> Currently if there is HotPlug event when the user is on the Camera
> selection dialog, the QComboBox doesn't update to reflect the change.
> 
> Add support for hotplugging / unplugging cameras.
> 
> Signed-off-by: Utkarsh Tiwari <utkarsh02t@gmail.com>
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
> Difference from v8:
>     1. Removed the part about QPointer from commit message, no longer
>     needed.
>     2. s/didn't/doesn't commit msg.
>     3. Rename cameraRemoved to removeCamera and cameraAdded to addCamera
>     4. Pass QString to cameraRemoved, cameraAdded instead of libcamera::Camera
>     5. Removed blank line between cameraAdded, cameraRemoved
>     6. Optimize cameraId passing cameraCombo_ , cameraAdded,
>     cameraRemoved.
>  src/qcam/cam_select_dialog.cpp | 12 ++++++++++++
>  src/qcam/cam_select_dialog.h   |  5 +++++
>  src/qcam/main_window.cpp       |  7 +++++--
>  3 files changed, 22 insertions(+), 2 deletions(-)
> 
> diff --git a/src/qcam/cam_select_dialog.cpp b/src/qcam/cam_select_dialog.cpp
> index a49d822b..f82930cc 100644
> --- a/src/qcam/cam_select_dialog.cpp
> +++ b/src/qcam/cam_select_dialog.cpp
> @@ -48,3 +48,15 @@ std::string CameraSelectorDialog::getCameraId()
>  {
>  	return cameraIdComboBox_->currentText().toStdString();
>  }
> +
> +/* Hotplug / Unplug Support. */
> +void CameraSelectorDialog::addCamera(QString camearaId)

s/camearaId/cameraId/

> +{
> +	cameraIdComboBox_->addItem(camearaId);
> +}
> +
> +void CameraSelectorDialog::removeCamera(QString cameraId)
> +{
> +	int cameraIndex = cameraIdComboBox_->findText(cameraId);
> +	cameraIdComboBox_->removeItem(cameraIndex);
> +}
> diff --git a/src/qcam/cam_select_dialog.h b/src/qcam/cam_select_dialog.h
> index c31f4f82..bd2dbc1e 100644
> --- a/src/qcam/cam_select_dialog.h
> +++ b/src/qcam/cam_select_dialog.h
> @@ -13,6 +13,7 @@
>  #include <libcamera/camera_manager.h>
>  
>  #include <QDialog>
> +#include <QString>
>  
>  class QComboBox;
>  
> @@ -27,6 +28,10 @@ public:
>  
>  	std::string getCameraId();
>  
> +	/* Hotplug / Unplug Support. */
> +	void addCamera(QString cameraId);
> +	void removeCamera(QString cameraId);
> +
>  private:
>  	libcamera::CameraManager *cm_;
>  
> diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
> index 14bcf03e..e8e22d49 100644
> --- a/src/qcam/main_window.cpp
> +++ b/src/qcam/main_window.cpp
> @@ -587,10 +587,12 @@ void MainWindow::stopCapture()
>  void MainWindow::processHotplug(HotplugEvent *e)
>  {
>  	Camera *camera = e->camera();
> +	QString cameraId = QString::fromStdString(camera->id());
>  	HotplugEvent::PlugEvent event = e->hotplugEvent();
>  
>  	if (event == HotplugEvent::HotPlug) {
> -		cameraCombo_->addItem(QString::fromStdString(camera->id()));
> +		cameraCombo_->addItem(cameraId);
> +		cameraSelectorDialog_->addCamera(cameraId);
>  	} else if (event == HotplugEvent::HotUnplug) {
>  		/* Check if the currently-streaming camera is removed. */
>  		if (camera == camera_.get()) {
> @@ -600,8 +602,9 @@ void MainWindow::processHotplug(HotplugEvent *e)
>  			cameraCombo_->setCurrentIndex(0);
>  		}
>  
> -		int camIndex = cameraCombo_->findText(QString::fromStdString(camera->id()));
> +		int camIndex = cameraCombo_->findText(cameraId);
>  		cameraCombo_->removeItem(camIndex);
> +		cameraSelectorDialog_->removeCamera(cameraId);
>  	}
>  }
>

Patch
diff mbox series

diff --git a/src/qcam/cam_select_dialog.cpp b/src/qcam/cam_select_dialog.cpp
index a49d822b..f82930cc 100644
--- a/src/qcam/cam_select_dialog.cpp
+++ b/src/qcam/cam_select_dialog.cpp
@@ -48,3 +48,15 @@  std::string CameraSelectorDialog::getCameraId()
 {
 	return cameraIdComboBox_->currentText().toStdString();
 }
+
+/* Hotplug / Unplug Support. */
+void CameraSelectorDialog::addCamera(QString camearaId)
+{
+	cameraIdComboBox_->addItem(camearaId);
+}
+
+void CameraSelectorDialog::removeCamera(QString cameraId)
+{
+	int cameraIndex = cameraIdComboBox_->findText(cameraId);
+	cameraIdComboBox_->removeItem(cameraIndex);
+}
diff --git a/src/qcam/cam_select_dialog.h b/src/qcam/cam_select_dialog.h
index c31f4f82..bd2dbc1e 100644
--- a/src/qcam/cam_select_dialog.h
+++ b/src/qcam/cam_select_dialog.h
@@ -13,6 +13,7 @@ 
 #include <libcamera/camera_manager.h>
 
 #include <QDialog>
+#include <QString>
 
 class QComboBox;
 
@@ -27,6 +28,10 @@  public:
 
 	std::string getCameraId();
 
+	/* Hotplug / Unplug Support. */
+	void addCamera(QString cameraId);
+	void removeCamera(QString cameraId);
+
 private:
 	libcamera::CameraManager *cm_;
 
diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
index 14bcf03e..e8e22d49 100644
--- a/src/qcam/main_window.cpp
+++ b/src/qcam/main_window.cpp
@@ -587,10 +587,12 @@  void MainWindow::stopCapture()
 void MainWindow::processHotplug(HotplugEvent *e)
 {
 	Camera *camera = e->camera();
+	QString cameraId = QString::fromStdString(camera->id());
 	HotplugEvent::PlugEvent event = e->hotplugEvent();
 
 	if (event == HotplugEvent::HotPlug) {
-		cameraCombo_->addItem(QString::fromStdString(camera->id()));
+		cameraCombo_->addItem(cameraId);
+		cameraSelectorDialog_->addCamera(cameraId);
 	} else if (event == HotplugEvent::HotUnplug) {
 		/* Check if the currently-streaming camera is removed. */
 		if (camera == camera_.get()) {
@@ -600,8 +602,9 @@  void MainWindow::processHotplug(HotplugEvent *e)
 			cameraCombo_->setCurrentIndex(0);
 		}
 
-		int camIndex = cameraCombo_->findText(QString::fromStdString(camera->id()));
+		int camIndex = cameraCombo_->findText(cameraId);
 		cameraCombo_->removeItem(camIndex);
+		cameraSelectorDialog_->removeCamera(cameraId);
 	}
 }