class library: Spawner - don't access PriorityQueue-array
[supercollider.git] / QtCollider / widgets / QcScope.cpp
blob1b9428021b6bd9b591437e7bf311cca4ea1c890d
1 /************************************************************************
3 * Copyright 2010 Jakob Leben (jakob.leben@gmail.com)
5 * This file is part of SuperCollider Qt GUI.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ************************************************************************/
22 #include "QcScope.h"
23 #include "../QcWidgetFactory.h"
25 #include <QPainter>
26 #include <QTimer>
28 static QcWidgetFactory<QcScope> factory;
30 QcScope::QcScope()
31 : bufNum( 0 ),
32 xOffset( 0.f ),
33 yOffset( 0.f ),
34 xZoom( 1.f ),
35 yZoom( 1.f ),
36 style( 0 ),
37 _bkg( QColor(0,0,0) )
39 memset( &buffer, 0, sizeof(SndBuf) );
40 timer = new QTimer( this );
41 timer->setInterval( 15 );
42 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
43 connect( timer, SIGNAL( timeout() ), this, SLOT( updateScope() ) );
46 QcScope::~QcScope()
48 free( buffer.data );
51 void QcScope::setBufferNumber( int n )
53 bufNum = n;
54 timer->start();
57 void QcScope::setWaveColors( const VariantList & newColors )
59 colors.clear();
60 Q_FOREACH( QVariant var, newColors.data ) {
61 QColor color = var.value<QColor>();
62 if( !color.isValid() )
63 colors.append( QColor( 0,0,0 ) );
64 else
65 colors.append( color );
69 int QcScope::updateInterval() const {
70 return timer->interval();
73 void QcScope::setUpdateInterval( int interval ) {
74 timer->setInterval( qMax(0, interval) );
77 int getScopeBuf( uint32 index, SndBuf *buf, bool& changed );
79 void QcScope::updateScope()
81 #ifndef NO_INTERNAL_SERVER
82 //printf("update\n");
83 bool changed;
84 getScopeBuf( bufNum, &buffer, changed );
85 if( changed ) {
86 //printf("channels: %i\n", buffer.channels);
87 update();
89 #endif
92 inline void QcScope::setPoint( QPointF& pt,
93 float x, float y, float xRatio, float yRatio,
94 int xStart, int yStart )
96 pt.setX( ((x + xOffset) * xRatio) + xStart );
97 pt.setY( yStart - ((y + yOffset) * yRatio) );
100 void QcScope::paint1D( bool overlapped, QPainter & p )
102 QRect area = rect();
103 float xRatio = xZoom * area.width() / (float )buffer.frames;
104 float yRatio = yZoom * area.height() * 0.5;
105 if( !overlapped ) yRatio /= buffer.channels;
107 int c, f, s;
108 QPointF pt1;
109 QPointF pt2;
111 for( c = 0; c < buffer.channels; c++ ) {
113 if( c < colors.count() ) p.setPen( colors[c] );
114 else p.setPen( QColor(255,255,255) );
116 float yCenter = area.height() * 0.5;
117 if( !overlapped ) {
118 yCenter *= ( c * 2 + 1);
119 yCenter /= buffer.channels;
121 yCenter += area.y();
123 setPoint( pt1, 0, buffer.data[c], xRatio, yRatio, area.x(), yCenter );
125 for( f = 1, s = c + buffer.channels;
126 f < buffer.frames;
127 f++, s += buffer.channels )
129 setPoint( pt2, f, buffer.data[s], xRatio, yRatio, area.x(), yCenter );
130 p.drawLine( pt1, pt2 );
131 pt1 = pt2;
136 void QcScope::paint2D( QPainter & p )
138 if( colors.count() ) p.setPen( colors[0] );
139 else p.setPen( QColor(255,255,255) );
141 QRect area = rect();
142 int minSize = qMin( area.width(), area.height() );
143 float xRatio = xZoom * minSize * 0.5;
144 float yRatio = yZoom * minSize * 0.5;
145 QPoint center = area.center();
146 int centerY = center.y();
147 int centerX = center.x();
149 QPointF pt1;
150 QPointF pt2;
151 float *data = buffer.data;
152 float y = buffer.channels > 1 ? data[1] : 0.f;
154 setPoint( pt1, data[0], y, xRatio, yRatio, centerX, centerY );
155 data += buffer.channels;
157 int f;
158 for( f = 1; f < buffer.frames; f++, data += buffer.channels )
160 if( buffer.channels > 1 ) y = data[1];
161 setPoint( pt2, data[0], y, xRatio, yRatio, centerX, centerY );
162 p.drawLine( pt1, pt2 );
163 pt1 = pt2;
167 void QcScope::paintEvent ( QPaintEvent * event )
169 Q_UNUSED( event );
171 QPainter p( this );
172 QRect area = rect();
173 p.fillRect( area, _bkg );
175 if( buffer.frames == 0 ) return;
177 if( style == 0 )
178 paint1D( false, p );
179 else if( style == 1 )
180 paint1D( true, p );
181 else if( style == 2 )
182 paint2D( p );