annotate widgets/Panner.cpp @ 172:d0b95a8cac96

* A start to a panner widget to go with thumbwheels
author Chris Cannam
date Wed, 18 Oct 2006 16:38:30 +0000
parents
children 9c40dc10c88c
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@172 30 m_rectHeight(1)
Chris@172 31 {
Chris@172 32 }
Chris@172 33
Chris@172 34 Panner::~Panner()
Chris@172 35 {
Chris@172 36 }
Chris@172 37
Chris@172 38 void
Chris@172 39 Panner::mousePressEvent(QMouseEvent *e)
Chris@172 40 {
Chris@172 41 }
Chris@172 42
Chris@172 43 void
Chris@172 44 Panner::mouseDoubleClickEvent(QMouseEvent *e)
Chris@172 45 {
Chris@172 46 }
Chris@172 47
Chris@172 48 void
Chris@172 49 Panner::mouseMoveEvent(QMouseEvent *e)
Chris@172 50 {
Chris@172 51 }
Chris@172 52
Chris@172 53 void
Chris@172 54 Panner::mouseReleaseEvent(QMouseEvent *e)
Chris@172 55 {
Chris@172 56 }
Chris@172 57
Chris@172 58 void
Chris@172 59 Panner::wheelEvent(QWheelEvent *e)
Chris@172 60 {
Chris@172 61 }
Chris@172 62
Chris@172 63 void
Chris@172 64 Panner::paintEvent(QPaintEvent *e)
Chris@172 65 {
Chris@172 66 QPainter paint(this);
Chris@172 67 paint.fillRect(rect(), palette().background().color());
Chris@172 68 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@172 69 paint.setPen(palette().dark().color());
Chris@172 70 paint.setBrush(palette().highlight().color());
Chris@172 71 paint.drawRect(QRectF(width() * m_rectX, height() - height() * m_rectY,
Chris@172 72 width() * m_rectWidth, height() * m_rectHeight));
Chris@172 73 }
Chris@172 74
Chris@172 75 void
Chris@172 76 Panner::normalise()
Chris@172 77 {
Chris@172 78 if (m_rectWidth > 1.0) m_rectWidth = 1.0;
Chris@172 79 if (m_rectHeight > 1.0) m_rectHeight = 1.0;
Chris@172 80 if (m_rectX + m_rectWidth > 1.0) m_rectX = 1.0 - m_rectWidth;
Chris@172 81 if (m_rectX < 0) m_rectX = 0;
Chris@172 82 if (m_rectY + m_rectHeight > 1.0) m_rectY = 1.0 - m_rectHeight;
Chris@172 83 if (m_rectY < 0) m_rectY = 0;
Chris@172 84 }
Chris@172 85
Chris@172 86 void
Chris@172 87 Panner::emitAndUpdate()
Chris@172 88 {
Chris@172 89 emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight);
Chris@172 90 emit rectCentreMoved(m_rectX + (m_rectWidth/2), m_rectY + (m_rectHeight/2));
Chris@172 91 update();
Chris@172 92 }
Chris@172 93
Chris@172 94 void
Chris@172 95 Panner::setRectExtents(float x0, float y0, float width, float height)
Chris@172 96 {
Chris@172 97 if (m_rectX == x0 &&
Chris@172 98 m_rectY == y0 &&
Chris@172 99 m_rectWidth == width &&
Chris@172 100 m_rectHeight == height) {
Chris@172 101 return;
Chris@172 102 }
Chris@172 103 m_rectX = x0;
Chris@172 104 m_rectY = y0;
Chris@172 105 m_rectWidth = width;
Chris@172 106 m_rectHeight = height;
Chris@172 107 normalise();
Chris@172 108 emitAndUpdate();
Chris@172 109 }
Chris@172 110
Chris@172 111 void
Chris@172 112 Panner::setRectWidth(float width)
Chris@172 113 {
Chris@172 114 if (m_rectWidth == width) return;
Chris@172 115 m_rectWidth = width;
Chris@172 116 normalise();
Chris@172 117 emitAndUpdate();
Chris@172 118 }
Chris@172 119
Chris@172 120 void
Chris@172 121 Panner::setRectHeight(float height)
Chris@172 122 {
Chris@172 123 if (m_rectHeight == height) return;
Chris@172 124 m_rectHeight = height;
Chris@172 125 normalise();
Chris@172 126 emitAndUpdate();
Chris@172 127 }
Chris@172 128
Chris@172 129 void
Chris@172 130 Panner::setRectCentreX(float x)
Chris@172 131 {
Chris@172 132 float x0 = x - m_rectWidth/2;
Chris@172 133 if (x0 == m_rectX) return;
Chris@172 134 m_rectX = x0;
Chris@172 135 normalise();
Chris@172 136 emitAndUpdate();
Chris@172 137 }
Chris@172 138
Chris@172 139 void
Chris@172 140 Panner::setRectCentreY(float y)
Chris@172 141 {
Chris@172 142 float y0 = y - m_rectWidth/2;
Chris@172 143 if (y0 == m_rectY) return;
Chris@172 144 m_rectY = y0;
Chris@172 145 normalise();
Chris@172 146 emitAndUpdate();
Chris@172 147 }
Chris@172 148
Chris@172 149 QSize
Chris@172 150 Panner::sizeHint() const
Chris@172 151 {
Chris@172 152 return QSize(30, 30);
Chris@172 153 }
Chris@172 154
Chris@172 155
Chris@172 156