defuze.me  Client
vumeterdisplay.cpp
00001 /**************************************************************************
00002 ** defuze.me Epitech Innovative Project
00003 **
00004 ** Copyright 2010-2011
00005 **   Athena Calmettes - Jocelyn De La Rosa - Francois Gaillard
00006 **   Adrien Jarthon - Alexandre Moore - Luc Peres - Arnaud Sellier
00007 **
00008 ** All rights reserved.
00009 **************************************************************************/
00010 
00011 #include "vumeterdisplay.hpp"
00012 #include <QPainter>
00013 #include <QPaintEvent>
00014 #include <QGradient>
00015 #include <QDebug>
00016 #include <QTimer>
00017 #include <QMutexLocker>
00018 #include "math.h"
00019 
00020 const int period = 20;
00021 
00022 VUMeterDisplay::VUMeterDisplay(QWidget *parent) :
00023     QWidget(parent)
00024 {
00025     level = 0;
00026     peak = 0;
00027     peakDuration = 0;
00028     setAutoFillBackground(false);
00029     setAttribute(Qt::WA_OpaquePaintEvent);
00030     gradient.setColorAt(1, QColor(39, 197, 224));
00031     gradient.setColorAt(0.4, QColor(205, 228, 42));
00032     gradient.setColorAt(0.2, QColor(205, 204, 0));
00033     gradient.setColorAt(0, QColor(255, 84, 0));
00034 
00035     QTimer *timer = new QTimer(this);
00036     connect(timer, SIGNAL(timeout()), SLOT(refresh()));
00037     timer->start(period);
00038 }
00039 
00040 void    VUMeterDisplay::setLevel(double level)
00041 {
00042     QMutexLocker    locker(&mutex);
00043     if (level > this->level)
00044         this->level = level;
00045     if (level > peak)
00046     {
00047         peak = level;
00048         peakDuration = 0;
00049     }
00050 }
00051 
00052 void    VUMeterDisplay::refresh()
00053 {
00054     if (peak > 0)
00055         repaint();
00056     {
00057         QMutexLocker    locker(&mutex);
00058         if (level > 0)
00059             level = (level + 1) * 0.99 - 1;
00060         if (peak > 0)
00061             peak = fmax(peak - (peakDuration / 200000.), 0);
00062         peakDuration += period;
00063     }
00064 }
00065 /*
00066 void    VUMeterDisplay::initializeGL()
00067 {
00068     glClearColor(0.0, 0.0, 0.0, 0.0);
00069 //  glEnable(GL_DEPTH_TEST);
00070 }
00071 
00072 void    VUMeterDisplay::resizeGL(int w, int h)
00073 {
00074     // setup viewport, projection etc.:
00075     glViewport(0, 0, (GLint)w, (GLint)h);
00076 }
00077 
00078 void    VUMeterDisplay::paintGL()
00079 {
00080     // draw the scene:
00081     glBegin(GL_QUADS);
00082 //  glVertex3f(...);
00083 //  glVertex3f(...);
00084     glEnd();
00085 }*/
00086 
00087 void    VUMeterDisplay::resizeEvent(QResizeEvent *)
00088 {
00089     // Set gradient
00090     gradient.setStart(rect().topLeft());
00091     gradient.setFinalStop(rect().bottomLeft());
00092 }
00093 
00094 void    VUMeterDisplay::paintEvent(QPaintEvent *)
00095 {
00096     QMutexLocker    locker(&mutex);
00097     painter.begin(this);
00098     QRect       zone = rect();
00099     QRect       pZone = rect();
00100 
00101     // Draw background
00102     painter.fillRect(rect(), QColor(30, 30, 30));
00103 
00104     // Draw bar
00105     if (level >= 0.01)
00106     {
00107         zone.setTop(rect().height() * (1-level));
00108         painter.fillRect(zone, gradient);
00109     }
00110 
00111     // Draw peak
00112     if (peak >= 0.025)
00113     {
00114         pZone.setTop(rect().height() * (1-peak));
00115         pZone.setHeight(2);
00116         painter.fillRect(pZone, gradient);
00117     }
00118 
00119     painter.end();
00120 }