Fader.cpp

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     
00008     This program is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU General Public License as
00010     published by the Free Software Foundation; either version 2 of the
00011     License, or (at your option) any later version.  See the file
00012     COPYING included with this distribution for more information.
00013 */
00014 
00032 #include "Fader.h"
00033 
00034 #include "base/AudioLevel.h"
00035 
00036 #include <QMouseEvent>
00037 #include <QPixmap>
00038 #include <QWheelEvent>
00039 #include <QPaintEvent>
00040 #include <QPainter>
00041 #include <QInputDialog>
00042 
00043 #include <iostream>
00044 
00045 Fader::Fader(QWidget *parent, bool withoutKnob) :
00046     QWidget(parent),
00047     m_withoutKnob(withoutKnob),
00048     m_value(1.0),
00049     m_peakLeft(0.0),
00050     m_peakRight(0.0),
00051     m_mousePressed(false)
00052 {
00053     setMinimumSize(116, 23);
00054     setMaximumSize(116, 23);
00055     resize(116, 23);
00056 
00057     QString background_path = ":/icons/fader_background.png";
00058     bool ok = m_back.load(background_path);
00059     if (ok == false) {
00060         std::cerr << "Fader: Error loading pixmap" << std::endl;
00061     }
00062 
00063     QString leds_path = ":/icons/fader_leds.png";
00064     ok = m_leds.load(leds_path);
00065     if (ok == false) {
00066         std::cerr <<  "Error loading pixmap" << std::endl;
00067     }
00068 
00069     QString knob_path = ":/icons/fader_knob.png";
00070     ok = m_knob.load(knob_path);
00071     if (ok == false) {
00072         std::cerr <<  "Error loading pixmap" << std::endl;
00073     }
00074 
00075     QString clip_path = ":/icons/fader_knob_red.png";
00076     ok = m_clip.load(clip_path);
00077     if (ok == false) {
00078         std::cerr <<  "Error loading pixmap" << std::endl;
00079     }
00080 }
00081 
00082 Fader::~Fader()
00083 {
00084 
00085 }
00086 
00087 void
00088 Fader::mouseMoveEvent(QMouseEvent *ev)
00089 {
00090     if (ev->button() == Qt::MidButton) {
00091         setValue(1.0);
00092         emit valueChanged(1.0);
00093         update();
00094         ev->accept();
00095         return;
00096     }
00097     if (!m_mousePressed) return;
00098 
00099     int x = ev->x();
00100     int diff = x - m_mousePressX;
00101     if (diff == 0) return;
00102 
00103     int vx = AudioLevel::multiplier_to_fader
00104         (m_mousePressValue, getMaxX(), AudioLevel::LongFader);
00105 
00106     vx += diff;
00107 
00108     if (vx > getMaxX()) vx = getMaxX();
00109     if (vx < 0) vx = 0;
00110 
00111     float fval = AudioLevel::fader_to_multiplier
00112         (vx, getMaxX(), AudioLevel::LongFader);
00113 
00114     setValue(fval);
00115     emit valueChanged(fval);
00116     ev->accept();
00117 }
00118 
00119 
00120 void
00121 Fader::mouseReleaseEvent(QMouseEvent *ev)
00122 {
00123     if (m_mousePressed) {
00124         mouseMoveEvent(ev);
00125         m_mousePressed = false;
00126     }
00127 }
00128 
00129 void
00130 Fader::mouseDoubleClickEvent(QMouseEvent *)
00131 {
00132     bool ok = false;
00133     float min = AudioLevel::fader_to_dB
00134         (0, getMaxX(), AudioLevel::LongFader);
00135     float max = AudioLevel::fader_to_dB
00136         (getMaxX(), getMaxX(), AudioLevel::LongFader);
00137     float deft = AudioLevel::multiplier_to_dB(m_value);
00138 
00139     float dB = QInputDialog::getDouble
00140         (this,
00141          tr("Enter new fader level"),
00142          tr("New fader level, from %1 to %2 dBFS:").arg(min).arg(max),
00143          deft, min, max, 3, &ok);
00144 
00145     if (ok) {
00146         float value = AudioLevel::dB_to_multiplier(dB);
00147         setValue(value);
00148         emit valueChanged(value);
00149         update();
00150     }
00151 }
00152 
00153 void
00154 Fader::mousePressEvent(QMouseEvent *ev)
00155 {
00156     if (ev->button() == Qt::MidButton ||
00157         ((ev->button() == Qt::LeftButton) &&
00158          (ev->modifiers() & Qt::ControlModifier))) {
00159         setValue(1.0);
00160         emit valueChanged(1.0);
00161         update();
00162         return;
00163     }
00164 
00165     if (ev->button() != Qt::LeftButton) return;
00166     m_mousePressed = true;
00167     m_mousePressX = ev->x();
00168     m_mousePressValue = getValue();
00169 }
00170 
00171 
00172 void
00173 Fader::wheelEvent(QWheelEvent *ev)
00174 {
00175     ev->accept();
00176 
00178 
00179     if (ev->delta() > 0) {
00180         setValue(m_value * 1.1);
00181     } else {
00182         setValue(m_value / 1.1);
00183     }
00184 
00185     update();
00186     emit valueChanged(getValue());
00187 }
00188 
00189 void
00190 Fader::enterEvent(QEvent *)
00191 {
00192     emit mouseEntered();
00193 }
00194 
00195 void
00196 Fader::leaveEvent(QEvent *)
00197 {
00198     emit mouseLeft();
00199 }
00200 
00201 void
00202 Fader::setValue(float v)
00203 {
00204     float max = AudioLevel::dB_to_multiplier(10.0);
00205 
00206     if (v > max) {
00207         v = max;
00208     } else if (v < 0.0) {
00209         v = 0.0;
00210     }
00211 
00212     if (m_value != v) {
00213         m_value = v;
00214         float db = AudioLevel::multiplier_to_dB(m_value);
00215         QString text;
00216         if (db <= AudioLevel::DB_FLOOR) {
00217             text = tr("Level: Off");
00218         } else {
00219             text = tr("Level: %1%2.%3%4 dB")
00220                 .arg(db < 0.0 ? "-" : "")
00221                 .arg(abs(int(db)))
00222                 .arg(abs(int(db * 10.0) % 10))
00223                 .arg(abs(int(db * 100.0) % 10));
00224         }
00225         std::cerr << "Fader: setting tooltip to \"" << text.toStdString() << "\"" << std::endl;
00226         QWidget::setToolTip(text);
00227         update();
00228     }
00229 }
00230 
00231 
00232 float
00233 Fader::getValue()
00234 {
00235     return m_value;
00236 }
00237 
00238 
00239 
00240 void
00241 Fader::setPeakLeft(float peak)
00242 {
00243     if (this->m_peakLeft != peak) {
00244         this->m_peakLeft = peak;
00245         update();
00246     }
00247 }
00248 
00249 
00250 void
00251 Fader::setPeakRight(float peak) 
00252 {
00253     if (this->m_peakRight != peak) {
00254         this->m_peakRight = peak;
00255         update();
00256     }
00257 }
00258 
00259 
00260 void
00261 Fader::paintEvent(QPaintEvent *)
00262 {
00263     QPainter painter(this);
00264 
00265     // background
00266     painter.drawPixmap(rect(), m_back, QRect(0, 0, 116, 23));
00267 
00268     int offset_L = AudioLevel::multiplier_to_fader(m_peakLeft, 116,
00269                                                    AudioLevel::IEC268LongMeter);
00270 
00271     painter.drawPixmap(QRect(0, 0, offset_L, 11), m_leds,
00272                        QRect(0, 0, offset_L, 11));
00273 
00274     int offset_R = AudioLevel::multiplier_to_fader(m_peakRight, 116,
00275                                                    AudioLevel::IEC268LongMeter);
00276 
00277     painter.drawPixmap(QRect(0, 11, offset_R, 11), m_leds,
00278                        QRect(0, 11, offset_R, 11));
00279 
00280     if (m_withoutKnob == false) {
00281 
00282         static const uint knob_width = 29;
00283         static const uint knob_height = 9;
00284 
00285         int x = AudioLevel::multiplier_to_fader(m_value, 116 - knob_width,
00286                                                 AudioLevel::LongFader);
00287 
00288         bool clipping = (m_peakLeft > 1.0 || m_peakRight > 1.0);
00289 
00290         painter.drawPixmap(QRect(x, 7, knob_width, knob_height),
00291                            clipping ? m_clip : m_knob,
00292                            QRect(0, 0, knob_width, knob_height));
00293     }
00294 }
00295 
00296 int
00297 Fader::getMaxX() const
00298 {
00299     return 116 - 12;
00300 }

Generated on Wed Feb 20 15:45:25 2008 for SonicVisualiser by  doxygen 1.5.1