qcam: Use QDialog for selection of cameras at startup

Currently we use QInputDialog convenience dialogs to allow the user to
select a camera. This doesn't allow adding of more information (such as
camera location, model etc).

Create a QDialog with a QFormLayout that shows a QComboBox with camera
Ids. Use a QDialogButtonBox to provide buttons for accepting and
cancelling the action.

The CameraSelectorDialog is only initialized the first time when the
MainWindow is created.

From this commit we cease to auto select the camera if only a single
camera is available to libcamera. We would always display the selection
dialog with the exception being that being if the camera is supplied on
the command line.

Signed-off-by: Utkarsh Tiwari <utkarsh02t@gmail.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Utkarsh Tiwari
2022-08-07 00:34:30 +05:30
parent f1776100f5
commit f03da23b56
5 changed files with 94 additions and 17 deletions

View File

@@ -0,0 +1,50 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2022, Utkarsh Tiwari <utkarsh02t@gmail.com>
*
* cam_select_dialog.cpp - qcam - Camera Selection dialog
*/
#include "cam_select_dialog.h"
#include <libcamera/camera.h>
#include <libcamera/camera_manager.h>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QString>
CameraSelectorDialog::CameraSelectorDialog(libcamera::CameraManager *cameraManager,
QWidget *parent)
: QDialog(parent), cm_(cameraManager)
{
/* Use a QFormLayout for the dialog. */
QFormLayout *layout = new QFormLayout(this);
/* Setup the camera id combo-box. */
cameraIdComboBox_ = new QComboBox;
for (const auto &cam : cm_->cameras())
cameraIdComboBox_->addItem(QString::fromStdString(cam->id()));
/* Setup the QDialogButton Box */
QDialogButtonBox *buttonBox =
new QDialogButtonBox(QDialogButtonBox::Ok |
QDialogButtonBox::Cancel);
connect(buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject);
/* Set the layout. */
layout->addRow("Camera:", cameraIdComboBox_);
layout->addWidget(buttonBox);
}
CameraSelectorDialog::~CameraSelectorDialog() = default;
std::string CameraSelectorDialog::getCameraId()
{
return cameraIdComboBox_->currentText().toStdString();
}

View File

@@ -0,0 +1,34 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2022, Utkarsh Tiwari <utkarsh02t@gmail.com>
*
* cam_select_dialog.h - qcam - Camera Selection dialog
*/
#pragma once
#include <string>
#include <libcamera/camera.h>
#include <libcamera/camera_manager.h>
#include <QDialog>
class QComboBox;
class CameraSelectorDialog : public QDialog
{
Q_OBJECT
public:
CameraSelectorDialog(libcamera::CameraManager *cameraManager,
QWidget *parent);
~CameraSelectorDialog();
std::string getCameraId();
private:
libcamera::CameraManager *cm_;
/* UI elements. */
QComboBox *cameraIdComboBox_;
};

View File

@@ -19,7 +19,6 @@
#include <QFileDialog>
#include <QImage>
#include <QImageWriter>
#include <QInputDialog>
#include <QMutexLocker>
#include <QStandardPaths>
#include <QStringList>
@@ -30,6 +29,7 @@
#include "../cam/image.h"
#include "cam_select_dialog.h"
#include "dng_writer.h"
#ifndef QT_NO_OPENGL
#include "viewfinder_gl.h"
@@ -144,6 +144,8 @@ MainWindow::MainWindow(CameraManager *cm, const OptionsParser::Options &options)
cm_->cameraAdded.connect(this, &MainWindow::addCamera);
cm_->cameraRemoved.connect(this, &MainWindow::removeCamera);
cameraSelectorDialog_ = new CameraSelectorDialog(cm_, this);
/* Open the camera and start capture. */
ret = openCamera();
if (ret < 0) {
@@ -290,24 +292,10 @@ void MainWindow::switchCamera(int index)
std::string MainWindow::chooseCamera()
{
QStringList cameras;
bool result;
/* If only one camera is available, use it automatically. */
if (cm_->cameras().size() == 1)
return cm_->cameras()[0]->id();
/* Present a dialog box to pick a camera. */
for (const std::shared_ptr<Camera> &cam : cm_->cameras())
cameras.append(QString::fromStdString(cam->id()));
QString id = QInputDialog::getItem(this, "Select Camera",
"Camera:", cameras, 0,
false, &result);
if (!result)
if (cameraSelectorDialog_->exec() != QDialog::Accepted)
return std::string();
return id.toStdString();
return cameraSelectorDialog_->getCameraId();
}
int MainWindow::openCamera()

View File

@@ -33,6 +33,7 @@
class QAction;
class QComboBox;
class CameraSelectorDialog;
class Image;
class HotplugEvent;
@@ -99,6 +100,8 @@ private:
QString title_;
QTimer titleTimer_;
CameraSelectorDialog *cameraSelectorDialog_;
/* Options */
const OptionsParser::Options &options_;

View File

@@ -18,6 +18,7 @@ qcam_sources = files([
'../cam/image.cpp',
'../cam/options.cpp',
'../cam/stream_options.cpp',
'cam_select_dialog.cpp',
'format_converter.cpp',
'main.cpp',
'main_window.cpp',
@@ -26,6 +27,7 @@ qcam_sources = files([
])
qcam_moc_headers = files([
'cam_select_dialog.h',
'main_window.h',
'viewfinder_qt.h',
])