annotate widgets/Panner.cpp @ 181:c4d407272010

* Remove some obsolete files
author Chris Cannam
date Wed, 15 Nov 2006 20:00:48 +0000
parents 836498e57f59
children 42118892f428
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@181 24 #include <cmath>
Martin@175 25
Chris@172 26 Panner::Panner(QWidget *parent) :
Chris@172 27 QWidget(parent),
Chris@172 28 m_rectX(0),
Chris@172 29 m_rectY(0),
Chris@172 30 m_rectWidth(1),
Chris@173 31 m_rectHeight(1),
Chris@173 32 m_defaultCentreX(0),
Chris@173 33 m_defaultCentreY(0),
Chris@173 34 m_defaultsSet(false),
Chris@174 35 m_thumbColour(palette().highlightedText().color()),
Chris@174 36 m_backgroundAlpha(255),
Chris@174 37 m_thumbAlpha(255),
Chris@173 38 m_clicked(false)
Chris@172 39 {
Chris@172 40 }
Chris@172 41
Chris@172 42 Panner::~Panner()
Chris@172 43 {
Chris@172 44 }
Chris@172 45
Chris@172 46 void
Chris@174 47 Panner::setAlpha(int backgroundAlpha, int thumbAlpha)
Chris@174 48 {
Chris@174 49 m_backgroundAlpha = backgroundAlpha;
Chris@174 50 m_thumbAlpha = thumbAlpha;
Chris@174 51 }
Chris@174 52
Chris@174 53 void
Chris@172 54 Panner::mousePressEvent(QMouseEvent *e)
Chris@172 55 {
Chris@173 56 if (e->button() == Qt::LeftButton) {
Chris@173 57 m_clicked = true;
Chris@173 58 m_clickPos = e->pos();
Chris@173 59 m_dragStartX = m_rectX;
Chris@173 60 m_dragStartY = m_rectY;
Chris@173 61 } else if (e->button() == Qt::MidButton) {
Chris@173 62 resetToDefault();
Chris@173 63 }
Chris@172 64 }
Chris@172 65
Chris@172 66 void
Chris@172 67 Panner::mouseDoubleClickEvent(QMouseEvent *e)
Chris@172 68 {
Chris@173 69 resetToDefault();
Chris@172 70 }
Chris@172 71
Chris@172 72 void
Chris@172 73 Panner::mouseMoveEvent(QMouseEvent *e)
Chris@172 74 {
Chris@173 75 if (!m_clicked) return;
Chris@173 76
Chris@173 77 float dx = float(e->pos().x() - m_clickPos.x()) / float(width());
Chris@173 78 float dy = float(e->pos().y() - m_clickPos.y()) / float(height());
Chris@173 79
Chris@173 80 m_rectX = m_dragStartX + dx;
Chris@173 81 m_rectY = m_dragStartY + dy;
Chris@173 82
Chris@173 83 normalise();
Chris@174 84 repaint();
Chris@174 85 emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight);
Chris@174 86 emit rectCentreMoved(centreX(), centreY());
Chris@172 87 }
Chris@172 88
Chris@172 89 void
Chris@172 90 Panner::mouseReleaseEvent(QMouseEvent *e)
Chris@172 91 {
Chris@173 92 if (!m_clicked) return;
Chris@173 93
Chris@173 94 mouseMoveEvent(e);
Chris@173 95 m_clicked = false;
Chris@172 96 }
Chris@172 97
Chris@172 98 void
Chris@172 99 Panner::wheelEvent(QWheelEvent *e)
Chris@172 100 {
Chris@173 101 if (e->delta() > 0) {
Chris@173 102 m_rectY += 0.1;
Chris@173 103 } else {
Chris@173 104 m_rectY -= 0.1;
Chris@173 105 }
Chris@173 106
Chris@173 107 normalise();
Chris@173 108 emitAndUpdate();
Chris@172 109 }
Chris@172 110
Chris@172 111 void
Chris@172 112 Panner::paintEvent(QPaintEvent *e)
Chris@172 113 {
Chris@172 114 QPainter paint(this);
Chris@172 115 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@173 116
Chris@173 117 QColor bg(palette().background().color());
Chris@174 118 bg.setAlpha(m_backgroundAlpha);
Chris@173 119
Chris@172 120 paint.setPen(palette().dark().color());
Chris@173 121 paint.setBrush(bg);
Chris@173 122 paint.drawRect(0, 0, width(), height());
Chris@173 123
Chris@174 124 QColor hl(m_thumbColour);
Chris@174 125 hl.setAlpha(m_thumbAlpha);
Chris@173 126
Chris@173 127 paint.setBrush(hl);
Chris@173 128
Chris@173 129 paint.drawRect(lrintf(width() * m_rectX),
Chris@173 130 lrintf(height() * m_rectY),
Chris@173 131 lrintf(width() * m_rectWidth),
Chris@173 132 lrintf(height() * m_rectHeight));
Chris@172 133 }
Chris@172 134
Chris@172 135 void
Chris@172 136 Panner::normalise()
Chris@172 137 {
Chris@172 138 if (m_rectWidth > 1.0) m_rectWidth = 1.0;
Chris@172 139 if (m_rectHeight > 1.0) m_rectHeight = 1.0;
Chris@172 140 if (m_rectX + m_rectWidth > 1.0) m_rectX = 1.0 - m_rectWidth;
Chris@172 141 if (m_rectX < 0) m_rectX = 0;
Chris@172 142 if (m_rectY + m_rectHeight > 1.0) m_rectY = 1.0 - m_rectHeight;
Chris@172 143 if (m_rectY < 0) m_rectY = 0;
Chris@173 144
Chris@173 145 if (!m_defaultsSet) {
Chris@173 146 m_defaultCentreX = centreX();
Chris@173 147 m_defaultCentreY = centreY();
Chris@173 148 m_defaultsSet = true;
Chris@173 149 }
Chris@172 150 }
Chris@172 151
Chris@172 152 void
Chris@172 153 Panner::emitAndUpdate()
Chris@172 154 {
Chris@172 155 emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight);
Chris@173 156 emit rectCentreMoved(centreX(), centreY());
Chris@172 157 update();
Chris@172 158 }
Chris@172 159
Chris@172 160 void
Chris@172 161 Panner::setRectExtents(float x0, float y0, float width, float height)
Chris@172 162 {
Chris@174 163 std::cerr << "Panner::setRectExtents(" << x0 << ", " << y0 << ", "
Chris@174 164 << width << ", " << height << ")" << std::endl;
Chris@174 165
Chris@172 166 if (m_rectX == x0 &&
Chris@172 167 m_rectY == y0 &&
Chris@172 168 m_rectWidth == width &&
Chris@172 169 m_rectHeight == height) {
Chris@172 170 return;
Chris@172 171 }
Chris@173 172
Chris@172 173 m_rectX = x0;
Chris@172 174 m_rectY = y0;
Chris@172 175 m_rectWidth = width;
Chris@172 176 m_rectHeight = height;
Chris@173 177
Chris@172 178 normalise();
Chris@172 179 emitAndUpdate();
Chris@172 180 }
Chris@172 181
Chris@172 182 void
Chris@172 183 Panner::setRectWidth(float width)
Chris@172 184 {
Chris@172 185 if (m_rectWidth == width) return;
Chris@172 186 m_rectWidth = width;
Chris@172 187 normalise();
Chris@172 188 emitAndUpdate();
Chris@172 189 }
Chris@172 190
Chris@172 191 void
Chris@172 192 Panner::setRectHeight(float height)
Chris@172 193 {
Chris@172 194 if (m_rectHeight == height) return;
Chris@172 195 m_rectHeight = height;
Chris@172 196 normalise();
Chris@172 197 emitAndUpdate();
Chris@172 198 }
Chris@172 199
Chris@172 200 void
Chris@172 201 Panner::setRectCentreX(float x)
Chris@172 202 {
Chris@172 203 float x0 = x - m_rectWidth/2;
Chris@172 204 if (x0 == m_rectX) return;
Chris@172 205 m_rectX = x0;
Chris@172 206 normalise();
Chris@172 207 emitAndUpdate();
Chris@172 208 }
Chris@172 209
Chris@172 210 void
Chris@172 211 Panner::setRectCentreY(float y)
Chris@172 212 {
Chris@173 213 float y0 = y - m_rectHeight/2;
Chris@172 214 if (y0 == m_rectY) return;
Chris@172 215 m_rectY = y0;
Chris@172 216 normalise();
Chris@172 217 emitAndUpdate();
Chris@172 218 }
Chris@172 219
Chris@172 220 QSize
Chris@172 221 Panner::sizeHint() const
Chris@172 222 {
Chris@172 223 return QSize(30, 30);
Chris@172 224 }
Chris@172 225
Chris@173 226 void
Chris@173 227 Panner::setDefaultRectCentre(float cx, float cy)
Chris@173 228 {
Chris@173 229 m_defaultCentreX = cx;
Chris@173 230 m_defaultCentreY = cy;
Chris@173 231 m_defaultsSet = true;
Chris@173 232 }
Chris@172 233
Chris@173 234 void
Chris@173 235 Panner::resetToDefault()
Chris@173 236 {
Chris@173 237 float x0 = m_defaultCentreX - m_rectWidth/2;
Chris@173 238 float y0 = m_defaultCentreY - m_rectHeight/2;
Chris@173 239 if (x0 == m_rectX && y0 == m_rectY) return;
Chris@173 240 m_rectX = x0;
Chris@173 241 m_rectY = y0;
Chris@173 242 normalise();
Chris@173 243 emitAndUpdate();
Chris@173 244 }
Chris@172 245
Chris@173 246