svgui  1.9
Panner.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "Panner.h"
17 
18 #include <QMouseEvent>
19 #include <QPaintEvent>
20 #include <QWheelEvent>
21 #include <QPainter>
22 
23 #include <iostream>
24 #include <cmath>
25 
26 Panner::Panner(QWidget *parent) :
27  QWidget(parent),
28  m_rectX(0),
29  m_rectY(0),
30  m_rectWidth(1),
31  m_rectHeight(1),
32  m_scrollUnit(0),
33  m_defaultCentreX(0),
34  m_defaultCentreY(0),
35  m_defaultsSet(false),
36  m_thumbColour(palette().highlightedText().color()),
37  m_backgroundAlpha(255),
38  m_thumbAlpha(255),
39  m_clicked(false),
40  m_dragStartX(0),
41  m_dragStartY(0)
42 {
43 }
44 
46 {
47 }
48 
49 void
50 Panner::setAlpha(int backgroundAlpha, int thumbAlpha)
51 {
52  m_backgroundAlpha = backgroundAlpha;
53  m_thumbAlpha = thumbAlpha;
54 }
55 
56 void
58 {
59  m_scrollUnit = unit;
60 }
61 
62 void
64 {
65  float unit = m_scrollUnit;
66  if (unit == 0.f) {
67  unit = float(m_rectHeight) / (6 * float(height()));
68  if (unit < 0.01) unit = 0.01;
69  }
70 
71  if (!up) {
72  m_rectY += unit;
73  } else {
74  m_rectY -= unit;
75  }
76 
77  normalise();
78  emitAndUpdate();
79 }
80 
81 void
82 Panner::mousePressEvent(QMouseEvent *e)
83 {
84  if (e->button() == Qt::MidButton ||
85  ((e->button() == Qt::LeftButton) &&
86  (e->modifiers() & Qt::ControlModifier))) {
88  } else if (e->button() == Qt::LeftButton) {
89  m_clicked = true;
90  m_clickPos = e->pos();
93  }
94 }
95 
96 void
98 {
99  if (e->button() != Qt::LeftButton) {
100  return;
101  }
102 
103  emit doubleClicked();
104 }
105 
106 void
107 Panner::mouseMoveEvent(QMouseEvent *e)
108 {
109  if (!m_clicked) return;
110 
111  float dx = float(e->pos().x() - m_clickPos.x()) / float(width());
112  float dy = float(e->pos().y() - m_clickPos.y()) / float(height());
113 
114  m_rectX = m_dragStartX + dx;
115  m_rectY = m_dragStartY + dy;
116 
117  normalise();
118  repaint();
120  emit rectCentreMoved(centreX(), centreY());
121 }
122 
123 void
125 {
126  if (!m_clicked) return;
127 
128  mouseMoveEvent(e);
129  m_clicked = false;
130 }
131 
132 void
133 Panner::wheelEvent(QWheelEvent *e)
134 {
135  scroll(e->delta() > 0);
136 }
137 
138 void
140 {
141  emit mouseEntered();
142 }
143 
144 void
146 {
147  emit mouseLeft();
148 }
149 
150 void
151 Panner::paintEvent(QPaintEvent *)
152 {
153  QPainter paint(this);
154  paint.setRenderHint(QPainter::Antialiasing, false);
155 
156  QColor bg(palette().background().color());
157  bg.setAlpha(m_backgroundAlpha);
158 
159  paint.setPen(palette().dark().color());
160  paint.setBrush(bg);
161  paint.drawRect(0, 0, width()-1, height()-1);
162 
163  QColor hl(m_thumbColour);
164  hl.setAlpha(m_thumbAlpha);
165 
166  paint.setBrush(hl);
167 
168  int rw = lrintf((width() - 1) * m_rectWidth);
169  int rh = lrintf((height() - 1) * m_rectHeight);
170  if (rw < 2) rw = 2;
171  if (rh < 2) rh = 2;
172 
173  paint.drawRect(lrintf(width() * m_rectX),
174  lrintf(height() * m_rectY),
175  rw, rh);
176 }
177 
178 void
180 {
181  if (m_rectWidth > 1.0) m_rectWidth = 1.0;
182  if (m_rectHeight > 1.0) m_rectHeight = 1.0;
183  if (m_rectX + m_rectWidth > 1.0) m_rectX = 1.0 - m_rectWidth;
184  if (m_rectX < 0) m_rectX = 0;
185  if (m_rectY + m_rectHeight > 1.0) m_rectY = 1.0 - m_rectHeight;
186  if (m_rectY < 0) m_rectY = 0;
187 
188  if (!m_defaultsSet) {
191  m_defaultsSet = true;
192  }
193 }
194 
195 void
197 {
199  emit rectCentreMoved(centreX(), centreY());
200  update();
201 }
202 
203 void
204 Panner::getRectExtents(float &x0, float &y0, float &width, float &height)
205 {
206  x0 = m_rectX;
207  y0 = m_rectY;
208  width = m_rectWidth;
209  height = m_rectHeight;
210 }
211 
212 void
213 Panner::setRectExtents(float x0, float y0, float width, float height)
214 {
215 // SVDEBUG << "Panner::setRectExtents(" << x0 << ", " << y0 << ", "
216 // << width << ", " << height << ")" << endl;
217 
218  if (m_rectX == x0 &&
219  m_rectY == y0 &&
220  m_rectWidth == width &&
221  m_rectHeight == height) {
222  return;
223  }
224 
225  m_rectX = x0;
226  m_rectY = y0;
227  m_rectWidth = width;
228  m_rectHeight = height;
229 
230  normalise();
231  emitAndUpdate();
232 }
233 
234 void
236 {
237  if (m_rectWidth == width) return;
238  m_rectWidth = width;
239  normalise();
240  emitAndUpdate();
241 }
242 
243 void
245 {
246  if (m_rectHeight == height) return;
247  m_rectHeight = height;
248  normalise();
249  emitAndUpdate();
250 }
251 
252 void
254 {
255  float x0 = x - m_rectWidth/2;
256  if (x0 == m_rectX) return;
257  m_rectX = x0;
258  normalise();
259  emitAndUpdate();
260 }
261 
262 void
264 {
265  float y0 = y - m_rectHeight/2;
266  if (y0 == m_rectY) return;
267  m_rectY = y0;
268  normalise();
269  emitAndUpdate();
270 }
271 
272 QSize
274 {
275  return QSize(30, 30);
276 }
277 
278 void
279 Panner::setDefaultRectCentre(float cx, float cy)
280 {
281  m_defaultCentreX = cx;
282  m_defaultCentreY = cy;
283  m_defaultsSet = true;
284 }
285 
286 void
288 {
289  float x0 = m_defaultCentreX - m_rectWidth/2;
290  float y0 = m_defaultCentreY - m_rectHeight/2;
291  if (x0 == m_rectX && y0 == m_rectY) return;
292  m_rectX = x0;
293  m_rectY = y0;
294  normalise();
295  emitAndUpdate();
296 }
297 
298 
float centreX() const
Definition: Panner.h:141
void normalise()
Definition: Panner.cpp:179
virtual void mousePressEvent(QMouseEvent *e)
Definition: Panner.cpp:82
virtual void leaveEvent(QEvent *)
Definition: Panner.cpp:145
float m_rectHeight
Definition: Panner.h:130
void getRectExtents(float &x0, float &y0, float &width, float &height)
Definition: Panner.cpp:204
int m_backgroundAlpha
Definition: Panner.h:138
Panner(QWidget *parent=0)
Definition: Panner.cpp:26
int m_thumbAlpha
Definition: Panner.h:139
virtual QSize sizeHint() const
Definition: Panner.cpp:273
void scroll(bool up)
Move up (if up is true) or down a bit.
Definition: Panner.cpp:63
float m_dragStartY
Definition: Panner.h:147
void setScrollUnit(float unit)
Set the amount the scroll() function or mouse wheel movement makes the panner rectangle move by.
Definition: Panner.cpp:57
void mouseEntered()
bool m_defaultsSet
Definition: Panner.h:135
virtual void mouseDoubleClickEvent(QMouseEvent *e)
Definition: Panner.cpp:97
QPoint m_clickPos
Definition: Panner.h:145
float m_rectX
Definition: Panner.h:127
void setRectWidth(float width)
Set the width of the panned rectangle as a fraction (0 -> 1) of that of the whole panner widget.
Definition: Panner.cpp:235
bool m_clicked
Definition: Panner.h:144
virtual void enterEvent(QEvent *)
Definition: Panner.cpp:139
float centreY() const
Definition: Panner.h:142
void setRectHeight(float height)
Set the height of the panned rectangle as a fraction (0 -> 1) of that of the whole panner widget.
Definition: Panner.cpp:244
virtual ~Panner()
Definition: Panner.cpp:45
void mouseLeft()
void setRectCentreY(float y)
Set the location of the centre of the panned rectangle on the y axis, as a proportion (0 -> 1) of the...
Definition: Panner.cpp:263
void emitAndUpdate()
Definition: Panner.cpp:196
virtual void mouseReleaseEvent(QMouseEvent *e)
Definition: Panner.cpp:124
virtual void wheelEvent(QWheelEvent *e)
Definition: Panner.cpp:133
void rectExtentsChanged(float, float, float, float)
Emitted when the panned rectangle is dragged or otherwise moved.
float m_dragStartX
Definition: Panner.h:146
void doubleClicked()
Emitted when the panner is double-clicked (for the "customer" code to pop up a value editing dialog,...
float m_defaultCentreX
Definition: Panner.h:133
void resetToDefault()
Definition: Panner.cpp:287
virtual void mouseMoveEvent(QMouseEvent *e)
Definition: Panner.cpp:107
float m_rectY
Definition: Panner.h:128
void setRectCentreX(float x)
Set the location of the centre of the panned rectangle on the x axis, as a proportion (0 -> 1) of the...
Definition: Panner.cpp:253
float m_rectWidth
Definition: Panner.h:129
void setDefaultRectCentre(float, float)
Definition: Panner.cpp:279
float m_defaultCentreY
Definition: Panner.h:134
void setRectExtents(float x0, float y0, float width, float height)
Set the extents of the panned rectangle within the overall panner widget.
Definition: Panner.cpp:213
QColor m_thumbColour
Definition: Panner.h:137
void rectCentreMoved(float, float)
Emitted when the rectangle is dragged or otherwise moved (as well as extentsChanged).
virtual void paintEvent(QPaintEvent *e)
Definition: Panner.cpp:151
void setAlpha(int backgroundAlpha, int thumbAlpha)
Definition: Panner.cpp:50
float m_scrollUnit
Definition: Panner.h:131