Fix display of capture error messages in the status bar.
[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         camera->setViewfinder(ui->viewFinder);
23         imageCapture = new QCameraImageCapture(camera);
24         connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), SLOT(updateCameraInfo()));
25         connect(imageCapture, SIGNAL(imageSaved(int,QString)), SLOT(showCaptureSuccess(int,QString)));
26         connect(imageCapture, SIGNAL(error(int,QCameraImageCapture::Error,QString)), SLOT(showCaptureError(int,QCameraImageCapture::Error,QString)));
27         updateCameraInfo();
28         connect(ui->pbCapture, SIGNAL(clicked()), SLOT(capture()));
29         camera->start();
30     }
31
32
33     ui->statusBar->showMessage(tr("There are %1 cameras").arg(checkCameraAvailability()));
34
35 }
36
37 MainWindow::~MainWindow() {
38     delete imageCapture;
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     QImageEncoderSettings imageSettings;
51     imageSettings.setCodec("image/jpeg");
52     imageCapture->setEncodingSettings(imageSettings);
53     camera->searchAndLock();
54     imageCapture->capture("test.jpg");
55     camera->unlock();
56 }
57
58
59 void MainWindow::showCaptureSuccess(int, QString fileName) {
60     ui->statusBar->showMessage(tr("Image captured and saved as '%1'.").arg(fileName));
61 }
62
63
64 void MainWindow::showCaptureError(int, QCameraImageCapture::Error error, QString errorString) {
65     ui->statusBar->showMessage(tr("Error when capturing image (error id %2): %1.").arg(errorString).arg(error));
66 }