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@172: This file copyright 2006 Chris Cannam. 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@172: 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@173: m_defaultCentreX(0), Chris@173: m_defaultCentreY(0), Chris@173: m_defaultsSet(false), Chris@173: m_clicked(false) Chris@172: { Chris@172: } Chris@172: Chris@172: Panner::~Panner() Chris@172: { Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::mousePressEvent(QMouseEvent *e) Chris@172: { Chris@173: 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: } else if (e->button() == Qt::MidButton) { Chris@173: resetToDefault(); Chris@173: } Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::mouseDoubleClickEvent(QMouseEvent *e) Chris@172: { Chris@173: resetToDefault(); 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@173: emitAndUpdate(); 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@173: if (e->delta() > 0) { Chris@173: m_rectY += 0.1; Chris@173: } else { Chris@173: m_rectY -= 0.1; Chris@173: } Chris@173: Chris@173: normalise(); Chris@173: emitAndUpdate(); Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::paintEvent(QPaintEvent *e) Chris@172: { Chris@172: QPainter paint(this); Chris@172: paint.setRenderHint(QPainter::Antialiasing, false); Chris@173: Chris@173: QColor bg(palette().background().color()); Chris@173: bg.setAlpha(80); Chris@173: Chris@172: paint.setPen(palette().dark().color()); Chris@173: paint.setBrush(bg); Chris@173: paint.drawRect(0, 0, width(), height()); Chris@173: Chris@173: QColor hl(palette().highlight().color()); Chris@173: hl.setAlpha(80); Chris@173: Chris@173: paint.setBrush(hl); Chris@173: Chris@173: paint.drawRect(lrintf(width() * m_rectX), Chris@173: lrintf(height() * m_rectY), Chris@173: lrintf(width() * m_rectWidth), Chris@173: lrintf(height() * m_rectHeight)); Chris@172: } Chris@172: Chris@172: void Chris@172: Panner::normalise() Chris@172: { Chris@172: if (m_rectWidth > 1.0) m_rectWidth = 1.0; Chris@172: if (m_rectHeight > 1.0) m_rectHeight = 1.0; Chris@172: if (m_rectX + m_rectWidth > 1.0) m_rectX = 1.0 - m_rectWidth; Chris@172: if (m_rectX < 0) m_rectX = 0; Chris@172: if (m_rectY + m_rectHeight > 1.0) m_rectY = 1.0 - 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@172: Panner::setRectExtents(float x0, float y0, float width, float height) Chris@172: { 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: