annotate widgets/Panner.cpp @ 174:96b8a790730a

* Wire up the Panner widget in the pane, and do some tidying in Pane
author Chris Cannam
date Thu, 19 Oct 2006 13:05:39 +0000
parents 9c40dc10c88c
children 836498e57f59
rev   line source
Chris@172 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@172 2
Chris@172 3 /*
Chris@172 4 Sonic Visualiser
Chris@172 5 An audio file viewer and annotation editor.
Chris@172 6 Centre for Digital Music, Queen Mary, University of London.
Chris@172 7 This file copyright 2006 Chris Cannam.
Chris@172 8
Chris@172 9 This program is free software; you can redistribute it and/or
Chris@172 10 modify it under the terms of the GNU General Public License as
Chris@172 11 published by the Free Software Foundation; either version 2 of the
Chris@172 12 License, or (at your option) any later version. See the file
Chris@172 13 COPYING included with this distribution for more information.
Chris@172 14 */
Chris@172 15
Chris@172 16 #include "Panner.h"
Chris@172 17
Chris@172 18 #include <QMouseEvent>
Chris@172 19 #include <QPaintEvent>
Chris@172 20 #include <QWheelEvent>
Chris@172 21 #include <QPainter>
Chris@172 22
Chris@172 23 #include <iostream>
Chris@172 24
Chris@172 25 Panner::Panner(QWidget *parent) :
Chris@172 26 QWidget(parent),
Chris@172 27 m_rectX(0),
Chris@172 28 m_rectY(0),
Chris@172 29 m_rectWidth(1),
Chris@173 30 m_rectHeight(1),
Chris@173 31 m_defaultCentreX(0),
Chris@173 32 m_defaultCentreY(0),
Chris@173 33 m_defaultsSet(false),
Chris@174 34 m_thumbColour(palette().highlightedText().color()),
Chris@174 35 m_backgroundAlpha(255),
Chris@174 36 m_thumbAlpha(255),
Chris@173 37 m_clicked(false)
Chris@172 38 {
Chris@172 39 }
Chris@172 40
Chris@172 41 Panner::~Panner()
Chris@172 42 {
Chris@172 43 }
Chris@172 44
Chris@172 45 void
Chris@174 46 Panner::setAlpha(int backgroundAlpha, int thumbAlpha)
Chris@174 47 {
Chris@174 48 m_backgroundAlpha = backgroundAlpha;
Chris@174 49 m_thumbAlpha = thumbAlpha;
Chris@174 50 }
Chris@174 51
Chris@174 52 void
Chris@172 53 Panner::mousePressEvent(QMouseEvent *e)
Chris@172 54 {
Chris@173 55 if (e->button() == Qt::LeftButton) {
Chris@173 56 m_clicked = true;
Chris@173 57 m_clickPos = e->pos();
Chris@173 58 m_dragStartX = m_rectX;
Chris@173 59 m_dragStartY = m_rectY;
Chris@173 60 } else if (e->button() == Qt::MidButton) {
Chris@173 61 resetToDefault();
Chris@173 62 }
Chris@172 63 }
Chris@172 64
Chris@172 65 void
Chris@172 66 Panner::mouseDoubleClickEvent(QMouseEvent *e)
Chris@172 67 {
Chris@173 68 resetToDefault();
Chris@172 69 }
Chris@172 70
Chris@172 71 void
Chris@172 72 Panner::mouseMoveEvent(QMouseEvent *e)
Chris@172 73 {
Chris@173 74 if (!m_clicked) return;
Chris@173 75
Chris@173 76 float dx = float(e->pos().x() - m_clickPos.x()) / float(width());
Chris@173 77 float dy = float(e->pos().y() - m_clickPos.y()) / float(height());
Chris@173 78
Chris@173 79 m_rectX = m_dragStartX + dx;
Chris@173 80 m_rectY = m_dragStartY + dy;
Chris@173 81
Chris@173 82 normalise();
Chris@174 83 repaint();
Chris@174 84 emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight);
Chris@174 85 emit rectCentreMoved(centreX(), centreY());
Chris@172 86 }
Chris@172 87
Chris@172 88 void
Chris@172 89 Panner::mouseReleaseEvent(QMouseEvent *e)
Chris@172 90 {
Chris@173 91 if (!m_clicked) return;
Chris@173 92
Chris@173 93 mouseMoveEvent(e);
Chris@173 94 m_clicked = false;
Chris@172 95 }
Chris@172 96
Chris@172 97 void
Chris@172 98 Panner::wheelEvent(QWheelEvent *e)
Chris@172 99 {
Chris@173 100 if (e->delta() > 0) {
Chris@173 101 m_rectY += 0.1;
Chris@173 102 } else {
Chris@173 103 m_rectY -= 0.1;
Chris@173 104 }
Chris@173 105
Chris@173 106 normalise();
Chris@173 107 emitAndUpdate();
Chris@172 108 }
Chris@172 109
Chris@172 110 void
Chris@172 111 Panner::paintEvent(QPaintEvent *e)
Chris@172 112 {
Chris@172 113 QPainter paint(this);
Chris@172 114 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@173 115
Chris@173 116 QColor bg(palette().background().color());
Chris@174 117 bg.setAlpha(m_backgroundAlpha);
Chris@173 118
Chris@172 119 paint.setPen(palette().dark().color());
Chris@173 120 paint.setBrush(bg);
Chris@173 121 paint.drawRect(0, 0, width(), height());
Chris@173 122
Chris@174 123 QColor hl(m_thumbColour);
Chris@174 124 hl.setAlpha(m_thumbAlpha);
Chris@173 125
Chris@173 126 paint.setBrush(hl);
Chris@173 127
Chris@173 128 paint.drawRect(lrintf(width() * m_rectX),
Chris@173 129 lrintf(height() * m_rectY),
Chris@173 130 lrintf(width() * m_rectWidth),
Chris@173 131 lrintf(height() * m_rectHeight));
Chris@172 132 }
Chris@172 133
Chris@172 134 void
Chris@172 135 Panner::normalise()
Chris@172 136 {
Chris@172 137 if (m_rectWidth > 1.0) m_rectWidth = 1.0;
Chris@172 138 if (m_rectHeight > 1.0) m_rectHeight = 1.0;
Chris@172 139 if (m_rectX + m_rectWidth > 1.0) m_rectX = 1.0 - m_rectWidth;
Chris@172 140 if (m_rectX < 0) m_rectX = 0;
Chris@172 141 if (m_rectY + m_rectHeight > 1.0) m_rectY = 1.0 - m_rectHeight;
Chris@172 142 if (m_rectY < 0) m_rectY = 0;
Chris@173 143
Chris@173 144 if (!m_defaultsSet) {
Chris@173 145 m_defaultCentreX = centreX();
Chris@173 146 m_defaultCentreY = centreY();
Chris@173 147 m_defaultsSet = true;
Chris@173 148 }
Chris@172 149 }
Chris@172 150
Chris@172 151 void
Chris@172 152 Panner::emitAndUpdate()
Chris@172 153 {
Chris@172 154 emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight);
Chris@173 155 emit rectCentreMoved(centreX(), centreY());
Chris@172 156 update();
Chris@172 157 }
Chris@172 158
Chris@172 159 void
Chris@172 160 Panner::setRectExtents(float x0, float y0, float width, float height)
Chris@172 161 {
Chris@174 162 std::cerr << "Panner::setRectExtents(" << x0 << ", " << y0 << ", "
Chris@174 163 << width << ", " << height << ")" << std::endl;
Chris@174 164
Chris@172 165 if (m_rectX == x0 &&
Chris@172 166 m_rectY == y0 &&
Chris@172 167 m_rectWidth == width &&
Chris@172 168 m_rectHeight == height) {
Chris@172 169 return;
Chris@172 170 }
Chris@173 171
Chris@172 172 m_rectX = x0;
Chris@172 173 m_rectY = y0;
Chris@172 174 m_rectWidth = width;
Chris@172 175 m_rectHeight = height;
Chris@173 176
Chris@172 177 normalise();
Chris@172 178 emitAndUpdate();
Chris@172 179 }
Chris@172 180
Chris@172 181 void
Chris@172 182 Panner::setRectWidth(float width)
Chris@172 183 {
Chris@172 184 if (m_rectWidth == width) return;
Chris@172 185 m_rectWidth = width;
Chris@172 186 normalise();
Chris@172 187 emitAndUpdate();
Chris@172 188 }
Chris@172 189
Chris@172 190 void
Chris@172 191 Panner::setRectHeight(float height)
Chris@172 192 {
Chris@172 193 if (m_rectHeight == height) return;
Chris@172 194 m_rectHeight = height;
Chris@172 195 normalise();
Chris@172 196 emitAndUpdate();
Chris@172 197 }
Chris@172 198
Chris@172 199 void
Chris@172 200 Panner::setRectCentreX(float x)
Chris@172 201 {
Chris@172 202 float x0 = x - m_rectWidth/2;
Chris@172 203 if (x0 == m_rectX) return;
Chris@172 204 m_rectX = x0;
Chris@172 205 normalise();
Chris@172 206 emitAndUpdate();
Chris@172 207 }
Chris@172 208
Chris@172 209 void
Chris@172 210 Panner::setRectCentreY(float y)
Chris@172 211 {
Chris@173 212 float y0 = y - m_rectHeight/2;
Chris@172 213 if (y0 == m_rectY) return;
Chris@172 214 m_rectY = y0;
Chris@172 215 normalise();
Chris@172 216 emitAndUpdate();
Chris@172 217 }
Chris@172 218
Chris@172 219 QSize
Chris@172 220 Panner::sizeHint() const
Chris@172 221 {
Chris@172 222 return QSize(30, 30);
Chris@172 223 }
Chris@172 224
Chris@173 225 void
Chris@173 226 Panner::setDefaultRectCentre(float cx, float cy)
Chris@173 227 {
Chris@173 228 m_defaultCentreX = cx;
Chris@173 229 m_defaultCentreY = cy;
Chris@173 230 m_defaultsSet = true;
Chris@173 231 }
Chris@172 232
Chris@173 233 void
Chris@173 234 Panner::resetToDefault()
Chris@173 235 {
Chris@173 236 float x0 = m_defaultCentreX - m_rectWidth/2;
Chris@173 237 float y0 = m_defaultCentreY - m_rectHeight/2;
Chris@173 238 if (x0 == m_rectX && y0 == m_rectY) return;
Chris@173 239 m_rectX = x0;
Chris@173 240 m_rectY = y0;
Chris@173 241 normalise();
Chris@173 242 emitAndUpdate();
Chris@173 243 }
Chris@172 244
Chris@173 245