[libcamera-devel] qcam: Don't ask for a camera when only one exists

Message ID 20191025103543.23058-1-kieran.bingham@ideasonboard.com
State Superseded
Headers show
Series
  • [libcamera-devel] qcam: Don't ask for a camera when only one exists
Related show

Commit Message

Kieran Bingham Oct. 25, 2019, 10:35 a.m. UTC
If there is only one camera exposed by libcamera, there is little value
in asking the user to choose it.

Automatically select it, and remove the need to ask the user to select
'ok' from a Dialog box.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 src/qcam/main_window.cpp | 33 +++++++++++++++++++++++----------
 src/qcam/main_window.h   |  1 +
 2 files changed, 24 insertions(+), 10 deletions(-)

Comments

Laurent Pinchart Oct. 25, 2019, 10:40 a.m. UTC | #1
Hi Kieran,

Thank you for the patch.

On Fri, Oct 25, 2019 at 11:35:43AM +0100, Kieran Bingham wrote:
> If there is only one camera exposed by libcamera, there is little value
> in asking the user to choose it.
> 
> Automatically select it, and remove the need to ask the user to select
> 'ok' from a Dialog box.
> 
> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> ---
>  src/qcam/main_window.cpp | 33 +++++++++++++++++++++++----------
>  src/qcam/main_window.h   |  1 +
>  2 files changed, 24 insertions(+), 10 deletions(-)
> 
> diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
> index ef4ad4a22dca..19a8801eafac 100644
> --- a/src/qcam/main_window.cpp
> +++ b/src/qcam/main_window.cpp
> @@ -66,27 +66,40 @@ void MainWindow::updateTitle()
>  	setWindowTitle(title_ + " : " + QString::number(fps, 'f', 2) + " fps");
>  }
>  
> -int MainWindow::openCamera(CameraManager *cm)
> +std::string MainWindow::chooseCamera(CameraManager *cm)
>  {
> -	std::string cameraName;
> +	QString name;
> +	bool result;
>  
> -	if (!options_.isSet(OptCamera)) {
> +	if (cm->cameras().size() == 1)
> +		return cm->cameras()[0]->name();
> +
> +	for (const std::shared_ptr<Camera> &cam : cm->cameras()) {
>  		QStringList cameras;
> -		bool result;
>  
> -		for (const std::shared_ptr<Camera> &cam : cm->cameras())
> -			cameras.append(QString::fromStdString(cam->name()));
> +		cameras.append(QString::fromStdString(cam->name()));
>  
>  		QString name = QInputDialog::getItem(this, "Select Camera",
>  						     "Camera:", cameras, 0,
>  						     false, &result);

Shouldn't this be outside of the for loop ?

>  		if (!result)
> -			return -EINVAL;
> +			return "";

You can return std::string() to avoid the conversion from const char *.

Apart from that,

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> +	}
>  
> -		cameraName = name.toStdString();
> -	} else {
> +	return name.toStdString();
> +}
> +
> +int MainWindow::openCamera(CameraManager *cm)
> +{
> +	std::string cameraName;
> +
> +	if (options_.isSet(OptCamera))
>  		cameraName = static_cast<std::string>(options_[OptCamera]);
> -	}
> +	else
> +		cameraName = chooseCamera(cm);
> +
> +	if (cameraName == "")
> +		return -EINVAL;
>  
>  	camera_ = cm->get(cameraName);
>  	if (!camera_) {
> diff --git a/src/qcam/main_window.h b/src/qcam/main_window.h
> index 6873155aaa03..30dd8743104d 100644
> --- a/src/qcam/main_window.h
> +++ b/src/qcam/main_window.h
> @@ -43,6 +43,7 @@ private Q_SLOTS:
>  	void updateTitle();
>  
>  private:
> +	std::string chooseCamera(CameraManager *cm);
>  	int openCamera(CameraManager *cm);
>  
>  	int startCapture();

Patch

diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
index ef4ad4a22dca..19a8801eafac 100644
--- a/src/qcam/main_window.cpp
+++ b/src/qcam/main_window.cpp
@@ -66,27 +66,40 @@  void MainWindow::updateTitle()
 	setWindowTitle(title_ + " : " + QString::number(fps, 'f', 2) + " fps");
 }
 
-int MainWindow::openCamera(CameraManager *cm)
+std::string MainWindow::chooseCamera(CameraManager *cm)
 {
-	std::string cameraName;
+	QString name;
+	bool result;
 
-	if (!options_.isSet(OptCamera)) {
+	if (cm->cameras().size() == 1)
+		return cm->cameras()[0]->name();
+
+	for (const std::shared_ptr<Camera> &cam : cm->cameras()) {
 		QStringList cameras;
-		bool result;
 
-		for (const std::shared_ptr<Camera> &cam : cm->cameras())
-			cameras.append(QString::fromStdString(cam->name()));
+		cameras.append(QString::fromStdString(cam->name()));
 
 		QString name = QInputDialog::getItem(this, "Select Camera",
 						     "Camera:", cameras, 0,
 						     false, &result);
 		if (!result)
-			return -EINVAL;
+			return "";
+	}
 
-		cameraName = name.toStdString();
-	} else {
+	return name.toStdString();
+}
+
+int MainWindow::openCamera(CameraManager *cm)
+{
+	std::string cameraName;
+
+	if (options_.isSet(OptCamera))
 		cameraName = static_cast<std::string>(options_[OptCamera]);
-	}
+	else
+		cameraName = chooseCamera(cm);
+
+	if (cameraName == "")
+		return -EINVAL;
 
 	camera_ = cm->get(cameraName);
 	if (!camera_) {
diff --git a/src/qcam/main_window.h b/src/qcam/main_window.h
index 6873155aaa03..30dd8743104d 100644
--- a/src/qcam/main_window.h
+++ b/src/qcam/main_window.h
@@ -43,6 +43,7 @@  private Q_SLOTS:
 	void updateTitle();
 
 private:
+	std::string chooseCamera(CameraManager *cm);
 	int openCamera(CameraManager *cm);
 
 	int startCapture();