svgui  1.9
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     m_mousePressX(0),
00053     m_mousePressValue(0)
00054 {
00055     setMinimumSize(116, 23);
00056     setMaximumSize(116, 23);
00057     resize(116, 23);
00058 
00059     QString background_path = ":/icons/fader_background.png";
00060     bool ok = m_back.load(background_path);
00061     if (ok == false) {
00062         cerr << "Fader: Error loading pixmap" << endl;
00063     }
00064 
00065     QString leds_path = ":/icons/fader_leds.png";
00066     ok = m_leds.load(leds_path);
00067     if (ok == false) {
00068         cerr <<  "Error loading pixmap" << endl;
00069     }
00070 
00071     QString knob_path = ":/icons/fader_knob.png";
00072     ok = m_knob.load(knob_path);
00073     if (ok == false) {
00074         cerr <<  "Error loading pixmap" << endl;
00075     }
00076 
00077     QString clip_path = ":/icons/fader_knob_red.png";
00078     ok = m_clip.load(clip_path);
00079     if (ok == false) {
00080         cerr <<  "Error loading pixmap" << endl;
00081     }
00082 }
00083 
00084 Fader::~Fader()
00085 {
00086 
00087 }
00088 
00089 void
00090 Fader::mouseMoveEvent(QMouseEvent *ev)
00091 {
00092     if (ev->button() == Qt::MidButton) {
00093         setValue(1.0);
00094         emit valueChanged(1.0);
00095         update();
00096         ev->accept();
00097         return;
00098     }
00099     if (!m_mousePressed) return;
00100 
00101     int x = ev->x();
00102     int diff = x - m_mousePressX;
00103     if (diff == 0) return;
00104 
00105     int vx = AudioLevel::multiplier_to_fader
00106         (m_mousePressValue, getMaxX(), AudioLevel::LongFader);
00107 
00108     vx += diff;
00109 
00110     if (vx > getMaxX()) vx = getMaxX();
00111     if (vx < 0) vx = 0;
00112 
00113     float fval = AudioLevel::fader_to_multiplier
00114         (vx, getMaxX(), AudioLevel::LongFader);
00115 
00116     setValue(fval);
00117     emit valueChanged(fval);
00118     ev->accept();
00119 }
00120 
00121 
00122 void
00123 Fader::mouseReleaseEvent(QMouseEvent *ev)
00124 {
00125     if (m_mousePressed) {
00126         mouseMoveEvent(ev);
00127         m_mousePressed = false;
00128     }
00129 }
00130 
00131 void
00132 Fader::mouseDoubleClickEvent(QMouseEvent *)
00133 {
00134     bool ok = false;
00135     float min = AudioLevel::fader_to_dB
00136         (0, getMaxX(), AudioLevel::LongFader);
00137     float max = AudioLevel::fader_to_dB
00138         (getMaxX(), getMaxX(), AudioLevel::LongFader);
00139     float deft = AudioLevel::multiplier_to_dB(m_value);
00140 
00141     float dB = QInputDialog::getDouble
00142         (this,
00143          tr("Enter new fader level"),
00144          tr("New fader level, from %1 to %2 dBFS:").arg(min).arg(max),
00145          deft, min, max, 3, &ok);
00146 
00147     if (ok) {
00148         float value = AudioLevel::dB_to_multiplier(dB);
00149         setValue(value);
00150         emit valueChanged(value);
00151         update();
00152     }
00153 }
00154 
00155 void
00156 Fader::mousePressEvent(QMouseEvent *ev)
00157 {
00158     if (ev->button() == Qt::MidButton ||
00159         ((ev->button() == Qt::LeftButton) &&
00160          (ev->modifiers() & Qt::ControlModifier))) {
00161         setValue(1.0);
00162         emit valueChanged(1.0);
00163         update();
00164         return;
00165     }
00166 
00167     if (ev->button() != Qt::LeftButton) return;
00168     m_mousePressed = true;
00169     m_mousePressX = ev->x();
00170     m_mousePressValue = getValue();
00171 }
00172 
00173 
00174 void
00175 Fader::wheelEvent(QWheelEvent *ev)
00176 {
00177     ev->accept();
00178 
00180 
00181     if (ev->delta() > 0) {
00182         setValue(m_value * 1.1);
00183     } else {
00184         setValue(m_value / 1.1);
00185     }
00186 
00187     update();
00188     emit valueChanged(getValue());
00189 }
00190 
00191 void
00192 Fader::enterEvent(QEvent *)
00193 {
00194     emit mouseEntered();
00195 }
00196 
00197 void
00198 Fader::leaveEvent(QEvent *)
00199 {
00200     emit mouseLeft();
00201 }
00202 
00203 void
00204 Fader::setValue(float v)
00205 {
00206     float max = AudioLevel::dB_to_multiplier(10.0);
00207 
00208     if (v > max) {
00209         v = max;
00210     } else if (v < 0.0) {
00211         v = 0.0;
00212     }
00213 
00214     if (m_value != v) {
00215         m_value = v;
00216         float db = AudioLevel::multiplier_to_dB(m_value);
00217         QString text;
00218         if (db <= AudioLevel::DB_FLOOR) {
00219             text = tr("Level: Off");
00220         } else {
00221             text = tr("Level: %1%2.%3%4 dB")
00222                 .arg(db < 0.0 ? "-" : "")
00223                 .arg(abs(int(db)))
00224                 .arg(abs(int(db * 10.0) % 10))
00225                 .arg(abs(int(db * 100.0) % 10));
00226         }
00227         cerr << "Fader: setting tooltip to \"" << text << "\"" << endl;
00228         QWidget::setToolTip(text);
00229         update();
00230     }
00231 }
00232 
00233 
00234 float
00235 Fader::getValue()
00236 {
00237     return m_value;
00238 }
00239 
00240 
00241 
00242 void
00243 Fader::setPeakLeft(float peak)
00244 {
00245     if (this->m_peakLeft != peak) {
00246         this->m_peakLeft = peak;
00247         update();
00248     }
00249 }
00250 
00251 
00252 void
00253 Fader::setPeakRight(float peak) 
00254 {
00255     if (this->m_peakRight != peak) {
00256         this->m_peakRight = peak;
00257         update();
00258     }
00259 }
00260 
00261 
00262 void
00263 Fader::paintEvent(QPaintEvent *)
00264 {
00265     QPainter painter(this);
00266 
00267     // background
00268     painter.drawPixmap(rect(), m_back, QRect(0, 0, 116, 23));
00269 
00270     int offset_L = AudioLevel::multiplier_to_fader(m_peakLeft, 116,
00271                                                    AudioLevel::IEC268LongMeter);
00272 
00273     painter.drawPixmap(QRect(0, 0, offset_L, 11), m_leds,
00274                        QRect(0, 0, offset_L, 11));
00275 
00276     int offset_R = AudioLevel::multiplier_to_fader(m_peakRight, 116,
00277                                                    AudioLevel::IEC268LongMeter);
00278 
00279     painter.drawPixmap(QRect(0, 11, offset_R, 11), m_leds,
00280                        QRect(0, 11, offset_R, 11));
00281 
00282     if (m_withoutKnob == false) {
00283 
00284         static const uint knob_width = 29;
00285         static const uint knob_height = 9;
00286 
00287         int x = AudioLevel::multiplier_to_fader(m_value, 116 - knob_width,
00288                                                 AudioLevel::LongFader);
00289 
00290         bool clipping = (m_peakLeft > 1.0 || m_peakRight > 1.0);
00291 
00292         painter.drawPixmap(QRect(x, 7, knob_width, knob_height),
00293                            clipping ? m_clip : m_knob,
00294                            QRect(0, 0, knob_width, knob_height));
00295     }
00296 }
00297 
00298 int
00299 Fader::getMaxX() const
00300 {
00301     return 116 - 12;
00302 }