Skip to content

Commit

Permalink
Kinda works
Browse files Browse the repository at this point in the history
  • Loading branch information
zaak committed Dec 22, 2017
1 parent b3cde0f commit 0550bf4
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 32 deletions.
8 changes: 6 additions & 2 deletions CamGrab.pro
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp \
console.cpp
console.cpp \
cameramanager.cpp \
dataawarecombobox.cpp

HEADERS += \
mainwindow.h \
console.h
console.h \
cameramanager.h \
dataawarecombobox.h

FORMS += \
mainwindow.ui
Expand Down
2 changes: 1 addition & 1 deletion CamGrab.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.5.0, 2017-12-21T18:39:49. -->
<!-- Written by QtCreator 4.5.0, 2017-12-22T16:14:37. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
65 changes: 65 additions & 0 deletions cameramanager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "cameramanager.h"
#include "console.h"

CameraManager::CameraManager(QObject *parent)
:QObject(parent)
{

}

QList<QCameraInfo> CameraManager::listCameras()
{
return QCameraInfo::availableCameras();

//// if (cameraInfo.deviceName() == "mycamera")
// camera = new QCamera(QCameraInfo::defaultCamera());

// camera->setViewfinder(ui->cameraViewFinder);
// camera->start();

// ui->statusBar->showMessage("Aaaaa");
}

QSharedPointer<QCamera> CameraManager::getCameraByDeviceName(const QString &deviceName)
{
QList<QCameraInfo> cameras = listCameras();

foreach (const QCameraInfo &cameraInfo, cameras) {
if (cameraInfo.deviceName() == deviceName) {
return QSharedPointer<QCamera>(new QCamera(cameraInfo));
}
}

return QSharedPointer<QCamera>(nullptr);
}

void CameraManager::changeSelectedCamera(const QVariant &variant)
{
changeSelectedCamera(variant.toString());
}

void CameraManager::changeSelectedCamera(const QString &deviceName)
{
QSharedPointer<QCamera> cameraPtr = getCameraByDeviceName(deviceName);

if (cameraPtr.isNull()) {
Console::error(QString("Couln't change selected camera to %1 (): null pointer returned").arg(deviceName));
}

selectedCamera = cameraPtr;
selectedCameraInfo = QCameraInfo(*cameraPtr.data());

emit changedSelectedCamera(cameraPtr);

Console::log(QString("Changed selected camera to %1").arg(selectedCameraInfo.description()));
}

QSharedPointer<QCamera> CameraManager::getSelectedCamera()
{
return selectedCamera;
}

QCameraInfo &CameraManager::getSelectedCameraInfo()
{
return selectedCameraInfo;
}
31 changes: 31 additions & 0 deletions cameramanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef CAMERAMANAGER_H
#define CAMERAMANAGER_H

#include <QObject>
#include <QList>
#include <QCameraInfo>
#include <QSharedPointer>

class CameraManager: public QObject
{
Q_OBJECT
private:
QSharedPointer<QCamera> selectedCamera;
QCameraInfo selectedCameraInfo;

public:
CameraManager(QObject *parent = 0);
QList<QCameraInfo> listCameras();
QSharedPointer<QCamera> getCameraByDeviceName(const QString &deviceName);
QSharedPointer<QCamera> getSelectedCamera();
QCameraInfo &getSelectedCameraInfo();

public slots:
void changeSelectedCamera(const QVariant &variant);
void changeSelectedCamera(const QString &deviceName);

signals:
void changedSelectedCamera(const QSharedPointer<QCamera> &);
};

#endif // CAMERAMANAGER_H
2 changes: 1 addition & 1 deletion console.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "console.h"

QTextEdit *Console::outputControl;
QTextEdit *Console::output;

14 changes: 10 additions & 4 deletions console.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
class Console
{
private:
static QTextEdit *outputControl;
static QTextEdit *output;

public:
static void setOutputControl(QTextEdit *textEdit)
{
outputControl = textEdit;
output = textEdit;
}

static void log(QString message)
static void log(QString message, QColor color = Qt::black)
{
outputControl->append(message + "\n");
output->setTextColor(color);
output->append(message);
}

static void error(QString message)
{
log(message, Qt::red);
}
};

Expand Down
12 changes: 12 additions & 0 deletions dataawarecombobox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "dataawarecombobox.h"

DataAwareComboBox::DataAwareComboBox(QWidget *parent)
:QComboBox(parent)
{
connect(this, SIGNAL(activated(int)), this, SLOT(triggerVariantActivated(int)));
}

void DataAwareComboBox::triggerVariantActivated(int index)
{
emit activated(itemData(index));
}
20 changes: 20 additions & 0 deletions dataawarecombobox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CAMERACOMBOBOX_H
#define CAMERACOMBOBOX_H

#include <QComboBox>

class DataAwareComboBox : public QComboBox
{
Q_OBJECT

private slots:
void triggerVariantActivated(int index);

public:
DataAwareComboBox(QWidget *parent = 0);

signals:
void activated(const QVariant &);
};

#endif // CAMERACOMBOBOX_H
1 change: 0 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug() << a.styleSheet();
MainWindow w;
w.show();

Expand Down
61 changes: 47 additions & 14 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,74 @@

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
ui(new Ui::MainWindow),
cameraManager(new CameraManager)
{
ui->setupUi(this);
Console::setOutputControl(ui->consoleOutput);

QWidget *horizontalSpacer = new QWidget();
QWidget *horizontalSpacer = new QWidget(ui->mainToolBar);
horizontalSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
ui->mainToolBar->addWidget(horizontalSpacer);

ui->mainToolBar->addWidget(new QComboBox());
cameraComboBox = new DataAwareComboBox(ui->mainToolBar);
cameraComboBoxAction = ui->mainToolBar->addWidget(cameraComboBox);

ui->consoleDockWidget->setVisible(false);
connect(cameraComboBox, SIGNAL(activated(QVariant)), cameraManager, SLOT(changeSelectedCamera(QVariant)));
connect(cameraManager, SIGNAL(changedSelectedCamera(QSharedPointer<QCamera>)), this, SLOT(onCameraChanged(QSharedPointer<QCamera>)));

//ui->consoleDockWidget->setVisible(false);

detectCameras();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::startCamera(bool enable)
void MainWindow::detectCameras()
{
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
QList<QCameraInfo> cameras = cameraManager->listCameras();

if (cameras.empty()) {
Console::error("No available cameras detected!");
cameraComboBoxAction->setVisible(false);
ui->consoleDockWidget->setVisible(true);
return;
}

Console::log(QString("Detected %1 cameras:").arg(cameras.size()));

foreach (const QCameraInfo &cameraInfo, cameras) {
Console::log(cameraInfo.description());
cameraComboBox->addItem(cameraInfo.description(), cameraInfo.deviceName());
}

cameraManager->changeSelectedCamera(cameras.first().deviceName());
}

qDebug() << cameraInfo.deviceName();
qDebug() << cameraInfo.description();
void MainWindow::onCameraChanged(const QSharedPointer<QCamera> &cameraPtr)
{
ui->actionToggleCamera->setChecked(false);

Console::log(cameraInfo.deviceName());
// if (cameraInfo.deviceName() == "mycamera")
camera = new QCamera(QCameraInfo::defaultCamera());
QCamera *camera = cameraPtr.data();
camera->setViewfinder(ui->cameraViewFinder);
}

camera->setViewfinder(ui->cameraViewFinder);
camera->start();
void MainWindow::toggleCamera(bool enable)
{
QCamera *camera = cameraManager->getSelectedCamera().data();
QCameraInfo cameraInfo = cameraManager->getSelectedCameraInfo();

qDebug() << "MainWindow::toggleCamera" << camera;
qDebug() << camera->availableMetaData();

ui->statusBar->showMessage("Aaaaa");
if (enable) {
Console::log(QString("Starting camera %1").arg(cameraInfo.description()));
camera->start();
} else {
Console::log(QString("Stopping camera %1").arg(cameraInfo.description()));
camera->stop();
}
}
76 changes: 76 additions & 0 deletions mainwindow.cpp.autosave
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "console.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
cameraManager(new CameraManager)
{
ui->setupUi(this);
Console::setOutputControl(ui->consoleOutput);

QWidget *horizontalSpacer = new QWidget(ui->mainToolBar);
horizontalSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
ui->mainToolBar->addWidget(horizontalSpacer);

cameraComboBox = new DataAwareComboBox(ui->mainToolBar);
cameraComboBoxAction = ui->mainToolBar->addWidget(cameraComboBox);

connect(cameraComboBox, SIGNAL(activated(QVariant)), cameraManager, SLOT(changeSelectedCamera(QVariant)));
connect(cameraManager, SIGNAL(changedSelectedCamera(QSharedPointer<QCamera>)), this, SLOT(onCameraChanged(QSharedPointer<QCamera>)));

//ui->consoleDockWidget->setVisible(false);

detectCameras();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::detectCameras()
{
QList<QCameraInfo> cameras = cameraManager->listCameras();

if (cameras.empty()) {
Console::error("No available cameras detected!");
cameraComboBoxAction->setVisible(false);
ui->consoleDockWidget->setVisible(true);
return;
}

Console::log(QString("Detected %1 cameras:").arg(cameras.size()));

foreach (const QCameraInfo &cameraInfo, cameras) {
Console::log(cameraInfo.description());
cameraComboBox->addItem(cameraInfo.description(), cameraInfo.deviceName());
}

cameraManager->changeSelectedCamera(cameras.first().deviceName());
}

void MainWindow::onCameraChanged(const QSharedPointer<QCamera> &cameraPtr)
{
ui->actionToggleCamera->setChecked(false);

QCamera *camera = cameraPtr.data();
camera->setViewfinder(ui->cameraViewFinder);
}

void MainWindow::toggleCamera(bool enable)
{
QCamera *camera = cameraManager->getSelectedCamera().data();
QCameraInfo cameraInfo = cameraManager->getSelectedCameraInfo();

qDebug() << "MainWindow::toggleCamera" << camera;

if (enable) {
Console::log(QString("Starting camera %1").arg(cameraInfo.description()));
camera->start();
} else {
Console::log(QString("Stopping camera %1").arg(cameraInfo.description()));
camera->stop();
}
}
12 changes: 10 additions & 2 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <QCameraViewfinder>
#include <QComboBox>
#include <QSpacerItem>
#include "cameramanager.h"
#include "dataawarecombobox.h"

namespace Ui {
class MainWindow;
Expand All @@ -23,9 +25,15 @@ class MainWindow : public QMainWindow
private:
Ui::MainWindow *ui;
QCamera *camera;
CameraManager *cameraManager;
DataAwareComboBox *cameraComboBox;
QAction *cameraComboBoxAction;

public slots:
void startCamera(bool a);
void detectCameras();

private slots:
void toggleCamera(bool enable);
void onCameraChanged(const QSharedPointer<QCamera> &cameraPtr);
};

#endif // MAINWINDOW_H
Loading

0 comments on commit 0550bf4

Please sign in to comment.