[libcamera-devel] [RFC PATCH] Add CameraName to the MainWindow

Utkarsh Tiwari utkarsh02t at gmail.com
Sat Mar 19 15:25:45 CET 2022


Showing Camera Id on the main window is quite non-intuitive for the user

Implementation: A duplicate function exists in cam/main.cpp . There must be a better way to do this instead of duplication of code
Signed-off-by: Utkarsh Tiwari <utkarsh02t at gmail.com>
---
 src/qcam/main_window.cpp | 61 +++++++++++++++++++++++++++++++++-------
 src/qcam/main_window.h   |  2 +-
 2 files changed, 52 insertions(+), 11 deletions(-)

diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
index dd0e51f5..35e737c7 100644
--- a/src/qcam/main_window.cpp
+++ b/src/qcam/main_window.cpp
@@ -12,8 +12,10 @@
 #include <string>
 
 #include <libcamera/camera_manager.h>
+#include <libcamera/property_ids.h>
 #include <libcamera/version.h>
 
+
 #include <QComboBox>
 #include <QCoreApplication>
 #include <QFileDialog>
@@ -197,7 +199,7 @@ int MainWindow::createToolbars()
 		this, &MainWindow::switchCamera);
 
 	for (const std::shared_ptr<Camera> &cam : cm_->cameras())
-		cameraCombo_->addItem(QString::fromStdString(cam->id()));
+		cameraCombo_->addItem(QString::fromStdString(cameraName(cam.get())));
 
 	toolbar_->addWidget(cameraCombo_);
 
@@ -287,6 +289,45 @@ void MainWindow::switchCamera(int index)
 	startStopAction_->setChecked(true);
 }
 
+std::string MainWindow::cameraName(const libcamera::Camera *camera)
+{
+	const ControlList &props = camera->properties();
+	bool addModel = true;
+	std::string name;
+
+	/*
+	 * Construct the name from the camera location, model and ID. The model
+	 * is only used if the location isn't present or is set to External.
+	 */
+	if (props.contains(properties::Location)) {
+		switch (props.get(properties::Location)) {
+		case properties::CameraLocationFront:
+			addModel = false;
+			name = "Internal front camera ";
+			break;
+		case properties::CameraLocationBack:
+			addModel = false;
+			name = "Internal back camera ";
+			break;
+		case properties::CameraLocationExternal:
+			name = "External camera ";
+			break;
+		}
+	}
+
+	if (addModel && props.contains(properties::Model)) {
+		/*
+		 * If the camera location is not availble use the camera model
+		 * to build the camera name.
+		 */
+		name = "'" + props.get(properties::Model) + "' ";
+	}
+
+	name += "(" + camera->id() + ")";
+
+	return name;
+}
+
 std::string MainWindow::chooseCamera()
 {
 	QStringList cameras;
@@ -311,24 +352,24 @@ std::string MainWindow::chooseCamera()
 
 int MainWindow::openCamera()
 {
-	std::string cameraName;
+	std::string cameraId;
 
 	/*
 	 * Use the camera specified on the command line, if any, or display the
 	 * camera selection dialog box otherwise.
 	 */
 	if (options_.isSet(OptCamera))
-		cameraName = static_cast<std::string>(options_[OptCamera]);
+		cameraId = static_cast<std::string>(options_[OptCamera]);
 	else
-		cameraName = chooseCamera();
+		cameraId = chooseCamera();
 
-	if (cameraName == "")
+	if (cameraId == "")
 		return -EINVAL;
 
 	/* Get and acquire the camera. */
-	camera_ = cm_->get(cameraName);
+	camera_ = cm_->get(cameraId);
 	if (!camera_) {
-		qInfo() << "Camera" << cameraName.c_str() << "not found";
+		qInfo() << "Camera" << cameraId.c_str() << "not found";
 		return -ENODEV;
 	}
 
@@ -339,7 +380,7 @@ int MainWindow::openCamera()
 	}
 
 	/* Set the combo-box entry with the currently selected Camera. */
-	cameraCombo_->setCurrentText(QString::fromStdString(cameraName));
+	cameraCombo_->setCurrentText(QString::fromStdString(cameraName(camera_.get())));
 
 	return 0;
 }
@@ -604,7 +645,7 @@ void MainWindow::processHotplug(HotplugEvent *e)
 	HotplugEvent::PlugEvent event = e->hotplugEvent();
 
 	if (event == HotplugEvent::HotPlug) {
-		cameraCombo_->addItem(QString::fromStdString(camera->id()));
+		cameraCombo_->addItem(QString::fromStdString(cameraName(camera)));
 	} else if (event == HotplugEvent::HotUnplug) {
 		/* Check if the currently-streaming camera is removed. */
 		if (camera == camera_.get()) {
@@ -614,7 +655,7 @@ void MainWindow::processHotplug(HotplugEvent *e)
 			cameraCombo_->setCurrentIndex(0);
 		}
 
-		int camIndex = cameraCombo_->findText(QString::fromStdString(camera->id()));
+		int camIndex = cameraCombo_->findText(QString::fromStdString(cameraName(camera)));
 		cameraCombo_->removeItem(camIndex);
 	}
 }
diff --git a/src/qcam/main_window.h b/src/qcam/main_window.h
index 3fbe872c..b31c584f 100644
--- a/src/qcam/main_window.h
+++ b/src/qcam/main_window.h
@@ -70,7 +70,7 @@ private Q_SLOTS:
 
 private:
 	int createToolbars();
-
+	std::string cameraName(const libcamera::Camera *camera);
 	std::string chooseCamera();
 	int openCamera();
 
-- 
2.25.1



More information about the libcamera-devel mailing list