00001
00002
00005
00006
00007 #include "vumeter.h"
00008 #include "qtrec.h"
00009
00010 #include <qpe/config.h>
00011 #include <qtimer.h>
00012 #include <qdrawutl.h>
00013
00014 #include <math.h>
00015
00016 VUMeter::VUMeter(QWidget *parent, const char *name, const int tr)
00017 : QWidget( parent, name )
00018 {
00019 tracks = tr;
00020 int i;
00021
00022 qWarning("initialize peakvalues");
00023 for(i = 0; i < tracks + 2; i++) {
00024 peak[i] = hold[i] = 32768;
00025 holdTime[i] = 20;
00026 }
00027
00028 colorScheme = 0;
00029
00030 readConf();
00031
00032
00033 if( colorScheme == 0 ) {
00034 for( i = 0; i < para.leds; i++) color[i] = green;
00035 color[0] = color[1] = red;
00036 color[2] = color[3] = color[4] = color[5] = yellow;
00037 } else {
00038 int j = para.leds - 4;
00039 for( i = 0; i < para.leds; i++) {
00040 int i16 = (j);
00041 color[i] = QColor(( 15 - i16) * 16, 255, 255, QColor::Hsv);
00042 j--;
00043 }
00044 }
00045
00046 buffer = new QPixmap();
00047 setBackgroundMode(NoBackground);
00048 vuTimer = new QTimer(this,"vu timer");
00049
00050 connect(vuTimer, SIGNAL(timeout()), this , SLOT(timeSlot()));
00051 }
00052
00053 VUMeter::~VUMeter(){
00054
00055 }
00056
00057 void VUMeter::update(){
00058 vuTimer->start(para.update, FALSE);
00059 if (para.onOff) {
00060 disconnect(vuTimer, SIGNAL(timeout()), this , SLOT(timeSlot()));
00061 connect(vuTimer, SIGNAL(timeout()), this , SLOT(timeSlot()));
00062 } else {
00063 disconnect(vuTimer, SIGNAL(timeout()), this , SLOT(timeSlot()));
00064 }
00065 resize();
00066 }
00067
00068 void VUMeter::slotOn() {
00069 connect(vuTimer, SIGNAL(timeout()), this , SLOT(timeSlot()));
00070 para.onOff = true;
00071 }
00072
00073 void VUMeter::slotOff() {
00074 disconnect(vuTimer, SIGNAL(timeout()), this , SLOT(timeSlot()));
00075 para.onOff = false;
00076 }
00077
00078 void VUMeter::slotProps() {
00079 }
00080
00081 void VUMeter::paintEvent(QPaintEvent* e) {
00082 Q_UNUSED(e);
00083 bitBlt(this, 0, 0, buffer);
00084 }
00085
00086
00087 void VUMeter::mousePressEvent(QMouseEvent* e) {
00088 Q_UNUSED(e);
00089 }
00090
00091
00092 void VUMeter::resizeEvent(QResizeEvent* event) {
00093 buffer->resize(event->size());
00094 resize();
00095 }
00096
00097 void VUMeter::resize() {
00098
00099 if(buffer == 0)
00100 qWarning("Dude NULL pixmap buffer!");
00101
00102 buffer->fill(black);
00103
00104 x = width() - 7; y = height() - 12;
00105 dx = x / (tracks + 2); dy = y / (para.leds + 2);
00106 ox = dx / 6 + 4 + (x - (tracks + 2) * dx) / 2;
00107 oy = dy / 5 + 5 + (y - (para.leds + 2) * dy) / 2;
00108 sx = (4 *dx) / 6; sy = (4 * dy) / 5;
00109
00110 QPainter painter; QString str; QFont font;
00111 int i, textOffset=0;
00112
00113 if( painter.begin(buffer) ==FALSE)
00114 qWarning("Painting pixmap did not work!");
00115 else {
00116 painter.setPen(green);
00117 qDrawShadePanel ( &painter, 0,0, width(),height(), colorGroup(), TRUE, 2, 0);
00118 if (2 * dy - 2 == 10) textOffset = 1;
00119 font = painter.font(); font.setPointSize( 2 * dy - 2);
00120 painter.setFont(font);
00121
00122 for(i = 0; i < tracks + 2; i++) {
00123 painter.setPen(green); painter.setBrush(green);
00124 str.sprintf("%d",i+1);
00125 if (i == tracks) str.sprintf("L");
00126 if (i == tracks + 1) str.sprintf("R");
00127
00128 painter.drawRect(ox + dx * i, oy + dy * (para.leds) - 2, sx, 2 * dy - 1);
00129 painter.setPen(black);
00130 painter.drawText(textOffset + ox + dx * i, oy + dy * (para.leds) - 2, sx, 2 * dy, AlignCenter, str);
00131 }
00132
00133 painter.end();
00134 paint();
00135 }
00136 }
00137
00138 void VUMeter::timeSlot() {
00139 int i;
00140 paint();
00141 for(i = 0; i < tracks + 2; i++) {
00142 peak[i] /= para.resoFactor;
00143 }
00144 }
00145
00146 void VUMeter::paint() {
00147 int i, k;
00148 float p, h ;
00149 QPainter painter;
00150 painter.begin(buffer);
00151 int c;
00152
00153 for(i = 0; i < tracks + 2; i++) {
00154 p = peak[i]; h = hold[i];
00155 if (p >= 32767) p = 32768;
00156 if (h >= 32767) h = 32768;
00157 for(k = para.leds + 1; k >= 2; k--) {
00158 c = para.leds + 1 - k;
00159 if (p >= 32768) {
00160 painter.setBrush(color[c]);
00161 } else {
00162 painter.setBrush(color[c].dark(300));
00163 }
00164 if (h >= 32768) {
00165 painter.setPen(color[c]);
00166 } else {
00167 painter.setPen(color[c].dark(300));
00168 }
00169 painter.drawRect(ox + dx * i, oy + dy * c, sx, sy);
00170 p *= para.resoFactor; h *= para.resoFactor;
00171 }
00172 if ( --holdTime[i] <= 0) hold[i] = peak[i];
00173 }
00174 painter.end();
00175 bitBlt(this, 0, 0, buffer);
00176 }
00177
00178 void VUMeter::setPeak(int a[]) {
00179 int i;
00180 for(i = 0; i < tracks + 2; i++) {
00181 if (a[i] > i_peak[i]) i_peak[i] = a[i];
00182 if (a[i] > i_hold[i]) { i_hold[i] = a[i]; holdTime[i] = para.hold; }
00183 }
00184 paint();
00185 }
00186
00187 void VUMeter::setPeak(float a[]) {
00188 int i;
00189 for(i = 0; i < tracks + 2; i++) {
00190 if (a[i] > peak[i]) peak[i] = a[i];
00191 if (a[i] > hold[i]) { hold[i] = a[i]; holdTime[i] = para.hold; }
00192 }
00193 paint();
00194 }
00195
00196 void VUMeter::startTimer() {
00197 vuTimer->start(para.update, FALSE);
00198 }
00199
00200 void VUMeter::stopTimer() {
00201 vuTimer->stop();
00202 }
00203
00204 void VUMeter::readConf() {
00205 Config config("OpieRec");
00206 config.setGroup("VU-Meter");
00207
00208 para.onOff = config.readBoolEntry("OnOff", true);
00209 para.update = config.readNumEntry("Update", 25);
00210 para.hold = config.readNumEntry("Hold", 20);
00211 para.reso = config.readNumEntry("Resolution", 3);
00212 para.leds = config.readNumEntry("LEDs", 20);
00213 para.resoFactor = pow(2, para.reso / 6.0);
00214
00215 colorScheme = config.readNumEntry("colorScheme", 1);
00216 }
00217
00218
00219 void VUMeter::writeConf() {
00220 Config config("OpieRec");
00221 config.setGroup("VU-Meter");
00222
00223 config.writeEntry("OnOff", para.onOff);
00224 config.writeEntry("Update", para.update);
00225 config.writeEntry("Hold", para.hold);
00226 config.writeEntry("Resolution", para.reso);
00227 config.writeEntry("LEDs", para.leds);
00228 }