Restructured code and included capture button on form.
[toast/barcode2cash.git] / qtapp / barcode2cash / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3
4 bool checkCameraAvailability()
5 {
6     if (QCameraInfo::availableCameras().count() > 0)
7         return true;
8     else
9         return false;
10 }
11
12 MainWindow::MainWindow(QWidget *parent) :
13     QMainWindow(parent),
14     ui(new Ui::MainWindow)
15 {
16     ui->setupUi(this);
17
18     QCameraInfo info = QCameraInfo::defaultCamera();
19     if (!info.isNull()) {
20         camera = new QCamera(info);
21         camera->setCaptureMode(QCamera::CaptureStillImage);
22         viewfinder = new QCameraViewfinder;
23         camera->setViewfinder(viewfinder);
24         viewfinder->show();
25         imageCapture = new QCameraImageCapture(camera);
26         connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), SLOT(updateCameraInfo()));
27         updateCameraInfo();
28         connect(ui->pbCapture, SIGNAL(clicked()), SLOT(capture()));
29     }
30
31
32     ui->statusBar->showMessage(tr("There are %1 cameras").arg(checkCameraAvailability()));
33
34 }
35
36 MainWindow::~MainWindow() {
37     delete imageCapture;
38     delete viewfinder;
39     delete camera;
40     delete ui;
41 }
42
43
44 void MainWindow::updateCameraInfo() {
45     ui->labCameraReady->setText(imageCapture->isReadyForCapture() ? "ready" : "not ready");
46 }
47
48
49 void MainWindow::capture() {
50     camera->start();
51     camera->searchAndLock();
52     imageCapture->capture("test.png");
53     camera->unlock();
54 }