File: ScaleWidget.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (240 lines) | stat: -rw-r--r-- 7,448 bytes parent folder | download
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/***************************************************************************
        ScaleWidget.cpp  -  widget for drawing a scale under an image
                             -------------------
    begin                : Sep 18 2001
    copyright            : (C) 2001 by Thomas Eschenbacher
    email                : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <math.h>
#include <stdlib.h>

#include <QFont>
#include <QPaintEvent>
#include <QPainter>

#include "libkwave/String.h"
#include "libkwave/Utils.h"

#include "libgui/ScaleWidget.h"

#define FONTSIZE 6

//***************************************************************************
Kwave::ScaleWidget::ScaleWidget(QWidget *parent)
    :QWidget(parent), m_low(0), m_high(100), m_logmode(false),
     m_unittext(_("%"))
{
}

//***************************************************************************
Kwave::ScaleWidget::ScaleWidget(QWidget *parent, int low, int high,
                                const QString &unit)
    :QWidget(parent), m_low(low), m_high(high), m_logmode(false),
     m_unittext(unit)
{
}

//***************************************************************************
Kwave::ScaleWidget::~ScaleWidget()
{
}

//***************************************************************************
void Kwave::ScaleWidget::setUnit(const QString &text)
{
    m_unittext = text;
    repaint();
}

//***************************************************************************
void Kwave::ScaleWidget::setLogMode(bool log)
{
    if (m_logmode == log) return;
    m_logmode = log;
    repaint();
}

//***************************************************************************
void Kwave::ScaleWidget::setMinMax(int min, int max)
{
    if ((m_low == min) && (m_high == max)) return;
    m_low  = min;
    m_high = max;
    repaint();
}

//***************************************************************************
void Kwave::ScaleWidget::paintText(QPainter &p, int x, int y,
                                   bool reverse, const QString &text)
{
    QFont font;
    font.setStyleHint(QFont::SansSerif);
    font.setFixedPitch(true);
    font.setPixelSize(FONTSIZE);
    font.setWeight(QFont::Thin);
    font.setStyle(QFont::StyleNormal);
    p.setFont(font);
    QFontMetrics fm(font);

    if (reverse) {
        x += FONTSIZE + 2;
        y -= FONTSIZE + 2;
    }

    QRect rect = fm.boundingRect(text);
    const int th = rect.height();
    const int tw = rect.width();
    p.drawText(x, y, tw, th,
        ((reverse) ? Qt::AlignLeft : Qt::AlignRight) | Qt::AlignBottom,
        text);
}

//***************************************************************************
void Kwave::ScaleWidget::drawLog(QPainter &p, int w, int h, bool inverse)
{
    // only use base 10 for now, tested with others too,
    // but not configurable through a property
    const int base = 10;

    int dir = (inverse) ? -1 : +1;

    p.setPen(palette().dark().color());
    p.drawLine (0, dir*(h-1), dir*w, dir*(h-1));
    p.drawLine (dir*(w-1), 0, dir*(w-1), dir*(h-1));

    p.setPen(palette().text().color());

    int a, x;
    const int h2 = h;

    Q_ASSERT(m_low >= 0);
    Q_ASSERT(m_high > m_low);

    int dec_lo = (m_low) ? Kwave::toInt(floor(log(m_low)/log(base))) : 0;
    int dec_hi = Kwave::toInt(ceil(log(m_high)/log(base)));
    int decades = qAbs(dec_hi - dec_lo) + 1;

    // check if we have enough space for the small lines within a decade
    int w1 = Kwave::toInt(w / decades); // pixels per decade
    bool small_lines = (w1 - Kwave::toInt(
        static_cast<double>(w1) * log(base-1)/log(base))) > 1;

    // print the lines
    for (a = 0; a < decades; ++a) {
        // big line, for each decade
        x = Kwave::toInt((w-1) * a / decades);
        p.drawLine (dir * x, dir * 1, dir * x, dir * (h2 - 2));

        w1 = Kwave::toInt((w - 1) * (a + 1) / decades) - x + 1;
        if (small_lines) {
            // small lines, within the decade
            for (int i = 1; i < base; i++) {
                int x1 = x + Kwave::toInt(static_cast<double>(w1) *
                    log(i) / log(base));
                p.drawLine (dir * x1, dir * 1, dir * x1, dir * ((h2 / 2) - 2));
            }
        }
    }

    // print the text
    for (a = 0; a < decades; ++a) {
        QString buf = _("%1 %2");
        int value = Kwave::toInt(pow(base, dec_lo + a));
        buf = buf.arg(value).arg(m_unittext);
        x = ((w - 1) * a) / decades;
        paintText(p, dir * (x + 4), dir * (h - FONTSIZE - 4), inverse, buf);
    }
}

//***************************************************************************
void Kwave::ScaleWidget::drawLinear(QPainter &p, int w, int h, bool inverse)
{
    int dir = (inverse) ? -1 : +1;

    p.setPen(palette().dark().color());
    p.drawLine(0, dir * (h - 1), dir * w, dir * (h - 1));
    p.drawLine(dir * (w - 1), 0, dir * (w - 1), dir * (h - 1));

    p.setPen(palette().text().color());

    int a, x;
    double ofs;
    double t = w - 1;
    int h2 = h;

    // print the lines
    while ((t / 10 > 1) && (h2 > 0)) {
        for (ofs = 0; ofs < w - 1; ofs += t) {
            for (a = 0; a < 4; ++a) {
                x = Kwave::toInt(ofs + (t * a / 4));
                p.drawLine (dir * x, dir * 1, dir * x, dir * (h2 - 2));
            }
        }
        h2 >>= 1;
        t /= 4;
    }

    // print the text
    for (a = 0; a < 4; ++a) {
        QString buf = _("%1 %2");
        int value = m_low + (((m_high - m_low)* (inverse ? (4 - a) : a)) / 4);
        buf = buf.arg(value).arg(m_unittext);
        x = ((w - 1) * a) / 4;
        paintText(p, dir * (x + 4), dir * (h - FONTSIZE - 4), inverse, buf);
    }

}

//***************************************************************************
void Kwave::ScaleWidget::paintEvent(QPaintEvent *)
{
    bool inverse = false;
    int h = height();
    int w = width();
    QPainter p;

    p.begin(this);
    p.save();
    p.setPen(palette().light().color());

    p.drawLine(0, 0, w, 0);
    if (h > w) {
        p.setWindow(-w, 0, w, h);
        p.rotate(-90);
        h = width();
        w = height();

        inverse = true;
    }

    (m_logmode) ? drawLog(p, w, h, inverse) : drawLinear(p, w, h, inverse);

    p.restore();
    p.end();
}

//***************************************************************************
QSize Kwave::ScaleWidget::sizeHint() const
{
    return QSize(4 * FONTSIZE, 4 * FONTSIZE);
}

//***************************************************************************
QSize Kwave::ScaleWidget::minimumSize() const
{
    return QSize(5 * 2 * FONTSIZE, 5 * 2 * FONTSIZE);
}

//***************************************************************************
//***************************************************************************