summaryrefslogtreecommitdiffstats
path: root/src/widget/wvumeter.cpp
blob: dcdcccdf5e63662639b53ef57f8ea42a31878296 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/***************************************************************************
                          wvumeter.cpp  -  description
                             -------------------
    begin                : Fri Jul 22 2003
    copyright            : (C) 2003 by Tue & Ken Haste Andersen
    email                : haste@diku.dk
***************************************************************************/

/***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************/

#include "widget/wvumeter.h"

#include <QPaintEvent>
#include <QPainter>
#include <QtDebug>
#include <QPixmap>

#include "widget/wpixmapstore.h"
#include "util/timer.h"

#define DEFAULT_FALLTIME 20
#define DEFAULT_FALLSTEP 1
#define DEFAULT_HOLDTIME 400
#define DEFAULT_HOLDSIZE 5

WVuMeter::WVuMeter(QWidget * parent) :
        WWidget(parent),
        m_iNoPos(0),
        m_pPixmapBack(NULL),
        m_pPixmapVu(NULL),
        m_iPeakHoldSize(0),
        m_iPeakPos(0),
        m_iPeakHoldCountdown(0) {
}

WVuMeter::~WVuMeter()
{
    resetPositions();
}

void WVuMeter::setup(QDomNode node)
{
    // Set pixmaps
    bool bHorizontal = false;
    if (!selectNode(node, "Horizontal").isNull() &&
        selectNodeQString(node, "Horizontal")=="true")
        bHorizontal = true;

    setPixmaps(getPath(selectNodeQString(node, "PathBack")),
               getPath(selectNodeQString(node, "PathVu")), bHorizontal);

    m_iPeakHoldSize = selectNodeInt(node, "PeakHoldSize");
    if(m_iPeakHoldSize < 0 || m_iPeakHoldSize > 100)
        m_iPeakHoldSize = DEFAULT_HOLDSIZE;

    m_iPeakFallStep = selectNodeInt(node, "PeakFallStep");
    if(m_iPeakFallStep < 1 || m_iPeakFallStep > 1000)
        m_iPeakFallStep = DEFAULT_FALLSTEP;

    m_iPeakHoldTime = selectNodeInt(node, "PeakHoldTime");
    if(m_iPeakHoldTime < 1 || m_iPeakHoldTime > 3000)
        m_iPeakHoldTime = DEFAULT_HOLDTIME;

    m_iPeakFallTime = selectNodeInt(node, "PeakFallTime");
    if(m_iPeakFallTime < 1 || m_iPeakFallTime > 1000)
        m_iPeakFallTime = DEFAULT_FALLTIME;
}

void WVuMeter::resetPositions()
{
    WPixmapStore::deletePixmap(m_pPixmapBack);
    m_pPixmapBack = NULL;
    WPixmapStore::deletePixmap(m_pPixmapVu);
    m_pPixmapVu = NULL;
}

void WVuMeter::setPixmaps(const QString &backFilename, const QString &vuFilename, bool bHorizontal)
{
    m_pPixmapBack = WPixmapStore::getPixmap(backFilename);
    if (!m_pPixmapBack || m_pPixmapBack->size() == QSize(0,0)) {
        qDebug() << "WVuMeter: Error loading back pixmap" << backFilename;
    } else {
        setFixedSize(m_pPixmapBack->size());
    }

    m_pPixmapVu = WPixmapStore::getPixmap(vuFilename);
    if (!m_pPixmapVu || m_pPixmapVu->size() == QSize(0,0)) {
        qDebug() << "WVuMeter: Error loading vu pixmap" << vuFilename;
    } else {
        m_bHorizontal = bHorizontal;
        if (m_bHorizontal) {
            m_iNoPos = m_pPixmapVu->width();
        } else {
            m_iNoPos = m_pPixmapVu->height();
        }
    }
}

void WVuMeter::setValue(double fValue)
{
    int idx = (int)(fValue * (float)(m_iNoPos)/128.);
    // Range check
    if (idx>m_iNoPos)
        idx = m_iNoPos;
    else if (idx<0)
        idx = 0;

    setPeak(idx);
    m_fValue = fValue;

    QTime currentTime = QTime::currentTime();
    int msecsElapsed = m_lastUpdate.msecsTo(currentTime);
    m_lastUpdate = currentTime;
    updateState(msecsElapsed);

    //Post a paintEvent() message, so that the widget repaints.
    update();
}

void WVuMeter::setPeak(int pos)
{
    if(pos > m_iPeakPos)
    {
        m_iPeakPos = pos;
        m_iPeakHoldCountdown = m_iPeakHoldTime;
    }
}

void WVuMeter::updateState(int msecsElapsed)
{
    // If we're holding at a peak then don't update anything
    m_iPeakHoldCountdown -= msecsElapsed;
    if (m_iPeakHoldCountdown > 0) {
        return;
    } else {
        m_iPeakHoldCountdown = 0;
    }

    // Otherwise, decrement the peak position by the fall step size times the
    // milliseconds elapsed over the fall time multiplier. The peak will fall
    // FallStep times (out of 128 steps) every FallTime milliseconds.

    m_iPeakPos -= float(m_iPeakFallStep) * float(msecsElapsed) / float(m_iPeakFallTime);
    if (m_iPeakPos < 0)
        m_iPeakPos = 0;
}

void WVuMeter::paintEvent(QPaintEvent *)
{
    ScopedTimer t("WVuMeter::paintEvent");
    if (m_pPixmapBack && m_pPixmapVu) {
        int idx = (int)(m_fValue*(float)(m_iNoPos)/128.);

        // Range check
        if (idx>m_iNoPos)
            idx = m_iNoPos;
        else if (idx<0)
            idx = 0;

        QPainter painter(this);
        // Draw back
        painter.drawPixmap(0, 0, *m_pPixmapBack);

        // Draw (part of) vu
        if (m_bHorizontal)
        {
            //This is a hack to fix something weird with horizontal VU meters:
            if(idx == 0)
                idx = 1;
            painter.drawPixmap(0, 0, *m_pPixmapVu, 0, 0, idx, m_pPixmapVu->height());
            if(m_iPeakHoldSize > 0 && m_iPeakPos > 0)
            {
                painter.drawPixmap(m_iPeakPos-m_iPeakHoldSize, 0, *m_pPixmapVu,
                    m_iPeakPos-m_iPeakHoldSize, 0, m_iPeakHoldSize, m_pPixmapVu->height());
            }
        }
        else
        {
            painter.drawPixmap(0, m_iNoPos-idx, *m_pPixmapVu, 0, m_iNoPos-idx, m_pPixmapVu->width(), idx);
            if(m_iPeakHoldSize > 0 && m_iPeakPos > 0)
            {
                painter.drawPixmap(0, m_pPixmapVu->height()-m_iPeakPos, *m_pPixmapVu,
                    0, m_pPixmapVu->height()-m_iPeakPos, m_pPixmapVu->width(), m_iPeakHoldSize);
            }
        }
    }
}