Chris@172: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@172: Chris@172: /* Chris@172: Sonic Visualiser Chris@172: An audio file viewer and annotation editor. Chris@172: Centre for Digital Music, Queen Mary, University of London. Chris@182: This file copyright 2006 QMUL. Chris@172: Chris@172: This program is free software; you can redistribute it and/or Chris@172: modify it under the terms of the GNU General Public License as Chris@172: published by the Free Software Foundation; either version 2 of the Chris@172: License, or (at your option) any later version. See the file Chris@172: COPYING included with this distribution for more information. Chris@172: */ Chris@172: Chris@172: #include "Panner.h" Chris@172: Chris@172: #include Chris@172: #include Chris@172: #include Chris@172: #include Chris@172: Chris@172: #include Chris@181: #include Martin@175: Chris@172: Panner::Panner(QWidget *parent) : Chris@172: QWidget(parent), Chris@172: m_rectX(0), Chris@172: m_rectY(0), Chris@172: m_rectWidth(1), Chris@173: m_rectHeight(1), Chris@256: m_scrollUnit(0), Chris@173: m_defaultCentreX(0), Chris@173: m_defaultCentreY(0), Chris@173: m_defaultsSet(false), Chris@174: m_thumbColour(palette().highlightedText().color()), Chris@174: m_backgroundAlpha(255), Chris@174: m_thumbAlpha(255), Chris@858: m_clicked(false), Chris@858: m_dragStartX(0), Chris@858: m_dragStartY(0) Chris@172: { Chris@172: } Chris@172: Chris@172: Panner::~Panner() Chris@172: { Chris@172: } Chris@172: Chris@172: void Chris@174: Panner::setAlpha(int backgroundAlpha, int thumbAlpha) Chris@174: { Chris@174: m_backgroundAlpha = backgroundAlpha; Chris@174: m_thumbAlpha = thumbAlpha; Chris@174: } Chris@174: Chris@174: void Chris@256: Panner::setScrollUnit(float unit) Chris@256: { Chris@256: m_scrollUnit = unit; Chris@256: } Chris@256: Chris@256: void Chris@256: Panner::scroll(bool up) Chris@256: { Chris@256: float unit = m_scrollUnit; Chris@256: if (unit == 0.f) { Chris@256: unit = float(m_rectHeight) / (6 * float(height())); Chris@908: if (unit < 0.01f) unit = 0.01f; Chris@256: } Chris@256: Chris@256: if (!up) { Chris@256: m_rectY += unit; Chris@256: } else { Chris@256: m_rectY -= unit; Chris@256: } Chris@256: Chris@256: normalise(); Chris@256: emitAndUpdate(); Chris@256: } Chris@256: Chris@256: void Chris@172: Panner::mousePressEvent(QMouseEvent *e) Chris@172: { Chris@188: if (e->button() == Qt::MidButton || Chris@188: ((e->button() == Qt::LeftButton) && Chris@188: (e->modifiers() & Qt::ControlModifier))) { Chris@188: resetToDefault(); Chris@188: } else if (e->button() == Qt::LeftButton) { Chris@173: m_clicked = true; Chris@173: m_clickPos = e->pos(); Chris@173: m_dragStartX = m_rectX; Chris@173: m_dragStartY = m_rectY; Chris@173: } Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::mouseDoubleClickEvent(QMouseEvent *e) Chris@172: { Chris@188: if (e->button() != Qt::LeftButton) { Chris@188: return; Chris@188: } Chris@188: Chris@188: emit doubleClicked(); Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::mouseMoveEvent(QMouseEvent *e) Chris@172: { Chris@173: if (!m_clicked) return; Chris@173: Chris@173: float dx = float(e->pos().x() - m_clickPos.x()) / float(width()); Chris@173: float dy = float(e->pos().y() - m_clickPos.y()) / float(height()); Chris@173: Chris@173: m_rectX = m_dragStartX + dx; Chris@173: m_rectY = m_dragStartY + dy; Chris@173: Chris@173: normalise(); Chris@174: repaint(); Chris@174: emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight); Chris@174: emit rectCentreMoved(centreX(), centreY()); Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::mouseReleaseEvent(QMouseEvent *e) Chris@172: { Chris@173: if (!m_clicked) return; Chris@173: Chris@173: mouseMoveEvent(e); Chris@173: m_clicked = false; Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::wheelEvent(QWheelEvent *e) Chris@172: { Chris@256: scroll(e->delta() > 0); Chris@172: } Chris@172: Chris@172: void Chris@189: Panner::enterEvent(QEvent *) Chris@189: { Chris@189: emit mouseEntered(); Chris@189: } Chris@189: Chris@189: void Chris@189: Panner::leaveEvent(QEvent *) Chris@189: { Chris@189: emit mouseLeft(); Chris@189: } Chris@189: Chris@189: void Chris@249: Panner::paintEvent(QPaintEvent *) Chris@172: { Chris@172: QPainter paint(this); Chris@172: paint.setRenderHint(QPainter::Antialiasing, false); Chris@173: Chris@173: QColor bg(palette().background().color()); Chris@174: bg.setAlpha(m_backgroundAlpha); Chris@173: Chris@172: paint.setPen(palette().dark().color()); Chris@173: paint.setBrush(bg); Chris@438: paint.drawRect(0, 0, width()-1, height()-1); Chris@173: Chris@174: QColor hl(m_thumbColour); Chris@174: hl.setAlpha(m_thumbAlpha); Chris@173: Chris@173: paint.setBrush(hl); Chris@173: Chris@908: int rw = int(lrintf(float(width() - 1) * m_rectWidth)); Chris@908: int rh = int(lrintf(float(height() - 1) * m_rectHeight)); Chris@437: if (rw < 2) rw = 2; Chris@437: if (rh < 2) rh = 2; Chris@437: Chris@908: paint.drawRect(int(lrintf(float(width()) * m_rectX)), Chris@908: int(lrintf(float(height()) * m_rectY)), Chris@437: rw, rh); Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::normalise() Chris@172: { Chris@908: if (m_rectWidth > 1.f) m_rectWidth = 1.f; Chris@908: if (m_rectHeight > 1.f) m_rectHeight = 1.f; Chris@908: if (m_rectX + m_rectWidth > 1.f) m_rectX = 1.f - m_rectWidth; Chris@172: if (m_rectX < 0) m_rectX = 0; Chris@908: if (m_rectY + m_rectHeight > 1.f) m_rectY = 1.f - m_rectHeight; Chris@172: if (m_rectY < 0) m_rectY = 0; Chris@173: Chris@173: if (!m_defaultsSet) { Chris@173: m_defaultCentreX = centreX(); Chris@173: m_defaultCentreY = centreY(); Chris@173: m_defaultsSet = true; Chris@173: } Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::emitAndUpdate() Chris@172: { Chris@172: emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight); Chris@173: emit rectCentreMoved(centreX(), centreY()); Chris@172: update(); Chris@172: } Chris@172: Chris@172: void Chris@188: Panner::getRectExtents(float &x0, float &y0, float &width, float &height) Chris@188: { Chris@188: x0 = m_rectX; Chris@188: y0 = m_rectY; Chris@188: width = m_rectWidth; Chris@188: height = m_rectHeight; Chris@188: } Chris@188: Chris@188: void Chris@172: Panner::setRectExtents(float x0, float y0, float width, float height) Chris@172: { Chris@587: // SVDEBUG << "Panner::setRectExtents(" << x0 << ", " << y0 << ", " Chris@585: // << width << ", " << height << ")" << endl; Chris@174: Chris@172: if (m_rectX == x0 && Chris@172: m_rectY == y0 && Chris@172: m_rectWidth == width && Chris@172: m_rectHeight == height) { Chris@172: return; Chris@172: } Chris@173: Chris@172: m_rectX = x0; Chris@172: m_rectY = y0; Chris@172: m_rectWidth = width; Chris@172: m_rectHeight = height; Chris@173: Chris@172: normalise(); Chris@172: emitAndUpdate(); Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::setRectWidth(float width) Chris@172: { Chris@172: if (m_rectWidth == width) return; Chris@172: m_rectWidth = width; Chris@172: normalise(); Chris@172: emitAndUpdate(); Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::setRectHeight(float height) Chris@172: { Chris@172: if (m_rectHeight == height) return; Chris@172: m_rectHeight = height; Chris@172: normalise(); Chris@172: emitAndUpdate(); Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::setRectCentreX(float x) Chris@172: { Chris@172: float x0 = x - m_rectWidth/2; Chris@172: if (x0 == m_rectX) return; Chris@172: m_rectX = x0; Chris@172: normalise(); Chris@172: emitAndUpdate(); Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::setRectCentreY(float y) Chris@172: { Chris@173: float y0 = y - m_rectHeight/2; Chris@172: if (y0 == m_rectY) return; Chris@172: m_rectY = y0; Chris@172: normalise(); Chris@172: emitAndUpdate(); Chris@172: } Chris@172: Chris@172: QSize Chris@172: Panner::sizeHint() const Chris@172: { Chris@172: return QSize(30, 30); Chris@172: } Chris@172: Chris@173: void Chris@173: Panner::setDefaultRectCentre(float cx, float cy) Chris@173: { Chris@173: m_defaultCentreX = cx; Chris@173: m_defaultCentreY = cy; Chris@173: m_defaultsSet = true; Chris@173: } Chris@172: Chris@173: void Chris@173: Panner::resetToDefault() Chris@173: { Chris@173: float x0 = m_defaultCentreX - m_rectWidth/2; Chris@173: float y0 = m_defaultCentreY - m_rectHeight/2; Chris@173: if (x0 == m_rectX && y0 == m_rectY) return; Chris@173: m_rectX = x0; Chris@173: m_rectY = y0; Chris@173: normalise(); Chris@173: emitAndUpdate(); Chris@173: } Chris@172: Chris@173: