Initial commit. corresponds to 1.0-1 release
[flashstrobe] / src / mainwindow.cpp
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
new file mode 100644 (file)
index 0000000..cfdfbe9
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+  Copyright (C) 2010 by Juan Carlos Torres <jucato@kdemail.net>
+
+  This program is free software; you can redistribute it and/or
+  modify it under the terms of the GNU General Public License as
+  published by the Free Software Foundation; either version 2 of
+  the License or (at your option) version 3 or any later version
+  accepted by the membership of KDE e.V. (or its successor appro-
+  ved by the membership of KDE e.V.), which shall act as a proxy
+  defined in Section 14 of version 3 of the license.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program. If not, see http://www.gnu.org/licenses/.
+*/
+
+
+#include "mainwindow.h"
+#include "camera.h"
+#include "musicplayer.h"
+
+#include <QFont>
+#include <QMessageBox>
+#include <QTimer>
+
+MainWindow::MainWindow(QWidget* parent): QWidget(parent)
+{
+    m_camera = new Camera(this);
+    m_timer = new QTimer(this);
+
+    //Setup the UI widgets and layout
+    m_ui = new Ui::MainWindow;
+    m_player = new MusicPlayer(this);
+
+    m_ui->setupUi(this);
+    m_ui->verticalLayout->insertWidget(0, m_player, 0);
+
+    m_ui->frequencyLabel->setFont(QFont("Sans Serif", 48));
+
+#ifdef Q_WS_MAEMO_5
+    setAttribute(Qt::WA_Maemo5PortraitOrientation,true);
+#endif
+
+    frequencyChanged(m_ui->frequencyDial->value());
+    connect(m_ui->frequencyDial, SIGNAL(valueChanged(int)), this, SLOT(frequencyChanged(int)));
+    connect(m_ui->strobeButton, SIGNAL(toggled(bool)), this, SLOT(toggleStrobe(bool)));
+    connect(m_camera, SIGNAL(shutterStateChanged(bool)), this, SLOT(toggleAppState(bool)));
+    connect(m_timer, SIGNAL(timeout()), m_camera, SLOT(strobe()));
+
+    // Start listening for camera shutter state changes
+    m_camera->registerDbusWatcher();
+
+    toggleAppState(m_camera->isShutterOpen());
+}
+
+MainWindow::~MainWindow()
+{
+    m_camera->deinit();
+}
+
+/**
+ * The user has changed the frequency, normally through the QDial.
+ * The flashing interval is computed as hertz (in milliseconds) while
+ * the actual flash LED timeout is computed as half of that (in microseconds).
+ *
+ * @param frequency Frequency in hertz
+ */
+void MainWindow::frequencyChanged(int frequency)
+{
+    m_ui->frequencyLabel->setText(QString("%1 bpm").arg(frequency*60));
+    m_camera->setTimeout((1000000/frequency)/2);
+    m_timer->setInterval(1000/frequency);
+}
+
+void MainWindow::toggleStrobe(bool checked)
+{
+    int timeout = m_ui->frequencyDial->value();
+    if (checked)
+    {
+        if (m_camera->init() == -1)
+        {
+            QMessageBox::information(this, tr("Camera Unavailable"),
+                                    tr("Please close any application using the camera"));
+            m_ui->strobeButton->setChecked(false);
+        }
+        else
+        {
+            m_camera->setTimeout((1000000/timeout)/2);
+            m_camera->setFlashIntensity(215);
+            m_timer->start(1000/timeout);
+        }
+    }
+    else
+    {
+        m_timer->stop();
+        m_camera->setTimeout(0);
+        m_camera->setFlashIntensity(0);
+        m_camera->deinit();
+    }
+}
+
+void MainWindow::toggleAppState(bool enable)
+{
+    m_ui->strobeButton->setEnabled(enable);
+    m_ui->frequencyDial->setEnabled(enable);
+
+    if (!enable)
+    {
+        QMessageBox::information(this, tr("Shutter Closed"),
+                                 tr("Please open the camera shutter before using"));
+
+        if (m_ui->strobeButton->isChecked())
+        {
+            m_ui->strobeButton->setChecked(false);
+        }
+    }
+}