![]() |
Mixxx
|
00001 /*************************************************************************** 00002 wvumeter.cpp - description 00003 ------------------- 00004 begin : Fri Jul 22 2003 00005 copyright : (C) 2003 by Tue & Ken Haste Andersen 00006 email : haste@diku.dk 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #include "wvumeter.h" 00019 #include "wpixmapstore.h" 00020 //Added by qt3to4: 00021 #include <QPaintEvent> 00022 #include <QtGui> 00023 #include <QtCore> 00024 #include <QtDebug> 00025 #include <QPixmap> 00026 00027 #define DEFAULT_FALLTIME 20 00028 #define DEFAULT_FALLSTEP 1 00029 #define DEFAULT_HOLDTIME 400 00030 #define DEFAULT_HOLDSIZE 5 00031 00032 00033 WVuMeter::WVuMeter(QWidget * parent) : WWidget(parent) 00034 { 00035 m_pPixmapBack = 0; 00036 m_pPixmapVu = 0; 00037 m_iPeakHoldSize = m_iPeakPos = 0; 00038 m_iPeakHoldCountdown = 0; 00039 m_iNoPos = 0; 00040 } 00041 00042 WVuMeter::~WVuMeter() 00043 { 00044 resetPositions(); 00045 } 00046 00047 void WVuMeter::setup(QDomNode node) 00048 { 00049 // Set pixmaps 00050 bool bHorizontal = false; 00051 if (!selectNode(node, "Horizontal").isNull() && 00052 selectNodeQString(node, "Horizontal")=="true") 00053 bHorizontal = true; 00054 00055 setPixmaps(getPath(selectNodeQString(node, "PathBack")), 00056 getPath(selectNodeQString(node, "PathVu")), bHorizontal); 00057 00058 m_iPeakHoldSize = selectNodeInt(node, "PeakHoldSize"); 00059 if(m_iPeakHoldSize < 0 || m_iPeakHoldSize > 100) 00060 m_iPeakHoldSize = DEFAULT_HOLDSIZE; 00061 00062 m_iPeakFallStep = selectNodeInt(node, "PeakFallStep"); 00063 if(m_iPeakFallStep < 1 || m_iPeakFallStep > 1000) 00064 m_iPeakFallStep = DEFAULT_FALLSTEP; 00065 00066 m_iPeakHoldTime = selectNodeInt(node, "PeakHoldTime"); 00067 if(m_iPeakHoldTime < 1 || m_iPeakHoldTime > 3000) 00068 m_iPeakHoldTime = DEFAULT_HOLDTIME; 00069 00070 m_iPeakFallTime = selectNodeInt(node, "PeakFallTime"); 00071 if(m_iPeakFallTime < 1 || m_iPeakFallTime > 1000) 00072 m_iPeakFallTime = DEFAULT_FALLTIME; 00073 } 00074 00075 void WVuMeter::resetPositions() 00076 { 00077 if (m_pPixmapBack) 00078 { 00079 WPixmapStore::deletePixmap(m_pPixmapBack); 00080 m_pPixmapBack = 0; 00081 WPixmapStore::deletePixmap(m_pPixmapVu); 00082 m_pPixmapVu = 0; 00083 } 00084 } 00085 00086 void WVuMeter::setPixmaps(const QString &backFilename, const QString &vuFilename, bool bHorizontal) 00087 { 00088 m_pPixmapBack = WPixmapStore::getPixmap(backFilename); 00089 if (!m_pPixmapBack || m_pPixmapBack->size()==QSize(0,0)) 00090 qDebug() << "WVuMeter: Error loading back pixmap" << backFilename; 00091 m_pPixmapVu = WPixmapStore::getPixmap(vuFilename); 00092 if (!m_pPixmapVu || m_pPixmapVu->size()==QSize(0,0)) 00093 qDebug() << "WVuMeter: Error loading vu pixmap" << vuFilename; 00094 00095 setFixedSize(m_pPixmapBack->size()); 00096 m_bHorizontal = bHorizontal; 00097 if (m_bHorizontal) 00098 m_iNoPos = m_pPixmapVu->width(); 00099 else 00100 m_iNoPos = m_pPixmapVu->height(); 00101 } 00102 00103 void WVuMeter::setValue(double fValue) 00104 { 00105 int idx = (int)(fValue * (float)(m_iNoPos)/128.); 00106 // Range check 00107 if (idx>m_iNoPos) 00108 idx = m_iNoPos; 00109 else if (idx<0) 00110 idx = 0; 00111 00112 setPeak(idx); 00113 m_fValue = fValue; 00114 00115 QTime currentTime = QTime::currentTime(); 00116 int msecsElapsed = m_lastUpdate.msecsTo(currentTime); 00117 m_lastUpdate = currentTime; 00118 updateState(msecsElapsed); 00119 00120 //Post a paintEvent() message, so that the widget repaints. 00121 update(); 00122 } 00123 00124 void WVuMeter::setPeak(int pos) 00125 { 00126 if(pos > m_iPeakPos) 00127 { 00128 m_iPeakPos = pos; 00129 m_iPeakHoldCountdown = m_iPeakHoldTime; 00130 } 00131 } 00132 00133 void WVuMeter::updateState(int msecsElapsed) 00134 { 00135 // If we're holding at a peak then don't update anything 00136 m_iPeakHoldCountdown -= msecsElapsed; 00137 if (m_iPeakHoldCountdown > 0) { 00138 return; 00139 } else { 00140 m_iPeakHoldCountdown = 0; 00141 } 00142 00143 // Otherwise, decrement the peak position by the fall step size times the 00144 // milliseconds elapsed over the fall time multiplier. The peak will fall 00145 // FallStep times (out of 128 steps) every FallTime milliseconds. 00146 00147 m_iPeakPos -= float(m_iPeakFallStep) * float(msecsElapsed) / float(m_iPeakFallTime); 00148 if (m_iPeakPos < 0) 00149 m_iPeakPos = 0; 00150 } 00151 00152 void WVuMeter::paintEvent(QPaintEvent *) 00153 { 00154 if (m_pPixmapBack!=0) 00155 { 00156 int idx = (int)(m_fValue*(float)(m_iNoPos)/128.); 00157 00158 // Range check 00159 if (idx>m_iNoPos) 00160 idx = m_iNoPos; 00161 else if (idx<0) 00162 idx = 0; 00163 00164 QPainter painter(this); 00165 // Draw back 00166 painter.drawPixmap(0, 0, *m_pPixmapBack); 00167 00168 // Draw (part of) vu 00169 if (m_bHorizontal) 00170 { 00171 //This is a hack to fix something weird with horizontal VU meters: 00172 if(idx == 0) 00173 idx = 1; 00174 painter.drawPixmap(0, 0, *m_pPixmapVu, 0, 0, idx, m_pPixmapVu->height()); 00175 if(m_iPeakHoldSize > 0 && m_iPeakPos > 0) 00176 { 00177 painter.drawPixmap(m_iPeakPos-m_iPeakHoldSize, 0, *m_pPixmapVu, 00178 m_iPeakPos-m_iPeakHoldSize, 0, m_iPeakHoldSize, m_pPixmapVu->height()); 00179 } 00180 } 00181 else 00182 { 00183 painter.drawPixmap(0, m_iNoPos-idx, *m_pPixmapVu, 0, m_iNoPos-idx, m_pPixmapVu->width(), idx); 00184 if(m_iPeakHoldSize > 0 && m_iPeakPos > 0) 00185 { 00186 painter.drawPixmap(0, m_pPixmapVu->height()-m_iPeakPos, *m_pPixmapVu, 00187 0, m_pPixmapVu->height()-m_iPeakPos, m_pPixmapVu->width(), m_iPeakHoldSize); 00188 } 00189 } 00190 } 00191 } 00192 00193 00194