[libcamera-devel] [PATCH v7 3/8] qcam: MainWindow: Replace cameraCombo_ with CameraSelectorDialog

Utkarsh Tiwari utkarsh02t at gmail.com
Tue Aug 9 22:50:37 CEST 2022


Replace the cameraCombo_ on the toolbar with a QPushButton which
displays the CameraSelectorDialog. This would allow the user to view
information about the camera when switching.

The QPushButton text is set to the camera Id currently in use.

Signed-off-by: Utkarsh Tiwari <utkarsh02t at gmail.com>
Reviewed-by: Kieran Bingham <kieran.bingham at ideasonboard.com>
---
Difference:
	1. cameraSwitchButton_ to cameraSelectButton_
 src/qcam/main_window.cpp | 44 +++++++++++++++++++---------------------
 src/qcam/main_window.h   |  5 +++--
 2 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
index 377b4650..15b150ec 100644
--- a/src/qcam/main_window.cpp
+++ b/src/qcam/main_window.cpp
@@ -193,14 +193,11 @@ int MainWindow::createToolbars()
 	connect(action, &QAction::triggered, this, &MainWindow::quit);
 
 	/* Camera selector. */
-	cameraCombo_ = new QComboBox();
-	connect(cameraCombo_, QOverload<int>::of(&QComboBox::activated),
+	cameraSelectButton_ = new QPushButton;
+	connect(cameraSelectButton_, &QPushButton::clicked,
 		this, &MainWindow::switchCamera);
 
-	for (const std::shared_ptr<Camera> &cam : cm_->cameras())
-		cameraCombo_->addItem(QString::fromStdString(cam->id()));
-
-	toolbar_->addWidget(cameraCombo_);
+	toolbar_->addWidget(cameraSelectButton_);
 
 	toolbar_->addSeparator();
 
@@ -260,14 +257,18 @@ void MainWindow::updateTitle()
  * Camera Selection
  */
 
-void MainWindow::switchCamera(int index)
+void MainWindow::switchCamera()
 {
 	/* Get and acquire the new camera. */
-	const auto &cameras = cm_->cameras();
-	if (static_cast<unsigned int>(index) >= cameras.size())
+	std::string newCameraId = chooseCamera();
+
+	if (newCameraId.empty())
+		return;
+
+	if (camera_ && newCameraId == camera_->id())
 		return;
 
-	const std::shared_ptr<Camera> &cam = cameras[index];
+	const std::shared_ptr<Camera> &cam = cm_->get(newCameraId);
 
 	if (cam->acquire()) {
 		qInfo() << "Failed to acquire camera" << cam->id().c_str();
@@ -300,9 +301,12 @@ std::string MainWindow::chooseCamera()
 	if (options_.isSet(OptCamera))
 		return static_cast<std::string>(options_[OptCamera]);
 
-	if (cameraSelectorDialog_->exec() == QDialog::Accepted)
-		return cameraSelectorDialog_->getCameraId();
-	else
+	if (cameraSelectorDialog_->exec() == QDialog::Accepted) {
+		std::string cameraId = cameraSelectorDialog_->getCameraId();
+		cameraSelectButton_->setText(QString::fromStdString(cameraId));
+
+		return cameraId;
+	} else
 		return std::string();
 }
 
@@ -326,8 +330,8 @@ int MainWindow::openCamera()
 		return -EBUSY;
 	}
 
-	/* Set the combo-box entry with the currently selected Camera. */
-	cameraCombo_->setCurrentText(QString::fromStdString(cameraName));
+	/* Set the camera switch button with the currently selected Camera id. */
+	cameraSelectButton_->setText(QString::fromStdString(cameraName));
 
 	return 0;
 }
@@ -591,22 +595,16 @@ void MainWindow::processHotplug(HotplugEvent *e)
 	Camera *camera = e->camera();
 	HotplugEvent::PlugEvent event = e->hotplugEvent();
 
-	if (event == HotplugEvent::HotPlug) {
-		cameraCombo_->addItem(QString::fromStdString(camera->id()));
-
+	if (event == HotplugEvent::HotPlug)
 		cameraSelectorDialog_->cameraAdded(camera);
-	} else if (event == HotplugEvent::HotUnplug) {
+	else if (event == HotplugEvent::HotUnplug) {
 		/* Check if the currently-streaming camera is removed. */
 		if (camera == camera_.get()) {
 			toggleCapture(false);
 			camera_->release();
 			camera_.reset();
-			cameraCombo_->setCurrentIndex(0);
 		}
 
-		int camIndex = cameraCombo_->findText(QString::fromStdString(camera->id()));
-		cameraCombo_->removeItem(camIndex);
-
 		cameraSelectorDialog_->cameraRemoved(camera);
 	}
 }
diff --git a/src/qcam/main_window.h b/src/qcam/main_window.h
index b01d2e59..bbdbb21c 100644
--- a/src/qcam/main_window.h
+++ b/src/qcam/main_window.h
@@ -24,6 +24,7 @@
 #include <QMutex>
 #include <QObject>
 #include <QPointer>
+#include <QPushButton>
 #include <QQueue>
 #include <QTimer>
 
@@ -61,7 +62,7 @@ private Q_SLOTS:
 	void quit();
 	void updateTitle();
 
-	void switchCamera(int index);
+	void switchCamera();
 	void toggleCapture(bool start);
 
 	void saveImageAs();
@@ -91,7 +92,7 @@ private:
 	/* UI elements */
 	QToolBar *toolbar_;
 	QAction *startStopAction_;
-	QComboBox *cameraCombo_;
+	QPushButton *cameraSelectButton_;
 	QAction *saveRaw_;
 	ViewFinder *viewfinder_;
 
-- 
2.25.1



More information about the libcamera-devel mailing list