Showing success and error messages.
[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         connect(imageCapture, SIGNAL(imageSaved(int,QString)), SLOT(showCaptureSuccess(int,QString)));
28         connect(imageCapture, SIGNAL(error(int,QCameraImageCapture::Error,QString)), SLOT(showCaptureError(int,QCameraImageCapture::Error,QString)));
29         updateCameraInfo();
30         connect(ui->pbCapture, SIGNAL(clicked()), SLOT(capture()));
31         camera->start();
32     }
33
34
35     ui->statusBar->showMessage(tr("There are %1 cameras").arg(checkCameraAvailability()));
36
37 }
38
39 MainWindow::~MainWindow() {
40     delete imageCapture;
41     delete viewfinder;
42     delete camera;
43     delete ui;
44 }
45
46
47 void MainWindow::updateCameraInfo() {
48     ui->labCameraReady->setText(imageCapture->isReadyForCapture() ? "ready" : "not ready");
49 }
50
51
52 void MainWindow::capture() {
53     QImageEncoderSettings imageSettings;
54     imageSettings.setCodec("image/jpeg");
55     imageCapture->setEncodingSettings(imageSettings);
56     camera->searchAndLock();
57     imageCapture->capture("test.jpg");
58     camera->unlock();
59 }
60
61
62 void MainWindow::showCaptureSuccess(int, QString fileName) {
63     ui->statusBar->showMessage(tr("Image captured and saved as '%1'.").arg(fileName));
64 }
65
66
67 void MainWindow::showCaptureError(int, QCameraImageCapture::Error error, QString errorString) {
68     ui->statusBar->showMessage(tr("Error when capturing image (error id %2): %1.").arg(errorString, error));
69 }