class library: Spawner - don't access PriorityQueue-array
[supercollider.git] / QtCollider / widgets / QcCanvas.cpp
blob4992d25be1e2d1b91f128429ba6baf03179aec46
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 "QcCanvas.h"
23 #include "../painting.h"
24 #include "../Common.h"
26 #include <QPainter>
28 QcCanvas::QcCanvas( QWidget *parent )
29 : QWidget( parent ),
30 _paint( false ),
31 _repaintNeeded( true ),
32 _clearOnRefresh( true ),
33 _clearOnce( false ),
34 _resize( false ),
35 _fps( 60.f ),
36 _fpsActual( 0.f ),
37 _timerId( 0 ),
38 _animating( false ),
39 _frameCount( 0 ),
40 _meterPeriod( 1000 ),
41 _meterFrames( 0 )
43 //_bkgColor = palette().color( QPalette::Background );
46 float QcCanvas::frameRate() const
48 return _fpsActual;
51 void QcCanvas::setFrameRate( float rate )
53 if( rate != _fps ) {
54 _fps = rate;
55 if( _animating && _fps > 0 ) {
56 // restart animation timer with new frame rate
57 killTimer( _timerId );
58 _timerId = startTimer( 1000.f / _fps );
63 void QcCanvas::refresh()
65 _repaintNeeded = true;
66 update();
69 void QcCanvas::clear()
71 _clearOnce = true;
74 void QcCanvas::animate( bool on )
76 if( on ) {
77 if( !_animating && _fps > 0 ) {
78 _frameCount = 0;
79 _animating = true;
80 _meterTime.start();
81 _timerId = startTimer( 1000.f / _fps );
82 _fpsTimer.start( _meterPeriod, this );
85 else if( _animating ) {
86 killTimer( _timerId );
87 _fpsTimer.stop();
88 _animating = false;
92 void QcCanvas::customEvent( QEvent *e )
94 if( e->type() == (QEvent::Type) QtCollider::Event_Refresh ) {
95 e->accept();
96 refresh();
100 void QcCanvas::resizeEvent( QResizeEvent * )
102 _resize = true;
103 refresh();
106 void QcCanvas::paintEvent( QPaintEvent * )
108 if( _paint && _repaintNeeded ) {
109 if( _resize ) {
110 _pixmap = QPixmap( size() );
111 _resize = false;
112 _clearOnce = true;
115 if( _clearOnRefresh || _clearOnce ) {
116 _pixmap.fill( QColor(0,0,0,0) );
117 _clearOnce = false;
120 QPainter pixPainter( &_pixmap );
121 Q_EMIT( painting(&pixPainter) );
122 _repaintNeeded = false;
125 QPainter p(this);
126 if( _bkgColor.isValid() ) p.fillRect( rect(), _bkgColor );
127 if( _paint ) p.drawPixmap( rect(), _pixmap );
130 void QcCanvas::timerEvent( QTimerEvent *e )
132 if( e->timerId() == _timerId ) {
133 ++_frameCount;
134 ++_meterFrames;
135 _repaintNeeded = true;
136 repaint();
138 else if( e->timerId() == _fpsTimer.timerId() ) {
139 // recalc actual fps
140 float dTime = _meterTime.elapsed();
141 _fpsActual = (dTime > 0) ? (_meterFrames * 1000.f / dTime) : 0.f;
142 // reset fps meter
143 _meterTime.restart();
144 _meterFrames = 0;