Chris@0: /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: A waveform viewer and audio annotation editor. Chris@5: Chris Cannam, Queen Mary University of London, 2005-2006 Chris@0: Chris@0: This is experimental software. Not for distribution. Chris@0: */ Chris@0: Chris@0: /** Chris@0: * A rotary dial widget. Chris@0: * Chris@0: * Based on an original design by Thorsten Wilms. Chris@0: * Chris@0: * Implemented as a widget for the Rosegarden MIDI and audio sequencer Chris@0: * and notation editor by Chris Cannam. Chris@0: * Chris@0: * Extracted into a standalone Qt3 widget by Pedro Lopez-Cabanillas Chris@0: * and adapted for use in QSynth. Chris@0: * Chris@0: * Ported to Qt4 by Chris Cannam. Chris@0: * Chris@0: * This file copyright 2003-2005 Chris Cannam, copyright 2005 Pedro Chris@0: * Lopez-Cabanillas. Chris@0: * Chris@0: * This program is free software; you can redistribute it and/or Chris@0: * modify it under the terms of the GNU General Public License as Chris@0: * published by the Free Software Foundation; either version 2 of the Chris@0: * License, or (at your option) any later version. See the file Chris@0: * COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "AudioDial.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: using std::endl; Chris@0: using std::cerr; Chris@0: Chris@0: Chris@0: //!!! Pedro updated his version to use my up/down response code from RG -- need to grab that code in preference to this version from Rui Chris@0: Chris@0: Chris@0: //------------------------------------------------------------------------- Chris@0: // AudioDial - Instance knob widget class. Chris@0: // Chris@0: Chris@0: #define AUDIO_DIAL_MIN (0.25 * M_PI) Chris@0: #define AUDIO_DIAL_MAX (1.75 * M_PI) Chris@0: #define AUDIO_DIAL_RANGE (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) Chris@0: Chris@0: Chris@0: // Constructor. Chris@0: AudioDial::AudioDial(QWidget *parent) : Chris@0: QDial(parent), Chris@0: m_knobColor(Qt::black), m_meterColor(Qt::white) Chris@0: { Chris@0: m_mouseDial = false; Chris@0: m_mousePressed = false; Chris@0: } Chris@0: Chris@0: Chris@0: // Destructor. Chris@0: AudioDial::~AudioDial (void) Chris@0: { Chris@0: } Chris@0: Chris@0: Chris@0: void AudioDial::paintEvent(QPaintEvent *) Chris@0: { Chris@0: QPainter paint; Chris@0: Chris@0: float angle = AUDIO_DIAL_MIN // offset Chris@0: + (AUDIO_DIAL_RANGE * Chris@0: (float(QDial::value() - QDial::minimum()) / Chris@0: (float(QDial::maximum() - QDial::minimum())))); Chris@0: int degrees = int(angle * 180.0 / M_PI); Chris@0: Chris@0: int ns = notchSize(); Chris@0: int numTicks = 1 + (maximum() + ns - minimum()) / ns; Chris@0: Chris@0: QColor knobColor(m_knobColor); Chris@0: if (knobColor == Qt::black) Chris@0: knobColor = palette().mid().color(); Chris@0: Chris@0: QColor meterColor(m_meterColor); Chris@0: if (!isEnabled()) Chris@0: meterColor = palette().mid().color(); Chris@0: else if (m_meterColor == Qt::white) Chris@0: meterColor = palette().highlight().color(); Chris@0: Chris@0: int m_size = width() < height() ? width() : height(); Chris@0: int scale = 1; Chris@0: int width = m_size - 2*scale, height = m_size - 2*scale; Chris@0: Chris@0: paint.begin(this); Chris@0: paint.setRenderHint(QPainter::Antialiasing, true); Chris@0: paint.translate(1, 1); Chris@0: Chris@0: QPen pen; Chris@0: QColor c; Chris@0: Chris@0: // Knob body and face... Chris@0: Chris@0: c = knobColor; Chris@0: pen.setColor(knobColor); Chris@0: pen.setWidth(scale * 2); Chris@0: pen.setCapStyle(Qt::FlatCap); Chris@0: Chris@0: paint.setPen(pen); Chris@0: paint.setBrush(c); Chris@0: Chris@0: int indent = (int)(width * 0.15 + 1); Chris@0: Chris@0: paint.drawEllipse(indent-1, indent-1, width-2*indent, width-2*indent); Chris@0: Chris@0: pen.setWidth(3 * scale); Chris@0: int pos = indent-1 + (width-2*indent) / 20; Chris@0: int darkWidth = (width-2*indent) * 3 / 4; Chris@0: while (darkWidth) { Chris@0: c = c.light(102); Chris@0: pen.setColor(c); Chris@0: paint.setPen(pen); Chris@0: paint.drawEllipse(pos, pos, darkWidth, darkWidth); Chris@0: if (!--darkWidth) break; Chris@0: paint.drawEllipse(pos, pos, darkWidth, darkWidth); Chris@0: if (!--darkWidth) break; Chris@0: paint.drawEllipse(pos, pos, darkWidth, darkWidth); Chris@0: ++pos; --darkWidth; Chris@0: } Chris@0: Chris@0: // Tick notches... Chris@0: Chris@0: if ( true/* notchesVisible() */) { Chris@0: // std::cerr << "Notches visible" << std::endl; Chris@0: pen.setColor(palette().dark().color()); Chris@0: pen.setWidth(scale); Chris@0: paint.setPen(pen); Chris@0: for (int i = 0; i < numTicks; ++i) { Chris@0: int div = numTicks; Chris@0: if (div > 1) --div; Chris@0: drawTick(paint, AUDIO_DIAL_MIN + (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) * i / div, Chris@0: width, true); Chris@0: } Chris@0: } Chris@0: Chris@0: // The bright metering bit... Chris@0: Chris@0: c = meterColor; Chris@0: pen.setColor(c); Chris@0: pen.setWidth(indent); Chris@0: paint.setPen(pen); Chris@0: Chris@0: // std::cerr << "degrees " << degrees << ", gives us " << -(degrees - 45) * 16 << std::endl; Chris@0: Chris@0: int arcLen = -(degrees - 45) * 16; Chris@0: if (arcLen == 0) arcLen = -16; Chris@0: Chris@0: paint.drawArc(indent/2, indent/2, Chris@0: width-indent, width-indent, (180 + 45) * 16, arcLen); Chris@0: Chris@0: paint.setBrush(Qt::NoBrush); Chris@0: Chris@0: // Shadowing... Chris@0: Chris@0: pen.setWidth(scale); Chris@0: paint.setPen(pen); Chris@0: Chris@0: // Knob shadow... Chris@0: Chris@0: int shadowAngle = -720; Chris@0: c = knobColor.dark(); Chris@0: for (int arc = 120; arc < 2880; arc += 240) { Chris@0: pen.setColor(c); Chris@0: paint.setPen(pen); Chris@0: paint.drawArc(indent, indent, Chris@0: width-2*indent, width-2*indent, shadowAngle + arc, 240); Chris@0: paint.drawArc(indent, indent, Chris@0: width-2*indent, width-2*indent, shadowAngle - arc, 240); Chris@0: c = c.light(110); Chris@0: } Chris@0: Chris@0: // Scale shadow... Chris@0: Chris@0: shadowAngle = 2160; Chris@0: c = palette().dark().color(); Chris@0: for (int arc = 120; arc < 2880; arc += 240) { Chris@0: pen.setColor(c); Chris@0: paint.setPen(pen); Chris@0: paint.drawArc(scale/2, scale/2, Chris@0: width-scale, width-scale, shadowAngle + arc, 240); Chris@0: paint.drawArc(scale/2, scale/2, Chris@0: width-scale, width-scale, shadowAngle - arc, 240); Chris@0: c = c.light(108); Chris@0: } Chris@0: Chris@0: // Undraw the bottom part... Chris@0: Chris@0: pen.setColor(palette().background().color()); Chris@0: pen.setWidth(scale * 4); Chris@0: paint.setPen(pen); Chris@0: paint.drawArc(scale/2, scale/2, Chris@0: width-scale, width-scale, -45 * 16, -92 * 16); Chris@0: Chris@0: // Scale ends... Chris@0: Chris@0: pen.setColor(palette().dark().color()); Chris@0: pen.setWidth(scale); Chris@0: paint.setPen(pen); Chris@0: for (int i = 0; i < numTicks; ++i) { Chris@0: if (i != 0 && i != numTicks - 1) continue; Chris@0: int div = numTicks; Chris@0: if (div > 1) --div; Chris@0: drawTick(paint, AUDIO_DIAL_MIN + (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) * i / div, Chris@0: width, false); Chris@0: } Chris@0: Chris@0: // Pointer notch... Chris@0: Chris@0: float hyp = float(width) / 2.0; Chris@0: float len = hyp - indent; Chris@0: --len; Chris@0: Chris@0: float x0 = hyp; Chris@0: float y0 = hyp; Chris@0: Chris@0: float x = hyp - len * sin(angle); Chris@0: float y = hyp + len * cos(angle); Chris@0: Chris@0: c = palette().dark().color(); Chris@0: pen.setColor(isEnabled() ? c.dark(130) : c); Chris@0: pen.setWidth(scale * 2); Chris@0: paint.setPen(pen); Chris@0: paint.drawLine(int(x0), int(y0), int(x), int(y)); Chris@0: Chris@0: paint.end(); Chris@0: } Chris@0: Chris@0: Chris@0: void AudioDial::drawTick(QPainter &paint, Chris@0: float angle, int size, bool internal) Chris@0: { Chris@0: float hyp = float(size) / 2.0; Chris@0: float x0 = hyp - (hyp - 1) * sin(angle); Chris@0: float y0 = hyp + (hyp - 1) * cos(angle); Chris@0: Chris@0: // cerr << "drawTick: angle " << angle << ", size " << size << ", internal " << internal << endl; Chris@0: Chris@0: if (internal) { Chris@0: Chris@0: float len = hyp / 4; Chris@0: float x1 = hyp - (hyp - len) * sin(angle); Chris@0: float y1 = hyp + (hyp - len) * cos(angle); Chris@0: Chris@0: paint.drawLine(int(x0), int(y0), int(x1), int(y1)); Chris@0: Chris@0: } else { Chris@0: Chris@0: float len = hyp / 4; Chris@0: float x1 = hyp - (hyp + len) * sin(angle); Chris@0: float y1 = hyp + (hyp + len) * cos(angle); Chris@0: Chris@0: paint.drawLine(int(x0), int(y0), int(x1), int(y1)); Chris@0: } Chris@0: } Chris@0: Chris@0: Chris@0: void AudioDial::setKnobColor(const QColor& color) Chris@0: { Chris@0: m_knobColor = color; Chris@0: update(); Chris@0: } Chris@0: Chris@0: Chris@0: void AudioDial::setMeterColor(const QColor& color) Chris@0: { Chris@0: m_meterColor = color; Chris@0: update(); Chris@0: } Chris@0: Chris@0: Chris@0: void AudioDial::setMouseDial(bool mouseDial) Chris@0: { Chris@0: m_mouseDial = mouseDial; Chris@0: } Chris@0: Chris@0: Chris@0: // Alternate mouse behavior event handlers. Chris@0: void AudioDial::mousePressEvent(QMouseEvent *mouseEvent) Chris@0: { Chris@0: if (m_mouseDial) { Chris@0: QDial::mousePressEvent(mouseEvent); Chris@0: } else if (mouseEvent->button() == Qt::LeftButton) { Chris@0: m_mousePressed = true; Chris@0: m_posMouse = mouseEvent->pos(); Chris@0: } Chris@0: } Chris@0: Chris@0: Chris@0: void AudioDial::mouseMoveEvent(QMouseEvent *mouseEvent) Chris@0: { Chris@0: if (m_mouseDial) { Chris@0: QDial::mouseMoveEvent(mouseEvent); Chris@0: } else if (m_mousePressed) { Chris@0: const QPoint& posMouse = mouseEvent->pos(); Chris@0: int v = QDial::value() Chris@0: + (posMouse.x() - m_posMouse.x()) Chris@0: + (m_posMouse.y() - posMouse.y()); Chris@0: if (v > QDial::maximum()) Chris@0: v = QDial::maximum(); Chris@0: else Chris@0: if (v < QDial::minimum()) Chris@0: v = QDial::minimum(); Chris@0: m_posMouse = posMouse; Chris@0: QDial::setValue(v); Chris@0: } Chris@0: } Chris@0: Chris@0: Chris@0: void AudioDial::mouseReleaseEvent(QMouseEvent *mouseEvent) Chris@0: { Chris@0: if (m_mouseDial) { Chris@0: QDial::mouseReleaseEvent(mouseEvent); Chris@0: } else if (m_mousePressed) { Chris@0: m_mousePressed = false; Chris@0: } Chris@0: } Chris@0: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "AudioDial.moc.cpp" Chris@0: #endif Chris@0: