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

Kieran Bingham kieran.bingham at ideasonboard.com
Fri Oct 25 13:02:24 CEST 2019


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.

Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham at ideasonboard.com>

--
v2:
 - I have no idea what (or rather why) I screwed up the preivous but it
   was very wrong.
 - I think I saw the loop and for some reason thought I copy/pasted it
   wrong and it was supposed to contain the whole block - so I
   (erroneously) expanded the scope of the loop.. Ooops. I'll go back to
   sleep.

 - This one works and is tested on a single, and multiple cameras ;-D
---
 src/qcam/main_window.cpp | 40 +++++++++++++++++++++++++---------------
 src/qcam/main_window.h   |  1 +
 2 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
index ef4ad4a22dca..3d5630922723 100644
--- a/src/qcam/main_window.cpp
+++ b/src/qcam/main_window.cpp
@@ -66,27 +66,37 @@ 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;
+	QStringList cameras;
+	bool result;
+
+	if (cm->cameras().size() == 1)
+		return cm->cameras()[0]->name();
 
-	if (!options_.isSet(OptCamera)) {
-		QStringList cameras;
-		bool result;
+	for (const std::shared_ptr<Camera> &cam : cm->cameras())
+		cameras.append(QString::fromStdString(cam->name()));
 
-		for (const std::shared_ptr<Camera> &cam : cm->cameras())
-			cameras.append(QString::fromStdString(cam->name()));
+	QString name = QInputDialog::getItem(this, "Select Camera",
+					     "Camera:", cameras, 0,
+					     false, &result);
+	if (!result)
+		return std::string();
 
-		QString name = QInputDialog::getItem(this, "Select Camera",
-						     "Camera:", cameras, 0,
-						     false, &result);
-		if (!result)
-			return -EINVAL;
+	return name.toStdString();
+}
 
-		cameraName = name.toStdString();
-	} else {
+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();
-- 
2.20.1



More information about the libcamera-devel mailing list