~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt/examples/radio/ampfrm.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2009-07-01 16:08:21 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20090701160821-gog44m4w7x2f4u6o
Tags: 5.2.0-1
* New upstream release.
* Add branch pull patch from svn r544.
* Bump Standards-Version to 3.8.2. No changes needed.
* Update installed manpages.
* Use qwt as base name for directory (drop version).
* Add missing warnings patch.
* Rewrite debian/rules: use dh.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "ampfrm.h"
 
2
#include <qwt_knob.h>
 
3
#include <qlayout.h>
 
4
#include <qlabel.h>
 
5
#include <qfont.h>
 
6
#include <qpen.h>
 
7
#include <qevent.h>
 
8
#include <qwt_thermo.h>
 
9
#include <qwt_round_scale_draw.h>
 
10
#include <qwt_math.h>
 
11
 
 
12
class Knob: public QWidget
 
13
{
 
14
public:
 
15
    Knob(const QString &title, double min, double max, QWidget *parent):
 
16
        QWidget(parent)
 
17
    {
 
18
        d_knob = new QwtKnob(this);
 
19
        d_knob->setRange(min, max, 0,1);
 
20
        d_knob->setScaleMaxMajor(10);
 
21
 
 
22
        d_knob->setKnobWidth(50);
 
23
 
 
24
        d_label = new QLabel(title, this);
 
25
        d_label->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
 
26
 
 
27
        setSizePolicy(QSizePolicy::MinimumExpanding, 
 
28
            QSizePolicy::MinimumExpanding);
 
29
    }
 
30
 
 
31
    virtual QSize sizeHint() const
 
32
    {
 
33
        QSize sz1 = d_knob->sizeHint();
 
34
        QSize sz2 = d_label->sizeHint();
 
35
 
 
36
        const int w = qwtMax(sz1.width(), sz2.width());
 
37
        const int h = sz1.height() + sz2.height();
 
38
            
 
39
        int off = d_knob->scaleDraw()->extent(QPen(), d_knob->font());
 
40
        off -= 10; // spacing
 
41
 
 
42
        return QSize(w, h - off);
 
43
    }
 
44
 
 
45
    double value() const
 
46
    {
 
47
        return d_knob->value();
 
48
    }
 
49
    
 
50
protected:
 
51
    virtual void resizeEvent(QResizeEvent *e)
 
52
    {
 
53
        const QSize sz = e->size();
 
54
 
 
55
        int h = d_label->sizeHint().height();
 
56
 
 
57
        d_label->setGeometry(0, sz.height() - h,
 
58
            sz.width(), h);
 
59
 
 
60
        h = d_knob->sizeHint().height();
 
61
        int off = d_knob->scaleDraw()->extent(QPen(), d_knob->font());
 
62
        off -= 10; // spacing
 
63
 
 
64
        d_knob->setGeometry(0, d_label->pos().y() - h + off,
 
65
            sz.width(), h);
 
66
    }
 
67
 
 
68
private:
 
69
    QwtKnob *d_knob;
 
70
    QLabel *d_label;
 
71
};
 
72
 
 
73
class Thermo: public QWidget
 
74
{
 
75
public:
 
76
    Thermo(const QString &title, QWidget *parent):
 
77
        QWidget(parent)
 
78
    {
 
79
        d_thermo = new QwtThermo(this);
 
80
        d_thermo->setPipeWidth(6);
 
81
        d_thermo->setRange(-40,10);
 
82
        d_thermo->setFillColor(Qt::green);
 
83
        d_thermo->setAlarmColor(Qt::red);
 
84
        d_thermo->setAlarmLevel(0.0);
 
85
        d_thermo->setAlarmEnabled(true);
 
86
 
 
87
        QLabel *label = new QLabel(title, this);
 
88
        label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
 
89
 
 
90
        QVBoxLayout *layout = new QVBoxLayout(this);
 
91
        layout->setMargin(0);
 
92
        layout->setSpacing(0);
 
93
        layout->addWidget(d_thermo, 10);
 
94
        layout->addWidget(label);
 
95
    }
 
96
 
 
97
    void setValue(double value)
 
98
    {
 
99
        d_thermo->setValue(value);
 
100
    }
 
101
 
 
102
private:
 
103
    QwtThermo *d_thermo;
 
104
};
 
105
 
 
106
AmpFrame::AmpFrame(QWidget *p): 
 
107
    QFrame(p)
 
108
{
 
109
    d_knbVolume = new Knob("Volume", 0.0, 10.0, this);
 
110
    d_knbBalance = new Knob("Balance", -10.0, 10.0, this);
 
111
    d_knbTreble = new Knob("Treble", -10.0, 10.0, this);
 
112
    d_knbBass = new Knob("Bass", -10.0, 10.0, this);
 
113
 
 
114
    d_thmLeft = new Thermo("Left [dB]", this);
 
115
    d_thmRight = new Thermo("Right [dB]", this);
 
116
 
 
117
    QHBoxLayout *layout = new QHBoxLayout(this);
 
118
    layout->setSpacing(0);
 
119
    layout->setMargin(10);
 
120
    layout->addWidget(d_knbVolume);
 
121
    layout->addWidget(d_knbBalance);
 
122
    layout->addWidget(d_knbTreble);
 
123
    layout->addWidget(d_knbBass);
 
124
    layout->addSpacing(20);
 
125
    layout->addStretch(10);
 
126
    layout->addWidget(d_thmLeft);
 
127
    layout->addSpacing(10);
 
128
    layout->addWidget(d_thmRight);
 
129
 
 
130
    (void)startTimer(50);    
 
131
}
 
132
 
 
133
void AmpFrame::timerEvent(QTimerEvent *)
 
134
{
 
135
    static double phs = 0;
 
136
    
 
137
    //
 
138
    //  This amplifier generates its own input signal...
 
139
    //
 
140
 
 
141
    const double sig_bass = (1.0 + 0.1 * d_knbBass->value()) 
 
142
        * sin(13.0 * phs);
 
143
    const double sig_mid_l = sin(17.0 * phs);
 
144
    const double sig_mid_r = cos(17.5 * phs);
 
145
    const double sig_trbl_l = 0.5 * (1.0 + 0.1 * d_knbTreble->value()) 
 
146
        * sin(35.0 * phs);
 
147
    const double sig_trbl_r = 0.5 * (1.0 + 0.1 * d_knbTreble->value()) 
 
148
        * sin(34.0 * phs);
 
149
 
 
150
    double sig_l = 0.05 * d_master * d_knbVolume->value() 
 
151
        * qwtSqr(sig_bass + sig_mid_l + sig_trbl_l);
 
152
    double sig_r = 0.05 * d_master * d_knbVolume->value()   
 
153
        * qwtSqr(sig_bass + sig_mid_r + sig_trbl_r);
 
154
    
 
155
    double balance = 0.1 * d_knbBalance->value(); 
 
156
    if (balance > 0) 
 
157
       sig_l *= (1.0 - balance);
 
158
    else
 
159
       sig_r *= (1.0 + balance);
 
160
 
 
161
    if (sig_l > 0.01)
 
162
       sig_l = 20.0 * log10(sig_l);
 
163
    else
 
164
       sig_l = -40.0;
 
165
 
 
166
    if (sig_r > 0.01)
 
167
       sig_r = 20.0 * log10(sig_r);
 
168
    else
 
169
       sig_r = - 40.0; 
 
170
 
 
171
    d_thmLeft->setValue(sig_l);
 
172
    d_thmRight->setValue(sig_r);
 
173
 
 
174
    phs += M_PI / 100;
 
175
    if (phs > M_PI) 
 
176
        phs = 0;
 
177
}
 
178
 
 
179
void AmpFrame::setMaster(double v)
 
180
{
 
181
    d_master = v;
 
182
}