~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to arts/tools/levelmeters.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <qlayout.h>
 
2
#include <qpainter.h>
 
3
#include <qfontmetrics.h>
 
4
#include <qlist.h>
 
5
#include <kled.h>
 
6
#include "levelmeters.h"
 
7
 
 
8
PeakBar::PeakBar(QWidget *parent): LevelMeter(parent) {
 
9
        clipped = false;
 
10
        displayMinPeak= false;
 
11
        horizontalMode= false;
 
12
        currentValue= 0.0f;
 
13
        
 
14
        lastValues.setAutoDelete( TRUE );
 
15
        
 
16
        setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
 
17
        setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred));
 
18
        setBackgroundMode(NoBackground);
 
19
        setMinimumSize(frameWidth()+7, 70);
 
20
}
 
21
 
 
22
void PeakBar::frameChanged() {
 
23
        setMinimumSize(frameWidth()+7, 70);
 
24
        QFrame::frameChanged();
 
25
}
 
26
 
 
27
QSize PeakBar::sizeHint() const {
 
28
        return QSize(13, 250);
 
29
}
 
30
 
 
31
void PeakBar::checkMinMax() {
 
32
        int mustRecheck= 0; // bool
 
33
        Observation *o;
 
34
 
 
35
        while ((o= lastValues.first()) && o->time.elapsed() > peakMillis) {
 
36
                lastValues.removeFirst();
 
37
                mustRecheck= 1;
 
38
        }
 
39
 
 
40
        if (mustRecheck) {
 
41
                maxValue= 0.f;
 
42
                minValue= 1.f;
 
43
                clipped = false;
 
44
                for (QListIterator<Observation> it(lastValues); it.current(); ++it) {
 
45
                        float value= it.current()->value;
 
46
                        if (value>maxValue)
 
47
                                maxValue= value;
 
48
                        if (value<minValue)
 
49
                                minValue= value;
 
50
                        if (value > 1.f)
 
51
                                clipped = true;
 
52
                }
 
53
        }
 
54
}
 
55
 
 
56
void PeakBar::drawContents(QPainter *p)
 
57
{
 
58
        QRect size= contentsRect();
 
59
 
 
60
        checkMinMax();
 
61
        
 
62
        p->setBrush(clipped ? darkRed : darkBlue);
 
63
        p->setPen(NoPen);
 
64
        p->drawRect(size);
 
65
                
 
66
        QRect bar= size;
 
67
        p->setBrush(clipped ? red : blue);
 
68
        if (horizontalMode) {
 
69
                bar.setWidth((int)(bar.width()*currentValue));
 
70
        } else {
 
71
                int newHeight= (int)(bar.height()*currentValue);
 
72
                bar.moveBy(0, bar.height()-newHeight);
 
73
                bar.setHeight(newHeight);
 
74
        }
 
75
        p->drawRect(bar);
 
76
                
 
77
        int y;
 
78
        // TODO: if (horizontalMode)
 
79
        if (displayMinPeak) {
 
80
                y= frameWidth()+size.height()-((int)(size.height()*minValue));
 
81
                p->setPen(white);
 
82
                p->drawLine(frameWidth(), y, frameWidth()+size.width()-1, y);
 
83
        }
 
84
        y= frameWidth()+size.height()-((int)(size.height()*maxValue));
 
85
        p->setPen(white);
 
86
        p->drawLine(frameWidth(), y, frameWidth()+size.width()-1, y);
 
87
}
 
88
                
 
89
void PeakBar::setValue(float f) {
 
90
        if (f > 1.f)
 
91
                clipped = true;
 
92
 
 
93
        currentValue= f;
 
94
        if (f>=maxValue)
 
95
                maxValue= f;
 
96
        if (displayMinPeak && (f<=minValue))
 
97
                minValue= f;
 
98
                
 
99
        lastValues.append(new Observation(f));
 
100
        
 
101
        repaint();
 
102
}
 
103
 
 
104
// -------------------------------------------------------------
 
105
 
 
106
PeakLevelMeters::PeakLevelMeters(QWidget *parent):
 
107
        StereoLevelMeter(parent), left(this), right(this), scaleView(this)
 
108
{
 
109
        QBoxLayout *layout= new QHBoxLayout(this);
 
110
        layout->addWidget(&left);
 
111
        //      layout->setStretchFactor(&left, 0);
 
112
        layout->addWidget(&right);
 
113
        //      layout->setStretchFactor(&right, 0);
 
114
        layout->addWidget(&scaleView);
 
115
        //      layout->setStretchFactor(&scaleView, 0);
 
116
        left.setLineWidth(2);
 
117
        right.setLineWidth(2);
 
118
        scaleView.setScaleMargins(right.frameWidth());
 
119
        setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred));
 
120
 
 
121
        setDbRange(36);
 
122
}
 
123
        
 
124
void PeakLevelMeters::setDbRange(int db) {
 
125
        dbRange= db;
 
126
        scaleView.setDbRange(db);
 
127
}
 
128
 
 
129
void PeakLevelMeters::setValues(float leftVal, float rightVal) {
 
130
        float f= 1.0f+levelToDB(leftVal)/dbRange;
 
131
        if (f<0.f) f= 0.f;
 
132
        left.setValue(f);
 
133
 
 
134
        f= 1.0f+levelToDB(rightVal)/dbRange;
 
135
        if (f<0.f) f= 0.f;
 
136
        right.setValue(f);
 
137
};
 
138
 
 
139
ScaleView::ScaleView(QWidget *parent): QFrame(parent) {
 
140
        font.setPixelSize(10);
 
141
        setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred));
 
142
}
 
143
 
 
144
void ScaleView::setDbRange(int db) {
 
145
        dbRange= db;
 
146
        repaint();
 
147
}
 
148
 
 
149
QSize ScaleView::sizeHint() const {
 
150
        return QSize(QFontMetrics(font).width("-88")+8, 250);
 
151
}
 
152
 
 
153
void ScaleView::drawContents(QPainter *p) {
 
154
        QRect size= contentsRect();
 
155
        size.rTop()+= upperMargin;
 
156
        size.rBottom()-= lowerMargin;
 
157
 
 
158
        QFrame::drawContents(p); // clear background
 
159
        
 
160
        int step=3;
 
161
        while (dbRange/step*20>size.height())
 
162
                step*=2;
 
163
 
 
164
        /*      float offset= ((float)size.height()*step)/dbRange, pos=0.f;
 
165
        p->setPen(black);
 
166
        p->drawLine(0, (, size.width()*3/5, y);*/
 
167
        
 
168
        p->setPen(black);
 
169
        p->setFont(font);
 
170
        QString numStr;
 
171
        for (int i=0; i<=dbRange; i++) {
 
172
                int y= size.top()+(size.height()-1)*i/dbRange;
 
173
                if (i%step==0) {
 
174
                        p->drawLine(0, y, 4, y);
 
175
                        numStr.setNum(-i);
 
176
                        p->drawText(8, y+5, numStr);
 
177
                } else
 
178
                        p->drawLine(0, y, 2, y);
 
179
        }
 
180
}
 
181
 
 
182
// -------------------------------------------------------------
 
183
 
 
184
LedMeter::LedMeter(QWidget *parent, bool blueState) : LevelMeter(parent) {
 
185
        setBackgroundColor(black);
 
186
        QBoxLayout * l = new QVBoxLayout( this );
 
187
        l->setAutoAdd(TRUE);
 
188
        for(int i=0;i<12;i++) {
 
189
                QColor c;
 
190
                if(blueState)
 
191
                        c = blue;
 
192
                else {
 
193
                        c = red;
 
194
                        if(i>=2) c = yellow;
 
195
                        if(i>=5) c = green;
 
196
                }
 
197
 
 
198
                // put each led in its own frame, since it seems to be broken
 
199
                QFrame *lframe = new QFrame(this);
 
200
                QBoxLayout *lfl = new QVBoxLayout( lframe );
 
201
                lfl->setAutoAdd(TRUE);
 
202
                leds[i] =
 
203
                        new KLed(c,KLed::Off, KLed::Sunken, KLed::Circular,lframe);
 
204
        }
 
205
}
 
206
 
 
207
void LedMeter::setValue(float f)
 
208
{
 
209
        //printf("value %f\n",f);
 
210
        for(int i=11;i>=0;i--)
 
211
                {
 
212
                        if(f > 0.06) leds[i]->setState(KLed::On);
 
213
                        else             leds[i]->setState(KLed::Off);
 
214
                        f /= 1.25;
 
215
                }
 
216
}
 
217
 
 
218
// -------------------------------------------------------------
 
219
 
 
220
StereoLedMeters::StereoLedMeters(QWidget *parent)
 
221
        : StereoLevelMeter(parent), left(this), right(this)
 
222
{
 
223
        QBoxLayout *layout= new QHBoxLayout(this);
 
224
        layout->addWidget(&left);
 
225
        layout->addWidget(&right);
 
226
}
 
227
        
 
228
void StereoLedMeters::setValues(float leftVal, float rightVal) {
 
229
        left.setValue(leftVal);
 
230
        right.setValue(rightVal);
 
231
}
 
232
 
 
233
#include "levelmeters.moc"