Created a website for Flash Strobe on Maemo Garage
[flashstrobe] / src / mainwindow.cpp
1 /*
2   Copyright (C) 2010 by Juan Carlos Torres <jucato@kdemail.net>
3
4   This program is free software; you can redistribute it and/or
5   modify it under the terms of the GNU General Public License as
6   published by the Free Software Foundation; either version 2 of
7   the License or (at your option) version 3 or any later version
8   accepted by the membership of KDE e.V. (or its successor appro-
9   ved by the membership of KDE e.V.), which shall act as a proxy
10   defined in Section 14 of version 3 of the license.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program. If not, see http://www.gnu.org/licenses/.
19 */
20
21
22 #include "mainwindow.h"
23 #include "camera.h"
24 #include "musicplayer.h"
25
26 #include <QFont>
27 #include <QMessageBox>
28 #include <QTimer>
29
30 MainWindow::MainWindow(QWidget* parent): QWidget(parent)
31 {
32     m_camera = new Camera(this);
33     m_timer = new QTimer(this);
34
35     //Setup the UI widgets and layout
36     m_ui = new Ui::MainWindow;
37     m_player = new MusicPlayer(this);
38
39     m_ui->setupUi(this);
40     m_ui->verticalLayout->insertWidget(0, m_player, 0);
41
42     m_ui->frequencyLabel->setFont(QFont("Sans Serif", 48));
43
44 #ifdef Q_WS_MAEMO_5
45     setAttribute(Qt::WA_Maemo5PortraitOrientation,true);
46 #endif
47
48     frequencyChanged(m_ui->frequencyDial->value());
49     connect(m_ui->frequencyDial, SIGNAL(valueChanged(int)), this, SLOT(frequencyChanged(int)));
50     connect(m_ui->strobeButton, SIGNAL(toggled(bool)), this, SLOT(toggleStrobe(bool)));
51     connect(m_camera, SIGNAL(shutterStateChanged(bool)), this, SLOT(toggleAppState(bool)));
52     connect(m_timer, SIGNAL(timeout()), m_camera, SLOT(strobe()));
53
54     // Start listening for camera shutter state changes
55     m_camera->registerDbusWatcher();
56
57     toggleAppState(m_camera->isShutterOpen());
58 }
59
60 MainWindow::~MainWindow()
61 {
62     m_camera->deinit();
63 }
64
65 /**
66  * The user has changed the frequency, normally through the QDial.
67  * The flashing interval is computed as hertz (in milliseconds) while
68  * the actual flash LED timeout is computed as half of that (in microseconds).
69  *
70  * @param frequency Frequency in hertz
71  */
72 void MainWindow::frequencyChanged(int frequency)
73 {
74     m_ui->frequencyLabel->setText(QString("%1 bpm").arg(frequency*60));
75     m_camera->setTimeout((1000000/frequency)/2);
76     m_timer->setInterval(1000/frequency);
77 }
78
79 void MainWindow::toggleStrobe(bool checked)
80 {
81     int timeout = m_ui->frequencyDial->value();
82     if (checked)
83     {
84         if (m_camera->init() == -1)
85         {
86             QMessageBox::information(this, tr("Camera Unavailable"),
87                                     tr("Please close any application using the camera"));
88             m_ui->strobeButton->setChecked(false);
89         }
90         else
91         {
92             m_camera->setTimeout((1000000/timeout)/2);
93             m_camera->setFlashIntensity(215);
94             m_timer->start(1000/timeout);
95         }
96     }
97     else
98     {
99         m_timer->stop();
100         m_camera->setTimeout(0);
101         m_camera->setFlashIntensity(0);
102         m_camera->deinit();
103     }
104 }
105
106 void MainWindow::toggleAppState(bool enable)
107 {
108     m_ui->strobeButton->setEnabled(enable);
109     m_ui->frequencyDial->setEnabled(enable);
110
111     if (!enable)
112     {
113         QMessageBox::information(this, tr("Shutter Closed"),
114                                  tr("Please open the camera shutter before using"));
115
116         if (m_ui->strobeButton->isChecked())
117         {
118             m_ui->strobeButton->setChecked(false);
119         }
120     }
121 }